Commit from One Laptop Per Child: Translation System by user HoboPrimate. 31 of 31...
[journal-activity.git] / journalentrybundle.py
blobb48d9dc50ef448ce9745da7892dfa8c13d1e7790
1 # Copyright (C) 2007, One Laptop Per Child
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 import os
18 import tempfile
19 import logging
20 import shutil
22 import json
24 import dbus
25 from sugar.datastore import datastore
26 from sugar.bundle.bundle import Bundle, MalformedBundleException, \
27 NotInstalledException
29 class JournalEntryBundle(Bundle):
30 """A Journal entry bundle
32 See http://wiki.laptop.org/go/Journal_entry_bundles for details
33 """
35 MIME_TYPE = 'application/vnd.olpc-journal-entry'
37 _zipped_extension = '.xoj'
38 _unzipped_extension = None
39 _infodir = None
41 def __init__(self, path):
42 Bundle.__init__(self, path)
44 def install(self):
45 if os.environ.has_key('SUGAR_ACTIVITY_ROOT'):
46 install_dir = os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'], 'data')
47 else:
48 install_dir = tempfile.gettempdir()
49 bundle_dir = os.path.join(install_dir, self._zip_root_dir)
50 uid = self._zip_root_dir
51 self._unzip(install_dir)
52 try:
53 metadata = self._read_metadata(bundle_dir)
54 jobject = datastore.create()
55 try:
56 for key, value in metadata.iteritems():
57 jobject.metadata[key] = value
59 preview = self._read_preview(uid, bundle_dir)
60 if preview is not None:
61 jobject.metadata['preview'] = dbus.ByteArray(preview)
63 jobject.metadata['uid'] = ''
64 jobject.file_path = os.path.join(bundle_dir, uid)
65 datastore.write(jobject)
66 finally:
67 jobject.destroy()
68 finally:
69 shutil.rmtree(bundle_dir, ignore_errors=True)
71 def _read_metadata(self, bundle_dir):
72 metadata_path = os.path.join(bundle_dir, '_metadata.json')
73 if not os.path.exists(metadata_path):
74 raise MalformedBundleException('Bundle must contain the file "_metadata.json".')
75 f = open(metadata_path, 'r')
76 try:
77 json_data = f.read()
78 finally:
79 f.close()
80 return json.read(json_data)
82 def _read_preview(self, uid, bundle_dir):
83 preview_path = os.path.join(bundle_dir, 'preview', uid)
84 if not os.path.exists(preview_path):
85 return ''
86 f = open(preview_path, 'r')
87 try:
88 preview_data = f.read()
89 finally:
90 f.close()
91 return preview_data
93 def is_installed(self):
94 # These bundles can be reinstalled as many times as desired.
95 return False