Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model.py
diff options
context:
space:
mode:
Diffstat (limited to 'model.py')
-rw-r--r--model.py133
1 files changed, 71 insertions, 62 deletions
diff --git a/model.py b/model.py
index dd4afdb..63992a2 100644
--- a/model.py
+++ b/model.py
@@ -17,38 +17,37 @@
import libxml2
from os import environ, makedirs, chmod
-from os.path import join, basename, isdir, split, normpath
+from os.path import join, basename, isdir, split, normpath, exists
import logging
import random
import gobject
import zipfile
import tempfile
-from sugar import profile
-from sugar.datastore import datastore
from sugar.activity.activity import get_bundle_path, get_activity_root
_logger = logging.getLogger('model')
+
class Pair(gobject.GObject):
__gproperties__ = {
- 'aimg' : (str, None, None, None, gobject.PARAM_READWRITE),
- 'asnd' : (str, None, None, None, gobject.PARAM_READWRITE),
+ 'aimg': (str, None, None, None, gobject.PARAM_READWRITE),
+ 'asnd': (str, None, None, None, gobject.PARAM_READWRITE),
'achar': (str, None, None, None, gobject.PARAM_READWRITE),
- 'bimg' : (str, None, None, None, gobject.PARAM_READWRITE),
- 'bsnd' : (str, None, None, None, gobject.PARAM_READWRITE),
+ 'bimg': (str, None, None, None, gobject.PARAM_READWRITE),
+ 'bsnd': (str, None, None, None, gobject.PARAM_READWRITE),
'bchar': (str, None, None, None, gobject.PARAM_READWRITE),
'aspeak': (str, None, None, None, gobject.PARAM_READWRITE),
'bspeak': (str, None, None, None, gobject.PARAM_READWRITE),
- 'color' : (gobject.TYPE_INT, 'Base', 'Base', 0, 10, 0, \
+ 'color': (gobject.TYPE_INT, 'Base', 'Base', 0, 10, 0, \
gobject.PARAM_READWRITE)
}
def __init__(self):
gobject.GObject.__init__(self)
- self._properties = {'aimg':None, 'asnd':None, 'achar':None, 'bimg':None,
- 'bsnd':None, 'bchar':None, 'color':100,
- 'aspeak':None, 'bspeak':None}
+ self._properties = {'aimg': None, 'asnd': None, 'achar': None,
+ 'bimg': None, 'bsnd': None, 'bchar': None,
+ 'color': 100, 'aspeak': None, 'bspeak': None}
def do_get_property(self, pspec):
"""Retrieve a particular property from our property dictionary
@@ -100,6 +99,10 @@ class Model(object):
'''
def __init__(self, game_path=None):
+ tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
+ self.temp_folder = tempfile.mkdtemp(dir=tmp_root)
+ chmod(self.temp_folder, 0777)
+
self.data = {}
if game_path is None:
@@ -113,18 +116,25 @@ class Model(object):
self.data['face'] = ''
self.data['align'] = '1'
+ self.data['divided'] = '0'
+ self.data['equal_pairs'] = '0'
try:
self.dtd = libxml2.parseDTD(None, join(get_bundle_path(),
'memorize.dtd'))
except libxml2.parserError, e:
- _logger.error('Init: no memorize.dtd found ' +str(e))
+ _logger.error('Init: no memorize.dtd found ' + str(e))
self.dtd = None
self.ctxt = libxml2.newValidCtxt()
self.pairs = {}
self.grid = []
+ # used to know if the game should be saved and reloaded
+ self.modified = False
+ logging.debug('Model init is_demo False')
+ self.is_demo = False
+
# used by the leader of the game to keep track of the game state
self.players = {}
self.player_active = 0
@@ -133,16 +143,20 @@ class Model(object):
self.started = 0
self.count = 0
+ def mark_modified(self):
+ logging.debug('Model mark_modified is_demo False')
+ self.is_demo = False
+ self.modified = True
+
def read(self, game_file):
- tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
- temp_folder = tempfile.mkdtemp(dir=tmp_root)
- chmod(temp_folder, 0777)
+ self.modified = False
+ self.count = 0
self.data['key'] = basename(game_file)
self.data['game_file'] = game_file
- self.data['path'] = temp_folder
+ self.data['path'] = self.temp_folder
self.data['pathimg'] = join(self.data['path'], 'images')
self.data['pathsnd'] = join(self.data['path'], 'sounds')
-
+
''' extracts files in the zip file '''
game_name = basename(game_file)[:-4]
zipFile = zipfile.ZipFile(game_file, "r")
@@ -155,14 +169,14 @@ class Model(object):
file(join(directory, name), 'wb').write(zipFile.read(each))
self.pairs = {}
-
+
''' reads the configuration from an xml file '''
try:
xml_file = join(environ['SUGAR_ACTIVITY_ROOT'],
self.data['path'], 'game.xml')
doc = libxml2.parseFile(xml_file)
if doc.validateDtd(self.ctxt, self.dtd):
-
+
# get the requested nodes
xpa = doc.xpathNewContext()
res = xpa.xpathEval("//*")
@@ -176,7 +190,7 @@ class Model(object):
for attribute in attributes:
if(attribute.name == 'text'):
pass
- else:
+ else:
pair.set_property(attribute.name,
attribute.content)
self.pairs[str(idpair)] = pair
@@ -185,19 +199,19 @@ class Model(object):
for attribute in attributes:
if(attribute.name == 'text'):
pass
- elif(attribute.name == 'name'):
+ elif(attribute.name == 'name'):
self.data['name'] = attribute.content
elif(attribute.name == 'scoresnd'):
self.data['scoresnd'] = attribute.content
- elif(attribute.name == 'winsnd'):
+ elif(attribute.name == 'winsnd'):
self.data['winsnd'] = attribute.content
- elif(attribute.name == 'divided'):
+ elif(attribute.name == 'divided'):
self.data['divided'] = attribute.content
- elif(attribute.name == 'face'):
+ elif(attribute.name == 'face'):
self.data['face'] = attribute.content
- elif(attribute.name == 'face1'):
+ elif(attribute.name == 'face1'):
self.data['face1'] = attribute.content
- elif(attribute.name == 'face2'):
+ elif(attribute.name == 'face2'):
self.data['face2'] = attribute.content
elif(attribute.name == 'align'):
self.data['align'] = attribute.content
@@ -211,43 +225,43 @@ class Model(object):
doc.freeDoc()
return 0
except libxml2.parserError, e:
- _logger.error('Read: Error parsing file ' +str(e))
+ _logger.error('Read: Error parsing file ' + str(e))
return 2
-
- def write(self, equal_pairs, divided):
+
+ def write(self):
''' writes the configuration to an xml file '''
doc = libxml2.newDoc("1.0")
root = doc.newChild(None, "memorize", None)
-
- if(self.data.get('name', None) != None):
+
+ if(self.data.get('name', None) != None):
root.setProp("name", self.data['name'])
-
- if divided:
+
+ if(self.data.get('divided', None) != None):
root.setProp('divided', '1')
root.setProp('face1', '1')
root.setProp('face2', '2')
else:
root.setProp('divided', '0')
-
- if equal_pairs:
- root.setProp('equal_pairs', str(equal_pairs))
-
- if(self.data.get('scoresnd', None) != None):
+
+ if(self.data.get('equal_pairs', None) != None):
+ root.setProp('equal_pairs', self.data['equal_pairs'])
+
+ if(self.data.get('scoresnd', None) != None):
root.setProp("scoresnd", self.data['scoresnd'])
- if(self.data.get('winsnd', None) != None):
+ if(self.data.get('winsnd', None) != None):
root.setProp("winsnd", self.data['winsnd'])
- if(self.data.get('divided', None) != None):
+ if(self.data.get('divided', None) != None):
root.setProp("divided", self.data['divided'])
- if(self.data.get('face', None) != None):
+ if(self.data.get('face', None) != None):
root.setProp("face", self.data['face'])
- if(self.data.get('face1', None) != None):
+ if(self.data.get('face1', None) != None):
root.setProp("face1", self.data['face1'])
- if(self.data.get('face2', None) != None):
+ if(self.data.get('face2', None) != None):
root.setProp("face2", self.data['face2'])
- if(self.data.get('align', None) != None):
+ if(self.data.get('align', None) != None):
root.setProp("align", self.data['align'])
- for key in self.pairs:
+ for key in self.pairs:
elem = root.newChild(None, "pair", None)
if self.pairs[key].props.aimg != None:
elem.setProp("aimg", self.pairs[key].props.aimg)
@@ -275,12 +289,11 @@ class Model(object):
return 2
doc.freeDoc()
return 0
-
def def_grid(self, size):
''' create the grid for the play from the pairs information
and shuffles the grid so they always appear in a different
- place
+ place
'''
psize = (size * size / 2)
_logger.debug('Size requested: %d', psize)
@@ -326,8 +339,8 @@ class Model(object):
i += 1
else:
break
-
- numpairs = len(self.pairs)
+
+ numpairs = len(self.pairs)
if numpairs < psize:
_logger.debug('Defgrid: Not enough pairs, requested=%s had=%s'
% (psize, numpairs))
@@ -348,16 +361,12 @@ class Model(object):
def set_data_grid(self, data, grid):
self.data = data
self.grid = grid
-
- def save_byte_array(self, path, title= None, color= None):
- if color == None:
- color = profile.get_color().to_string()
- _logger.debug('Save new game in datastore')
-
- # Saves the zip in datastore
- gameObject = datastore.create()
- gameObject.metadata['title'] = title
- gameObject.metadata['mime_type'] = 'application/x-memorize-project'
- gameObject.metadata['icon-color'] = color
- gameObject.file_path = path
- datastore.write(gameObject)
+
+ def create_temp_directories(self):
+ temp_img_folder = join(self.temp_folder, 'images')
+ temp_snd_folder = join(self.temp_folder, 'sounds')
+
+ if not exists(temp_img_folder):
+ makedirs(temp_img_folder)
+ if not exists(temp_snd_folder):
+ makedirs(temp_snd_folder)