Skip to content

Instantly share code, notes, and snippets.

@ilius
Created January 17, 2021 23:24
Show Gist options
  • Save ilius/f89689acbb46efdfe352c13ea9b10cb4 to your computer and use it in GitHub Desktop.
Save ilius/f89689acbb46efdfe352c13ea9b10cb4 to your computer and use it in GitHub Desktop.
pharmaplus.py
# -*- coding: utf-8 -*-
from formats_common import *
import html
enable = True
format = 'Pharmaplus'
description = 'Pharmaplus (SQLite3)'
extensions = ()
readOptions = []
writeOptions = []
tools = [
{
"name": "Pharmaplus",
"web": "",
"platforms": ["Android"],
# "license": "",
},
]
class Reader(object):
def __init__(self, glos):
self._glos = glos
self._clear()
def _clear(self):
self._filename = ''
self._con = None
self._cur = None
def open(self, filename):
from sqlite3 import connect
self._filename = filename
self._con = connect(filename)
self._cur = self._con.cursor()
self._glos.setDefaultDefiFormat("h")
def __len__(self):
self._cur.execute("select count(*) from book_contents")
return self._cur.fetchone()[0]
def __iter__(self):
from pyglossary.langs.writing_system import getWritingSystemFromText
alternateDict = {}
self._cur.execute("select brand_name, generic_name from book_contents")
for row in self._cur.fetchall():
if row[0] in alternateDict:
alternateDict[row[0]].append(row[1])
else:
alternateDict[row[0]] = [row[1]]
self._cur.execute(
"select brand_name, generic_name, drug_category, theraputic_group, content, pregnancy, lactation, indication_dosage, administration, contra_indications, precautions, interactions, side_effects, compatibility, stability from book_contents"
" order by id"
)
# FIXME: iteration over self._cur stops after one entry
# and self._cur.fetchone() returns None
# for row in self._cur:
for row in self._cur.fetchall():
brand_name = row[0]
generic_name = row[1]
drug_category = row[2]
theraputic_group = row[3]
content = row[4]
pregnancy = row[5]
lactation = row[6]
indication_dosage = row[7]
administration = row[8]
contra_indications = row[9]
precautions = row[10]
interactions = row[11]
side_effects = row[12]
compatibility = row[13]
stability = row[14]
word = brand_name
definition = generic_name
if definition is None:
definition = ""
if brand_name:
definition += f'<br><b>Brand Name:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{brand_name}</div></a>'
if theraputic_group:
definition += f'<br><b>Theraputic Group:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{theraputic_group}</div></a>'
if drug_category:
definition += f'<br><b>Category:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{drug_category}</div></a>'
if content:
definition += f'<br><b>Content:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{content}</div></a>'
if pregnancy:
definition += f'<br><b>Pregnancy:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{pregnancy}</div></a>'
if lactation:
definition += f'<br><b>Lactation:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{lactation}</div></a>'
if indication_dosage:
definition += f'<br><b>Indication and dosage:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{indication_dosage}</div></a>'
if administration:
definition += f'<br><b>Administration:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{administration}</div></a>'
if contra_indications:
definition += f'<br><b>Contraindications:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{contra_indications}</div></a>'
if precautions:
definition += f'<br><b>Precautions:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{precautions}</div></a>'
if interactions:
definition += f'<br><b>Interactions:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{interactions}</div></a>'
if side_effects:
definition += f'<br><b>Side effects:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{side_effects}</div></a>'
if compatibility:
definition += f'<br><b>Compatibility:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{compatibility}</div></a>'
if stability:
definition += f'<br><b>Stability:</b> <a> <div style="white-space: pre-wrap; word-break: keep-all;">{stability}</div></a>'
words = [brand_name, generic_name]
if brand_name in alternateDict:
words += alternateDict[brand_name]
words = [
word
for word in words
if word
]
yield self._glos.newEntry(
words,
definition,
defiFormat="h",
)
def close(self):
if self._cur:
self._cur.close()
if self._con:
self._con.close()
self._clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment