from lxml import etree import time import docing import filing 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 reports import Reports from xpaths import Xpaths from profile import Profile class Repis(Nitpo): def __init__(self, do_verbose=False): """report issues""" super().__init__() # a basename for the thing to send self.base = None # current repcode self.repcode = None # state variable for the current fufi self.repis_fufi = None self.omnibus = Omnibus(do_verbose=do_verbose) self.emailer = Emailer(do_verbose=do_verbose) self.infile = Infile(do_verbose=do_verbose) self.itresu = Itresu(do_verbose=do_verbose) self.xpaths = Xpaths(do_verbose=do_verbose) self.kapro = Kapro(do_verbose=do_verbose) self.profile = Profile() # # orimas for additional deduping, read only for # # sending to profiles self.orimas = None if not self.has_conf('folders', 'mail'): raise Exception("I need a 'mail' folder.") #self.do_verbose = do_verbose def run(self, fufi): #the_iteresu = Itresu(do_verbose=args.v) # # the_iteresu.update(fufi) #the_infile = Infile(do_verbose=args.v) #repcode = the_infile.get_repcode(fufi) #the_kapro = Kapro(do_verbose=args.v) #the_kapro.update(repcode) #print(fufi) # repcode = self.infile.get_repcode(fufi) ## om self.repis_fufi = fufi self.base = self.infile.get_base(fufi) self.repcode = self.infile.get_repcode(fufi) self.kapro.update(self.repcode) self.send_omnibus(fufi) if not self.has_conf('repis', 'to_profiles'): return if not self.conf['repis']['to_profiles'] == 'true': return reports = Reports() orimas = reports.get_orimas(get_cache_only=True) self.orimas = orimas['e'] self.send_to_profiles(fufi) def send_to_profiles(self, fufi): # should be done by the calling bin # self.kapro.update(repcode) emads = self.kapro.load(self.repcode) # print(emads) for emad in emads: # print(emad) self.send_to_profile(fufi, emad) def send_to_profile(self, fufi, emad): surks_docu = self.profile.load(emad) # # get the repcodes in the profile repcodes = self.profile.repcodes(surks_docu) # # take account of the orima count_sleep = 0 if self.has_conf('repis', 'wait'): count_sleep = int(self.conf['repis']['wait']) if emad in self.orimas: for repcode in self.orimas[emad]: repcodes[repcode] = 1 # # now call the itresu, fills itresu.dups self.itresu.get_dups(fufi) if self.itresu.are_any_left(fufi, repcodes): out = self.send(emad, surks_docu.getroot(), only=repcodes) if out is not None and count_sleep > 0: time.sleep(count_sleep) else: print(f"repis: no items left to send to {emad}.") def send_omnibus(self, fufi): omnibus_doc = self.omnibus.read() if omnibus_doc is None: # print("repis: I have no omnibus.") return surk_docus = self.omnibus.disect(omnibus_doc) if len(surk_docus) == 0: return None # avoid running prepare every time we send a mail mail_base_fufi = self.conf['folders']['mail'] + '/' + self.base # # now call the itresu, fills itesu.dups self.itresu.get_dups(fufi) if not self.itresu.are_any_left(fufi, None): print("repis: no items left to send for omnibus.") return None filing.prepare(mail_base_fufi) # get the subs for the itresu for emad in surk_docus: self.send(emad, surk_docus[emad], is_omnibus=True) # docing.inject(fufi, omnibus_doc) def send(self, emad, surk_docu, only=None, is_omnibus=False): base = self.base + '/' + self.profile.relfi_from_emad(emad)[:-4] #if emad == 'nitpo_s@openlib.org': # print(only) #print(etree.tostring(surk_docu, pretty_print=True).decode()) maix = filing.parse_lax(self.repis_fufi) if maix is None: return None maix_surk = docing.inject(maix, surk_docu) spro = self.xpaths.get_spro(surk_docu, repcode=self.repcode) # # only for the omnibus, the spros=a means no dedup if not (is_omnibus and spro == 'a'): # # None is all repcodes here, omnibus only subs_ele = self.itresu.xml(self.repis_fufi, only) maix_surk = docing.inject(maix, subs_ele) ## the mail xml out = self.emailer.prepare(maix_surk, base=base, only=only) return out #print(spro)