import datetime import email.parser import email.utils import os import mailbox import re import sys import time import random from lxml import etree from lxml.builder import ElementMaker import filing import dating from emailer import Emailer from nitpo import Nitpo from profile import Profile from kapro import Kapro from reports import Reports from xpaths import Xpaths import subprocess class Brore(Nitpo): def __init__(self, do_verbose=False): """find data about """ super().__init__() self.do_verbose = do_verbose self.kapro = Kapro(do_verbose=do_verbose) self.profile = Profile(do_verbose=do_verbose) self.reports = Reports(do_verbose=do_verbose) self.xpaths = Xpaths() self.emailer = Emailer(do_verbose=do_verbose) self.check_conf('folders', 'empros') #self.N = "{%s}" % self.const['ns'] #self.E = ElementMaker(nsmap={None: self.const['ns']}) #mbox_fufi = self.conf['files']['bouncebox'] #rfc = r'[Rr][Ff][Cc]822; *([^\n ]+)' self.sleep = 60 if self.has_conf('brore', 'sleep'): self.sleep = int(self.conf['brore', 'sleep']) return None def to_all(self, empro, do_kapro=True, do_sleep=0, do_max_mails=0, dont_send=True): if(do_kapro): self.kapro.build_lib() lib = self.kapro.load_lib() random.shuffle(lib) count_mails = 0 if do_sleep == 0: do_sleep = self.sleep for fufi in lib: if do_max_mails != 0 and count_mails >= do_max_mails: sys.exit() count_mails += 1 base = empro + '/' + self.profile.base_from_fufi(fufi) maix = filing.parse_lax(fufi) out = self.emailer.prepare(maix, base=base, empro=empro, dont_send=dont_send) if out is not None and do_sleep > 0: time.sleep(do_sleep) if count_mails >= do_max_mails: print("brore reaches {do_max_mails} mails") def to_report(self, empro, repcode, do_kapro=True, do_sleep=None, do_max_mails=0, dont_send=False): report_doc = self.reports.load(repcode) if report_doc is None: print(f"brore has no data for report {repcode}") return False report_ele = report_doc.getroot() if do_kapro: self.kapro.build_lib() if not self.reports.does_it_exist(repcode): print(f"brore does not see your report {repcode}") sys.exit(0) emads = self.kapro.load(repcode) count_mails = 0 for emad in emads: profile_fufi = self.profile.fufi_from_emad(emad) base = empro + '/' + self.profile.base_from_fufi(profile_fufi) profile_doc = filing.parse_lax(profile_fufi) profile_ele = profile_doc.getroot() profile_ele.append(report_ele) maix = etree.ElementTree(profile_ele) if self.is_dev(): filing.install_xml(profile_ele, '/home/ernad/nitpo/test.xml') print("the end") quit() out = self.emailer.prepare(maix, base=base, empro=empro, dont_send=dont_send) if out is not None: if do_sleep is not None: print(f"brore: sleep {do_sleep}") time.sleep(do_sleep) else: print("brore: sleep 100") time.sleep(100) count_mails += 1 if do_max_mails is not None and count_mails == do_max_mails: print("the end of to_report") return True return True