diff options
Diffstat (limited to 'atoidejouer/db/story.py')
-rw-r--r-- | atoidejouer/db/story.py | 125 |
1 files changed, 91 insertions, 34 deletions
diff --git a/atoidejouer/db/story.py b/atoidejouer/db/story.py index d386563..476b610 100644 --- a/atoidejouer/db/story.py +++ b/atoidejouer/db/story.py @@ -8,28 +8,76 @@ logger = logging.getLogger('atoidejouer') import sqlite3; # atoidejouer import -from atoidejouer.tools import storage +from atoidejouer.tools import image, storage + + +WHERE_COLS = [ + 'id', + 'title', + 'mime_type', + 'time', + 'timestamp' + ] + +SET_COLS = [ + 'layer', + 'x', + 'y', + 'duration', + 'loop' + ] + +INSERT_COLS = [ + 'title', + 'mime_type', + 'time', + 'layer', + 'timestamp', + 'x', + 'y', + 'duration', + 'loop' + ] + +NUMERIC_COLS = [ + 'time', + 'layer', + 'x', + 'y', + 'duration', + 'loop' + ] class Key(object): - def __init__(self, id=None, name=None, mime_type=None, timestamp=None, **kargs): - """ - """ - self.id, self.name, self.mime_type, self.timestamp = id, name, mime_type, timestamp + def __init__(self, id=None, title=None, mime_type=None, timestamp=None, **kargs): + self.id, self.title, self.mime_type, self.timestamp = id, title, mime_type, timestamp # ensure value if select row returns None value for arg in ['time', 'layer', 'x', 'y', 'duration', 'loop', 'path']: setattr(self, arg, kargs[arg] if arg in kargs and kargs[arg] else 0) + def _get_obj(self): + for _ds_obj in storage.journal_query({ + 'mime_type': self.mime_type, + 'title': self.title + }): + return _ds_obj + + def get_preview(self): + if self.mime_type == 'image/png': + _ds_obj = self._get_obj() + return storage.get_pixbuf_from_data( + _ds_obj.metadata['preview'], + size=(64, 48) + ) + else: + _path = storage.get_image_path('sound', dir_='data') + return image.get_pixbuf(_path, 64, 48) + def get_path(self): if self.path == 0: - _ds_obj = None - for _ds_obj in storage.journal_query({ - 'mime_type': self.mime_type, - 'timestamp': self.timestamp, - 'title': self.name - }): - break + _ds_obj = self._get_obj() self.path = _ds_obj.file_path if _ds_obj else 0 return self.path @@ -37,23 +85,23 @@ class Key(object): self.path = path def __repr__(self): - return "%s|name=%s|mime_type=%s|timestamp=%s|time=%s|layer=%s|"\ - "x=%s|y=%s|dur=%s|loop=%s"\ - % (self.id, self.name, self.mime_type, self.timestamp, + return "%s|title=%s|mime_type=%s|timestamp=%s|time=%s|layer=%s|"\ + "x=%s|y=%s|duration=%s|loop=%s"\ + % (self.id, self.title, self.mime_type, self.timestamp, self.time, self.layer, self.x, self.y, self.duration, self.loop) def __cmp__(self, other): return cmp( - (self.name, self.mime_type, self.timestamp, self.time, self.layer), - (other.name, other.mime_type, self.timestamp, other.time, other.layer) + (self.title, self.mime_type, self.timestamp, self.time, self.layer), + (other.title, other.mime_type, self.timestamp, other.time, other.layer) ) def create(self): return "create table story("\ "id integer primary key autoincrement not null,"\ - "name text,"\ + "title text,"\ "mime_type text,"\ "timestamp text,"\ "time integer,"\ @@ -67,34 +115,36 @@ class Key(object): def insert(self): columns = list() values = list() - for c in ['name', 'mime_type', 'timestamp', 'time', 'layer']: + for c in INSERT_COLS: v = getattr(self, c) - if v and v != -1: + if v is not None and v != -1: columns.append(c) - values.append(str(v) if c in ['time', 'layer'] else "'%s'" % v) + values.append(str(v) if c in NUMERIC_COLS else "'%s'" % v) return "insert into story (%s) values (%s)" % ( ",".join(columns), ",".join(values) ) - def _params(self, crit, joiner=" and "): + def _params(self, crit): values = list() - for c in ['name', 'mime_type', 'time', 'layer', 'timestamp']: + joiner = ' and ' if crit == 'where' else ', ' + cols = WHERE_COLS if crit == 'where' else SET_COLS + for c in cols: v = getattr(self, c) - if v and v != -1: - v = v if v in ['time', 'layer'] else "'%s'" % v + if v is not None and v != -1: + v = str(v) if c in NUMERIC_COLS else "'%s'" % v values.append("%s=%s" % (c, v)) return "%s %s" % (crit, joiner.join(values)) def where(self): """Prepares simple where query according OO parameters. """ - return self._params("where") + return self._params('where') def set(self): """Prepares simple where query according OO parameters. """ - return self._params("set", joiner=",") + return self._params('set') def select(self): """Prepares simple select query according OO parameters. @@ -151,15 +201,21 @@ class DB(object): yield self.obj(**row) row = cur.fetchone() - def all(self): + def _all(self, query=None): cur = self.con.cursor() - cur.execute("select * from story") + query = "select * from %s" % self.name\ + if query is None\ + else query + cur.execute(query) for obj in self._fetch(cur): yield obj cur.close() def get(self, obj): cur = self.con.cursor() + # DEBUG + # logger.debug('[db.story] get - query: %s' % obj.select()) + # DEBUG cur.execute(obj.select()) for obj in self._fetch(cur): yield obj @@ -169,11 +225,12 @@ class DB(object): for one in self.get(obj): return one - def get_layout_max(self): - return 10 - - def get_duration_max(self): - return 10 + def get_max(self, max_): + query = "select %(max_)s from %(name)s order by -%(max_)s"\ + % {'name': self.name, 'max_': max_} + for obj in self._all(query=query): + return getattr(obj, max_) + return 0 def update(self, obj): cur = self.con.cursor() |