From c1954705fa0123dc91265098b3e375b4919572de Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Sat, 07 Feb 2009 11:45:22 +0000 Subject: Mix in Documnt with more OOP; add id for all objects --- diff --git a/char.py b/char.py index 59a4460..7855161 100644 --- a/char.py +++ b/char.py @@ -29,37 +29,22 @@ def load(): custom.frames[i] = f class Frame: - def __init__(self, image, type): - self._type = type + def __init__(self, id): + self.id = id self._thumb = None self._orig = None - self._filename = None - - if type == theme.RESTORED: - tmpfile = os.path.join(theme.SESSION_PATH, '.tmp.png') - file(tmpfile, 'w').write(image) - self._orig = theme.pixbuf(tmpfile) - os.unlink(tmpfile) - elif type == theme.CUSTOM: - self._thumb = theme.CUSTOM_FRAME_THUMB - elif type == theme.EMPTY: - self._type = theme.PREINSTALLED - self._thumb = theme.EMPTY_THUMB - self._orig = theme.EMPTY_ORIG - else: - self._filename = image def read(self): - if not self._orig: - return '' - else: + if self._orig: return pixbuf2str(self._orig) + else: + return '' def empty(self): - return self._thumb == theme.EMPTY_THUMB + return False def custom(self): - return self._type != theme.PREINSTALLED + return True def thumb(self): if self._thumb == None: @@ -67,21 +52,61 @@ class Frame: return self._thumb def orig(self): + return self._orig + + def select(self): + return True; + +class PreinstalledFrame(Frame): + def __init__(self, filename): + Frame.__init__(self, filename) + self._filename = filename + + def custom(self): + return False + + def orig(self): if self._orig == None: - if self._type != theme.PREINSTALLED: - return theme.EMPTY_ORIG self._orig = theme.pixbuf(self._filename) return self._orig +class EmptyFrame(Frame): + def __init__(self): + Frame.__init__(self, None) + self._thumb = theme.EMPTY_THUMB + self._orig = theme.EMPTY_ORIG + + def custom(self): + return False + + def empty(self): + return True + +class RestoredFrame(Frame): + def __init__(self, id, data): + Frame.__init__(self, id) + self._orig = theme.str2pixbuf(data) + +class CustomFrame(Frame): + def __init__(self): + Frame.__init__(self, None) + self._thumb = theme.CUSTOM_FRAME_THUMB + + def orig(self): + if self._orig == None: + return theme.EMPTY_ORIG + return self._orig + def select(self): - if self._type != theme.CUSTOM or self._orig: + if self._orig: return True; - self._orig = theme.choose(lambda t, file: theme.pixbuf(file)) - self._thumb = theme.scale(self.orig()) - return self._orig != None - - def filename(self): - return self._filename + self.id, self._orig = theme.choose(lambda jobject: (jobject.object_id, + theme.pixbuf(jobject.file_path)), (None, None)) + if self.id: + self._thumb = theme.scale(self._orig) + return True + else: + return False class Char: def __init__(self, name, thumbfile, dir): @@ -90,16 +115,15 @@ class Char: if dir: for i in sorted(glob.glob(theme.path(dir, '*'))): - self.frames.append(Frame( - os.path.join(dir, os.path.basename(i)), - theme.PREINSTALLED)) + self.frames.append(PreinstalledFrame( + os.path.join(dir, os.path.basename(i)))) for i in range(len(self.frames)-1, theme.FRAME_ROWS*theme.FRAME_COLS): - self.frames.append(Frame(None, theme.EMPTY)) + self.frames.append(EmptyFrame()) self._thumb = theme.pixbuf(thumbfile, theme.THUMB_SIZE) else: for i in range(0, theme.FRAME_ROWS*theme.FRAME_COLS): - self.frames.append(Frame(None, theme.CUSTOM)) + self.frames.append(CustomFrame()) self._thumb = theme.CUSTOM_FRAME_THUMB def thumb(self): @@ -107,7 +131,7 @@ class Char: def clean(self, index): if self.frames[index].custom(): - self.frames[index] = Frame(None, theme.CUSTOM) + self.frames[index] = CustomFrame() THEMES = ( Char(_('Elephant'), 'images/pics/Elephant/bigelephant0.gif', diff --git a/document.py b/document.py index 2af457a..8d43895 100644 --- a/document.py +++ b/document.py @@ -21,27 +21,26 @@ import theme from sound import * from ground import * from utils import * - -from char import Frame +from char import * class Document: tape = [] ground = None sound = None - for i in range(theme.TAPE_COUNT): - tape.append(Frame(None, theme.EMPTY)) + tape.append(EmptyFrame()) def clean(index): from char import Frame - Document.tape[index] = Frame(None, theme.EMPTY) + Document.tape[index] = EmptyFrame() def save(filepath): zip = ZipFile(filepath, 'w') cfg = { 'ground': {}, 'sound' : {}, + 'frames': {}, 'tape' : [] } def _save(node, arcname, value): @@ -51,30 +50,25 @@ def save(filepath): zip.writestr(arcname, value.read()) else: node['custom'] = False - node['name'] = value.name + node['name'] = unicode(value.name) node['id'] = value.id _save(cfg['ground'], 'ground.png', Document.ground) _save(cfg['sound'], 'sound', Document.sound) - arcfiles = {} for i, frame in enumerate( [i for i in set(Document.tape) if not i.empty() and i.custom()]): - arcfiles[frame] = 'frame%03d.png' % i - zip.writestr(arcfiles[frame], frame.read()) + arcname = 'frame%03d.png' % i + cfg['frames'][frame.id] = arcname + zip.writestr(arcname, frame.read()) for i, frame in enumerate(Document.tape): - if frame.empty(): - continue - node = {} - if frame.custom(): - node['custom'] = True - node['filename'] = arcfiles[frame] - else: - node['custom'] = False - node['filename'] = frame.filename() - node['index'] = i - cfg['tape'].append(node) + if not frame.empty(): + node = {} + node['custom'] = frame.custom() + node['id'] = frame.id + node['index'] = i + cfg['tape'].append(node) zip.writestr('MANIFEST', cjson.encode(cfg)) zip.close() @@ -96,19 +90,17 @@ def load(filepath): Document.ground = _load(cfg['ground'], RestoredGround, PreinstalledGround) Document.sound = _load(cfg['sound'], RestoredSound, PreinstalledSound) - loaded = {} + frames = {} + + for id, arcname in cfg['frames'].items(): + frames[id] = RestoredFrame(id, zip.read(arcname)) + for node in cfg['tape']: i = node['index'] - if i >= theme.TAPE_COUNT: - continue - filename = node['filename'] - if node['custom']: - frame = loaded.get(filename) - if not frame: - frame = loaded[filename] = Frame( - zip.read(filename), theme.RESTORED) - Document.tape[i] = frame - else: - Document.tape[i] = Frame(filename, theme.PREINSTALLED) + if i < theme.TAPE_COUNT: + if node['custom']: + Document.tape[i] = frames[node['id']] + else: + Document.tape[i] = PreinstalledFrame(node['id']) zip.close() diff --git a/ground.py b/ground.py index fdf3bf8..4aef242 100644 --- a/ground.py +++ b/ground.py @@ -34,7 +34,7 @@ class Ground: return True def read(self): - pixbuf2str(self._orig) + return theme.pixbuf2str(self._orig) def thumb(self): if not self._thumb: @@ -61,17 +61,19 @@ class CustomGround(Ground): self._orig = theme.pixbuf(filename) def select(self): - return theme.choose(lambda jobject: JournalGround(jobject)) + try: + return theme.choose(lambda jobject: JournalGround(jobject)) + except: + return None class RestoredGround(Ground): def __init__(self, name, id, data): Ground.__init__(self, name, id) - self._orig = str2pixbuf(data) + self._orig = theme.str2pixbuf(data) class JournalGround(Ground): def __init__(self, jobject): - Ground.__init__(self, jobject.props.metadata['title'], - jobject.object_id) + Ground.__init__(self, jobject.metadata['title'], jobject.object_id) self._orig = theme.pixbuf(jobject.file_path) THEMES = [ diff --git a/sound.py b/sound.py index 863595d..7beb9e8 100644 --- a/sound.py +++ b/sound.py @@ -15,6 +15,7 @@ import os import gtk import gst +import shutil from glob import glob from gettext import gettext as _ @@ -97,9 +98,9 @@ class RestoredSound(Sound): class JournalSound(Sound): def __init__(self, jobject): soundfile = os.path.join(theme.SESSION_PATH, jobject.object_id) - Sound.__init__(self, jobject.props.metadata['title'], + Sound.__init__(self, jobject.metadata['title'], jobject.object_id, soundfile, theme.SOUND_CUSTOM) - os.rename(jobject.file_path, soundfile) + shutil.copy(jobject.file_path, soundfile) THEMES = [ PreinstalledSound(_('Gobble'), 'sounds/gobble.wav'), diff --git a/theme.py b/theme.py index a43e58b..1d803a5 100644 --- a/theme.py +++ b/theme.py @@ -20,12 +20,6 @@ from math import ceil from sugar.activity.activity import get_bundle_path, get_activity_root from sugar.graphics import style -PREINSTALLED = 0 -CUSTOM = 1 -JOURNAL = 2 -RESTORED = 3 -EMPTY = 4 - SOUND_SPEAKER = 'images/sounds/speaker.png' SOUND_MUTE = 'images/sounds/custom.png' SOUND_CUSTOM = 'images/sounds/custom.png' @@ -112,7 +106,7 @@ EMPTY_THUMB = scale(EMPTY_ORIG) CUSTOM_FRAME_ORIG = pixbuf('images/pics/custom.png') CUSTOM_FRAME_THUMB = scale(CUSTOM_FRAME_ORIG) -def choose(out_fun): +def choose(out_fun, default=None): from sugar.graphics.objectchooser import ObjectChooser chooser = ObjectChooser() @@ -124,13 +118,13 @@ def choose(out_fun): if result == gtk.RESPONSE_ACCEPT: jobject = chooser.get_selected_object() if jobject and jobject.file_path: - return out_fun(jobject.metadata['title'], jobject.file_path) + return out_fun(jobject) finally: if jobject: jobject.destroy() chooser.destroy() del chooser - return None + return default def pixbuf2str(pixbuf): def push(data, buffer): @@ -144,7 +138,7 @@ def pixbuf2str(pixbuf): def str2pixbuf(data): tmpfile = os.path.join(SESSION_PATH, '.tmp.png') file(tmpfile, 'w').write(data) - out = theme.pixbuf(tmpfile) + out = pixbuf(tmpfile) os.unlink(tmpfile) return out -- cgit v0.9.1