Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcfr/tools/storage.py
diff options
context:
space:
mode:
Diffstat (limited to 'olpcfr/tools/storage.py')
-rw-r--r--olpcfr/tools/storage.py150
1 files changed, 150 insertions, 0 deletions
diff --git a/olpcfr/tools/storage.py b/olpcfr/tools/storage.py
new file mode 100644
index 0000000..3e6ebbf
--- /dev/null
+++ b/olpcfr/tools/storage.py
@@ -0,0 +1,150 @@
+# python import
+import os
+
+# olpcfr import
+from olpcfr import BUNDLE, ROOT
+
+
+ACTIVITY_NAMES = {
+ 'paint': 'org.laptop.Oficina',
+ 'record': 'org.laptop.RecordActivity',
+ }
+
+
+def check_dir(path):
+ if os.path.exists(path):
+ pass
+ else:
+ os.mkdir(path)
+
+
+def check_file(path):
+ if os.path.exists(path):
+ pass
+ else:
+ _f = open(path, 'wb')
+ _f.write('')
+ _f.close()
+
+
+def get_path(path=None, bundle=True):
+ # ..
+ path = 'static' if path is None else path
+ # ..
+ if bundle is True:
+ return os.path.join(BUNDLE, path)
+ else:
+ return os.path.join(ROOT, path)
+
+
+def list_dir(path=None, bundle=True):
+ # ..
+ path = get_path(path=path, bundle=bundle)
+ # ..
+ return os.listdir(path)
+
+
+def get_sound_path(filename, path=None, bundle=True, ext=None):
+ # ..
+ path = get_path(path=path, bundle=bundle)
+ filename = filename if ext is None else '%s.%s' % (filename, ext)
+ # ..
+ return os.path.join(path, filename)
+
+
+def get_sound_path(filename, path=None, bundle=True, ext='ogg'):
+ # ..
+ return get_sound_path(filename, path=path, bundle=bundle, ext=ext)
+
+
+def get_image_path(filename, path=None, bundle=True, ext='png'):
+ # ..
+ return get_sound_path(filename, path=path, bundle=bundle, ext=ext)
+
+
+def __do_query(query):
+ from sugar.datastore import datastore
+ # find in ds
+ _results, _count = datastore.find(query, sorting='timestamp')
+ for _r in _results:
+ # get meta
+ _m = _r.get_metadata()
+ if 'activity' in query:
+ yield _r
+ elif _m['activity'] == '':
+ yield _r
+ else:
+ continue
+
+
+def get_journal_objects(activity_name=None, mime_type=None):
+ # init
+ _query = dict()
+ # prepare query name
+ if activity_name is None\
+ and mime_type is None:
+ return []
+ elif mime_type is None:
+ return __do_query({'activity': ACTIVITY_NAMES[activity_name]})
+ else:
+ return __do_query({'mime_type': mime_type})
+
+
+def list_info_from_journal(activity_name=None, mime_type=None):
+ # get objects first
+ _objs = get_journal_objects(activity_name=activity_name, mime_type=mime_type)
+ # make unique titles
+ _titles = {}
+ # return infos
+ for _o in _objs:
+ # get meta
+ _m = _o.get_metadata()
+ # get title
+ _t = _m['title']
+ # ensure description
+ _d = _m['description'] if 'description' in _m else ''
+ _p = _m['preview'] if 'preview' in _m else None
+ # little check
+ if _t in _titles:
+ # udpate reg
+ _titles[_t] += 1
+ # update value to show
+ _t = '%s (%s)' % (_t, _titles[_t])
+ # init title reg
+ else:
+ _titles[_t] = 1
+ # ensure info
+ yield {
+ 'activity_id' : _m['activity_id'],
+ 'description' : _d,
+ 'timestamp' : _m['timestamp'],
+ 'preview' : _p,
+ 'title' : _t,
+ }
+
+
+def list_files_from_journal(activity_name=None, mime_type=None):
+ # get objects first
+ _objs = get_journal_objects(activity_name=activity_name,
+ mime_type=mime_type)
+ # return paths
+ for _o in _objs:
+ # TODO open the files
+ yield _o.get_file_path()
+
+
+def get_path_from_journal(timestamp, mime_type):
+ from sugar.datastore import datastore
+ # ..
+ _query = {
+ 'timestamp': int(timestamp),
+ 'mime_type': mime_type
+ }
+ # find in ds
+ _results, _count = datastore.find(_query)
+ # ..
+ if _count == 1:
+ # get path
+ return _results[0].get_file_path()
+ else:
+ return None