X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/c03a740f65ea24ba6567b0ef75f3785faae37d42..cb1976ef371904b45d7961212cd87595a9486284:/floof/lib/fixtures.py?ds=sidebyside diff --git a/floof/lib/fixtures.py b/floof/lib/fixtures.py index 0b2f3ec..8cec8ff 100644 --- a/floof/lib/fixtures.py +++ b/floof/lib/fixtures.py @@ -7,15 +7,15 @@ from floof import model as model """ This module can be used for loading data into your models, for example when setting up default application data, -unit tests, JSON export/import and importing/exporting legacy data. Data is serialized to and from the JSON format. +unit tests, JSON export/import and importing/exporting legacy data. Data is serialized to and from the JSON format. """ VALID_FIXTURE_FILE_EXTENSIONS = ['.json'] def load_data(model, filename=None, base_dir=None): - """Installs provided fixture files into given model. Filename may be directory, file or list of dirs or files. If filename is - None, assumes that source file is located in fixtures/model_module_name/model_tablename.yaml of your application directory, - for example MyProject/fixtures/news/newsitems.yaml. The base_dir argument is the top package of the application unless + """Installs provided fixture files into given model. Filename may be directory, file or list of dirs or files. If filename is + None, assumes that source file is located in fixtures/model_module_name/model_tablename.yaml of your application directory, + for example MyProject/fixtures/news/newsitems.yaml. The base_dir argument is the top package of the application unless specified. You can also pass the name of a table instead of a model class.""" if type(model) is types.StringType: @@ -28,17 +28,17 @@ def load_data(model, filename=None, base_dir=None): def load_data_to_table(table, filename=None, base_dir=None): """Installs data directly into a table. Useful if table does not have a corresponding model, for example a many-to-many join table. """ - + if filename is None: filename = _default_fixture_path_for_table(table, base_dir) _load_data_to_table(table, filename) - + def dump_data(model, filename=None, **params): - """Dumps data to given destination. Params are optional arguments for selecting data. If filename is None, assumes that destination - file is located in fixtures/model_module_name/model_name_lowercase.yaml of your application directory, for example + """Dumps data to given destination. Params are optional arguments for selecting data. If filename is None, assumes that destination + file is located in fixtures/model_module_name/model_name_lowercase.yaml of your application directory, for example MyProject/fixtures/news/newsitem.yaml. """ - + if filename is None: filename = _default_fixture_path_for_model(model) _dump_data_to_file(model, filename, **params) @@ -52,7 +52,7 @@ def _default_fixture_path_for_model(model, base_dir=None): module_dirs = model.__module__.split('.', 2)[-1].split('.') for dir in module_dirs: path = os.path.join(path, dir) - return os.path.join(path, model.table.name + '.json') + return os.path.join(path, model.table.name + '.json') def _default_fixture_path_for_table(table, base_dir=None): if base_dir is None: @@ -66,14 +66,14 @@ def _default_fixture_path_for_table(table, base_dir=None): def _is_fixture_file(filename): basename, ext = os.path.splitext(filename) return (ext.lower() in VALID_FIXTURE_FILE_EXTENSIONS) - + def _load_data_from_dir(model, dirname): for dirpath, dirnames, filenames in os.walk(dirname): for filename in filenames: _load_data_from_file(model, filename) - + def _load_data_from_file(model, filename): - if not _is_fixture_file(filename): + if not _is_fixture_file(filename): return fp = file(filename, 'r') data = simplejson.load(fp) @@ -86,11 +86,11 @@ def _load_data_from_file(model, filename): elif type(data) is types.DictType: retval = {} for key, item in data.iteritems(): - retval[key] = _load_instance_from_dict(model, item) + retval[key] = _load_instance_from_dict(model, item) return retval def _load_data_to_table(tablename, filename): - if not _is_fixture_file(filename): + if not _is_fixture_file(filename): return fp = file(filename, 'r') data = simplejson.load(fp) @@ -104,7 +104,7 @@ def _load_data_to_table(tablename, filename): for key, item in data.iteritems(): table.insert(item).execute() return data - + def _dump_data_to_file(model, filename, **params): if params: queryset = model.select_by(**params) @@ -116,9 +116,9 @@ def _dump_data_to_file(model, filename, **params): fp = file(filename, 'w') simplejson.dump(data, fp) fp.close() - + def _load_instance_from_dict(model, dict): - if not dict: return + if not dict: return instance = model() fields = model._descriptor.fields.keys() for k, v in dict.iteritems(): @@ -135,5 +135,5 @@ def _dump_instance_to_dict(instance): for field in fields: d[field] = getattr(instance, field) return d - + __all__ = ['load_data', 'dump_data']