Failed Conditions
Pull Request — master (#1127)
by Mischa
01:56
created

coalib.output.dbus.DbusApp   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %
Metric Value
dl 0
loc 45
rs 10
wmc 5
1
import os
2
3
from coalib.output.dbus.DbusDocument import DbusDocument
4
5
6
class DbusApp:
7
    """
8
    Stores data about each client that connects to the DbusServer
9
    """
10
    def __init__(self, app_id, name=""):
11
        self.app_id = app_id
12
        self.name = name
13
14
        self.docs = {}
15
        self.__next_doc_id = 0
16
17
    def _next_doc_id(self):
18
        self.__next_doc_id += 1
19
        return self.__next_doc_id
20
21
    def create_document(self, path):
22
        """
23
        Create a new dbus document.
24
25
        :param path:        The path to the document to be created.
26
        :param object_path: The dbus object path to use as the base for the
27
                            document object path.
28
        :param object_path: The connection to which the new ddocument object
29
                            path should be added.
30
        :return:            a DbusDocument object.
31
        """
32
        path = os.path.abspath(os.path.expanduser(path))
33
        doc = DbusDocument(doc_id=self._next_doc_id(), path=path)
34
        self.docs[path] = doc
35
36
        return doc
37
38
    def dispose_document(self, path):
39
        """
40
        Dispose of the document with the given path. It fails silently if the
41
        document does not exist. If there are no more documents in the app,
42
        the app is disposed.
43
44
        :param path: The path to the document.
45
        """
46
        path = os.path.abspath(os.path.expanduser(path))
47
        try:
48
            return self.docs.pop(path)
49
        except KeyError:
50
            return None
51