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): |
39 | 39 | if not os.path.exists(dir_path): |
40 | 40 | os.makedirs(dir_path) |
41 | 41 | |
42 | | destination_path = os.path.join(dir_path, 'data') |
| 42 | destination_path = layoutmanager.get_instance().get_data_path(uid) |
43 | 43 | if file_path: |
44 | 44 | if not os.path.isfile(file_path): |
45 | 45 | raise ValueError('No file at %r' % file_path) |
… |
… |
class FileStore(object): |
83 | 83 | deleting this file. |
84 | 84 | |
85 | 85 | """ |
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) |
88 | 87 | if not os.path.exists(file_path): |
89 | 88 | logging.debug('Entry %r doesnt have any file' % uid) |
90 | 89 | return '' |
… |
… |
class FileStore(object): |
145 | 144 | return destination_path |
146 | 145 | |
147 | 146 | 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) |
150 | 148 | |
151 | 149 | def delete(self, uid): |
152 | 150 | """Remove the file associated to a given entry. |
153 | 151 | |
154 | 152 | """ |
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) |
157 | 154 | if os.path.exists(file_path): |
158 | 155 | os.remove(file_path) |
159 | 156 | |
160 | 157 | 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) |
167 | 160 | |
168 | 161 | logging.debug('removing %r' % new_file) |
169 | 162 | os.remove(new_file) |
diff --git a/src/carquinyol/layoutmanager.py b/src/carquinyol/layoutmanager.py
index 42db46f..acee83d 100644
a
|
b
|
class LayoutManager(object): |
65 | 65 | # os.path.join() is just too slow |
66 | 66 | return '%s/%s/%s' % (self._root_path, uid[:2], uid) |
67 | 67 | |
| 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 | |
68 | 74 | def get_root_path(self): |
69 | 75 | return self._root_path |
70 | 76 | |
diff --git a/src/carquinyol/metadatareader.c b/src/carquinyol/metadatareader.c
index 08be17e..73bfe4a 100644
a
|
b
|
|
8 | 8 | static PyObject *byte_array_type = NULL; |
9 | 9 | |
10 | 10 | int |
11 | | add_property(char *metadata_path, char *property_name, PyObject *dict, |
| 11 | add_property(const char *metadata_path, char *property_name, PyObject *dict, |
12 | 12 | int must_exist) |
13 | 13 | { |
14 | 14 | int file_path_size; |
… |
… |
cleanup: |
125 | 125 | } |
126 | 126 | |
127 | 127 | static PyObject * |
128 | | read_from_properties_list (char *metadata_path, PyObject *properties) |
| 128 | read_from_properties_list (const char *metadata_path, PyObject *properties) |
129 | 129 | { |
130 | 130 | PyObject *dict = PyDict_New(); |
131 | 131 | |
… |
… |
cleanup: |
148 | 148 | } |
149 | 149 | |
150 | 150 | static PyObject * |
151 | | read_all_properties (char *metadata_path) |
| 151 | read_all_properties (const char *metadata_path) |
152 | 152 | { |
153 | 153 | PyObject *dict = PyDict_New(); |
154 | 154 | DIR *dir_stream = NULL; |
… |
… |
metadatareader_retrieve(PyObject *unused, PyObject *args) |
198 | 198 | { |
199 | 199 | PyObject *dict = NULL; |
200 | 200 | PyObject *properties = NULL; |
201 | | const char *dir_path = NULL; |
202 | | char *metadata_path = NULL; |
| 201 | const char *metadata_path = NULL; |
203 | 202 | |
204 | | if (!PyArg_ParseTuple(args, "sO:retrieve", &dir_path, &properties)) |
| 203 | if (!PyArg_ParseTuple(args, "sO:retrieve", &metadata_path, &properties)) |
205 | 204 | return NULL; |
206 | 205 | |
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 | | |
216 | 206 | if ((properties != Py_None) && (PyList_Size(properties) > 0)) { |
217 | 207 | dict = read_from_properties_list(metadata_path, properties); |
218 | 208 | } else { |
219 | 209 | dict = read_all_properties(metadata_path); |
220 | 210 | } |
221 | 211 | |
222 | | PyMem_Free(metadata_path); |
223 | | |
224 | 212 | return dict; |
225 | 213 | } |
226 | 214 | |
227 | 215 | static 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")}, |
229 | 219 | {NULL, NULL, 0, NULL} |
230 | 220 | }; |
231 | 221 | |
diff --git a/src/carquinyol/metadatastore.py b/src/carquinyol/metadatastore.py
index 8461ef7..b75c755 100644
a
|
b
|
MAX_SIZE = 256 |
9 | 9 | class MetadataStore(object): |
10 | 10 | |
11 | 11 | 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) |
17 | 13 | if not os.path.exists(metadata_path): |
18 | 14 | os.makedirs(metadata_path) |
19 | 15 | else: |
… |
… |
class MetadataStore(object): |
39 | 35 | f.close() |
40 | 36 | |
41 | 37 | 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) |
44 | 40 | |
45 | 41 | 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) |
48 | 43 | for key in os.listdir(metadata_path): |
49 | 44 | os.remove(os.path.join(metadata_path, key)) |
50 | 45 | os.rmdir(metadata_path) |
51 | 46 | |
52 | 47 | 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) |
55 | 49 | property_path = os.path.join(metadata_path, key) |
56 | 50 | if os.path.exists(property_path): |
57 | 51 | return open(property_path, 'r').read() |
… |
… |
class MetadataStore(object): |
59 | 53 | return None |
60 | 54 | |
61 | 55 | 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) |
64 | 57 | property_path = os.path.join(metadata_path, key) |
65 | 58 | open(property_path, 'w').write(value) |
diff --git a/src/carquinyol/migration.py b/src/carquinyol/migration.py
index 02f8e68..ed82558 100644
a
|
b
|
def migrate_from_0(): |
61 | 61 | |
62 | 62 | def _migrate_metadata(root_path, old_root_path, uid): |
63 | 63 | 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) |
65 | 65 | os.makedirs(metadata_path) |
66 | 66 | |
67 | 67 | old_metadata_path = os.path.join(old_root_path, uid + '.metadata') |
… |
… |
def _migrate_metadata(root_path, old_root_path, uid): |
93 | 93 | |
94 | 94 | def _migrate_file(root_path, old_root_path, uid): |
95 | 95 | 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) |
97 | 97 | os.rename(os.path.join(old_root_path, uid), |
98 | | os.path.join(dir_path, 'data')) |
| 98 | new_data_path) |
99 | 99 | |
100 | 100 | |
101 | 101 | def _migrate_preview(root_path, old_root_path, uid): |
102 | 102 | 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) |
104 | 104 | os.rename(os.path.join(old_root_path, 'preview', uid), |
105 | 105 | os.path.join(metadata_path, 'preview')) |