modm_data.owl.identifier

 1# Copyright 2016, Niklas Hauser
 2# SPDX-License-Identifier: MPL-2.0
 3
 4import copy
 5import string
 6from collections import OrderedDict, defaultdict
 7
 8
 9class DeviceIdentifier:
10    def __init__(self, naming_schema=None):
11        self.naming_schema = naming_schema
12        self._properties = OrderedDict()
13        self.__string = None
14        self.__ustring = None
15        self.__hash = None
16
17    @property
18    def _ustring(self):
19        if self.__ustring is None:
20            self.__ustring = "".join([k + self._properties[k] for k in sorted(self._properties.keys())])
21            if self.naming_schema: self.__ustring += self.naming_schema;
22        return self.__ustring
23
24    def copy(self):
25        identifier = DeviceIdentifier(self.naming_schema)
26        identifier._properties = copy.deepcopy(self._properties)
27        identifier.__string = self.__string
28        identifier.__ustring = self.__ustring
29        identifier.__hash = self.__hash
30        return identifier
31
32    def keys(self):
33        return self._properties.keys()
34
35    @property
36    def string(self):
37        # if no naming schema is available, throw up
38        if self.naming_schema is None:
39            raise ValueError("Naming schema is missing!")
40        # Use the naming schema to generate the string
41        if self.__string is None:
42            self.__string = string.Formatter().vformat(
43                    self.naming_schema, (), defaultdict(str, **self._properties))
44        return self.__string
45
46    def set(self, key, value):
47        self.__hash = None
48        self.__string = None
49        self.__ustring = None
50        self._properties[key] = value
51
52    def get(self, key, default=None):
53        return self._properties.get(key, default)
54
55    def __getitem__(self, key):
56        return self.get(key, None)
57
58    def __getattr__(self, attr):
59        val = self.get(attr, None)
60        if val is None:
61            raise AttributeError("'{}' has no property '{}'".format(repr(self), attr))
62        return val
63
64    def __eq__(self, other):
65        return self._ustring == other._ustring
66
67    def __ne__(self, other):
68        return not self == other
69
70    def __hash__(self):
71        if self.__hash is None:
72            self.__hash = hash(self._ustring)
73        return self.__hash
74
75    def __str__(self):
76        return self.string
77
78    def __repr__(self):
79        return self.string if self.naming_schema else "DeviceId({})".format(self._ustring)
class DeviceIdentifier:
10class DeviceIdentifier:
11    def __init__(self, naming_schema=None):
12        self.naming_schema = naming_schema
13        self._properties = OrderedDict()
14        self.__string = None
15        self.__ustring = None
16        self.__hash = None
17
18    @property
19    def _ustring(self):
20        if self.__ustring is None:
21            self.__ustring = "".join([k + self._properties[k] for k in sorted(self._properties.keys())])
22            if self.naming_schema: self.__ustring += self.naming_schema;
23        return self.__ustring
24
25    def copy(self):
26        identifier = DeviceIdentifier(self.naming_schema)
27        identifier._properties = copy.deepcopy(self._properties)
28        identifier.__string = self.__string
29        identifier.__ustring = self.__ustring
30        identifier.__hash = self.__hash
31        return identifier
32
33    def keys(self):
34        return self._properties.keys()
35
36    @property
37    def string(self):
38        # if no naming schema is available, throw up
39        if self.naming_schema is None:
40            raise ValueError("Naming schema is missing!")
41        # Use the naming schema to generate the string
42        if self.__string is None:
43            self.__string = string.Formatter().vformat(
44                    self.naming_schema, (), defaultdict(str, **self._properties))
45        return self.__string
46
47    def set(self, key, value):
48        self.__hash = None
49        self.__string = None
50        self.__ustring = None
51        self._properties[key] = value
52
53    def get(self, key, default=None):
54        return self._properties.get(key, default)
55
56    def __getitem__(self, key):
57        return self.get(key, None)
58
59    def __getattr__(self, attr):
60        val = self.get(attr, None)
61        if val is None:
62            raise AttributeError("'{}' has no property '{}'".format(repr(self), attr))
63        return val
64
65    def __eq__(self, other):
66        return self._ustring == other._ustring
67
68    def __ne__(self, other):
69        return not self == other
70
71    def __hash__(self):
72        if self.__hash is None:
73            self.__hash = hash(self._ustring)
74        return self.__hash
75
76    def __str__(self):
77        return self.string
78
79    def __repr__(self):
80        return self.string if self.naming_schema else "DeviceId({})".format(self._ustring)
DeviceIdentifier(naming_schema=None)
11    def __init__(self, naming_schema=None):
12        self.naming_schema = naming_schema
13        self._properties = OrderedDict()
14        self.__string = None
15        self.__ustring = None
16        self.__hash = None
naming_schema
def copy(self):
25    def copy(self):
26        identifier = DeviceIdentifier(self.naming_schema)
27        identifier._properties = copy.deepcopy(self._properties)
28        identifier.__string = self.__string
29        identifier.__ustring = self.__ustring
30        identifier.__hash = self.__hash
31        return identifier
def keys(self):
33    def keys(self):
34        return self._properties.keys()
string
36    @property
37    def string(self):
38        # if no naming schema is available, throw up
39        if self.naming_schema is None:
40            raise ValueError("Naming schema is missing!")
41        # Use the naming schema to generate the string
42        if self.__string is None:
43            self.__string = string.Formatter().vformat(
44                    self.naming_schema, (), defaultdict(str, **self._properties))
45        return self.__string
def set(self, key, value):
47    def set(self, key, value):
48        self.__hash = None
49        self.__string = None
50        self.__ustring = None
51        self._properties[key] = value
def get(self, key, default=None):
53    def get(self, key, default=None):
54        return self._properties.get(key, default)