File: imagereceiver.py

package info (click to toggle)
gr-satellites 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,836 kB
  • sloc: python: 29,546; cpp: 5,448; ansic: 1,247; sh: 118; makefile: 24
file content (86 lines) | stat: -rw-r--r-- 2,415 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2020 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

import sys

from ..feh import FehOpener
from .filereceiver import FileReceiver


class ImageReceiver(FileReceiver):
    """
    Class to reassemble images transmitted in chunks and display them
    in real-time using feh

    This implements the generic framework. Specific protocols should
    inherit from this class and implement some FileReceiver methods
    """
    def __init__(self, path, verbose=False, display=False, fullscreen=True):
        """
        Builds a new ImageReceiver

        Args:
            path: directory where files should be stored (str or pathlib.Path)
            verbose: verbose reporting of events (bool)
            display: display image in real-time using feh (bool)
            fullscreen: run feh in fullscreen (bool)
        """
        super().__init__(path, verbose)
        self._feh = FehOpener(fullscreen) if display else None
        self._name = 'ImageReceiver'

    def filename(self, fid):
        """
        Generates a filename based on the file id

        The default implementation uses the fid.jpg as filename

        Args:
           fid: file id (usually int)
        """
        return f'{fid}.jpg'

    def _new_file(self, fid):
        """
        Creates a new file

        Args:
            fid: file id of the new file (usually int)
        Returns:
            the new file
        """
        f = super()._new_file(fid)
        f.displaying = False
        return f

    def push_chunk(self, chunk):
        """
        Processes a new chunk

        The user should call this function whenever a new chunk
        is received.

        Args:
            chunk: the file chunk (bytes)
        """
        super().push_chunk(chunk)
        if self._current_file is None:
            return
        f = self._files[self._current_file]
        if f.write_pointer >= 10*64:
            # Enough data to display
            if self._feh is not None and not f.displaying:
                f.displaying = True
                try:
                    self._feh.open(f.path)
                except Exception as e:
                    print('Unable to open feh image viewer',
                          file=sys.stderr)
                    print(e, file=sys.stderr)