Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/tools/storage.py
diff options
context:
space:
mode:
Diffstat (limited to 'atoidejouer/tools/storage.py')
-rw-r--r--atoidejouer/tools/storage.py108
1 files changed, 71 insertions, 37 deletions
diff --git a/atoidejouer/tools/storage.py b/atoidejouer/tools/storage.py
index c8db5ac..0164053 100644
--- a/atoidejouer/tools/storage.py
+++ b/atoidejouer/tools/storage.py
@@ -1,12 +1,15 @@
# python import
-import logging, os, shutil, tarfile, time
+import dbus, logging, os, shutil, tarfile, time
# ..
from gettext import gettext as _
# gtk import
import gtk
+# png import from pypng
+from lib import png
+
# sugar import
from sugar.activity import activity
from sugar.datastore import datastore
@@ -20,6 +23,10 @@ ACTIVITY_NAMES = {
}
+def get_tmp_path(ext='png'):
+ return os.path.join(activity.get_activity_root(), 'tmp.%s' % ext)
+
+
def get_config_path():
# get bundle path
_bundle_path = activity.get_bundle_path()
@@ -86,6 +93,64 @@ def get_pixbuf_from_data(data, image_type=None, size=None):
return _loader.get_pixbuf()
+def png_from_pixbuf(filename, activity_id, timestamp):
+ # prepare outpath
+ _out_path = get_image_path(filename)
+ if os.path.exists(_out_path):
+ return
+ else:
+ pass
+ # prepare inpath
+ _in_path = get_path_from_journal(activity_id, timestamp)
+ # init png reader
+ _reader = png.Reader(filename=_in_path)
+ # read the file
+ _w, _h, _pixels, _metadata = _reader.read()
+ # init working vars
+ _new_pixels = list()
+ _first_color = None
+ # update vars
+ for _i, _row in enumerate(_pixels):
+ # init new row
+ _new_row = list()
+ for _j, _col in enumerate(_row):
+ # upate rgb
+ if _j % 3 == 0:
+ _rgb = [_col]
+ continue
+ else:
+ _rgb.append(_col)
+ # update color first and after
+ if _j % 3 == 2:
+ # keep the first color
+ if _first_color is None:
+ _first_color = _rgb
+ else:
+ pass
+ # make it alpha if first
+ if _rgb == _first_color:
+ _new_row.extend([0, 0, 0, 0])
+ else:
+ _rgb.append(255)
+ _new_row.extend(_rgb)
+ else:
+ continue
+ # add new row
+ _new_pixels.append(_new_row)
+ # update alpha meta
+ _metadata['alpha'] = True
+ _metadata['planes'] = 4
+ # write the new image with alpha
+ _new_png = open(_out_path, 'wb')
+ _writer = png.Writer(_w, _h, **_metadata)
+ _writer.write(_new_png, _new_pixels)
+ _new_png.close()
+ # just in case
+ del _reader
+ del _pixels
+ del _new_pixels
+
+
def get_journal_objects(activity_name):
# prepare query
_query = {'activity': ACTIVITY_NAMES[activity_name]}
@@ -117,12 +182,6 @@ def list_info_from_journal(activity_name):
# init title reg
else:
_titles[_t] = 1
-
- # DEBUG
- logger.debug('[utils] _info_from_jnl - activity_id: %s' % _m['activity_id'])
- logger.debug('[utils] _info_from_jnl - timestamp: %s' % _m['timestamp'])
- # DEBUG
-
# ensure info
yield {
'activity_id' : _m['activity_id'],
@@ -142,46 +201,21 @@ def list_files_from_journal(activity_name):
yield _o.get_file_path()
-def get_file_from_journal(activity_name, activity_id, timestamp):
+def get_path_from_journal(activity_id, timestamp):
# prepare query
_query = {
- 'activity': ACTIVITY_NAMES[activity_name],
- 'activity_id': activity_id,
- 'timestamp': timestamp,
+ 'activity_id': str(activity_id),
+ 'timestamp': int(timestamp)
}
# find in ds
_results, _count = datastore.find(_query)
+ # ..
if _count == 1:
# get path
- _path = _results[0].get_file_path()
+ return _results[0].get_file_path()
else:
return None
- # ...
- _temp = os.path.join('/home', 'florent', '%s.tmp' % activity_name)
-
- # DEBUG
- logger.debug('[utils] _file_from_jnl - _temp: %s' % _temp)
- # DEBUG
-
- # ...
- os.link(_path, _temp)
-
- # DEBUG
- logger.debug('[utils] _file_from_jnl - _path: %s' % _path)
- # DEBUG
-
- _f = open(_temp)
-
- # DEBUG
- logger.debug('[utils] _file_from_jnl - _len: %s' % len(_f.read()))
- # DEBUG
-
- _f.close()
-
- # return it
- return _results
-
def __check_dir(dir_name, parent='data'):
# get activity path