extensions.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from flask_sqlalchemy import SQLAlchemy
  2. db = SQLAlchemy()
  3. from datetime import datetime, timezone, timedelta
  4. def dict_row(r_in):
  5. import sqlalchemy
  6. d_out = {}
  7. l_keys = []
  8. if type(r_in)==sqlalchemy.engine.row.Row:
  9. l_keys = list(r_in._fields)
  10. else:
  11. l_keys = list(r_in.__dict__.keys())
  12. r_in = r_in.__dict__
  13. # end of type check on r_in
  14. for I in range(len(l_keys)):
  15. sk = l_keys[I]
  16. d_out[sk] = r_in[I]
  17. return d_out
  18. #end of dict_row function
  19. def dict_instance(o_item):
  20. d_in = o_item.__dict__
  21. d_out = {}
  22. l_keys = list(d_in.keys())
  23. for s_key in l_keys:
  24. if not s_key.startswith("_"):
  25. d_out[s_key] = d_in[s_key] if d_in[s_key] is not None else ""
  26. # end of making sure does not start with _
  27. # end of looping through l_keys
  28. return d_out
  29. # end of dict_instance function
  30. def extract_exc(s_additional=""):
  31. import sys, traceback
  32. d_out = {"i_lineno": 0, "s_filename": "", "s_except": "", "s_tb": "", "s_additional": s_additional}
  33. exc_type, exc_obj, exc_tb = sys.exc_info()
  34. if exc_obj is not None and exc_tb is not None:
  35. s_exception = str(exc_obj)
  36. o_frame = exc_tb.tb_frame
  37. s_filename = o_frame.f_code.co_filename
  38. i_lineno = exc_tb.tb_lineno
  39. s_tb = traceback.format_exc()
  40. d_out["i_lineno"] = i_lineno
  41. d_out["s_filename"] = s_filename
  42. d_out["s_except"] = s_exception
  43. d_out["s_tb"] = s_tb
  44. # write_log
  45. s_logging = f"line no.: {d_out['i_lineno']}\nfilename: {d_out['s_filename']}\nexcept: {d_out['s_except']}\nadditional: {d_out['s_additional']}"
  46. write_log(s_logging)
  47. return d_out
  48. else:
  49. return False
  50. # end of making sure an exception has occurred
  51. # end of extract_exc function
  52. def check_date(s_input):
  53. """use dateutil.parser.parse to validate and return possible date values"""
  54. from dateutil import parser
  55. bl_out, dt_out = (False, datetime.now())
  56. try:
  57. dt_out = parser.parse(s_input)
  58. bl_out = True
  59. except:
  60. bl_out, dt_out = (False, datetime.now())
  61. # end of try-catch to check for possible valid date
  62. return (bl_out, dt_out)
  63. # end of check_date function
  64. def write_log(s_val):
  65. """trouble-shooting..."""
  66. import os, app
  67. from flask import request
  68. # if int(os.getenv("FLASK_DEBUG", "0"))!=1: return True
  69. s_url = request.url.replace(request.url_root, "")
  70. from datetime import datetime, timezone, timedelta
  71. s_path = app.__path__[0] + "/logging/cvdb_log.txt"
  72. s_path = os.path.realpath(s_path)
  73. s_dt = datetime.now().astimezone(timezone(timedelta(hours=2))).strftime("%Y-%m-%d %H:%M:%S")
  74. with open(s_path, "a") as f:
  75. f.write(f"{s_dt} - {s_url}: {s_val}\n---\n")
  76. f.close()
  77. # end of with for file
  78. return True
  79. # end of write_log function