def element_children(self, element): """ Return a list of the children of the supplied element, following references as required. """ children = [] for child1 in element.iterchildren(tag=etree.Element): if self.tag(child1) == "ref": if not "name" in child1.keys(): debug.deprint("Warning: Encountered reference with no name") continue name = child1.get("name") xpath = self.tree.xpath('/t:grammar/t:define[@name="' + name + '"]', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'}) if len(xpath) == 0: debug.deprint("Warning: Schema reference %s not found" % name, 0) continue for child2 in self.element_children(xpath[0]): children.append(child2) else: children.append(child1) return children
def valid_node(self, eid): if isinstance(eid, tree.Tree): eidtree = eid eid = eid.schemaname if eid == ":start": try: node = self.tree.xpath( '/t:grammar/t:start', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'})[0] except: debug.deprint( "No valid start node found. Are you using a library Relax-NG file like spud_base.rng?", 0) sys.exit(0) else: xpath = self.tree.xpath(eid) if len(xpath) == 0: debug.deprint("Warning: no element with XPath %s" % eid) return None node = xpath[0] node = self.to_tree(node) if eidtree is not None: if eidtree.parent is not None: eidtree.parent.children.append(node) eidtree.parent.children.remove(eidtree) node.set_parent(eidtree.parent) node.attrs = eidtree.attrs node.cardinality = eidtree.cardinality return node
def valid_node(self, eid): if isinstance(eid, tree.Tree): eidtree = eid eid = eid.schemaname if eid == ":start": try: node = self.tree.xpath('/t:grammar/t:start', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'})[0] except: debug.deprint("No valid start node found. Are you using a library Relax-NG file like spud_base.rng?", 0) sys.exit(0) else: xpath = self.tree.xpath(eid) if len(xpath) == 0: debug.deprint("Warning: no element with XPath %s" % eid) return None node = xpath[0] node = self.to_tree(node) if eidtree is not None: if eidtree.parent is not None: eidtree.parent.children.append(node) eidtree.parent.children.remove(eidtree) node.set_parent(eidtree.parent) node.attrs = eidtree.attrs node.cardinality = eidtree.cardinality return node
def element_children(self, element): """ Return a list of the children of the supplied element, following references as required. """ children = [] for child1 in element.iterchildren(tag=etree.Element): if self.tag(child1) == "ref": if not "name" in child1.keys(): debug.deprint( "Warning: Encountered reference with no name") continue name = child1.get("name") xpath = self.tree.xpath( '/t:grammar/t:define[@name="' + name + '"]', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'}) if len(xpath) == 0: debug.deprint( "Warning: Schema reference %s not found" % name, 0) continue for child2 in self.element_children(xpath[0]): children.append(child2) else: children.append(child1) return children
def matches(self, xpath): try: return self.applies(xpath) except Exception: debug.deprint( "Warning: plugin %s raised an exception in matching function." % self.name, 0) return False
def set_text (self, txt): gtk.TextBuffer.set_text(self,"") try: self.parsed,self.txt,self.separator = pango.parse_markup(txt,u'\x00') except: debug.deprint('Escaping text, we seem to have a problem here!', 2) txt=xml.sax.saxutils.escape(txt) self.parsed,self.txt,self.separator = pango.parse_markup(txt,u'\x00') self.attrIter = self.parsed.get_iterator() self.add_iter_to_buffer() while self.attrIter.next(): self.add_iter_to_buffer()
def wrapper(*args, **kwargs): try: f(*args, **kwargs) except: debug.deprint("Plugin raised an exception:", 0) tb = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) tb_msg = "" for tbline in tb: tb_msg += tbline debug.deprint(tb_msg, 0)
def valid_children(self, eid): if isinstance(eid, tree.Tree): eid = eid.schemaname if eid == ":start": try: node = self.tree.xpath( '/t:grammar/t:start', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'})[0] except: debug.deprint( "No valid start node found. Are you using a library Relax-NG file like spud_base.rng?", 0) sys.exit(0) else: xpath = self.tree.xpath(eid) if len(xpath) == 0: debug.deprint("Warning: no element with XPath %s" % eid) return [] node = xpath[0] results = [] for child in self.element_children(node): self.append(results, self.to_tree(child)) if eid == ":start" and len(results) != 1: debug.deprint( "Error: there must be exactly one root element in an XML document, but found:", 0) for result in results: debug.deprint(" %s" % result.name, 0) sys.exit(1) return results
def read(self, xmlfile, root = None): try: doc = etree.parse(xmlfile) except etree.XMLSyntaxError as e: debug.dprint("Invalid XML.") debug.dprint(e) return None self.lost_eles = [] self.added_eles = [] self.lost_attrs = [] self.added_attrs = [] if root is None: datatree = self.valid_children(":start")[0] else: datatree = self.valid_node(root) xmlnode = doc.getroot() self.xml_read_merge(datatree, xmlnode) self.xml_read_core(datatree, xmlnode, doc) if len(self.lost_eles) != 0: debug.deprint("WARNING: Lost XML elements:\n" + str(self.lost_eles)) if len(self.added_eles) != 0: debug.deprint("WARNING: Added XML elements:\n" + str(self.added_eles)) if len(self.lost_attrs) != 0: debug.deprint("WARNING: Lost XML attributes:\n" + str(self.lost_attrs)) if len(self.added_eles) != 0: debug.deprint("WARNING: Added XML attributes:\n" + str(self.added_attrs)) return datatree
def valid_children(self, eid): if isinstance(eid, tree.Tree): eid = eid.schemaname if eid == ":start": try: node = self.tree.xpath('/t:grammar/t:start', namespaces={'t': 'http://relaxng.org/ns/structure/1.0'})[0] except: debug.deprint("No valid start node found. Are you using a library Relax-NG file like spud_base.rng?", 0) sys.exit(0) else: xpath = self.tree.xpath(eid) if len(xpath) == 0: debug.deprint("Warning: no element with XPath %s" % eid) return [] node = xpath[0] results = [] for child in self.element_children(node): self.append(results, self.to_tree(child)) if eid == ":start" and len(results) != 1: debug.deprint("Error: there must be exactly one root element in an XML document, but found:", 0) for result in results: debug.deprint(" %s" % result.name, 0) sys.exit(1) return results
def set_text(self, txt): gtk.TextBuffer.set_text(self, "") try: self.parsed, self.txt, self.separator = pango.parse_markup( txt, u'\x00') except: debug.deprint('Escaping text, we seem to have a problem here!', 2) txt = xml.sax.saxutils.escape(txt) self.parsed, self.txt, self.separator = pango.parse_markup( txt, u'\x00') self.attrIter = self.parsed.get_iterator() self.add_iter_to_buffer() while self.attrIter.next(): self.add_iter_to_buffer()
def valid_node(self, eid): if isinstance(eid, tree.Tree) or isinstance(eid, choice.Choice): eidtree = eid eid = eid.schemaname xpath = self.tree.xpath(eid) if len(xpath) == 0: debug.deprint("Warning: no element with XPath %s" % eid) return None node = xpath[0] node = self.to_tree(node) if eidtree is not None: node.cardinality = eidtree.cardinality node.parent = eidtree.parent return node
def configure_plugins(suffix): homedir = os.path.expanduser('~') dirs = [ os.path.join(homedir, ".diamond", "plugins", suffix), "/home/wade/Downloads/multifluids_icferst-master/libspud/../share/diamond/plugins/" + suffix ] if sys.platform != "win32" and sys.platform != "win64": dirs.append("/etc/diamond/plugins/" + suffix) for dir in dirs: sys.path.insert(0, dir) try: for file in os.listdir(dir): module_name, ext = os.path.splitext(file) if ext == ".py": try: debug.deprint("Attempting to import " + module_name, 1) module = __import__(module_name) except: debug.deprint("Plugin raised an exception:", 0) tb = traceback.format_exception( sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) tb_msg = "" for tbline in tb: tb_msg += tbline debug.deprint(tb_msg, 0) except OSError: pass
def configure_plugins(suffix): homedir = os.path.expanduser('~') dirs = [] if "DIAMOND_CONFIG_PATH" in os.environ: dirs += os.environ["DIAMOND_CONFIG_PATH"].split(":") dirs.append(os.path.join(homedir, ".diamond", "plugins", suffix)) dirs.append("/home/kanishk/fluidity/libspud/../share/diamond/plugins/" + suffix) if sys.platform != "win32" and sys.platform != "win64": dirs.append("/etc/diamond/plugins/" + suffix) for dir in dirs: sys.path.insert(0, dir) try: for file in os.listdir(dir): module_name, ext = os.path.splitext(file) if ext == ".py": try: debug.deprint("Attempting to import " + module_name, 1) module = __import__(module_name) except: debug.deprint("Plugin raised an exception:", 0) tb = traceback.format_exception( sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) tb_msg = "" for tbline in tb: tb_msg += tbline debug.deprint(tb_msg, 0) except OSError: pass
def cb_attribute(self, element, facts): if not "name" in element.keys(): debug.deprint("Warning: Encountered attribute with no name") return newfacts = {} name = element.get("name") for child in self.element_children(element): tag = self.tag(child) f = self.callbacks[tag] x = f(child, newfacts) if "attrs" not in facts: facts["attrs"] = {} try: datatype = newfacts["datatype"] except: debug.deprint("Warning: Encountered attribute with no datatype") return curval = None if isinstance(datatype, tuple): new_datatype = [] for x in datatype: if not x is None: new_datatype.append(x) datatype = new_datatype if len(datatype) == 0: datatype = None elif len(datatype) == 1: datatype = datatype[0] if isinstance(datatype, str): curval = datatype datatype = 'fixed' else: curval = None else: l_values = [] l_data = [] for x in datatype: if isinstance(x, str): l_values.append(x) else: l_data.append(x) if len(l_data) > 0: debug.deprint( "Warning: Attribute %s has multiple datatypes - using first one" % name) if len(l_values) == 0: datatype = l_data[0] else: datatype = tuple([tuple(l_values)] + l_data[0]) else: datatype = tuple(l_values) facts["attrs"][name] = (datatype, curval)
def cb_element(self, element, facts): newfacts = {} if "cardinality" in facts: newfacts["cardinality"] = facts["cardinality"] if "name" in element.keys(): newfacts["name"] = element.get("name") else: debug.deprint("Warning: Encountered element with no name") newfacts['schemaname'] = self.tree.getpath(element) for child in self.element_children(element): tag = self.tag(child) if tag not in ['element', 'optional', 'zeroOrMore', 'oneOrMore', 'ignore']: f = self.callbacks[tag] x = f(child, newfacts) try: d = newfacts["datatype"] if isinstance(d, tuple): new_d = [] for x in d: if x is not None: new_d.append(x) d = tuple(new_d) newfacts["datatype"] = d if len(d) == 0: newfacts["datatype"] = None elif len(d) == 1 and isinstance(d[0], plist.List): newfacts["datatype"] = d[0] else: l_values = [] l_data = [] for x in d: if isinstance(x, str): l_values.append(x) else: l_data.append(x) if len(l_data) > 1: if "name" in element.keys(): debug.deprint("Warning: Element %s has multiple datatypes - using first one" % newfacts["name"]) else: debug.deprint("Warning: Unnamed element has multiple datatypes - using first one") if len(l_data) > 0: if len(l_values) == 0: newfacts["datatype"] = l_data[0] else: newfacts["datatype"] = tuple([tuple(l_values)] + l_data[0]) except KeyError: pass return tree.Tree(**newfacts)
def cb_attribute(self, element, facts): if not "name" in element.keys(): debug.deprint("Warning: Encountered attribute with no name") return newfacts = {} name = element.get("name") for child in self.element_children(element): tag = self.tag(child) f = self.callbacks[tag] x = f(child, newfacts) if "attrs" not in facts: facts["attrs"] = {} try: datatype = newfacts["datatype"] except: debug.deprint("Warning: Encountered attribute with no datatype") return curval = None if isinstance(datatype, tuple): new_datatype = [] for x in datatype: if not x is None: new_datatype.append(x) datatype = new_datatype if len(datatype) == 0: datatype = None elif len(datatype) == 1: datatype = datatype[0] if isinstance(datatype, str): curval = datatype datatype = 'fixed' else: curval = None else: l_values = [] l_data = [] for x in datatype: if isinstance(x, str): l_values.append(x) else: l_data.append(x) if len(l_data) > 0: debug.deprint("Warning: Attribute %s has multiple datatypes - using first one" % name) if len(l_values) == 0: datatype = l_data[0] else: datatype = tuple([tuple(l_values)] + l_data[0]) else: datatype = tuple(l_values) facts["attrs"][name] = (datatype, curval)
def read(self, xmlfile, root=None, stub=False): try: doc = etree.parse(xmlfile) except etree.XMLSyntaxError as e: debug.dprint("Invalid XML.") debug.dprint(e) return None self.lost_eles = [] self.added_eles = [] self.lost_attrs = [] self.added_attrs = [] if root is None: datatree = self.valid_children(":start")[0] else: if stub: datatree = self.valid_node_stub(root) else: datatree = self.valid_node(root) xmlnode = doc.getroot() self.xml_read_merge(datatree, xmlnode) self.xml_read_core(datatree.get_current_tree(), xmlnode, doc) if len(self.lost_eles) != 0: debug.deprint("WARNING: Lost XML elements:\n" + str(self.lost_eles)) if len(self.added_eles) != 0: debug.deprint("WARNING: Added XML elements:\n" + str(self.added_eles)) if len(self.lost_attrs) != 0: debug.deprint("WARNING: Lost XML attributes:\n" + str(self.lost_attrs)) if len(self.added_eles) != 0: debug.deprint("WARNING: Added XML attributes:\n" + str(self.added_attrs)) return datatree
def configure_plugins(suffix): homedir = os.path.expanduser('~') dirs = [] # devel plugins first dirs.append('../plugins/' + suffix) dirs.append("../../plugins/" + suffix) dirs.extend([ os.path.join(homedir, ".stk", "plugins", suffix), "/usr/local/share/plugins/" + suffix, "/usr/share/plugins/" + suffix ]) if sys.platform != "win32" and sys.platform != "win64": dirs.append("/etc/stk/plugins/" + suffix) if sys.platform == 'darwin': dirs.append(os.path.join(stk_path, "plugins", suffix)) if sys.platform.startswith("win"): dirs.append("plugins/" + suffix) for dir in dirs: sys.path.insert(0, dir) try: dir_list = os.listdir(dir) for file in dir_list: module_name, ext = os.path.splitext(file) if ext == ".py": try: debug.deprint("Attempting to import " + module_name, 1) module = __import__(module_name) except: debug.deprint("Plugin raised an exception:", 0) tb = traceback.format_exception( sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) tb_msg = "" for tbline in tb: tb_msg += tbline debug.deprint(tb_msg, 0) except OSError: pass
def report_signal(self, sender): debug.deprint("A plugin has altered the XML: reloading.", 1)
def cb_nsname(self, element, facts): debug.deprint("nsName element found. Yet to handle.", 0)
def preprocess(schemafile): p = etree.XMLParser(remove_comments=True) ns = 'http://relaxng.org/ns/structure/1.0' if 'http' in schemafile: schemafile_handle = urllib2.urlopen(schemafile) else: schemafile_handle = open(schemafile) try: tree = etree.parse(schemafile_handle, p) except Exception: debug.deprint("Error: %s is not a valid Relax NG schema" % schemafile, 0) sys.exit(1) # # deal with include # includes = tree.xpath('/t:grammar//t:include', namespaces={'t': ns}) for include in includes: include_parent = include.getparent() include_index = list(include_parent).index(include) # find the file file = None filename = include.attrib["href"] possible_files = [ os.path.join(os.path.dirname(schemafile), filename), filename ] possible_files.append( os.path.join( "/home/wade/Downloads/multifluids_icferst-master/libspud/../share/spud", filename)) possible_files.append( os.path.join( os.path.dirname(__file__) + "/../../schema", filename)) for possible_file in possible_files: try: if 'http' in possible_file: file = urllib2.urlopen(possible_file) else: file = open(possible_file) break except IOError: debug.deprint( "IOError when searching for included file " + filename, 1) if file is None: debug.deprint( "Error: could not locate included file %s" % filename, 0) debug.deprint("Path: %s" % possible_files) sys.exit(1) # parse the included xml file and steal all the nodes include_tree = etree.parse(file, p) nodes_to_take = include_tree.xpath('/t:grammar/*', namespaces={'t': ns}) # here's where the magic happens: for node in nodes_to_take: include_parent.insert(include_index, copy.deepcopy(node)) # now delete the include: include_parent.remove(include) grammar_list = tree.xpath('/t:grammar', namespaces={'t': ns}) # If the .rnc didn't include a start = prefix, then no valid # grammar tag will be present. Let the user know. if len(grammar_list) == 0: debug.deprint("Error: No grammar tag present in schema.", 0) sys.exit(1) grammar = grammar_list[0] defines = {} define_nodes = tree.xpath('/t:grammar//t:define', namespaces={'t': ns}) # # deal with combine="interleave" # # first, fetch all the plain definitions for define in define_nodes: if "combine" not in define.attrib: name = define.attrib["name"] defines[name] = define # now look for interleaves with those for define in define_nodes: if "combine" in define.attrib and define.attrib[ "combine"] == "interleave": name = define.attrib["name"] if name not in defines: defines[name] = define else: matching_defn = defines[name] for child in define: matching_defn.append(copy.deepcopy(child)) # # deal with combine="choice" # combine_names = [] for define in define_nodes: if "combine" in define.attrib and define.attrib["combine"] == "choice": name = define.attrib["name"] combine_names.append(name) combine_names = list(set(combine_names)) for name in combine_names: xpath = tree.xpath('/t:grammar//t:define[@name="%s"]' % name, namespaces={'t': ns}) choices = [] for node in xpath: choices = choices + list(node) define = etree.Element("define") define.attrib["name"] = name choice = etree.Element("choice") define.append(choice) for x in choices: choice.append(x) defines[name] = define # delete all the define nodes from the xml for define in define_nodes: parent = define.getparent() parent.remove(define) # add the modified defines back to the grammar for define in defines.values(): grammar.append(define) return etree.tostring(tree, xml_declaration=True, encoding='utf-8', pretty_print=True)
def get_tags_from_attrs(self, font, lang, attrs): tags = [] if font: font, fontattrs = self.fontdesc_to_attrs(font) fontdesc = font.to_string() if fontattrs: attrs.extend(fontattrs) if fontdesc and fontdesc != 'Normal': if not self.tags.has_key(font.to_string()): tag = self.create_tag() tag.set_property('font-desc', font) if not self.tagdict.has_key(tag): self.tagdict[tag] = {} self.tagdict[tag]['font_desc'] = font.to_string() self.tags[font.to_string()] = tag tags.append(self.tags[font.to_string()]) if lang: if not self.tags.has_key(lang): tag = self.create_tag() tag.set_property('language', lang) self.tags[lang] = tag tags.append(self.tags[lang]) if attrs: for a in attrs: if a.type == pango.ATTR_FOREGROUND: gdkcolor = self.pango_color_to_gdk(a.color) key = 'foreground%s' % self.color_to_hex(gdkcolor) if not self.tags.has_key(key): self.tags[key] = self.create_tag() self.tags[key].set_property('foreground-gdk', gdkcolor) self.tagdict[self.tags[key]] = {} self.tagdict[self.tags[key]][ 'foreground'] = "#%s" % self.color_to_hex(gdkcolor) tags.append(self.tags[key]) if a.type == pango.ATTR_BACKGROUND: gdkcolor = self.pango_color_to_gdk(a.color) key = 'background%s' % self.color_to_hex(gdkcolor) if not self.tags.has_key(key): self.tags[key] = self.create_tag() self.tags[key].set_property('background-gdk', gdkcolor) self.tagdict[self.tags[key]] = {} self.tagdict[self.tags[key]][ 'background'] = "#%s" % self.color_to_hex(gdkcolor) tags.append(self.tags[key]) if self.pango_translation_properties.has_key(a.type): prop = self.pango_translation_properties[a.type] #print 'setting property %s of %s (type: %s)'%(prop,a,a.type) val = getattr(a, 'value') #tag.set_property(prop,val) mval = val if self.attval_to_markup.has_key(prop): #print 'converting ',prop,' in ',val if self.attval_to_markup[prop].has_key(val): mval = self.attval_to_markup[prop][val] else: debug.deprint( "hmmm, didn't know what to do with value %s" % val, 2) key = "%s%s" % (prop, val) if not self.tags.has_key(key): self.tags[key] = self.create_tag() self.tags[key].set_property(prop, val) self.tagdict[self.tags[key]] = {} self.tagdict[self.tags[key]][prop] = mval tags.append(self.tags[key]) return tags
__set_default("diffsub", "indianred") schemata = {} for dir in [os.path.join(path, "schemata") for path in dirs]: try: for file in os.listdir(dir): if file[-1] == "~" or file[0] == ".": #skip files like .nfs0000 continue # bloody emacs # Skip item gracefully here if there's a problem. # This is useful if the schemata files are in a subversion # repository and there's pesky .svn folders around. try: handle = open(os.path.join(dir, file)) except: debug.deprint("Failure to examine entry " + file + " in folder " + dir + ".") continue lines = [x.strip() for x in handle if x.strip()] if len(lines) < 2: debug.deprint("Warning: Found schema registration file \"" + file + "\", but file is improperly formatted - schema type not registered", 0) continue # Expand environment variables in the schema path alias = {} for i in range(1, len(lines)): line = lines[i] keyvalue = [x.strip() for x in line.split("=")] if len(keyvalue) == 1: key, value = ("default", keyvalue[0]) elif len(keyvalue) == 2:
# Here we hard-code a default for phyml # so that users don't have to tweak this to run it. schemata = {'phyml': ('Phylogenetic Storage Language', {None: dirs[0]})} for dir in [os.path.join(path, "schemata") for path in dirs]: try: for file in os.listdir(dir): if file[-1] == "~" or file[0] == ".": #skip files like .nfs0000 continue # bloody emacs # Skip item gracefully here if there's a problem. # This is useful if the schemata files are in a subversion # repository and there's pesky .svn folders around. try: handle = open(os.path.join(dir, file)) except: debug.deprint("Failure to examine entry " + file + " in folder " + dir + ".") continue lines = [x.strip() for x in handle if x.strip()] if len(lines) < 2: debug.deprint(os.path.join(dir, file), 0) debug.deprint( "Warning: Found schema registration file \"" + file + "\", but file is improperly formatted - schema type not registered", 0) continue # Expand environment variables in the schema path alias = {} for i in range(1, len(lines)): line = lines[i]
def cb_except(self, element, facts): debug.deprint("except element found. Yet to handle.", 0)
def get_tags_from_attrs (self, font,lang,attrs): tags = [] if font: font,fontattrs = self.fontdesc_to_attrs(font) fontdesc = font.to_string() if fontattrs: attrs.extend(fontattrs) if fontdesc and fontdesc!='Normal': if not self.tags.has_key(font.to_string()): tag=self.create_tag() tag.set_property('font-desc',font) if not self.tagdict.has_key(tag): self.tagdict[tag]={} self.tagdict[tag]['font_desc']=font.to_string() self.tags[font.to_string()]=tag tags.append(self.tags[font.to_string()]) if lang: if not self.tags.has_key(lang): tag = self.create_tag() tag.set_property('language',lang) self.tags[lang]=tag tags.append(self.tags[lang]) if attrs: for a in attrs: if a.type == pango.ATTR_FOREGROUND: gdkcolor = self.pango_color_to_gdk(a.color) key = 'foreground%s'%self.color_to_hex(gdkcolor) if not self.tags.has_key(key): self.tags[key]=self.create_tag() self.tags[key].set_property('foreground-gdk',gdkcolor) self.tagdict[self.tags[key]]={} self.tagdict[self.tags[key]]['foreground']="#%s"%self.color_to_hex(gdkcolor) tags.append(self.tags[key]) if a.type == pango.ATTR_BACKGROUND: gdkcolor = self.pango_color_to_gdk(a.color) key = 'background%s'%self.color_to_hex(gdkcolor) if not self.tags.has_key(key): self.tags[key]=self.create_tag() self.tags[key].set_property('background-gdk',gdkcolor) self.tagdict[self.tags[key]]={} self.tagdict[self.tags[key]]['background']="#%s"%self.color_to_hex(gdkcolor) tags.append(self.tags[key]) if self.pango_translation_properties.has_key(a.type): prop=self.pango_translation_properties[a.type] #print 'setting property %s of %s (type: %s)'%(prop,a,a.type) val=getattr(a,'value') #tag.set_property(prop,val) mval = val if self.attval_to_markup.has_key(prop): #print 'converting ',prop,' in ',val if self.attval_to_markup[prop].has_key(val): mval = self.attval_to_markup[prop][val] else: debug.deprint("hmmm, didn't know what to do with value %s"%val, 2) key="%s%s"%(prop,val) if not self.tags.has_key(key): self.tags[key]=self.create_tag() self.tags[key].set_property(prop,val) self.tagdict[self.tags[key]]={} self.tagdict[self.tags[key]][prop]=mval tags.append(self.tags[key]) return tags
def cb_anyname(self, element, facts): debug.deprint("anyName element found. Yet to handle.", 0)
def cb_element(self, element, facts): newfacts = {} if "cardinality" in facts: newfacts["cardinality"] = facts["cardinality"] if "name" in element.keys(): newfacts["name"] = element.get("name") else: debug.deprint("Warning: Encountered element with no name") newfacts['schemaname'] = self.tree.getpath(element) for child in self.element_children(element): tag = self.tag(child) if tag not in [ 'element', 'optional', 'zeroOrMore', 'oneOrMore', 'ignore' ]: f = self.callbacks[tag] x = f(child, newfacts) try: d = newfacts["datatype"] if isinstance(d, tuple): new_d = [] for x in d: if x is not None: new_d.append(x) d = tuple(new_d) newfacts["datatype"] = d if len(d) == 0: newfacts["datatype"] = None elif len(d) == 1 and isinstance(d[0], plist.List): newfacts["datatype"] = d[0] else: l_values = [] l_data = [] for x in d: if isinstance(x, str): l_values.append(x) else: l_data.append(x) if len(l_data) > 1: if "name" in element.keys(): debug.deprint( "Warning: Element %s has multiple datatypes - using first one" % newfacts["name"]) else: debug.deprint( "Warning: Unnamed element has multiple datatypes - using first one" ) if len(l_data) > 0: if len(l_values) == 0: newfacts["datatype"] = l_data[0] else: newfacts["datatype"] = tuple([tuple(l_values)] + l_data[0]) except KeyError: pass return tree.Tree(**newfacts)
# Here we hard-code a default for flml # so that users don't have to tweak this to run it. schemata = {'flml': ('Fluidity markup language', 'http://amcg.ese.ic.ac.uk/svn/fluidity/tags/4.0-release/schemas/fluidity_options.rng')} for dir in dirs: try: for file in os.listdir(dir): if file[-1] == "~" or file[0] == ".": #skip files like .nfs0000 continue # bloody emacs # Skip item gracefully here if there's a problem. # This is useful if the schemata files are in a subversion # repository and there's pesky .svn folders around. try: handle = open(os.path.join(dir, file)) except: debug.deprint("Failure to examine entry " + file + " in folder " + dir + ".") continue newSchemata = [x.strip() for x in handle] if len(newSchemata) < 2: debug.deprint("Warning: Found schema registration file \"" + file + "\", but file is improperly formatted - schema type not registered", 0) continue # Expand environment variables in the schema path newSchemata[1] = os.path.expandvars(newSchemata[1]) if not os.path.exists(newSchemata[1]) and 'http' not in newSchemata[1]: debug.deprint("Warning: not a valid path: %s" % newSchemata[1], 0) debug.deprint("schema type not registered") continue schemata[file] = tuple(newSchemata) debug.dprint("Registered schema type: " + file) except OSError: pass
'http://amcg.ese.ic.ac.uk/svn/fluidity/tags/4.0-release/schemas/fluidity_options.rng' ) } for dir in dirs: try: for file in os.listdir(dir): if file[-1] == "~" or file[0] == ".": #skip files like .nfs0000 continue # bloody emacs # Skip item gracefully here if there's a problem. # This is useful if the schemata files are in a subversion # repository and there's pesky .svn folders around. try: handle = open(os.path.join(dir, file)) except: debug.deprint("Failure to examine entry " + file + " in folder " + dir + ".") continue newSchemata = [x.strip() for x in handle] if len(newSchemata) < 2: debug.deprint( "Warning: Found schema registration file \"" + file + "\", but file is improperly formatted - schema type not registered", 0) continue # Expand environment variables in the schema path newSchemata[1] = os.path.expandvars(newSchemata[1]) if not os.path.exists( newSchemata[1]) and 'http' not in newSchemata[1]: debug.deprint("Warning: not a valid path: %s" % newSchemata[1], 0) debug.deprint("schema type not registered")