diff options
author | Aleksey Lim <alsroot@member.fsf.org> | 2009-03-01 22:51:59 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@member.fsf.org> | 2009-03-02 06:32:19 (GMT) |
commit | fd1c197ee8e860c743556ecf15fe930bb5283464 (patch) | |
tree | 30066cad7f2d46f3efdf2c62d4c23e48e7f2c066 | |
parent | 13fb8a2f7bdc0c5d884398e6a67c61d001e62ac7 (diff) |
Load/Save jobject
-rw-r--r-- | activity.py | 23 | ||||
-rw-r--r-- | book.py | 56 |
2 files changed, 49 insertions, 30 deletions
diff --git a/activity.py b/activity.py index c6b8655..9f3f2d5 100644 --- a/activity.py +++ b/activity.py @@ -39,20 +39,10 @@ class InfoslicerActivity(SharedActivity): self.connect('init', self._init_cb) self.connect('tube', self._tube_cb) - def read_file(self, filepath): - pass - - def write_file(self, filepath): - # XXX this method will be invoked on every props.status changing - # thus ignore it and use can_close() instead to save data on exit - pass - - def can_close(self): - book.teardown() - return True - def _init_cb(self, sender): - book.init() + book.wiki = book.WikiBook() + if not book.custom: + book.custom = book.CustomBook() self.edit_page = 1 self.edit = edit.View() @@ -76,6 +66,13 @@ class InfoslicerActivity(SharedActivity): toolbox.set_current_toolbar(1) self.show_all() + def read_file(self, filepath): + book.custom = book.CustomBook(filepath) + + def write_file(self, filepath): + book.wiki.sync() + book.custom.sync(filepath) + def set_edit_sensitive(self, enable): self.edit_bar.props.sensitive = enable self.edit_page = (enable and 1 or 2) @@ -19,6 +19,7 @@ import logging import gobject import cjson import shutil +import zipfile from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT from gettext import gettext as _ @@ -126,9 +127,13 @@ class Book(gobject.GObject): index.write(cjson.encode(data)) index.close() - def __init__(self, preinstalled, dirname): + def sync(self): + self.sync_article() + self.sync_index() + + def __init__(self, preinstalled, root): gobject.GObject.__init__(self) - self.root = os.path.join(get_activity_root(), dirname) + self.root = root self.index = [] self.uid = None self.revision = 0 @@ -149,7 +154,7 @@ class Book(gobject.GObject): if not self.uid: self.uid = str(uuid.uuid1()) self.revision = 1 - os.makedirs(self.root, 0777) + os.makedirs(self.root, 0775) for i in preinstalled: filepath = os.path.join(get_bundle_path(), 'examples', i[1]) @@ -207,21 +212,38 @@ class WikiBook(Book): (_('Tiger (from en.wikipedia.org)'), "tiger-wikipedia.dita"), (_('Giraffe (from en.wikipedia.org)'), "giraffe-wikipedia.dita"), (_('Zebra (from en.wikipedia.org)'), "zebra-wikipedia.dita") ] - Book.__init__(self, PREINSTALLED, 'data/book') + + root = os.path.join(get_activity_root(), 'data', 'book') + Book.__init__(self, PREINSTALLED, root) class CustomBook(Book): - def __init__(self): + def __init__(self, filepath=None): PREINSTALLED = [ (_('Giraffe'), "giraffe-blank.dita") ] - Book.__init__(self, PREINSTALLED, 'tmp/book') - -def init(): - global wiki, custom - wiki = WikiBook() - custom = CustomBook() - -def teardown(): - wiki.sync_article() - wiki.sync_index() - custom.sync_article() - custom.sync_index() + + root = os.path.join(get_activity_root(), 'tmp', 'book') + shutil.rmtree(root, True) + + if not filepath: + Book.__init__(self, PREINSTALLED, root) + else: + zip = zipfile.ZipFile(filepath, 'r') + for i in zip.namelist(): + path = os.path.join(root, i) + os.makedirs(os.path.dirname(path), 0775) + file(path, 'wb').write(zip.read(i)) + zip.close() + + Book.__init__(self, [], root) + + def sync(self, filepath): + Book.sync(self) + + logger.debug('close: save to %s' % filepath) + + zip = zipfile.ZipFile(filepath, 'w') + for root, dirs, files in os.walk(self.root): + relpath = root.replace(self.root, '', 1) + for i in files: + zip.write(os.path.join(root, i), os.path.join(relpath, i)) + zip.close() |