Skip to content

Instantly share code, notes, and snippets.

@filmor
Created April 27, 2019 18:14
Show Gist options
  • Save filmor/536e3f9f76f18064f3291b65428278fd to your computer and use it in GitHub Desktop.
Save filmor/536e3f9f76f18064f3291b65428278fd to your computer and use it in GitHub Desktop.
Convert Einheitsübersetzung from a given APK to a (zipped) Sword module
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from zipfile import ZipFile\n",
"from shutil import make_archive\n",
"from tempfile import TemporaryDirectory\n",
"import sqlite3\n",
"import xml.etree.cElementTree as ET\n",
"import subprocess\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"path = \"Downloads/Die Bibel Einheitsübersetzung 2016_v1.2.4_apkpure.com.apk\"\n",
"sqlite_path = \"assets/bibelwerk_eue.sqlite\"\n",
"\n",
"version = \"1.2.4\"\n",
"work = \"GerEue2018\"\n",
"desc = \"German Einheitsübersetzung 2016 (rev. 2018)\"\n",
"versification = \"Catholic\"\n",
"output_path = f\"Downloads/{work}\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"codes_to_osis = '\\nMOS1 Gen\\nMOS2 Exod\\nMOS3 Lev\\nMOS4 Num\\nMOS5 Deut\\nJOSU Josh\\nRICH Judg\\nRUT0 Ruth\\nSAM1 1Sam\\nSAM2 2Sam\\nKOE1 1Kgs\\nKOE2 2Kgs\\nCHR1 1Chr\\nCHR2 2Chr\\nESRA Ezra\\nNEHE Neh\\nTOBI Tob\\nJUDI Jdt\\nESTE Esth\\nMAK1 1Macc\\nMAK2 2Macc\\nIJOB Job\\nPSAL Ps\\nSPRI Prov\\nKOHE Eccl\\nHOHE Song\\nWEIS Wis\\nSIRA Sir\\nJESA Isa\\nJERE Jer\\nKLAG Lam\\nBARU Bar\\nEZEC Ezek\\nDANI Dan\\nHOSE Hos\\nJOEL Joel\\nAMOS Amos\\nOBAD Obad\\nJONA Jonah\\nMICH Mic\\nNAHU Nah\\nHABA Hab\\nZEFA Zeph\\nHAGG Hag\\nSACH Zech\\nMALE Mal\\nMATT Matt\\nMARK Mark\\nLUKA Luke\\nJOHA John\\nAPOS Acts\\nROEM Rom\\nKOR1 1Cor\\nKOR2 2Cor\\nGALA Gal\\nEPHE Eph\\nPHIP Phil\\nKOLO Col\\nTHE1 1Thess\\nTHE2 2Thess\\nTIM1 1Tim\\nTIM2 2Tim\\nTITU Titus\\nPHIL Phlm\\nHEBR Heb\\nJAKO Jas\\nPET1 1Pet\\nPET2 2Pet\\nJOH1 1John\\nJOH2 2John\\nJOH3 3John\\nJUDA Jude\\nOFFE Rev\\n'\n",
"codes_to_osis = dict([i.split(\" \", 1) for i in codes_to_osis.splitlines() if i])\n",
"\n",
"mod_conf = f\"\"\"[{work}]\n",
"Description={desc}\n",
"DataPath=./modules/texts/ztext/{work.lower()}\n",
"ModDrv=zText\n",
"SourceType=OSIS\n",
"Encoding=UTF-8\n",
"CompressType=ZIP\n",
"BlockType=BOOK\n",
"Versification={versification}\n",
"Feature=NoParagraphs\n",
"Version={version}\n",
"Lang=de\n",
"Obsoletes=GerEue2016\n",
"Obsoletes=GerEue1980\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"books = {}\n",
"data = {}\n",
"\n",
"with TemporaryDirectory() as d:\n",
" with ZipFile(path) as z:\n",
" extracted = z.extract(sqlite_path, path=d)\n",
"\n",
" c = sqlite3.connect(extracted)\n",
"\n",
" cur = c.execute(\"\"\"\n",
" select code, chapter, verse, text from revidiert_verses\n",
" join books on books.id = book_id\n",
" order by code, chapter, verse\n",
" \"\"\")\n",
"\n",
" while True:\n",
" fetched = cur.fetchone()\n",
" if fetched is None:\n",
" break\n",
"\n",
" code, chapter, verse, text = fetched\n",
" if isinstance(chapter, list):\n",
" break\n",
"\n",
" book = codes_to_osis[code]\n",
" books.setdefault(book, set()).add(chapter)\n",
"\n",
" verses = data.setdefault((book, chapter), {})\n",
" verses[verse] = text"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def process_verse(el, text):\n",
" mapping = {\n",
" \" \": \" \",\n",
" \"<i>\": \"\",\n",
" \"</i>\": \"\",\n",
" \"<caps>\": \"\",\n",
" \"</caps>\": \"\",\n",
" \"<br>\": \"\\n\",\n",
" \"<br/>\": \"\\n\"\n",
" }\n",
" \n",
" for key, val in mapping.items():\n",
" text = text.replace(key, val)\n",
" el.text = text"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"root = ET.Element(\n",
" \"osis\",\n",
" attrib={\n",
" \"xmlns\": \"http://www.bibletechnologies.net/2003/OSIS/namespace\",\n",
" \"xmlns:xml\": \"http://www.w3.org/XML/1998/namespace\",\n",
" \"xmlns:xsi\": \"http://www.w3.org/2001/XMLSchema-instance\",\n",
" \"xsi:schemaLocation\": \"http://www.bibletechnologies.net/2003/OSIS/namespace http://www.bibletechnologies.net/osisCore.2.1.1.xsd\",\n",
" }\n",
")\n",
"\n",
"osis_text = ET.SubElement(\n",
" root, \"osisText\",\n",
" osisIDWork=work,\n",
" osisRefWork=\"bible\",\n",
" canonical=\"true\",\n",
" \n",
" attrib={\"xml:lang\": \"de-DE\"}\n",
")\n",
"\n",
"header = ET.SubElement(osis_text, \"header\")\n",
"work_obj = ET.SubElement(header, \"work\", osisWork=work)\n",
"\n",
"for book, chapters in books.items():\n",
" el = ET.SubElement(\n",
" osis_text, \"div\",\n",
" type=\"book\", osisID=book, canonical=\"true\"\n",
" )\n",
"\n",
" for chapter in chapters:\n",
" ch_el = ET.SubElement(el, \"chapter\", osisID=f\"{book}.{chapter}\")\n",
" \n",
" verses = data[(book, chapter)]\n",
" \n",
" for verse, verse_text in verses.items():\n",
" verse_el = ET.SubElement(ch_el, \"verse\", osisID=f\"{book}.{chapter}.{verse}\")\n",
" process_verse(verse_el, verse_text)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"with TemporaryDirectory() as d:\n",
" d = Path(d)\n",
" mod_path = d / \"modules/texts/ztext\" / work.lower()\n",
" mod_path.mkdir(parents=True)\n",
" res = subprocess.Popen(\n",
" [\"osis2mod\", mod_path, \"-\", \"-z\", \"z\", \"-v\", versification],\n",
" stdin=subprocess.PIPE,\n",
" \n",
" )\n",
" \n",
" tree = ET.ElementTree()\n",
" tree._setroot(root)\n",
" tree.write(res.stdin, xml_declaration=True, encoding=\"utf8\")\n",
" \n",
" mods_d = d / \"mods.d\"\n",
" mods_d.mkdir()\n",
" conf = mods_d / f\"{work.lower()}.conf\"\n",
" conf.write_text(mod_conf, encoding=\"utf8\")\n",
" \n",
" make_archive(output_path, \"zip\", d)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment