Skip to content

Commit

Permalink
Include default and valid values for list-like fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeboers committed Dec 2, 2015
1 parent 64fca38 commit 89c82d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
16 changes: 16 additions & 0 deletions sgschema/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ def __init__(self, entity, name):
self.entity = entity
self.name = name

#: Type names allowed for ``entity`` or ``multi_entity`` fields.
self.allowed_entity_types = set()
self.data_type = None

#: Default value of the field; set for ``checkbox``, ``list``, and
#: ``status_list`` fields.
self.default_value = None

#: Allowed values for ``list`` and ``status_list`` fields.
self.valid_values = None

def _reduce_raw(self, schema, raw_field):

self.data_type = raw_field['data_type']['value']
self.default_value = raw_field['properties']['default_value']['value']

raw_private = schema.raw_private['entity_fields'][self.entity.name].get(self.name, {})

Expand All @@ -24,15 +33,22 @@ def _reduce_raw(self, schema, raw_field):
types_ = raw_private['allowed_entity_types'] or []
self.allowed_entity_types = set(types_[:])

elif self.data_type in ('list', 'status_list'):
self.valid_values = raw_field['properties']['valid_values']['value'][:]

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.default_value = raw.pop('default_value', None)
self.valid_values = list(raw.pop('valid_values', ()))
if raw:
raise ValueError('unknown field keys: %s' % ', '.join(sorted(raw)))

def _dump(self):
return dict((k, v) for k, v in (
('allowed_entity_types', sorted(self.allowed_entity_types)),
('data_type', self.data_type),
('default_value', self.default_value),
('valid_values', self.valid_values),
) if v)

6 changes: 6 additions & 0 deletions sgschema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def _dump(self):
('entity_tags', self.entity_tags),
) if v)

def _dump_raw(self, base_path):
"""Private API for testing and development; no other real purpose."""
for name in 'fields', 'entities', 'private':
with open(base_path + name + '.json', 'w') as fh:
fh.write(json.dumps(getattr(self, 'raw_' + name), indent=4, sort_keys=True))

def dump(self, path):
"""Save the schema as JSON to the given path.
Expand Down
18 changes: 12 additions & 6 deletions tests/schema/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@

parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name')
parser.add_argument('base_url')
parser.add_argument('script_name')
parser.add_argument('api_key')
parser.add_argument('-r', '--registry-name')
parser.add_argument('base_url', nargs='?')
parser.add_argument('script_name', nargs='?')
parser.add_argument('api_key', nargs='?')
args = parser.parse_args()


shotgun = shotgun_api3.Shotgun(args.base_url, args.script_name, args.api_key)
if args.registry_name or not (args.base_url or args.script_name or args.api_key):
import shotgun_api3_registry
shotgun = shotgun_api3_registry.connect(args.registry_name)
else:
shotgun = shotgun_api3.Shotgun(args.base_url, args.script_name, args.api_key)

schema = sgschema.Schema()
schema.read(shotgun)


if not args.name:
parsed = urlparse.urlparse(args.base_url)
parsed = urlparse.urlparse(shotgun.base_url)
args.name = parsed.netloc.split('.')[0]

here = os.path.dirname(__file__)

schema.dump(os.path.join(here, args.name + '.raw.json'), raw=True)
schema._dump_raw(os.path.join(here, args.name + '.'))
schema.dump(os.path.join(here, args.name + '.json'))

0 comments on commit 89c82d8

Please sign in to comment.