From 89c82d8a6412e68a5deeee42c5e25b8fd0a8ac02 Mon Sep 17 00:00:00 2001 From: Mike Boers Date: Wed, 2 Dec 2015 12:05:18 -0800 Subject: [PATCH] Include default and valid values for list-like fields --- sgschema/field.py | 16 ++++++++++++++++ sgschema/schema.py | 6 ++++++ tests/schema/dump.py | 18 ++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/sgschema/field.py b/sgschema/field.py index 080f944..7b913d2 100644 --- a/sgschema/field.py +++ b/sgschema/field.py @@ -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, {}) @@ -24,9 +33,14 @@ 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))) @@ -34,5 +48,7 @@ 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) diff --git a/sgschema/schema.py b/sgschema/schema.py index fb92712..9615ffb 100644 --- a/sgschema/schema.py +++ b/sgschema/schema.py @@ -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. diff --git a/tests/schema/dump.py b/tests/schema/dump.py index 51a0d45..d36fbba 100644 --- a/tests/schema/dump.py +++ b/tests/schema/dump.py @@ -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'))