diff options
Diffstat (limited to 'plugins/_library.py')
-rwxr-xr-x | plugins/_library.py | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/plugins/_library.py b/plugins/_library.py new file mode 100755 index 0000000..60e6922 --- /dev/null +++ b/plugins/_library.py @@ -0,0 +1,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 |