#import copy import os import sys #import sys #import lxml.etree as etree import glob from lxml.builder import ElementMaker from lxml import etree #import dating import filing import docing # import subscription from emailer import Emailer from nitpo import Nitpo # from infile import Infile # from itresu import Itresu # from omnibus import Omnibus #from kapro import Kapro from sheets import Sheets #from profile import Profile #import ElementMaker #from subscription import Subscription class Reports(Nitpo): def __init__(self, do_verbose=False): """subscription profiles""" super().__init__() if not self.has_conf('ext', 'report'): print("reports: need an [ext][report] configuration", file=sys.stderr) sys.exit(1) if not self.has_conf('folders', 'reports'): print("reports: I see no reports directory.") sys.exit(1) # # orims structure to maintain self.orimas = {} self.ext_len = len(self.conf['ext']['report']) self.sheets = Sheets(do_verbose) self.reports_fudi = self.conf['folders']['reports'] self.reports_glob = self.reports_fudi + '/*' def fufis(self, only_repcode=None): """gets the report files""" if not self.has_conf('folders', 'reports'): print("reports: I see no reports directory.") quit() fufis = {} print(self.reports_glob) for report_fufi in glob.glob(self.reports_glob): if report_fufi.endswith('~'): continue test_fufi = report_fufi if report_fufi.endswith('.gz'): test_fufi = report_fufi[:-3] if not test_fufi.endswith(self.conf['ext']['report']): print(f"reports: I skip {report_fufi}") continue report_bana = os.path.basename(test_fufi) repcode = report_bana[:-self.ext_len] print(repcode) if only_repcode is not None and repcode != only_repcode: continue fufis[repcode] = report_fufi return fufis def get_orimas(self, only_repcode=None, do_lazy=True, get_cache_only=False): """get orimas and store in [files][orimas]""" if not self.has_conf('files', 'orimas'): print("reports: I need a [files][orimas] configuration.") sys.exit(1) orima_fufi = self.conf['files']['orimas'] fufis = self.fufis(only_repcode=only_repcode) i_need_to_save = False if os.path.isfile(orima_fufi) and (do_lazy or get_cache_only): orimas = filing.load(orima_fufi) if get_cache_only: return orimas else: orimas = {} # # by report orimas['r'] = {} # # by emad orimas['e'] = {} # # by emad, only first emad in report data orimas['o'] = {} for repcode in fufis: fufi = fufis[repcode] # # lazy mode: don't read files older than the cache files # # once per week should be run with non-lazy mode if do_lazy and not filing.donere(orima_fufi, [fufi]): continue i_need_to_save = True # # reports are is the report directory, with the repcode followed # # by the extension repdoc = filing.parse_lax(fufi) # # gives a newline separated text of emad emads = str(self.sheets.get_result('orimas', repdoc)) emads = emads.split('\n') if repcode in orimas: print(f"reports: looks like several configurations of report {repcode}", file=sys.stderr) orimas['r'][repcode] = [] count_emads = 0 for emad in emads: emad = emad.lower() if len(emad) == 0: continue if repcode not in orimas['r']: orimas['r'][repcode] = [] orimas['r'][repcode].append(emad) # # index the emails as well, e for all occurences, # # o for only the first emad if emad not in orimas['e']: orimas['e'][emad] = [] #if len(repcode) == 0: # continue if count_emads == 0: if emad not in orimas['o']: orimas['o'][emad] = [] orimas['o'][emad] = repcode count_emads += 1 orimas['e'][emad].append(repcode) self.orimas = orimas if i_need_to_save: filing.dump(orima_fufi, orimas) return orimas # def clear_repcode_from_orimas(self, repcode): # """could be used to incrementally from orimas""" # orimas = self.orimas # if repcode not in orimas['r']: # return orimas # old_emads = orimas['r'][repcode] # del orimas['r'][repcode] # for emad in old_emads: # if emad not in orimas['e']: # continue # if repcode not in orimas['e'][emad]: # continue # del orimas['e'][emad][repcode] # self.orimas = orimas # return orimas