Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/book.py
blob: 215a1171858469dc2f81c603088a427dbd16091c (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import uuid
import logging
from gi.repository import GObject
import json
import shutil
import zipfile
from gettext import gettext as _

from sugar3.activity.activity import get_bundle_path, get_activity_root

import net
from infoslicer.processing.Article import Article
from infoslicer.processing import Article_Builder

logger = logging.getLogger('infoslicer')

wiki = None
custom = None

image_root = os.path.join(get_activity_root(), 'data', 'book')

class Book(GObject.GObject):
    __gsignals__ = {
        'article-selected' : (GObject.SignalFlags.RUN_FIRST, None, [object]),
        'article-added'    : (GObject.SignalFlags.RUN_FIRST, None, [object]),
        'article-deleted'  : (GObject.SignalFlags.RUN_FIRST, None, [object])}

    def get_article(self):
        return self._article

    def set_article(self, title):
        if self._article and self._article.article_title == title:
            return

        logger.debug('set_article: %s' % title)
        self.sync_article()

        if title is None:
            return

        index, entry = self.find(title)

        if entry:
            content = self._load(entry['uid'])
            if content:
                data = Article_Builder.get_article_from_dita(
                        image_root, content)
                self._article = Article(data)
            else:
                self._article = Article()
        else:
            entry = self._create(title, uuid.uuid1())
            self._article = Article()

        self._article.uid = entry['uid']
        self._article.article_title = title
        GObject.idle_add(self._emit_article_selected)

    article = GObject.property(type=object,
            getter=get_article, setter=set_article)

    def _emit_article_selected(self):
        self.emit('article-selected', self._article)

    # save current article
    def sync_article(self):
        # stub
        pass

    def create(self, title, content):
        uid = str(uuid.uuid1())
        content = net.image_handler(self.root, uid, content)
        self._save(uid, content)
        self._create(title, uid)

    def remove(self, title):
        index, entry = self.find(title)

        if not entry:
            logger.debug('cannot find %s to remove' % title)
            return

        if self._article and self._article.article_title == title:
            self._article = None

        shutil.rmtree(os.path.join(self.root, entry['uid']), True)
        del self.index[index]
        self.sync_index()

        self.emit('article-deleted', title)

    def find(self, title):
        for i, entry in enumerate(self.index):
            if entry['title'] == title:
                return (i, entry)
        return (None, None)

    def find_by_uuid(self, uid):
        for i in self.index:
            if i['uid'] == uid:
                return i
        return None

    def sync_index(self):
        data = { 'uid'      : self.uid,
                 'index'    : self.index,
                 'revision' : self.revision }

        index = file(os.path.join(self.root, 'index'), 'w')
        index.write(json.dumps(data))
        index.close()

    def sync(self):
        self.sync_article()
        self.sync_index()

    def __init__(self, preinstalled, root):
        GObject.GObject.__init__(self)
        self.root = root
        self.index = []
        self.uid = None
        self.revision = 0
        self._article = None

        if os.path.exists(self.root):
            try:
                index = file(os.path.join(self.root, 'index'), 'r')
                data = json.loads(index.read())
                self.uid = data['uid']
                self.index = data['index']
                self.revision = data['revision']
                if self.index:
                    self.props.article = self.index[0]['title']
            except:
                logger.debug('cannot find index file; use empty')

        if not self.uid:
            self.uid = str(uuid.uuid1())
            self.revision = 1

            if not os.path.exists(self.root):
                os.makedirs(self.root, 0775)

            for i in preinstalled:
                filepath = os.path.join(get_bundle_path(), 'examples', i[1])

                logger.debug("install library: opening %s" % filepath)
                open_file = open(filepath, "r")
                contents = open_file.read()
                open_file.close()

                logger.debug("install library: saving page %s" % i[0])
                self.create(i[0], contents)
                logger.debug("install library: save successful")
                self.sync_index()

    def _create(self, title, uid):
        entry = { 'title': title, 'uid': str(uid), 'ready': False }
        self.index.append(entry)
        self.emit('article-added', title)
        return entry

    def _load(self, uid):
        logger.debug('load article %s' % uid)

        path = os.path.join(self.root, str(uid), 'page.dita')
        if not os.access(path, os.F_OK):
            logger.debug('_load: cannot find %s' % path)
            return None

        page = open(path, "r")
        output = page.read()
        page.close()

        return output

    def _save(self, uid, contents):
        directory = os.path.join(self.root, str(uid))

        if not os.path.exists(directory):
            os.makedirs(directory, 0777)

        contents = contents.replace(
                '<prolog>', '<prolog>\n<resourceid id="%s" />'
                % uuid.uuid1(), 1)

        file = open(os.path.join(directory, 'page.dita'), 'w')
        file.write(contents)
        file.close()

        logger.debug('save: %s' % directory)

class WikiBook(Book):
    def __init__(self):
        PREINSTALLED = [
            (_('Lion (from en.wikipedia.org)'),     "lion-wikipedia.dita"),
            (_('Tiger (from en.wikipedia.org)'),    "tiger-wikipedia.dita"),
            (_('Giraffe (from en.wikipedia.org)'),  "giraffe-wikipedia.dita"),
            (_('Zebra (from en.wikipedia.org)'),    "zebra-wikipedia.dita") ]

        Book.__init__(self, PREINSTALLED, image_root)

class CustomBook(Book):
    def __init__(self, filepath=None):
        PREINSTALLED = [
            (_('Giraffe'), "giraffe-blank.dita") ]

        root = os.path.join(get_activity_root(), 'tmp', 'book')
        shutil.rmtree(root, True)

        if not filepath:
            Book.__init__(self, PREINSTALLED, root)
        else:
            zip = zipfile.ZipFile(filepath, 'r')
            for i in zip.namelist():
                path = os.path.join(root, i)
                os.makedirs(os.path.dirname(path), 0775)
                file(path, 'wb').write(zip.read(i))
            zip.close()

            Book.__init__(self, [], root)

    def sync(self, filepath):
        Book.sync(self)

        logger.debug('close: save to %s' % filepath)

        zip = zipfile.ZipFile(filepath, 'w')
        for root, dirs, files in os.walk(self.root):
            relpath = root.replace(self.root, '', 1)
            for i in files:
                zip.write(os.path.join(root, i), os.path.join(relpath, i))
        zip.close()

    def sync_article(self):
        if not self._article:
            return

        self.find_by_uuid(self._article.uid)['title'] = \
                self._article.article_title

        contents = Article_Builder.get_dita_from_article(
                image_root, self._article)

        self._save(self._article.uid, contents)