File: importer_service_ipc.py

package info (click to toggle)
git-ubuntu 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,688 kB
  • sloc: python: 13,378; sh: 480; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 2,760 bytes parent folder | download
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
"""Objects common to multiple IPC endpoint types"""
from collections import namedtuple
import json

class JSONMixin:
    """Mixin to provide JSON encoding and decoding methods"""
    def to_bytes(self):
        """Convert instance to bytes

        :rtype: bytes
        :returns: representation of self as bytes
        """
        return json.dumps(self._asdict()).encode('utf-8')

    @classmethod
    def from_bytes(cls, blob):
        """Construct instance from bytes

        :param bytes blob: the return value of a previous call to to_bytes()
        :rtype: cls
        :returns: a new instance of cls
        """
        return cls(**json.loads(blob.decode('utf-8')))

# The WorkerRequest and WorkerReply classes embed what is effectively
# importer_service.ImportRequest and also add some IPC-related attributes. We
# could embed into them ImportRequest attributes as a whole but this would make
# encoding and decoding more complicated. While ImportRequest is just two
# fields, let's keep it simple and just have two fields in WorkerRequest and
# WorkerReply that map to the ImportRequest and handle them manually as
# required.

class WorkerRequest(
    namedtuple('WorkerRequest', ['package', 'level']),
    JSONMixin,
):
    """Representation of a request to a worker sent over IPC

    :ivar str package: the source package for which an import is requested.
        This is an encoded representation of
        importer_service.ImportRequest.package.
    :ivar int level: whether the request is for an incremental or full
        reimport. This is an encoded representation of
        importer_service.ImportRequest.level.
    """
    __slots__ = ()

class WorkerReply(
    namedtuple('WorkerReply', [
        'identity',
        'previous_package',
        'previous_level',
        'previous_result',
    ]),
    JSONMixin,
):
    """Representation of a reply from a worker sent over IPC

    :ivar str identity: the identity of the worker sending the reply.
    :ivar (str or None) package: the source package for which an import was
        previously requested. This is an encoded representation of
        importer_service.ImportRequest.package. None if the reply is a request
        for work when no previous work was assigned.
    :ivar (int or None) level: whether the previous request was for an
        incremental or full reimport. This is an encoded representation of
        importer_service.ImportRequest.level. None if the reply is a request
        for work when no previous work was assigned.
    :ivar (bool or None) previous_result: the success (True) or failure (False)
        result of the previous request. None if the reply is a request for work
        when no previous work was assigned.
    """
    __slots__ = ()