From 07b74725415a2c501383b9f074d18519ac817887 Mon Sep 17 00:00:00 2001 From: Sascha Silbe Date: Wed, 19 Aug 2009 13:01:50 +0000 Subject: use layoutmanager as much as possible --- diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py index b96c323..0e018bd 100644 --- a/src/carquinyol/filestore.py +++ b/src/carquinyol/filestore.py @@ -39,7 +39,7 @@ class FileStore(object): if not os.path.exists(dir_path): os.makedirs(dir_path) - destination_path = os.path.join(dir_path, 'data') + destination_path = layoutmanager.get_instance().get_data_path(uid) if file_path: if not os.path.isfile(file_path): raise ValueError('No file at %r' % file_path) @@ -83,8 +83,7 @@ class FileStore(object): deleting this file. """ - dir_path = layoutmanager.get_instance().get_entry_path(uid) - file_path = os.path.join(dir_path, 'data') + file_path = layoutmanager.get_instance().get_data_path(uid) if not os.path.exists(file_path): logging.debug('Entry %r doesnt have any file' % uid) return '' @@ -145,25 +144,19 @@ class FileStore(object): return destination_path def get_file_path(self, uid): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - return os.path.join(dir_path, 'data') + return layoutmanager.get_instance().get_data_path(uid) def delete(self, uid): """Remove the file associated to a given entry. """ - dir_path = layoutmanager.get_instance().get_entry_path(uid) - file_path = os.path.join(dir_path, 'data') + file_path = layoutmanager.get_instance().get_data_path(uid) if os.path.exists(file_path): os.remove(file_path) def hard_link_entry(self, new_uid, existing_uid): - existing_file = os.path.join( - layoutmanager.get_instance().get_entry_path(existing_uid), - 'data') - new_file = os.path.join( - layoutmanager.get_instance().get_entry_path(new_uid), - 'data') + existing_file = layoutmanager.get_instance().get_data_path(existing_uid) + new_file = layoutmanager.get_instance().get_data_path(new_uid) logging.debug('removing %r' % new_file) os.remove(new_file) diff --git a/src/carquinyol/layoutmanager.py b/src/carquinyol/layoutmanager.py index 42db46f..acee83d 100644 --- a/src/carquinyol/layoutmanager.py +++ b/src/carquinyol/layoutmanager.py @@ -65,6 +65,12 @@ class LayoutManager(object): # os.path.join() is just too slow return '%s/%s/%s' % (self._root_path, uid[:2], uid) + def get_data_path(self, uid): + return '%s/%s/%s/data' % (self._root_path, uid[:2], uid) + + def get_metadata_path(self, uid): + return '%s/%s/%s/metadata' % (self._root_path, uid[:2], uid) + def get_root_path(self): return self._root_path diff --git a/src/carquinyol/metadatareader.c b/src/carquinyol/metadatareader.c index 08be17e..73bfe4a 100644 --- a/src/carquinyol/metadatareader.c +++ b/src/carquinyol/metadatareader.c @@ -8,7 +8,7 @@ static PyObject *byte_array_type = NULL; int -add_property(char *metadata_path, char *property_name, PyObject *dict, +add_property(const char *metadata_path, char *property_name, PyObject *dict, int must_exist) { int file_path_size; @@ -125,7 +125,7 @@ cleanup: } static PyObject * -read_from_properties_list (char *metadata_path, PyObject *properties) +read_from_properties_list (const char *metadata_path, PyObject *properties) { PyObject *dict = PyDict_New(); @@ -148,7 +148,7 @@ cleanup: } static PyObject * -read_all_properties (char *metadata_path) +read_all_properties (const char *metadata_path) { PyObject *dict = PyDict_New(); DIR *dir_stream = NULL; @@ -198,34 +198,24 @@ metadatareader_retrieve(PyObject *unused, PyObject *args) { PyObject *dict = NULL; PyObject *properties = NULL; - const char *dir_path = NULL; - char *metadata_path = NULL; + const char *metadata_path = NULL; - if (!PyArg_ParseTuple(args, "sO:retrieve", &dir_path, &properties)) + if (!PyArg_ParseTuple(args, "sO:retrieve", &metadata_path, &properties)) return NULL; - // Build path to the metadata directory - int metadata_path_size = strlen(dir_path) + 10; - metadata_path = PyMem_Malloc(metadata_path_size); - if (metadata_path == NULL) { - PyErr_NoMemory(); - return NULL; - } - snprintf (metadata_path, metadata_path_size, "%s/%s", dir_path, "metadata"); - if ((properties != Py_None) && (PyList_Size(properties) > 0)) { dict = read_from_properties_list(metadata_path, properties); } else { dict = read_all_properties(metadata_path); } - PyMem_Free(metadata_path); - return dict; } static PyMethodDef metadatareader_functions[] = { - {"retrieve", metadatareader_retrieve, METH_VARARGS, PyDoc_STR("Read a dictionary from a file")}, + {"retrieve", metadatareader_retrieve, METH_VARARGS, + PyDoc_STR("Read a dictionary from a directory with a single file " \ + "(containing the content) per key")}, {NULL, NULL, 0, NULL} }; diff --git a/src/carquinyol/metadatastore.py b/src/carquinyol/metadatastore.py index 8461ef7..b75c755 100644 --- a/src/carquinyol/metadatastore.py +++ b/src/carquinyol/metadatastore.py @@ -9,11 +9,7 @@ MAX_SIZE = 256 class MetadataStore(object): def store(self, uid, metadata): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - if not os.path.exists(dir_path): - os.makedirs(dir_path) - - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) if not os.path.exists(metadata_path): os.makedirs(metadata_path) else: @@ -39,19 +35,17 @@ class MetadataStore(object): f.close() def retrieve(self, uid, properties=None): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - return metadatareader.retrieve(dir_path, properties) + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) + return metadatareader.retrieve(metadata_path, properties) def delete(self, uid): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) for key in os.listdir(metadata_path): os.remove(os.path.join(metadata_path, key)) os.rmdir(metadata_path) def get_property(self, uid, key): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) property_path = os.path.join(metadata_path, key) if os.path.exists(property_path): return open(property_path, 'r').read() @@ -59,7 +53,6 @@ class MetadataStore(object): return None def set_property(self, uid, key, value): - dir_path = layoutmanager.get_instance().get_entry_path(uid) - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) property_path = os.path.join(metadata_path, key) open(property_path, 'w').write(value) diff --git a/src/carquinyol/migration.py b/src/carquinyol/migration.py index 02f8e68..ed82558 100644 --- a/src/carquinyol/migration.py +++ b/src/carquinyol/migration.py @@ -61,7 +61,7 @@ def migrate_from_0(): def _migrate_metadata(root_path, old_root_path, uid): dir_path = layoutmanager.get_instance().get_entry_path(uid) - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) os.makedirs(metadata_path) old_metadata_path = os.path.join(old_root_path, uid + '.metadata') @@ -93,13 +93,13 @@ def _migrate_metadata(root_path, old_root_path, uid): def _migrate_file(root_path, old_root_path, uid): if os.path.exists(os.path.join(old_root_path, uid)): - dir_path = layoutmanager.get_instance().get_entry_path(uid) + new_data_path = layoutmanager.get_instance().get_data_path(uid) os.rename(os.path.join(old_root_path, uid), - os.path.join(dir_path, 'data')) + new_data_path) def _migrate_preview(root_path, old_root_path, uid): dir_path = layoutmanager.get_instance().get_entry_path(uid) - metadata_path = os.path.join(dir_path, 'metadata') + metadata_path = layoutmanager.get_instance().get_metadata_path(uid) os.rename(os.path.join(old_root_path, 'preview', uid), os.path.join(metadata_path, 'preview')) -- cgit v0.9.1