PYTHON 32
Feedlines.py By cbettis on 9th April 2022 01:06:19 PM
  1. import argparse
  2. import configparser
  3. import logging
  4. import logging.handlers
  5. from logging import config
  6. import os
  7. import sqlite3
  8. from sqlite3 import Error
  9. import string
  10. import sys
  11.  
  12. CONFIG = r'.\config\feedlines.ini'
  13. DB = r'C:\development\data\verses.sqlite'
  14. TERMINATORS = ('.','!','?')
  15.  
  16. logging.config.fileConfig(CONFIG)
  17. infolog = logging.getLogger('infolog')
  18. errorlog = logging.getLogger('errorlog')
  19.  
  20. def configure():
  21.     global CONFIG, DB
  22.     config_parser = configparser.ConfigParser()
  23.     config_parser.read(CONFIG)
  24.     if config_parser['DATA']['db'] != None:
  25.         DB = config_parser['DATA']['db']
  26.     print(f'DB = {DB}')
  27.  
  28. def is_empty(text):
  29.     if not isinstance(text,str):
  30.         return True
  31.     else:
  32.         return len(text) == 0 or text.isspace()
  33.  
  34. def sqlConnection():
  35.     global DB
  36.     try:
  37.         con = sqlite3.connect(DB)
  38.         return con
  39.     except Exception as err:
  40.         errorlog.exception(err)
  41.         print("EXCEPTION OCCURRED. PLEASE SEE LOGS.")
  42.  
  43. def check_source(sourceid):
  44.     ret = -1
  45.     try:
  46.         con = sqlConnection()
  47.         cur = con.cursor()
  48.         cur.execute(f"SELECT rwid FROM source where sourceid = {sourceid};")
  49.         data = cur.fetchone()
  50.         if data != None:
  51.             ret = data[0]
  52.     except Exception as err:
  53.         errorlog.exception(err)
  54.         print('EXCEPTION OCCURRED. PLEASE SEE LOGS.')
  55.     finally:
  56.         cur.close()
  57.         con.close()
  58.         return ret
  59.  
  60. def insert_source(sourcename, byline=None):
  61.     result = -1
  62.     if is_empty(byline):
  63.         sql_text = f"INSERT INTO source (sourcename) VALUES (?);"
  64.         params = (sourcename)
  65.     else:
  66.         sql_text = f"INSERT INTO source(sourcename, byline) values (?,?);"
  67.         params = (sourcename, byline)
  68.     try:
  69.         con = sqlConnection()
  70.         cur = con.cursor()
  71.         cur.execute(sql_text, params)
  72.         con.commit()
  73.         cur.execute("SELECT seq FROM sqlite_sequence WHERE name='source';")
  74.         data = cur.fetchone()
  75.         if data != None:
  76.             result = data[0]
  77.     except Exception as err:
  78.         errorlog.exception(err)
  79.         print('EXCEPTION OCCURRED. PLEASE SEE LOGS.')
  80.     finally:
  81.         cur.close()
  82.         con.close()
  83.         return result
  84.  
  85. def get_source(sourcename, byline=None):
  86.     result = -1
  87.     if is_empty(byline):
  88.         sql_text = f"SELECT rwid FROM source WHERE sourcename=?;"
  89.         params = (sourcename)
  90.     else:
  91.         sql_text = f"SELECT rwid FROM source WHERE sourcename=? and byline=?;"
  92.         params = (sourcename, byline)
  93.     try:
  94.         con = sqlConnection()
  95.         cur = con.cursor()
  96.         cur.execute(sql_text, params)
  97.         data = cur.fetchone()
  98.         if data != None:
  99.             result = data[0]
  100.         else:
  101.             result = insert_source(sourcename, byline)
  102.     except Exception as err:
  103.         errorlog.exception(err)
  104.         print('EXCEPTION OCCURRED. PLEASE SEE LOGS.')
  105.     finally:
  106.         cur.close()
  107.         con.close()
  108.         return result
  109.  
  110. def insert_line(vtext, sourceid=None, sourcename=None, byline=None):
  111.     if sourceid != None:
  112.         if check_source(sourceid) != sourceid:
  113.             raise ValueError("Specified source Id not found in database.")
  114.     elif not is_empty(sourcename):
  115.         sourceid = get_source(sourcename, byline)
  116.     if sourceid == None or sourceid < 1:
  117.         sql_text = f"INSERT INTO vline(verseline) VALUES (?);"
  118.         params = (vtext)
  119.     else:
  120.         sql_text = f"INSERT INTO vline(sourceid, verseline) VALUES (?, ?);"
  121.         params = (sourceid, vtext)
  122.     try:
  123.         con = sqlConnection()
  124.         cur = con.cursor()
  125.         cur.execute(sql_text, params)
  126.         con.commit()
  127.     except ValueError as ve:
  128.         errorlog.exception(ve)
  129.         print('ValueError ENCOUNTERED. PLEASE SEE LOGS.')
  130.     except Exception as err:
  131.         errorlog.exception(err)
  132.         print('EXCEPTION OCCURRED. PLEASE SEE LOGS.')
  133.     finally:
  134.         cur.close()
  135.         con.close()
  136.  
  137. def is_header(linetext):
  138.     if not isinstance(linetext, str):
  139.         return False
  140.     return linetext.startswith('#')
  141.  
  142. def is_terminated(linetext):
  143.     if not isinstance(linetext, str):
  144.         return False
  145.     return linetext.endswith('.') or linetext.endswith('?') or linetext.endswith('!')
  146.  
  147. def process_lines(line1, line2, source_name, by_line):
  148.     line1 = line1.strip()
  149.     line2 = line2.strip()
  150.     if not is_header(line2) and not is_empty(line2):
  151.         if is_terminated(line2):
  152.             if is_header(line1) or is_terminated(line1) or (is_empty(line1) and line2[0] == line2[0].upper()):
  153.                 insert_line(line2, sourcename=source_name, byline=by_line)
  154.  
  155. def feed(fpath, sourcename, byline):
  156.     # open file for reading
  157.     line1 = None
  158.     line2 = None
  159.     with open(fpath, 'r') as f:
  160.         line1 = f.readline()
  161.         line2 = f.readline()
  162.         process_lines(line1, line2, sourcename, byline)
  163.         while True:
  164.             line1 = line2
  165.             line2 = f.readline()
  166.             if not line2:
  167.                 break
  168.             process_lines(line1, line2, sourcename, byline)
  169.  
  170. def main(argv):
  171.     parser = argparse.ArgumentParser()
  172.     parser.add_argument('infile', help='path to source file; should be a markdown file with headers denoted by one or more "#" symbols.')
  173.     parser.add_argument('-s', '--sourcename', help='Source name to use in `source` table (defaults to physical filename).')
  174.     parser.add_argument('-b', '--byline', help='Byline to use in `source` table.')
  175.     args = parser.parse_args()
  176.     configure()
  177.     fpath = args.infile
  178.     sourcename = os.path.basename(fpath)
  179.     byline = None
  180.     if args.sourcename:
  181.         sourcename = args.sourcename
  182.     if args.byline:
  183.         byline = args.byline
  184.     feed(fpath, sourcename, byline)
  185.     print('... DONE ...')
  186.  
  187. if __name__ == "__main__":
  188.     main(sys.argv)

Caracabe Heap is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.