git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
d6bb2a3
Branches
Tags
develop-client_server
master
typescript
fs-words.git
tools
common.py
moved to flat files
Christian Fraß
commited
d6bb2a3
at 2017-04-17 17:05:04
common.py
Blame
History
Raw
import json as _json import os as _os import re as _re def dokuwiki_to(data): content = "" content += (" * type: %s\n" % (data["type"])) content += (" * description: %s\n" % ("?" if (data["description"] is None) else data["description"])) content += (" * tags\n") for tag in data["tags"]: content += (" * %s\n" % (tag)) content += (" * translations\n") for (language , lemmata) in data["translations"].items(): content += (" * %s\n" % (language)) for lemma in lemmata: content += (" * %s\n" % (lemma)) return content def dokuwiki_from(content): data = {} state = "type" lines = content.split("\n") for line in lines: if (line != ""): if (state == "type"): result = _re.match("^ \\* type: (.*)$", line) if (result is not None): data["type"] = result.group(1) state = "description" else: raise ValueError("type expected in line '%s' while being in state '%s'" % (line, state)) elif (state == "description"): result = _re.match("^ \\* description: (.*)$", line) if (result is not None): data["description"] = (None if (result.group(1) == "?") else result.group(1)) state = "tags" else: raise ValueError("description expected in line '%s' while being in state '%s'" % (line, state)) elif (state == "tags"): result = _re.match("^ \\* tags$", line) if (result is not None): data["tags"] = [] state = "tagentry" else: raise ValueError("tags expected in line '%s' while being in state '%s'" % (line, state)) elif (state == "tagentry"): result = _re.match("^ \\* (.*)$", line) if (result is not None): data["tags"].append(result.group(1)) state = "tagentry" else: result = _re.match("^ \\* translations$", line) if (result is not None): data["translations"] = {} state = "language" else: raise ValueError("tag or translations expected in line '%s' while being in state '%s'" % (line, state)) elif (state == "language"): result = _re.match("^ \\* (.*)$", line) if (result is not None): language = result.group(1) data["translations"][language] = [] state = "lemma" else: raise ValueError("language expected in line '%s' while being in state '%s'" % (line, state)) elif (state == "lemma"): result = _re.match("^ \\* (.*)$", line) if (result is not None): lemma = result.group(1) data["translations"][language].append(lemma) state = "lemma" else: result = _re.match("^ \\* (.*)$", line) if (result is not None): language = result.group(1) data["translations"][language] = [] state = "lemma" else: raise ValueError("language or lemma expected in line '%s' while being in state '%s'" % (line, state)) else: raise ValueError("unhandled state '%s'" % (state)) return data def json_to(data): content = _json.dumps(data, indent = "\t", ensure_ascii = False) return content def json_from(content): data = _json.loads(content) return data def filemap(folder_from, convert_from, folder_to, convert_to, namemapper): for name_from in _os.listdir(folder_from): path_from = _os.path.join(folder_from, name_from) handle_from = open(path_from, "r") content_from = handle_from.read() handle_from.close() data = convert_from(content_from) name_to = namemapper(name_from) path_to = _os.path.join(folder_to, name_to) content_to = convert_to(data) handle_to = open(path_to, "w") handle_to.write(content_to) handle_to.close()