|
| 1 | +""" |
| 2 | + ----------------------------------------------------------------------------- |
| 3 | + Copyright (c) 2009-2019, Shotgun Software Inc |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without |
| 6 | + modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | + - Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + - Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer in the documentation |
| 13 | + and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + - Neither the name of the Shotgun Software Inc nor the names of its |
| 16 | + contributors may be used to endorse or promote products derived from this |
| 17 | + software without specific prior written permission. |
| 18 | +
|
| 19 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +
|
| 30 | +----------------------------------------------------------------------------- |
| 31 | +""" |
| 32 | +# TODO: Python3 support |
| 33 | +# TODO: Logging not printing |
| 34 | +import cPickle as pickle |
| 35 | +import os |
| 36 | + |
| 37 | +from .errors import MockgunError |
| 38 | +from .schema import SchemaFactory |
| 39 | + |
| 40 | +# Highest protocol that Python 2.4 supports, which is the earliest version of Python we support. |
| 41 | +# Actually, this is the same version that Python 2.7 supports at the moment! |
| 42 | +_HIGHEST_24_PICKLE_PROTOCOL = 2 |
| 43 | + |
| 44 | + |
| 45 | +# Global private values to cache the schema in. |
| 46 | +__schema, __schema_entity = None, None |
| 47 | + |
| 48 | + |
| 49 | +def _get_schema(force=False): |
| 50 | + """ |
| 51 | + _get_schema will get the schema from the SchemaFactory and cache it. |
| 52 | +
|
| 53 | + :param bool force: If set, force will always query the latest schema from disk. |
| 54 | + :return: schema dictionary from disk |
| 55 | + """ |
| 56 | + global __schema, __schema_entity |
| 57 | + from .mockgun import Shotgun |
| 58 | + if not __schema or force is True: |
| 59 | + schema_path, schema_entity_path = Shotgun.get_schema_paths() |
| 60 | + if not schema_path or not schema_entity_path: |
| 61 | + raise MockgunError("You must set the schema paths on the Mockgun instance first.") |
| 62 | + __schema, __schema_entity = SchemaFactory.get_schemas(schema_path, schema_entity_path) |
| 63 | + return __schema |
| 64 | + |
| 65 | + |
| 66 | +def _get_schema_entity(force=False): |
| 67 | + """ |
| 68 | + _get_schema_entity will get the schema_entity from the SchemaFactory and cache it. |
| 69 | +
|
| 70 | + :param bool force: If set, force will always query the latest schema_entity from disk. |
| 71 | + :return: schema_entity dictionary from disk |
| 72 | + """ |
| 73 | + global __schema, __schema_entity |
| 74 | + from .mockgun import Shotgun |
| 75 | + if not __schema_entity or force is True: |
| 76 | + schema_path, schema_entity_path = Shotgun.get_schema_paths() |
| 77 | + if not schema_path or not schema_entity_path: |
| 78 | + raise MockgunError("You must set the schema paths on the Mockgun instance first.") |
| 79 | + __schema, __schema_entity = SchemaFactory.get_schemas(schema_path, schema_entity_path) |
| 80 | + return __schema_entity |
| 81 | + |
| 82 | + |
| 83 | +def _get_entity_fields(entity): |
| 84 | + """ |
| 85 | + _get_entity_fields will return a list of the fields on an entity as strings |
| 86 | + :param str entity: Shotgun entity that we want the schema for |
| 87 | + :return: List of the field names for the provided entity |
| 88 | + :rtype: list[str] |
| 89 | + """ |
| 90 | + schema = _get_schema() |
| 91 | + return schema[entity].keys() |
| 92 | + |
| 93 | + |
| 94 | +def _read_data_(shotgun, entity): |
| 95 | + """ |
| 96 | + _read_data_ will return all of the entries for the provided entity. |
| 97 | + It will get all fields for the entity from the Mockgun schema. |
| 98 | +
|
| 99 | + :param shotgun: Shotgun instance used to query a live site |
| 100 | + :param str entity: Shotgun entity that we want the schema for |
| 101 | + :return: List of found entities |
| 102 | + :rtype: list[dict] |
| 103 | + """ |
| 104 | + try: |
| 105 | + return shotgun.find( |
| 106 | + entity, |
| 107 | + filters=[], |
| 108 | + fields=_get_entity_fields(entity) |
| 109 | + ) |
| 110 | + except Exception as err: |
| 111 | + print(" Exception: %s" % str(err)) |
| 112 | + import traceback |
| 113 | + traceback.print_exc() |
| 114 | + return [] |
| 115 | + |
| 116 | + |
| 117 | +class DatabaseFactory(object): |
| 118 | + """ |
| 119 | + Allows to instantiate a pickled database. |
| 120 | + """ |
| 121 | + _database_cache = None |
| 122 | + _database_cache_path = None |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def get_database(cls, database_path): |
| 126 | + """ |
| 127 | + Retrieves the schemas from disk. |
| 128 | +
|
| 129 | + :param str database_path: Path to the database. |
| 130 | +
|
| 131 | + :returns: Dictionary holding the database. |
| 132 | + :rtype: dict |
| 133 | + """ |
| 134 | + if not os.path.exists(database_path): |
| 135 | + raise MockgunError("Cannot locate Mockgun database file '%s'!" % database_path) |
| 136 | + |
| 137 | + # Poor man's attempt at a cache. All of our use cases deal with a single pair of files |
| 138 | + # for the duration of the unit tests, so keep a cache for both inputs. We don't want |
| 139 | + # to deal with ever growing caches anyway. Just having this simple cache has shown |
| 140 | + # speed increases of up to 500% for Toolkit unit tests alone. |
| 141 | + |
| 142 | + if database_path != cls._database_cache_path: |
| 143 | + cls._database_cache = cls._read_file(database_path) |
| 144 | + cls._database_cache_path = database_path |
| 145 | + |
| 146 | + return cls._database_cache |
| 147 | + |
| 148 | + @classmethod |
| 149 | + def _read_file(cls, path): |
| 150 | + fh = open(path, "rb") |
| 151 | + try: |
| 152 | + return pickle.load(fh) |
| 153 | + finally: |
| 154 | + fh.close() |
| 155 | + |
| 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 | + |
| 178 | + |
| 179 | +# ---------------------------------------------------------------------------- |
| 180 | +# Utility methods |
| 181 | +def generate_data(shotgun, data_file_path, entity_subset=None): |
| 182 | + """ |
| 183 | + Helper method for mockgun. |
| 184 | + Generates the data files needed by the mocker by connecting to a real shotgun |
| 185 | + and downloading the information for that site. Once the generated data |
| 186 | + files are being passed to mockgun, it will mimic the site's database structure. |
| 187 | +
|
| 188 | + :param shotgun: Shotgun instance |
| 189 | + :param data_file_path: Path where to write the main data file to |
| 190 | + :param entity_subset: Optional subset of entities to generate data for. |
| 191 | + If not passed, it will default to all entities |
| 192 | + """ |
| 193 | + |
| 194 | + if not entity_subset: |
| 195 | + entity_subset = _get_schema().keys() |
| 196 | + |
| 197 | + database = {} |
| 198 | + for entity in entity_subset: |
| 199 | + print("Requesting data for: %s" % entity) |
| 200 | + database[entity] = _read_data_(shotgun, entity) |
| 201 | + |
| 202 | + DatabaseFactory.set_database(database, data_file_path) |
0 commit comments