Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | class BaseModel: | ||||
@classmethod | @classmethod | ||||
def from_dict(cls, d): | def from_dict(cls, d): | ||||
"""Takes a dictionary representing a tree of SWH objects, and | """Takes a dictionary representing a tree of SWH objects, and | ||||
recursively builds the corresponding objects.""" | recursively builds the corresponding objects.""" | ||||
if not isinstance(d, dict): | if not isinstance(d, dict): | ||||
raise TypeError( | raise TypeError( | ||||
'%s.from_dict expects a dict, not %r' % (cls.__name__, d)) | '%s.from_dict expects a dict, not %r' % (cls.__name__, d)) | ||||
kwargs = {} | |||||
for (name, attribute) in attr.fields_dict(cls).items(): | for (name, attribute) in attr.fields_dict(cls).items(): | ||||
type_ = attribute.type | type_ = attribute.type | ||||
# Heuristic to detect `Optional[X]` and unwrap it to `X`. | # Heuristic to detect `Optional[X]` and unwrap it to `X`. | ||||
if contains_optional_validator(attribute.validator): | if contains_optional_validator(attribute.validator): | ||||
if name not in d: | if name not in d: | ||||
continue | continue | ||||
if d[name] is None: | if d[name] is None: | ||||
del d[name] | |||||
continue | continue | ||||
else: | else: | ||||
type_ = type_.__args__[0] | type_ = type_.__args__[0] | ||||
# Construct an object of the expected type | # Construct an object of the expected type | ||||
if issubclass(type_, BaseModel): | if issubclass(type_, BaseModel): | ||||
d[name] = type_.from_dict(d[name]) | kwargs[name] = type_.from_dict(d[name]) | ||||
elif issubclass(type_, Enum): | elif issubclass(type_, Enum): | ||||
d[name] = type_(d[name]) | kwargs[name] = type_(d[name]) | ||||
else: | else: | ||||
pass | kwargs[name] = d[name] | ||||
return cls(**d) | return cls(**kwargs) | ||||
@attr.s | @attr.s | ||||
class Person(BaseModel): | class Person(BaseModel): | ||||
"""Represents the author/committer of a revision or release.""" | """Represents the author/committer of a revision or release.""" | ||||
name = attr.ib(type=bytes) | name = attr.ib(type=bytes) | ||||
email = attr.ib(type=bytes) | email = attr.ib(type=bytes) | ||||
fullname = attr.ib(type=bytes) | fullname = attr.ib(type=bytes) | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | def to_dict(self): | ||||
'branches': { | 'branches': { | ||||
name: branch.to_dict() | name: branch.to_dict() | ||||
for (name, branch) in self.branches.items() | for (name, branch) in self.branches.items() | ||||
} | } | ||||
} | } | ||||
@classmethod | @classmethod | ||||
def from_dict(cls, d): | def from_dict(cls, d): | ||||
d['branches'] = { | d = { | ||||
**d, | |||||
'branches': { | |||||
name: SnapshotBranch.from_dict(branch) | name: SnapshotBranch.from_dict(branch) | ||||
for (name, branch) in d['branches'].items() | for (name, branch) in d['branches'].items() | ||||
} | } | ||||
} | |||||
return cls(**d) | return cls(**d) | ||||
@attr.s | @attr.s | ||||
class Release(BaseModel): | class Release(BaseModel): | ||||
id = attr.ib(type=Sha1Git) | id = attr.ib(type=Sha1Git) | ||||
name = attr.ib(type=bytes) | name = attr.ib(type=bytes) | ||||
message = attr.ib(type=bytes) | message = attr.ib(type=bytes) | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | class Directory(BaseModel): | ||||
def to_dict(self): | def to_dict(self): | ||||
dir_ = attr.asdict(self) | dir_ = attr.asdict(self) | ||||
dir_['entries'] = [entry.to_dict() for entry in self.entries] | dir_['entries'] = [entry.to_dict() for entry in self.entries] | ||||
return dir_ | return dir_ | ||||
@classmethod | @classmethod | ||||
def from_dict(cls, d): | def from_dict(cls, d): | ||||
d['entries'] = list(map(DirectoryEntry.from_dict, d['entries'])) | d = { | ||||
**d, | |||||
'entries': list(map(DirectoryEntry.from_dict, d['entries'])) | |||||
} | |||||
return super().from_dict(d) | return super().from_dict(d) | ||||
@attr.s | @attr.s | ||||
class Content(BaseModel): | class Content(BaseModel): | ||||
sha1 = attr.ib(type=bytes) | sha1 = attr.ib(type=bytes) | ||||
sha1_git = attr.ib(type=Sha1Git) | sha1_git = attr.ib(type=Sha1Git) | ||||
sha256 = attr.ib(type=bytes) | sha256 = attr.ib(type=bytes) | ||||
Show All 36 Lines |