def __init__(self, id, value=None, attributes=None): v = None if value is not None: v = Pattern([TextElement(value["entity"].value)]) attrs = [] if attributes: for attr in attributes: attrs.append( Attribute(Identifier(attr["name"]), Pattern([TextElement(attr["entity"].value)]))) super().__init__(Identifier(id), v, attrs)
def add_simple_message(fname, key, message): orig = "" if os.path.exists(fname): with open(fname) as file: orig = file.read() obj = parse(orig) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {fname} {ent}") obj.body.append(Message(Identifier(key), Pattern([TextElement(message)]))) modified = serialize(obj, with_junk=True) # escape leading dots modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) # ensure the resulting serialized file is valid by parsing again obj = parse(modified) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"introduced junk! {fname} {ent}") # it's ok, write it out with open(fname, "w") as file: file.write(modified)
def add_simple_message(fname, key, message): orig = "" if os.path.exists(fname): orig = open(fname).read() obj = parse(orig) obj.body.append(Message(Identifier(key), Pattern([TextElement(message)]))) modified = serialize(obj) open(fname, "w").write(modified)