diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2011-10-09 00:34:18 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2011-10-09 00:34:18 (GMT) |
commit | 9427e19af9e0d0dfada5130c3f0c833a2fbb322e (patch) | |
tree | 6928a261ab66845290c92a54f13693c76b1f4eba /model.py | |
parent | 96729953028b3f6cdfef61bfbc173cfb65b3ec19 (diff) |
Initial persistence
Diffstat (limited to 'model.py')
-rw-r--r-- | model.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/model.py b/model.py new file mode 100644 index 0000000..676874c --- /dev/null +++ b/model.py @@ -0,0 +1,73 @@ +import os +import sys +import logging +import simplejson +from sugar.activity import activity +import zipfile + + +class GameModel: + + def __init__(self): + self.data = {} + self.data['questions'] = [] + """ + question = {'question' = '', + 'type' = '' # TEXT / GRAPH + 'replies' [ {'text': '', + 'valid': } # True / False + 'file_graph_display' = '', + 'file_graph_mask' = ''} + """ + + def write(self, file_name): + + instance_path = os.path.join(activity.get_activity_root(), 'instance') + + data_file_name = 'data.json' + f = open(os.path.join(instance_path, data_file_name), 'w') + try: + simplejson.dump(self.data, f) + finally: + f.close() + + logging.error('write file_name %s', file_name) + + z = zipfile.ZipFile(file_name, 'w') + z.write(os.path.join(instance_path, data_file_name).encode('ascii', + 'ignore'), data_file_name.encode('ascii', 'ignore')) + """ + for box in page.boxs: + if (box.image_name != ''): + z.write(os.path.join(instance_path, + box.image_name).encode('ascii', 'ignore'), + box.image_name.encode('ascii', 'ignore')) + """ + z.close() + + def read(self, file_name): + + logging.error('model.read %s', file_name) + instance_path = os.path.join(activity.get_activity_root(), 'instance') + z = zipfile.ZipFile(file_name, 'r') + for zipped_file in z.namelist(): + if (zipped_file != './'): + try: + logging.error('extrayendo %s', zipped_file) + # la version de python en las xo no permite hacer + # extract :( + # z.extract(file_name,instance_path) + data = z.read(zipped_file) + fout = open(os.path.join(instance_path, zipped_file), 'w') + fout.write(data) + fout.close() + except: + logging.error('Error extrayendo %s', zipped_file) + z.close() + data_file_name = 'data.json' + + f = open(os.path.join(instance_path, data_file_name), 'r') + try: + self.data = simplejson.load(f) + finally: + f.close() |