Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model.py
blob: e2f5addca6f8668a37ce309929fdeac2ebdbb896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import logging
import simplejson
from sugar3.activity import activity
import zipfile


class GameModel:

    QUESTION_TYPE_TEXT = 'TEXT'
    QUESTION_TYPE_GRAPHIC = 'GRAPHIC'

    def __init__(self):
        self.data = {}
        self.data['questions'] = []
        """
        question = {'question' = '',
                    'type' = ''  # TEXT / GRAPH
                    'replies' [ {'text': '',
                                'valid': } # True / False
                    'file_graph_display' = '',
                    'file_graph_mask' = ''}
        """

        self.data['resources'] = []
        """
        resource = {'title' = '',
                    'file_image' = ''
                    'file_text' = ''}
        """
        self.data['last_resource_id'] = 0
        self.data['last_question_id'] = 0

        self.data['map_data'] = None

        state = {'displayed_questions': [],
                'replied_questions': [],
                'actions_log': []}

        self.data['state'] = state

    def get_new_resource_id(self):
        self.data['last_resource_id'] = self.data['last_resource_id'] + 1
        return self.data['last_resource_id']

    def get_new_question_id(self):
        self.data['last_question_id'] = self.data['last_question_id'] + 1
        return self.data['last_question_id']

    def register_displayed_question(self, id_question):
        if id_question not in self.data['state']['displayed_questions']:
            self.data['state']['displayed_questions'].append(id_question)

    def register_replied_question(self, id_question):
        if id_question not in self.data['state']['replied_questions']:
            self.data['state']['replied_questions'].append(id_question)

    def get_resource(self, id_resource):
        id_resource = int(id_resource)
        for resource in self.data['resources']:
            if resource['id_resource'] == int(id_resource):
                return resource
        logging.error('ERROR: resource %s not found', id_resource)
        return None

    def get_question(self, id_question):
        for question in self.data['questions']:
            if question['id_question'] == int(id_question):
                return question
        logging.error('ERROR: question %s not found', id_question)
        return None

    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 resource in self.data['resources']:
            z.write(resource['file_image'], os.path.join('resources',
                    os.path.basename(resource['file_image'])))
            z.write(resource['file_text'], os.path.join('resources',
                    os.path.basename(resource['file_text'])))

        for question in self.data['questions']:
            if question['type'] == self.QUESTION_TYPE_GRAPHIC:
                z.write(question['file_image'], os.path.join('resources',
                        os.path.basename(question['file_image'])))
                z.write(question['file_image_reply'],
                        os.path.join('resources',
                        os.path.basename(question['file_image_reply'])))

        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')
        self.check_resources_directory()
        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)

            if not 'resources' in self.data:
                self.data['resources'] = []

        finally:
            f.close()

    def check_resources_directory(self):
        resource_path = os.path.join(activity.get_activity_root(),
                'instance', 'resources')
        if not os.path.exists(resource_path):
            os.makedirs(resource_path)