Skip to content

Commit 4cd2e58

Browse files
authored
Merge pull request #287 from mpsonntag/idRename
[1.4] 'id' attribute renaming and small fixes LGTM
2 parents eeff592 + 10233f3 commit 4cd2e58

21 files changed

+99
-208
lines changed

doc/example_odMLs/ex_2.odml

Lines changed: 0 additions & 47 deletions
This file was deleted.

doc/example_odMLs/odml_ex_1_generator.py

Lines changed: 0 additions & 63 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

odml/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __eq__(self, obj):
2929
return False
3030

3131
for key in self._format:
32-
if key == "id":
32+
if key == "id" or key == "oid":
3333
continue
3434
elif getattr(self, key) != getattr(obj, key):
3535
return False

odml/doc.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class BaseDocument(base.Sectionable):
1919

2020
_format = format.Document
2121

22-
def __init__(self, author=None, date=None, version=None, repository=None, id=None):
22+
def __init__(self, author=None, date=None, version=None, repository=None, oid=None):
2323
super(BaseDocument, self).__init__()
2424
try:
25-
if id is not None:
26-
self._id = str(uuid.UUID(id))
25+
if oid is not None:
26+
self._id = str(uuid.UUID(oid))
2727
else:
2828
self._id = str(uuid.uuid4())
2929
except ValueError as e:
@@ -41,22 +41,30 @@ def __repr__(self):
4141
return "<Doc %s by %s (%d sections)>" % (self._version, self._author,
4242
len(self._sections))
4343

44+
@property
45+
def oid(self):
46+
"""
47+
The uuid for the document. Required for entity creation and comparison,
48+
saving and loading.
49+
"""
50+
return self.id
51+
4452
@property
4553
def id(self):
4654
"""
4755
The uuid for the document.
4856
"""
4957
return self._id
5058

51-
def new_id(self, id=None):
59+
def new_id(self, oid=None):
5260
"""
5361
new_id sets the id of the current object to a RFC 4122 compliant UUID.
5462
If an id was provided, it is assigned if it is RFC 4122 UUID format compliant.
5563
If no id was provided, a new UUID is generated and assigned.
56-
:param id: UUID string as specified in RFC 4122.
64+
:param oid: UUID string as specified in RFC 4122.
5765
"""
58-
if id is not None:
59-
self._id = str(uuid.UUID(id))
66+
if oid is not None:
67+
self._id = str(uuid.UUID(oid))
6068
else:
6169
self._id = str(uuid.uuid4())
6270

odml/format.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class Property(Format):
108108
}
109109
_map = {
110110
'dependencyvalue': 'dependency_value',
111-
'type': 'dtype'
111+
'type': 'dtype',
112+
'id': 'oid'
112113
}
113114
_rdf_map = {
114115
'id': _ns.hasId,
@@ -142,6 +143,7 @@ class Section(Format):
142143
_map = {
143144
'section': 'sections',
144145
'property': 'properties',
146+
'id': 'oid'
145147
}
146148
_rdf_map = {
147149
'id': _ns.hasId,
@@ -168,7 +170,8 @@ class Document(Format):
168170
'repository': 0,
169171
}
170172
_map = {
171-
'section': 'sections'
173+
'section': 'sections',
174+
'id': 'oid'
172175
}
173176
_rdf_map = {
174177
'id': _ns.hasId,

odml/property.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BaseProperty(base.BaseObject):
1616
def __init__(self, name=None, value=None, parent=None, unit=None,
1717
uncertainty=None, reference=None, definition=None,
1818
dependency=None, dependency_value=None, dtype=None,
19-
value_origin=None, id=None):
19+
value_origin=None, oid=None):
2020
"""
2121
Create a new Property. If a value without an explicitly stated dtype
2222
has been provided, the method will try to infer the value's dtype.
@@ -45,13 +45,13 @@ def __init__(self, name=None, value=None, parent=None, unit=None,
4545
if dtype is not given, the type is deduced from the values.
4646
Check odml.DType for supported data types.
4747
:param value_origin: Reference where the value originated from e.g. a file name.
48-
:param id: UUID string as specified in RFC 4122. If no id is provided,
48+
:param oid: object id, UUID string as specified in RFC 4122. If no id is provided,
4949
an id will be generated and assigned. An id has to be unique
5050
within an odML Document.
5151
"""
5252
try:
53-
if id is not None:
54-
self._id = str(uuid.UUID(id))
53+
if oid is not None:
54+
self._id = str(uuid.UUID(oid))
5555
else:
5656
self._id = str(uuid.uuid4())
5757
except ValueError as e:
@@ -101,19 +101,30 @@ def __setitem__(self, key, item):
101101
raise ValueError("odml.Property.__setitem__: passed value cannot be "
102102
"converted to data type \'%s\'!" % self._dtype)
103103

104+
@property
105+
def oid(self):
106+
"""
107+
The uuid for the property. Required for entity creation and comparison,
108+
saving and loading.
109+
"""
110+
return self.id
111+
104112
@property
105113
def id(self):
114+
"""
115+
The uuid for the property.
116+
"""
106117
return self._id
107118

108-
def new_id(self, id=None):
119+
def new_id(self, oid=None):
109120
"""
110-
new_id sets the id of the current object to an RFC 4122 compliant UUID.
121+
new_id sets the object id of the current object to an RFC 4122 compliant UUID.
111122
If an id was provided, it is assigned if it is RFC 4122 UUID format compliant.
112123
If no id was provided, a new UUID is generated and assigned.
113-
:param id: UUID string as specified in RFC 4122.
124+
:param oid: UUID string as specified in RFC 4122.
114125
"""
115-
if id is not None:
116-
self._id = str(uuid.UUID(id))
126+
if oid is not None:
127+
self._id = str(uuid.UUID(oid))
117128
else:
118129
self._id = str(uuid.uuid4())
119130

odml/section.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,24 @@
1616
class BaseSection(base.Sectionable):
1717
""" An odML Section """
1818
type = None
19-
# id = None
19+
reference = None # the *import* property
2020
_link = None
2121
_include = None
22-
reference = None # the *import* property
23-
2422
_merged = None
2523

2624
_format = format.Section
2725

2826
def __init__(self, name=None, type=None, parent=None,
2927
definition=None, reference=None,
30-
repository=None, link=None, include=None, id=None):
28+
repository=None, link=None, include=None, oid=None):
3129

3230
# Sets _sections Smartlist and _repository to None, so run first.
3331
super(BaseSection, self).__init__()
3432
self._props = base.SmartList(BaseProperty)
3533

3634
try:
37-
if id is not None:
38-
self._id = str(uuid.UUID(id))
35+
if oid is not None:
36+
self._id = str(uuid.UUID(oid))
3937
else:
4038
self._id = str(uuid.uuid4())
4139
except ValueError as e:
@@ -76,19 +74,30 @@ def __len__(self):
7674
"""
7775
return len(self._sections) + len(self._props)
7876

77+
@property
78+
def oid(self):
79+
"""
80+
The uuid for the section. Required for entity creation and comparison,
81+
saving and loading.
82+
"""
83+
return self.id
84+
7985
@property
8086
def id(self):
87+
"""
88+
The uuid for the section.
89+
"""
8190
return self._id
8291

83-
def new_id(self, id=None):
92+
def new_id(self, oid=None):
8493
"""
8594
new_id sets the id of the current object to a RFC 4122 compliant UUID.
8695
If an id was provided, it is assigned if it is RFC 4122 UUID format compliant.
8796
If no id was provided, a new UUID is generated and assigned.
88-
:param id: UUID string as specified in RFC 4122.
97+
:param oid: UUID string as specified in RFC 4122.
8998
"""
90-
if id is not None:
91-
self._id = str(uuid.UUID(id))
99+
if oid is not None:
100+
self._id = str(uuid.UUID(oid))
92101
else:
93102
self._id = str(uuid.uuid4())
94103

odml/tools/dict_parser.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def to_dict(self, odml_document):
3333
tag = getattr(odml_document, attr)
3434

3535
if tag:
36-
parsed_doc[attr] = tag
36+
# Always use the arguments key attribute name when saving
37+
parsed_doc[i] = tag
3738

3839
return parsed_doc
3940

@@ -60,7 +61,8 @@ def get_sections(self, section_list):
6061
tag = getattr(section, attr)
6162

6263
if tag:
63-
section_dict[attr] = tag
64+
# Always use the arguments key attribute name when saving
65+
section_dict[i] = tag
6466

6567
section_seq.append(section_dict)
6668

@@ -89,7 +91,8 @@ def get_properties(props_list):
8991
prop.dtype.endswith("-tuple") and len(prop.value) > 0:
9092
prop_dict["value"] = "(%s)" % ";".join(prop.value[0])
9193
else:
92-
prop_dict[attr] = tag
94+
# Always use the arguments key attribute name when saving
95+
prop_dict[i] = tag
9396

9497
props_seq.append(prop_dict)
9598

@@ -128,9 +131,9 @@ def to_odml(self, parsed_doc):
128131
raise ParserException("Invalid odML document: Could not find odml-version.")
129132

130133
elif self.parsed_doc.get('odml-version') != FORMAT_VERSION:
131-
msg = ("Cannot read file: invalid odML document format version '%s'. \n"
132-
"This package supports odML format versions: '%s'."
133-
% (self.parsed_doc.get('odml-version'), FORMAT_VERSION))
134+
msg = ("Cannot parse odML document with format version '%s'. \n"
135+
"\tUse the 'tools.VersionConverter' to import previous odML formats."
136+
% self.parsed_doc.get('odml-version'))
134137
raise InvalidVersionException(msg)
135138

136139
self.parsed_doc = self.parsed_doc['Document']
@@ -143,7 +146,8 @@ def to_odml(self, parsed_doc):
143146
if attr == 'sections':
144147
doc_secs = self.parse_sections(self.parsed_doc['sections'])
145148
elif attr:
146-
doc_attrs[i] = self.parsed_doc[i]
149+
# Make sure to always use the correct odml format attribute name
150+
doc_attrs[odmlfmt.Document.map(attr)] = self.parsed_doc[i]
147151

148152
doc = odmlfmt.Document.create(**doc_attrs)
149153
for sec in doc_secs:
@@ -166,7 +170,8 @@ def parse_sections(self, section_list):
166170
elif attr == 'sections':
167171
children_secs = self.parse_sections(section['sections'])
168172
elif attr:
169-
sec_attrs[attr] = section[attr]
173+
# Make sure to always use the correct odml format attribute name
174+
sec_attrs[odmlfmt.Section.map(attr)] = section[attr]
170175

171176
sec = odmlfmt.Section.create(**sec_attrs)
172177
for prop in sec_props:
@@ -188,7 +193,8 @@ def parse_properties(self, props_list):
188193
for i in _property:
189194
attr = self.is_valid_attribute(i, odmlfmt.Property)
190195
if attr:
191-
prop_attrs[attr] = _property[attr]
196+
# Make sure to always use the correct odml format attribute name
197+
prop_attrs[odmlfmt.Property.map(attr)] = _property[attr]
192198

193199
prop = odmlfmt.Property.create(**prop_attrs)
194200
odml_props.append(prop)

0 commit comments

Comments
 (0)