keyboardlayouteditor

[Fork] Archive of the keyboard layout editor.
git clone https://git.jojolepro.com/keyboardlayouteditor.git
Log | Files | Refs | README | LICENSE

commit 3f7ffd346cd9b9614a95962e10c365ff01db09a2
parent 3a70f1314318941d9c9a2b7c3b175553b2ac1dab
Author: simos.lists <simos.lists@70737e48-4f4a-0410-8df8-290828ad50c4>
Date:   Thu, 19 Jun 2008 21:25:12 +0000

XKB<->XML, now write to files

git-svn-id: http://keyboardlayouteditor.googlecode.com/svn/trunk@50 70737e48-4f4a-0410-8df8-290828ad50c4

Diffstat:
MXKBGrammar/parse_xkb_to_xml.py | 13++++++++++---
MXKBGrammar/parse_xml_to_xkb.py | 66+++++++++++++++++++++++++++++++++++-------------------------------
2 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/XKBGrammar/parse_xkb_to_xml.py b/XKBGrammar/parse_xkb_to_xml.py @@ -87,6 +87,7 @@ doc = etree.ElementTree(layout) layout.attrib['layoutname'] = os.path.basename(xkbfilename) +print "Processing", os.path.basename(xkbfilename), "...", for symbols in result.tree.getChildren(): eSymbol = etree.SubElement(layout, 'symbols') for mapobject in symbols.getChildren(): @@ -155,8 +156,10 @@ for symbols in result.tree.getChildren(): ksname = elem_keysyms[0].getChild(0).getText() eKeySyms = etree.SubElement(eKeySymGroup, 'typegroup', value=ksname[1:-1]) else: - print "Unexpected error!" - sys.exit(-2) + """ We are probably processing level3; we keep first item """ + ksname = elem_keysyms[0].getChild(0).getText() + eKeySyms = etree.SubElement(eKeySymGroup, 'typegroup', value=ksname[1:-1]) + print "Possibly processing level3" if len(getChildrenListByType(keyset, ELEM_VIRTUALMODS)): for vmods in elem_virtualmods: etree.SubElement(eKeySymGroup, 'tokenvirtualmodifiers', value=vmods.getChild(0).getText()) @@ -168,6 +171,10 @@ for symbols in result.tree.getChildren(): print "\tInternal error at map level,", mapobject.getText() # sys.exit(-2) -print etree.tostring(layout, pretty_print=True) +fout = open(os.path.basename(xkbfilename) + ".xml", "w") +fout.write(etree.tostring(layout, pretty_print=True)) +fout.close() + +print " done." #pdb.set_trace() diff --git a/XKBGrammar/parse_xml_to_xkb.py b/XKBGrammar/parse_xml_to_xkb.py @@ -9,94 +9,98 @@ import pdb import antlr3 from lxml import etree -xkbfilename = "xkbsample.xml" +xmlfilename = "xkbsample.xml" if len(sys.argv) > 1: - xkbfilename = sys.argv[1] + xmlfilename = sys.argv[1] try: - xkbfile = open(xkbfilename, 'r') - xkbfile.close + xmlfile = open(xmlfilename, 'r') + xmlfile.close except OSError: - print "Could not open file ", xkbfilename, ". Aborting..." + print "Could not open file ", xmlfilename, ". Aborting..." sys.exit(-1) -doc = etree.parse(xkbfilename) +doc = etree.parse(xmlfilename) # for elem in doc.getiterator(): # print elem.tag, "has", len(elem), "children", elem.attrib, elem.text TABS="\t\t\t\t\t\t\t\t\t\t\t\t" +xkbfilename = os.path.basename(xmlfilename)[:-4] +fout = open(xkbfilename, "w") + def recurse_tree(node, depth): if node.tag == "layout": - print "Filename: ", node.attrib["layoutname"] + print "Parsing", node.attrib["layoutname"] for n in node: recurse_tree(n, depth+1) elif node.tag == "symbols": for k in node: if k.tag == "mapoption": - print k.text, + fout.write(k.text) + fout.write(" ") elif k.tag == "mapname": - print "\"%(s)s\"\n{" % { "s": k.text } + fout.write("\"%(s)s\"\n{\n" % { "s": k.text }) elif k.tag == "mapmaterial": for t in k: if t.tag == "tokenname": - print "\tname = \"%(s)s\";" % { "s": t.attrib["name"] } + fout.write("\tname = \"%(s)s\";\n" % { "s": t.attrib["name"] }) elif t.tag == "tokeninclude": - print "\tinclude \"%(s)s\"" % { "s": t.text } + fout.write("\tinclude \"%(s)s\"\n" % { "s": t.text }) elif t.tag == "tokentype": - print "\tkey.type = \"%(s)s\";" % { "s": t.text } + fout.write("\tkey.type = \"%(s)s\";\n" % { "s": t.text }) elif t.tag == "tokenmodifiermap": - print "\tmodifier_map %(s)s {" % { "s": t.attrib['state'] }, + fout.write("\tmodifier_map %(s)s {" % { "s": t.attrib['state'] }) count_mm = len(t) for mm in t: if mm.tag == "keycodex": - print "<%(s)s>" % { "s": mm.attrib["value"] }, + fout.write("<%(s)s>" % { "s": mm.attrib["value"] }) elif mm.tag == "keycode": - print "%(s)s" % { "s": mm.attrib["value"] }, + fout.write("%(s)s" % { "s": mm.attrib["value"] }) if count_mm > 1: - sys.stdout.write(", ") + fout.write(", ") count_mm -= 1 - print "};" + fout.write("};\n") elif t.tag == "tokenkey": - print "\t", + fout.write("\t") if t.attrib["override"] == "True": - print "override", + fout.write("override") for tk in t: if tk.tag == "keycodename": - print "key <%(s)s> {" % { "s": tk.text }, + fout.write("key <%(s)s> { " % { "s": tk.text }) elif tk.tag == "keysymgroup": gotitem = False for ks in tk: if ks.tag == "typegroup": if gotitem: - sys.stdout.write(", ") - print "type = \"%(s)s\"" % { "s": ks.attrib["value"] }, + fout.write(", ") + fout.write("type = \"%(s)s\" " % { "s": ks.attrib["value"] }) gotitem = True elif ks.tag == "tokenvirtualmodifiers": if gotitem: - sys.stdout.write(", ") - print "virtualMods = %(s)s" % { "s": ks.attrib["value"] }, + fout.write(", ") + fout.write("virtualMods = %(s)s " % { "s": ks.attrib["value"] }) elif ks.tag == "symbolsgroup": if gotitem: - sys.stdout.write(", ") + fout.write(", ") gotitem = True - print "[", + fout.write("[ ") count_sg = len(ks) for sg in ks: if sg.tag == "symbol": if count_sg > 1: - print "%(s)s," % { "s": sg.text }, + fout.write("%(s)s, " % { "s": sg.text }) else: - print "%(s)s" % { "s": sg.text }, + fout.write("%(s)s " % { "s": sg.text }) count_sg -= 1 else: print "ERROR" sys.exit(-1) - print "]", - print "};" - print "};\n" + fout.write("]") + fout.write(" };\n") + fout.write("};\n\n") recurse_tree(doc.getroot(), 0)