forked from vfxetc/sgschema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
308 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/sandbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from .field import Field | ||
from .utils import cached_property | ||
|
||
|
||
class Entity(object): | ||
|
||
def __init__(self, schema, name): | ||
|
||
self.schema = schema | ||
self.name = name | ||
|
||
self.fields = {} | ||
|
||
self._aliases = set() | ||
self._tags = set() | ||
|
||
self._field_aliases = {} | ||
self._field_tags = {} | ||
|
||
@cached_property | ||
def field_aliases(self): | ||
field_aliases = dict(self._field_aliases) | ||
for field in self.fields.itervalues(): | ||
for alias in field._aliases: | ||
field_aliases[alias] = field.name | ||
return field_aliases | ||
|
||
@cached_property | ||
def field_tags(self): | ||
field_tags = {k: set(v) for k, v in self._field_tags.iteritems()} | ||
for field in self.fields.itervalues(): | ||
for tag in field._tags: | ||
field_tags.setdefault(tag, set()).add(field.name) | ||
return field_tags | ||
|
||
@cached_property | ||
def aliases(self): | ||
aliases = set(self._aliases) | ||
for k, v in self.schema._entity_aliases.iteritems(): | ||
if v == self.name: | ||
aliases.add(k) | ||
return aliases | ||
|
||
@cached_property | ||
def tags(self): | ||
tags = set(self._tags) | ||
for k, v in self.schema._entity_tags.iteritems(): | ||
if self.name in v: | ||
tags.add(k) | ||
return tags | ||
|
||
def _get_or_make_field(self, name): | ||
try: | ||
return self.fields[name] | ||
except KeyError: | ||
return self.fields.setdefault(name, Field(self, name)) | ||
|
||
def _reduce_raw(self, schema, raw_entity): | ||
pass | ||
|
||
def _load(self, raw): | ||
for name, value in raw.pop('fields', {}).iteritems(): | ||
self._get_or_make_field(name)._load(value) | ||
|
||
self._field_aliases.update(raw.pop('field_aliases', {})) | ||
self._field_tags.update(raw.pop('field_tags', {})) | ||
|
||
self._aliases.update(raw.pop('aliases', ())) | ||
self._tags.update(raw.pop('tags', ())) | ||
|
||
if raw: | ||
raise ValueError('unknown entity keys: %s' % ', '.join(sorted(raw))) | ||
|
||
def _dump(self): | ||
return {k: v for k, v in ( | ||
('fields', {field.name: field._dump() for field in self.fields.itervalues()}), | ||
('tags', sorted(self.tags)), | ||
('aliases', sorted(self.aliases)), | ||
) if v} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from .utils import cached_property | ||
|
||
class Field(dict): | ||
|
||
def __init__(self, entity, name): | ||
|
||
self.entity = entity | ||
self.name = name | ||
|
||
self.allowed_entity_types = set() | ||
self.data_type = None | ||
|
||
self._aliases = set() | ||
self._tags = set() | ||
|
||
@cached_property | ||
def aliases(self): | ||
aliases = set(self._aliases) | ||
for k, v in self.entity._field_aliases.iteritems(): | ||
if v == self.name: | ||
aliases.add(k) | ||
return aliases | ||
|
||
@cached_property | ||
def tags(self): | ||
tags = set(self._tags) | ||
for k, v in self.entity._field_tags.iteritems(): | ||
if self.name in v: | ||
tags.add(k) | ||
return tags | ||
|
||
def _reduce_raw(self, schema, raw_field): | ||
|
||
self.data_type = raw_field['data_type']['value'] | ||
|
||
raw_private = schema._raw_private['entity_fields'][self.entity.name].get(self.name, {}) | ||
|
||
if raw_private.get('identifier_column'): | ||
self._tags.add('identifier_column') | ||
|
||
if self.data_type in ('entity', 'multi_entity'): | ||
types_ = raw_private['allowed_entity_types'] or [] | ||
self.allowed_entity_types = set(types_[:]) | ||
|
||
def _load(self, raw): | ||
self.allowed_entity_types.update(raw.pop('allowed_entity_types', ())) | ||
self.data_type = raw.pop('data_type', self.data_type) | ||
self._aliases.update(raw.pop('aliases', ())) | ||
self._tags.update(raw.pop('tags', ())) | ||
if raw: | ||
raise ValueError('unknown field tags: %s' % ', '.join(sorted(raw))) | ||
|
||
def _dump(self): | ||
return {k: v for k, v in ( | ||
('aliases', sorted(self.aliases)), | ||
('allowed_entity_types', sorted(self.allowed_entity_types)), | ||
('data_type', self.data_type), | ||
('tags', sorted(self.tags)), | ||
) if v} | ||
|
Oops, something went wrong.