Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9345790
D2174.id7414.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Subscribers
None
D2174.id7414.diff
View Options
diff --git a/swh/model/model.py b/swh/model/model.py
--- a/swh/model/model.py
+++ b/swh/model/model.py
@@ -28,7 +28,20 @@
def to_dict(self):
"""Wrapper of `attr.asdict` that can be overridden by subclasses
that have special handling of some of the fields."""
- return attr.asdict(self)
+ ret = attr.asdict(self, recurse=False)
+ for k, v in ret.items():
+ if isinstance(v, BaseModel):
+ ret[k] = v.to_dict()
+ elif isinstance(v, Enum):
+ ret[k] = v.value
+ elif isinstance(v, dict):
+ ret[k] = {kk: vv.to_dict() if isinstance(vv, BaseModel) else vv
+ for kk, vv in v.items()}
+ elif isinstance(v, list):
+ ret[k] = [vv.to_dict() if isinstance(vv, BaseModel) else vv
+ for vv in v]
+
+ return ret
@classmethod
def from_dict(cls, d):
@@ -127,7 +140,6 @@
ov = super().to_dict()
if ov['visit'] is None:
del ov['visit']
- ov['origin'] = self.origin.to_dict()
return ov
@classmethod
@@ -178,11 +190,6 @@
raise ValueError('Wrong length for bytes identifier: %d' %
len(value))
- def to_dict(self):
- branch = attr.asdict(self)
- branch['target_type'] = branch['target_type'].value
- return branch
-
@classmethod
def from_dict(cls, d):
return cls(
@@ -196,15 +203,6 @@
id = attr.ib(type=Sha1Git)
branches = attr.ib(type=Dict[bytes, Optional[SnapshotBranch]])
- def to_dict(self):
- return {
- 'id': self.id,
- 'branches': {
- name: branch.to_dict() if branch else None
- for (name, branch) in self.branches.items()
- }
- }
-
@classmethod
def from_dict(cls, d):
return cls(
@@ -237,9 +235,7 @@
raise ValueError('release date must be None if author is None.')
def to_dict(self):
- rel = attr.asdict(self)
- rel['date'] = self.date.to_dict() if self.date is not None else None
- rel['target_type'] = rel['target_type'].value
+ rel = super().to_dict()
if rel['metadata'] is None:
del rel['metadata']
return rel
@@ -280,13 +276,6 @@
parents = attr.ib(type=List[Sha1Git],
default=attr.Factory(list))
- def to_dict(self):
- rev = attr.asdict(self)
- rev['date'] = self.date.to_dict()
- rev['committer_date'] = self.committer_date.to_dict()
- rev['type'] = rev['type'].value
- return rev
-
@classmethod
def from_dict(cls, d):
d = d.copy()
@@ -316,11 +305,6 @@
id = attr.ib(type=Sha1Git)
entries = attr.ib(type=List[DirectoryEntry])
- def to_dict(self):
- dir_ = attr.asdict(self)
- dir_['entries'] = [entry.to_dict() for entry in self.entries]
- return dir_
-
@classmethod
def from_dict(cls, d):
return cls(
@@ -367,7 +351,7 @@
'Must not provide a reason if content is not absent.')
def to_dict(self):
- content = attr.asdict(self)
+ content = super().to_dict()
for field in ('data', 'reason', 'ctime'):
if content[field] is None:
del content[field]
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jul 3, 3:31 PM (1 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3229705
Attached To
D2174: model: make to_dict() properly recursive
Event Timeline
Log In to Comment