#import copy #import os #import sys #import lxml.etree as etree #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 xpaths import Xpaths from profile import Profile #from subscription import Subscription class Sender(Nitpo): def __init__(self, do_verbose=False): """subscription profiles""" 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() if not self.has_conf('folders', 'mail'): raise Exception("I need a 'mail' folder.") #self.do_verbose = do_verbose def repis(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('action', 'send_to_profiles'): return if not self.conf['action']['send_to_profiles'] == 'true': return 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) # # now call the itresu, fills itresu.dups # out = self.itresu.get_dups(fufi, limit_repcodes=repcodes) self.itresu.get_dups(fufi) if self.itresu.are_any_left(fufi, repcodes): self.send(emad, surks_docu.getroot(), only=repcodes) else: print("sender: no items left to send.") def send_omnibus(self, fufi): omnibus_doc = self.omnibus.read() if omnibus_doc is None: # print("sender: 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 out = self.itresu.get_dups(fufi) if out is None: # # no active docids to send # print("sender: no items to send.") return None filing.prepare(mail_base_fufi) # get the subs for the itresu for emad in surk_docus: self.send(emad, surk_docus[emad]) # docing.inject(fufi, omnibus_doc) def send(self, emad, surk_docu, only=None): 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 False maix_surk = docing.inject(maix, surk_docu) spro = self.xpaths.get_spro(surk_docu, repcode=self.repcode) if spro == 'v': # # 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 self.emailer.prepare(maix_surk, base=base, only=only) #print(spro)