from nitpo import Nitpo import copy import filing # import subprocess import json # only needed for testnig import time # debugging import docing from reports import Reports import lxml.etree as etree # from kapro import Kapro from profile import Profile from surks import Surks from vakis import Vakis from xpaths import Xpaths class Fecha(Nitpo): def __init__(self, do_verbose=False): super().__init__() self.do_verbose = do_verbose self.profile = Profile() self.reports = Reports() self.surks = Surks() self.vakis = Vakis() self.xpaths = Xpaths() self.out = {} return None def output(self, env, preps): is_it_parsed = self.parse_uri(env, preps) if not is_it_parsed: ## signals error return {} return self.out def parse_uri(self, env, preps): raw_uri = env['RAW_URI'] self.out['raw_uri'] = env['RAW_URI'] repcode = None emad = None out = preps['res']['re'].fullmatch(raw_uri) if out is None: # # let server.py generate a human response return False #else: # self.out['perid'] = out.groups(0)[2] repcode = out.groups(0)[0] ## syntax check for repcode, if it fails, ## we use a human-readable page if not preps['res']['repcode'].fullmatch(repcode): return False ## now we need to get started with proding a fetcha output web_ele = copy.deepcopy(preps['web_ele']) emad = out.groups(0)[1] # # questionable whether we should # if not self.reports.does_it_exist(repcode): if not self.reports.does_it_exist(repcode): web_ele.attrib['status'] = 'bad_report' # time.sleep(6) web_ele.attrib['repcode'] = repcode self.out['repcode'] = repcode web_ele.attrib['emad'] = emad self.out['emad'] = emad web_ele = self.deal_with_profile(repcode, emad, web_ele) ## for debugging # filing.install_xml(web_ele, '/tmp/fecha.xml') web_doc = etree.ElementTree(web_ele) filing.install_xml(web_ele, '/tmp/fecha.xml') message = str(preps['sheets']['fecha'](web_doc)) self.out['message'] = message return True def deal_with_profile(self, repcode, emad, web_ele): profile_doc = self.profile.load(emad) bounced = self.profile.is_bounced(profile_doc) if bounced is not None: web_ele.attrib['bounced'] = bounced self.out['bounced'] = bounced ## web_ele contains the profile_doc web_ele = self.surks.fecha_add(profile_doc, repcode, emad, web_ele) if 'changed' not in web_ele.attrib: return web_ele profile_ele = self.xpaths.none_or_one(web_ele, '/n:web/n:profile') if profile_ele is None: # # nothing to write, should not be the case return web_ele #print(etree.tostring(web_ele, pretty_print=True).decode()) self.profile.doc = etree.ElementTree(profile_ele) ## register vakis profile_fufi = self.profile.fufi_from_emad(emad) self.vakis.read_single(profile_fufi) self.profile.write(profile_doc) return web_ele