def setup(): donri = Root(id="donri", affixes=["dor", "do'i"]) donri.history("en").revise(object=Definition(definition="x1 is the daytime...", notes="See also {nicte}...")) with db() as root: root["entries"]["donri"] = donri yield
def import_xml(file, locale): """Import data from a jbovlaste XML export.""" from stutuz import db from stutuz.models import Definition, Root, Compound, Particle, Loan from stutuz.utils.tex import Tex import xml.etree.cElementTree as etree with db() as root: doc = etree.parse(file) for element in doc.getiterator('valsi'): id = element.get('word').decode('utf-8') if id in root['entries']: entry = root['entries'][id] else: case = lambda x: element.get('type') == x if case('gismu'): entry = Root() elif case('lujvo'): entry = Compound() elif case('cmavo'): entry = Particle() elif case("fu'ivla"): entry = Loan() elif case('experimental gismu'): entry = Root(experimental=True) elif case('experimental cmavo'): entry = Particle(experimental=True) else: continue entry.id = id root['entries'][entry.id] = entry definition = Definition() for subelement in element: text = subelement.text.decode('utf-8') case = lambda x: subelement.tag == x if case('definition'): definition.definition = Tex(text) elif case('notes'): definition.notes = Tex(text) elif case('rafsi') and text not in entry.affixes: entry.affixes.append(text) elif case('selmaho'): entry.class_ = text entry.history(locale).revise(definition, comment='Imported from XML.')
def xml(client): """XML export contains the right elements and attributes""" cipra = Root(id='cipra', affixes=['cip']) cipra.history('en').revise(object=Definition( definition='x1 is a test...', notes='Also examination, proxy measure, validation...' )) with db() as root: root['entries']['cipra'] = cipra response = client.get('/dict/+export/en/') xpaths = ( '/dictionary/direction[@from="Lojban"][@to="English"]', '//valsi[@word="cipra"][@type="gismu"]/rafsi = "cip"', '//definition = "x1 is a test..."', '//notes = "Also examination, proxy measure, validation..."' ) for path in xpaths: assert_xml(response, path)