Skip to content

Commit 78864ff

Browse files
author
Alex
committed
Implement database dumping
The functionality now lives on the DatabaseFactory as `set_database`.
1 parent b055b48 commit 78864ff

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

shotgun_api3/lib/mockgun/data.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"""
3232
# TODO: Python3 support
3333
# TODO: Logging not printing
34-
# TODO: Dump database ability
3534
import cPickle as pickle
3635
import os
3736

@@ -154,6 +153,28 @@ def _read_file(cls, path):
154153
finally:
155154
fh.close()
156155

156+
@classmethod
157+
def _write_file(cls, data, path):
158+
fh = open(path, "rb")
159+
try:
160+
return pickle.dump(data, fh, protocol=_HIGHEST_24_PICKLE_PROTOCOL)
161+
finally:
162+
fh.close()
163+
164+
@classmethod
165+
def set_database(cls, database, database_path):
166+
"""
167+
Writes the schemas to disk.
168+
169+
:param dict database: The database in memory.
170+
:param str database_path: Path to the database.
171+
"""
172+
if database_path != cls._database_cache_path:
173+
cls._database_cache_path = database_path
174+
cls._database_cache = database
175+
176+
cls._write_file(database, database_path)
177+
157178

158179
# ----------------------------------------------------------------------------
159180
# Utility methods
@@ -178,9 +199,4 @@ def generate_data(shotgun, data_file_path, entity_subset=None):
178199
print("Requesting data for: %s" % entity)
179200
database[entity] = _read_data_(shotgun, entity)
180201

181-
fh = open(data_file_path, "wb")
182-
try:
183-
pickle.dump(database, fh, protocol=_HIGHEST_24_PICKLE_PROTOCOL)
184-
finally:
185-
fh.close()
186-
202+
DatabaseFactory.set_database(database, data_file_path)

shotgun_api3/lib/mockgun/mockgun.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ def upload(self, entity_type, entity_id, path, field_name=None, display_name=Non
447447
def upload_thumbnail(self, entity_type, entity_id, path, **kwargs):
448448
pass
449449

450+
def dump_database(self):
451+
DatabaseFactory.set_database(self._db, self.get_database_path())
452+
450453
###################################################################################################
451454
# internal methods and members
452455

0 commit comments

Comments
 (0)