Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/_library.py
blob: 60e6922695f28d57151e69706026db99483f4c5c (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
__PLUGIN_NAME__   = 'library'
LIB = '/home/olpc/Activities/ImageQuizPlus.activity/flashcards/library'
OUTPATH = '/home/olpc/Activities/ImageQuizPlus.activity/flashcards/flashcards'

from path import path
from xmlio import Xmlio
import xml.etree.ElementTree as ET

'''
 List of Services:
 - ImageQuiz.py, Line 37: "Hook-In Services"
 - http://wiki.laptop.org/index.php?title=XO_ImageQuiz/Plugins#Overview
'''
#
# /media/SDCARD02 should be global for easy change
# top level menu items are folders in /media/SDCARD02/library
# change is to make this happen dynamically based on directory (using path.py)
# top level categories are folders in selected top level item
# sub categories are decks in selected category
# selected deck is copied to /home/olpc/ImageQuizPlus.activity/flashcards (adding Leitner attributes)
# if deck references image or sound files, they are copied to /home/olpc/ImageQuizPlus.activity/flashcards/image or sound.
# finish by returning to main menu
#
# strategy
# first get menu working without doing anything (print xxx.xml selected)
# next add print for image copied or sound copied
# debug source is /home/tonya/Desktop/ImageQuizPlus.activity/flashcards/library
# debug destination is /home/tonya/Desktop/ImageQuizPlus.activity/flashcards
#
# next actually copy deck adding Leitner attributes
#
# modify flashcard.py to use Leitner attributes (first ignore as in quiz mode)

def clickOnItem2():
    #here we need to display list of decks already checked out
    #print "Return to Library"
    d = path(OUTPATH)
    count = 0
    y = 110
    for item in d.files():
        brdr = False
        sf.add_text_item(item.name, (300, y+50*count), remove_deck, (d, item.name), brdr)
        count += 1

def remove_deck(s):
        #print s[0], s[1], 'selected'
        d = path(OUTPATH)
        deck = d.joinpath(s[1])
        #print 'remove deck', deck
        path.remove(deck)
        #we need to redisplay menu with deck removed
        sf.clear_text_items()
        clickOnItem2()

def clickOnItem1():
        #here we need to display categories (folders in selected folder)
	#print "Demoplugin Menu Item 1"
        #make path to folder
        #here we need to use path to make a list of directories
        brdr = False
        d = path(LIB)
        dsel = d.joinpath(sf.current_caption())
        count = 0
        y = 110
        for folder in dsel.dirs():
            #print folder, folder.name, len(folder.files()), len(folder.dirs())
            sf.add_text_item("%s (%s)" % (unicode(folder.name), str(len(folder.dirs()))), (300,y + 50 * count),
               ask_subcat, (dsel, folder.name), brdr)
            count += 1

def ask_subcat(s):
        global sourcepath
        #print s[0], s[1], 'selected'
        brdr = True
        sf.clear_text_items()
        d = path(s[0])
        dsel = d.joinpath(s[1])
        #this is source for image and sound files
        sourcepath = d
        count = 0
        y = 70
        for f in dsel.listdir():
            #may be deck or folder
            if f.isdir():
                cnt = len(f.listdir())
                brdr = False
            else:  
                tree = Xmlio(f)
	        root = tree.getroot()
	        cnt = len(root)
                brdr = True
            if count < 12:
                sf.add_text_item("%s (%s)" % (unicode(f.namebase), str(cnt)), (200,y + 50 * count), 
                    make_local, (dsel,f.name), brdr)
            elif count < 24:
                sf.add_text_item("%s (%s)" % (unicode(f.namebase), str(cnt)), (450,y + 50 * (count - 12)), 
                    make_local, (dsel,f.name), brdr)
            else:
                sf.add_text_item("%s (%s)" % (unicode(f.namebase), str(cnt)), (700,y + 50 * (count - 24)), 
                    make_local, (dsel,f.name), brdr)
            count += 1

def make_local(s):
        global sourcepath
        #this could be deck or folder
        #print s[0], s[1], 'deck selected'
        count = 0
        sel = path(s[0])
        selp = sel.joinpath(s[1])
        if selp.isdir():
            ask_subcat(s)
            return
        #print 'should be file', selp
        deck = Xmlio(selp)
        cards = deck.getroot()
        outpath = path(OUTPATH)
        fullpath = outpath.joinpath(s[1])
        #print fullpath
        outpath = path(OUTPATH)
        soundsource = path(sourcepath).joinpath('sound')
        imagesource = path(sourcepath).joinpath('image')
        outdeck = Xmlio(root = "quiz")
        outcards = outdeck.getroot()
        for card in cards:
            #copy card to outcard
            question_node = card.find('question')
            if question_node:
                temp = question_node.findtext('sound') 
                if temp:
                    sounds = temp.split('/')
                    for item in sounds:
                        item = item[:-4] + '.ogg'
                        #copy sounds here
                        source = soundsource.joinpath(item)
                        temp = outpath.joinpath('sound')
                        destination = temp.joinpath(item)
                        #print 'sound to copy', item, 'from', source, 'to', destination
                        path.copy(source,destination)
                    sound = '/'.join(sounds)
                else:
                    sound = ''
                hint = question_node.findtext('hint')
            answer_node = card.find('answer')
            if answer_node:
                more = answer_node.findtext('more')
                temp = answer_node.findtext('image')
                if temp:
                    image = temp[:-4] + '.png'
                else:
                    image = ''
            count += 1
            outcard = ET.SubElement(outcards, 'card', id = str(count), bin = str(0),
                            count = str(0), shown = '', think = '')
            question = ET.SubElement(outcard, 'question')
            question.text = question_node.text
            qsound = ET.SubElement(question, 'sound')
            qsound.text = sound
            #copy sound
            #print 'sound', sound
            qhint = ET.SubElement(question, 'hint')
            qhint.text = hint
            answer = ET.SubElement(outcard, 'answer')
            answer.text = answer_node.text
            aimage = ET.SubElement(answer, 'image')
            aimage.text = image
            #copy image
            source = path(imagesource).joinpath(image)
            temp = outpath.joinpath('image')
            destination = temp.joinpath(image)
            #print 'image to copy', image, 'from', source, 'to', destination
            path.copy(source,destination)
            amore = ET.SubElement(answer, 'more')
            amore.text = more
           
        #write outdeck
        outdeck.save(fullpath)
        sf.clear_text_items()
		
def debug():
	pass
	
def load():
	global sf
	sf = __SERVICES__.frontend;

#	#print __SERVICES__.db.query("SELECT * FROM xoquiz WHERE 1")
	sf.add_menu_dir('/library', 'Library')
        #here we need to use path to make a list of directories
        d = path(LIB)
        for folder in d.dirs():
            #print folder
            if not folder[-3:] == 'bzr':
	        sf.add_menu_item('/library', folder.name, clickOnItem1)
        #to remove completed decks
        sf.add_menu_item('/library','return quiz', clickOnItem2)

def close():
	pass