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
25 from sugar
.datastore
import datastore
26 from sugar
.bundle
.bundle
import Bundle
, MalformedBundleException
, \
29 class JournalEntryBundle(Bundle
):
30 """A Journal entry bundle
32 See http://wiki.laptop.org/go/Journal_entry_bundles for details
35 MIME_TYPE
= 'application/vnd.olpc-journal-entry'
37 _zipped_extension
= '.xoj'
38 _unzipped_extension
= None
41 def __init__(self
, path
):
42 Bundle
.__init
__(self
, path
)
45 if os
.environ
.has_key('SUGAR_ACTIVITY_ROOT'):
46 install_dir
= os
.path
.join(os
.environ
['SUGAR_ACTIVITY_ROOT'], 'data')
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
)
53 metadata
= self
._read
_metadata
(bundle_dir
)
54 jobject
= datastore
.create()
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
)
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')
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
):
86 f
= open(preview_path
, 'r')
88 preview_data
= f
.read()
93 def is_installed(self
):
94 # These bundles can be reinstalled as many times as desired.