def parse_tree(s, C_locale=False): """ This function can load both new (3.15.2) and older learning trees. This function will convert old format to new format, and always return new format files. """ if C_locale: def ifu(s): # We do the isinstance check because this way we can handle # people manually editing the data file and adding strings that # are plain ascii, but not marked as unicode ( u'' ) return s if isinstance(s, unicode) else unicode(s) ifunc = ifu else: # The headings and links to exercises or pages should not have # accels (defined in gtk+ by underlines "_") or ellipsis "…" that # are used in menu items and command button labels. So we remove # them, if found, in the string. This way we can reuse strings # with accels that are translated, saving translator work. def _s(s): return _(s).replace("_", "").replace(u"…", "") ifunc = _s namespace = { 'FileHeader': FileHeader, 'Page': Page, 'Column': Column, 'LinkList': LinkList, 'Paragraph': Paragraph, '_': ifunc, '__builtins__': {}, } ret = eval(s, namespace, namespace) if isinstance(ret, dict): name = ret['title'] ret = convert_old_learningtree_data(ret) ret.m_name = name if isinstance(ret, FileHeader): return ret[0] else: if not isinstance(ret, Page): ret = convert_old_learningtree_data(ret) # Convert from solfege 3.15.2 to 3.15.3 file format # New front page files should store the filename, not the uuid lessonfile.require_mgr() def cnv_2_3(lesson_id): try: return lessonfile.mk_uri(lessonfile.mgr.get(lesson_id, 'filename')) except lessonfile.mgr.UnknownLessonID, e: return unicode(e) ret.foreach_file(cnv_2_3) return ret
def get_uuid_to_filename_mapping(self, callback=None): """ Parse all lesson files we can find and return a dict mapping the lesson_id to file names. Current lessonfile manager code cause us to parse the user contributed files in ~/lessonfiles too. """ count = 0 mapping = {} lessonfile.require_mgr() for uuid, data in lessonfile.mgr.parse(False): mapping[uuid] = lessonfile.mk_uri(data['filename']) count += 1 callback(_("Files read: %i") % count) logging.info("get_uuid_to_filename_mapping: scanned %i files." % count) return count, mapping
def load_file(self, filename): p = Dataparser({'yes': True, 'no': False}, {}) p.parse_file(filename) s = p.globals.setdefault('output_format', 'midi') s = s.lower() if s in self.g_output: self.g_output[s].set_active(True) else: # MIDI is the default format self.g_output['midi'].set_active(True) self.m_filename = filename if p.globals['fileformat_version'] == 1: lessonfile.require_mgr() self.g_named_tracks.set_active(p.globals.get('named_tracks', False)) for lesson in p.blocklists.setdefault('lesson', []): # In this loop we will set the filename column of the liststore # to None if there is any problem, and set a title mentioning # the unknown lesson_id or filename. if p.globals['fileformat_version'] == 1: try: filename = lessonfile.mgr.get(lesson['lesson_id'], 'filename') filename = lessonfile.mk_uri(filename) except lessonfile.mgr.UnknownLessonID, e: filename = None else: filename = lesson['filename'] if filename: if os.path.exists(lessonfile.uri_expand(filename)): fn = filename title = self.get_lessonfile_title(filename) else: fn = None title = _(u"«<b>%s</b>» was not found") % filename self.g_liststore.append((fn, title, lesson['count'], lesson['repeat'], lesson['delay'])) else: self.g_liststore.append((filename, _("Unknown lesson_id: <b>%s</b>") % lesson['lesson_id'], lesson['count'], lesson['repeat'], lesson['delay']))
def load_file(self, filename): """ This function can load both format version 1.0 and 2.0 """ tree = et.ElementTree() tree.parse(filename) # section.find('text').text will be none if it was not set # when saved, and we need a string. if tree.find("title").text: self.g_title.set_text(tree.find("title").text) else: self.g_title.set_text("") sheet = tree.find("sheet") if tree.find('output_format').text == 'latex': self.g_latex_radio.set_active(True) else: self.g_latex_radio.set_active(False) self.g_liststore.clear() self.m_sections = [] for section in tree.findall("section"): d = {} lessonfilename = section.find('filename') if lessonfilename == None: # fileformat 1.0 lessonfile.require_mgr() lesson_id = section.find('lesson_id').text lessonfilename = lessonfile.mgr.get(lesson_id, 'filename') lessonfilename = lessonfile.mk_uri(lessonfilename) else: lessonfilename = lessonfilename.text # It seems that the elementtree parser will return str if # there are no non-ascii chars in the filename. So lets # make unicode of it if it is str if isinstance(lessonfilename, str): lessonfilename = unicode(lessonfilename) d['filename'] = lessonfilename # section.find('text').text will be none if it was not set # when saved, and d['title'] need to be a string if section.find('title').text: d['title'] = section.find('title').text else: d['title'] = "" if section.find('intervals') != None: d['intervals'] = eval(section.find('intervals').text, {}, {}) else: if lessonfile.infocache.get(lessonfilename, 'module') == 'harmonicinterval': d['intervals'] = [] gu.dialog_ok("FIXME: «%s» was saved with a buggy version of solfege, so you must set the intervals by selecting the file in the dialog and clicking the intervals to be asked. Sorry!" % lessonfilename) d['count'] = int(section.find('count').text) d['line_len'] = int(section.find('line_len').text) d['qtype'] = int(section.find('qtype').text) d['questions'] = [] for question in section.findall("question"): q = {'question': {}, 'answer': {}} q['question']['music'] = question.find("students").find("music").text q['question']['name'] = question.find("students").find("name").text q['answer']['music'] = question.find("teachers").find("music").text q['answer']['name'] = question.find("teachers").find("name").text d['questions'].append(q) self.m_sections.append(d) try: # Check that the filename is valid lessonfile.infocache.get(lessonfilename, 'title') self.g_liststore.append((d['title'], lessonfilename)) except lessonfile.infocache.InfoCacheException, e: self.g_liststore.append((_("«%s» not found") % str(e), None)) self.g_treeview.set_cursor((0,))