Skip to content

Commit 0d306b9

Browse files
author
Alex
committed
Add schema writing functionality to SchemaFactory class
This works opposite to the get_schemas function
1 parent be1f679 commit 0d306b9

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

shotgun_api3/lib/mockgun/schema.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ def get_schemas(cls, schema_path, schema_entity_path):
7979

8080
return cls._schema_cache, cls._schema_entity_cache
8181

82+
@classmethod
83+
def set_schemas(cls, schema, schema_path, schema_entity, schema_entity_path):
84+
"""
85+
Retrieves the schemas from disk.
86+
87+
:param str schema: Schema data.
88+
:param str schema_path: Path to the schema on disk.
89+
:param str schema_entity: Entities schema data.
90+
:param str schema_entity_path: Path to the entities schema on disk.
91+
"""
92+
93+
if schema_path != cls._schema_cache_path:
94+
cls._schema_cache_path = schema_path
95+
cls._schema_cache = schema
96+
cls._write_file(schema, schema_path)
97+
98+
if schema_entity_path != cls._schema_entity_cache_path:
99+
cls._schema_entity_cache_path = schema_entity_path
100+
cls._schema_entity_cache = schema_entity
101+
cls._write_file(schema_entity, schema_entity_path)
102+
82103
@classmethod
83104
def _read_file(cls, path):
84105
fh = open(path, "rb")
@@ -87,6 +108,14 @@ def _read_file(cls, path):
87108
finally:
88109
fh.close()
89110

111+
@classmethod
112+
def _write_file(cls, data, path):
113+
fh = open(path, "rb")
114+
try:
115+
return pickle.dump(data, fh, protocol=_HIGHEST_24_PICKLE_PROTOCOL)
116+
finally:
117+
fh.close()
118+
90119

91120
# Highest protocol that Python 2.4 supports, which is the earliest version of Python we support.
92121
# Actually, this is the same version that Python 2.7 supports at the moment!
@@ -102,23 +131,11 @@ def generate_schema(shotgun, schema_file_path, schema_entity_file_path):
102131
and downloading the schema information for that site. Once the generated schema
103132
files are being passed to mockgun, it will mimic the site's schema structure.
104133
105-
:param sg_url: Shotgun site url
106-
:param sg_script: Script name to connect with
107-
:param sg_key: Script key to connect with
134+
:param shotgun: Shotgun instance
108135
:param schema_file_path: Path where to write the main schema file to
109136
:param schema_entity_file_path: Path where to write the entity schema file to
110137
"""
111138

112139
schema = shotgun.schema_read()
113-
fh = open(schema_file_path, "wb")
114-
try:
115-
pickle.dump(schema, fh, protocol=_HIGHEST_24_PICKLE_PROTOCOL)
116-
finally:
117-
fh.close()
118-
119140
schema_entity = shotgun.schema_entity_read()
120-
fh = open(schema_entity_file_path, "wb")
121-
try:
122-
pickle.dump(schema_entity, fh, protocol=_HIGHEST_24_PICKLE_PROTOCOL)
123-
finally:
124-
fh.close()
141+
SchemaFactory.set_schemas(schema, schema_file_path, schema_entity, schema_entity_file_path)

0 commit comments

Comments
 (0)