Ticket #1207: 0001-use-layoutmanager-as-much-as-possible.patch

File 0001-use-layoutmanager-as-much-as-possible.patch, 9.1 KB (added by sascha_silbe, 15 years ago)

use layoutmanager as much as possible

  • src/carquinyol/filestore.py

    From fe0e8d10f7b6fa48bbb719c96efe42e75a27b301 Mon Sep 17 00:00:00 2001
    From: Sascha Silbe <sascha@silbe.org>
    Date: Wed, 19 Aug 2009 15:01:50 +0200
    Subject: [PATCH] use layoutmanager as much as possible
    
    ---
     src/carquinyol/filestore.py     |   19 ++++++-------------
     src/carquinyol/layoutmanager.py |    6 ++++++
     src/carquinyol/metadatareader.c |   26 ++++++++------------------
     src/carquinyol/metadatastore.py |   19 ++++++-------------
     src/carquinyol/migration.py     |    8 ++++----
     5 files changed, 30 insertions(+), 48 deletions(-)
    
    diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py
    index b96c323..0e018bd 100644
    a b class FileStore(object): 
    3939        if not os.path.exists(dir_path):
    4040            os.makedirs(dir_path)
    4141
    42         destination_path = os.path.join(dir_path, 'data')
     42        destination_path = layoutmanager.get_instance().get_data_path(uid)
    4343        if file_path:
    4444            if not os.path.isfile(file_path):
    4545                raise ValueError('No file at %r' % file_path)
    class FileStore(object): 
    8383           deleting this file.
    8484
    8585        """
    86         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    87         file_path = os.path.join(dir_path, 'data')
     86        file_path = layoutmanager.get_instance().get_data_path(uid)
    8887        if not os.path.exists(file_path):
    8988            logging.debug('Entry %r doesnt have any file' % uid)
    9089            return ''
    class FileStore(object): 
    145144        return destination_path
    146145
    147146    def get_file_path(self, uid):
    148         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    149         return os.path.join(dir_path, 'data')
     147        return layoutmanager.get_instance().get_data_path(uid)
    150148
    151149    def delete(self, uid):
    152150        """Remove the file associated to a given entry.
    153151
    154152        """
    155         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    156         file_path = os.path.join(dir_path, 'data')
     153        file_path = layoutmanager.get_instance().get_data_path(uid)
    157154        if os.path.exists(file_path):
    158155            os.remove(file_path)
    159156
    160157    def hard_link_entry(self, new_uid, existing_uid):
    161         existing_file = os.path.join(
    162                 layoutmanager.get_instance().get_entry_path(existing_uid),
    163                 'data')
    164         new_file = os.path.join(
    165                 layoutmanager.get_instance().get_entry_path(new_uid),
    166                 'data')
     158        existing_file = layoutmanager.get_instance().get_data_path(existing_uid)
     159        new_file = layoutmanager.get_instance().get_data_path(new_uid)
    167160
    168161        logging.debug('removing %r' % new_file)
    169162        os.remove(new_file)
  • src/carquinyol/layoutmanager.py

    diff --git a/src/carquinyol/layoutmanager.py b/src/carquinyol/layoutmanager.py
    index 42db46f..acee83d 100644
    a b class LayoutManager(object): 
    6565        # os.path.join() is just too slow
    6666        return '%s/%s/%s' % (self._root_path, uid[:2], uid)
    6767
     68    def get_data_path(self, uid):
     69        return '%s/%s/%s/data' % (self._root_path, uid[:2], uid)
     70
     71    def get_metadata_path(self, uid):
     72        return '%s/%s/%s/metadata' % (self._root_path, uid[:2], uid)
     73
    6874    def get_root_path(self):
    6975        return self._root_path
    7076
  • src/carquinyol/metadatareader.c

    diff --git a/src/carquinyol/metadatareader.c b/src/carquinyol/metadatareader.c
    index 08be17e..73bfe4a 100644
    a b  
    88static PyObject *byte_array_type = NULL;
    99
    1010int
    11 add_property(char *metadata_path, char *property_name, PyObject *dict,
     11add_property(const char *metadata_path, char *property_name, PyObject *dict,
    1212             int must_exist)
    1313{
    1414    int file_path_size;
    cleanup: 
    125125}
    126126
    127127static PyObject *
    128 read_from_properties_list (char *metadata_path, PyObject *properties)
     128read_from_properties_list (const char *metadata_path, PyObject *properties)
    129129{
    130130    PyObject *dict = PyDict_New();
    131131
    cleanup: 
    148148}
    149149
    150150static PyObject *
    151 read_all_properties (char *metadata_path)
     151read_all_properties (const char *metadata_path)
    152152{
    153153    PyObject *dict = PyDict_New();
    154154        DIR *dir_stream = NULL;
    metadatareader_retrieve(PyObject *unused, PyObject *args) 
    198198{
    199199    PyObject *dict = NULL;
    200200    PyObject *properties = NULL;
    201     const char *dir_path = NULL;
    202     char *metadata_path = NULL;
     201    const char *metadata_path = NULL;
    203202
    204     if (!PyArg_ParseTuple(args, "sO:retrieve", &dir_path, &properties))
     203    if (!PyArg_ParseTuple(args, "sO:retrieve", &metadata_path, &properties))
    205204        return NULL;
    206205
    207     // Build path to the metadata directory
    208     int metadata_path_size = strlen(dir_path) + 10;
    209     metadata_path = PyMem_Malloc(metadata_path_size);
    210         if (metadata_path == NULL) {
    211         PyErr_NoMemory();
    212         return NULL;
    213         }
    214     snprintf (metadata_path, metadata_path_size, "%s/%s", dir_path, "metadata");
    215 
    216206    if ((properties != Py_None) && (PyList_Size(properties) > 0)) {
    217207        dict = read_from_properties_list(metadata_path, properties);
    218208    } else {
    219209        dict = read_all_properties(metadata_path);
    220210    }
    221211
    222     PyMem_Free(metadata_path);
    223 
    224212    return dict;
    225213}
    226214
    227215static PyMethodDef metadatareader_functions[] = {
    228     {"retrieve", metadatareader_retrieve, METH_VARARGS, PyDoc_STR("Read a dictionary from a file")},
     216    {"retrieve", metadatareader_retrieve, METH_VARARGS,
     217        PyDoc_STR("Read a dictionary from a directory with a single file " \
     218              "(containing the content) per key")},
    229219    {NULL, NULL, 0, NULL}
    230220};
    231221
  • src/carquinyol/metadatastore.py

    diff --git a/src/carquinyol/metadatastore.py b/src/carquinyol/metadatastore.py
    index 8461ef7..b75c755 100644
    a b MAX_SIZE = 256 
    99class MetadataStore(object):
    1010
    1111    def store(self, uid, metadata):
    12         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    13         if not os.path.exists(dir_path):
    14             os.makedirs(dir_path)
    15 
    16         metadata_path = os.path.join(dir_path, 'metadata')
     12        metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    1713        if not os.path.exists(metadata_path):
    1814            os.makedirs(metadata_path)
    1915        else:
    class MetadataStore(object): 
    3935                f.close()
    4036
    4137    def retrieve(self, uid, properties=None):
    42         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    43         return metadatareader.retrieve(dir_path, properties)
     38        metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
     39        return metadatareader.retrieve(metadata_path, properties)
    4440
    4541    def delete(self, uid):
    46         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    47         metadata_path = os.path.join(dir_path, 'metadata')
     42        metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    4843        for key in os.listdir(metadata_path):
    4944            os.remove(os.path.join(metadata_path, key))
    5045        os.rmdir(metadata_path)
    5146
    5247    def get_property(self, uid, key):
    53         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    54         metadata_path = os.path.join(dir_path, 'metadata')
     48        metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    5549        property_path = os.path.join(metadata_path, key)
    5650        if os.path.exists(property_path):
    5751            return open(property_path, 'r').read()
    class MetadataStore(object): 
    5953            return None
    6054
    6155    def set_property(self, uid, key, value):
    62         dir_path = layoutmanager.get_instance().get_entry_path(uid)
    63         metadata_path = os.path.join(dir_path, 'metadata')
     56        metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    6457        property_path = os.path.join(metadata_path, key)
    6558        open(property_path, 'w').write(value)
  • src/carquinyol/migration.py

    diff --git a/src/carquinyol/migration.py b/src/carquinyol/migration.py
    index 02f8e68..ed82558 100644
    a b def migrate_from_0(): 
    6161
    6262def _migrate_metadata(root_path, old_root_path, uid):
    6363    dir_path = layoutmanager.get_instance().get_entry_path(uid)
    64     metadata_path = os.path.join(dir_path, 'metadata')
     64    metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    6565    os.makedirs(metadata_path)
    6666
    6767    old_metadata_path = os.path.join(old_root_path, uid + '.metadata')
    def _migrate_metadata(root_path, old_root_path, uid): 
    9393
    9494def _migrate_file(root_path, old_root_path, uid):
    9595    if os.path.exists(os.path.join(old_root_path, uid)):
    96         dir_path = layoutmanager.get_instance().get_entry_path(uid)
     96        new_data_path = layoutmanager.get_instance().get_data_path(uid)
    9797        os.rename(os.path.join(old_root_path, uid),
    98                   os.path.join(dir_path, 'data'))
     98                  new_data_path)
    9999
    100100
    101101def _migrate_preview(root_path, old_root_path, uid):
    102102    dir_path = layoutmanager.get_instance().get_entry_path(uid)
    103     metadata_path = os.path.join(dir_path, 'metadata')
     103    metadata_path = layoutmanager.get_instance().get_metadata_path(uid)
    104104    os.rename(os.path.join(old_root_path, 'preview', uid),
    105105              os.path.join(metadata_path, 'preview'))