from copy import deepcopy # import docing import json import lxml.etree as etree #from lxml.builder import ElementMaker # from nitpo import Nitpo # from sheets import Sheets from webenv import Webenv # from web import Web from kolar import Kolar from fecha import Fecha # # variables we set at the start and then pass to the server preps = {} # web = Web() ## called requests kolar = Kolar() ## fetched requests fecha = Fecha() def on_starting(server): # app.logger.info("Starting Server") # out_file = open('/tmp/logo.out', 'w') # out_file.write('start') webenv = Webenv() global preps preps = webenv.prepare() # out_file.write(str(preps)) # out_file.close() # out_file.close() def app(environ, start_response): raw_uri = environ['RAW_URI'] if preps['res']['called_url'].match(raw_uri): output = kolar.output(environ, preps) return html(output, start_response) out_dict = fecha.output(environ, preps) # # if this is not a correct request, false is return if not any(out_dict): error = url_error(environ) return html(error, start_response) # # ok response with json data = json.dumps(out_dict).encode() + b'\n' start_response("200 OK", [ ("Cache-Control", 'no-cache, no-store, must-revalidate'), ("Access-Control-Allow-Origin", '*'), ("Content-Type", 'application/json'), ("Content-Length", str(len(data))) ]) return iter([data]) def html(out, start_response): data = out.encode() start_response("200 OK", [ ("Cache-Control", 'no-cache, no-store, must-revalidate'), ("Content-Type", 'text/html'), ("Content-Length", str(len(data))) ]) return iter([data]) def url_error(env): raw_uri = env['RAW_URI'] ## full_uri for reporting full_uri = raw_uri web_ele = deepcopy(preps['web_ele']) if raw_uri.startswith('//'): # # add the pathstart full_uri = pathstart() + '/' + raw_uri[2:] web_ele.attrib['raw_uri'] = raw_uri # if not self.preps['res']['url_u'].match(raw_uri): web_ele.attrib['status'] = '400' web_ele.attrib['error'] = 'wrong_url' web_ele.attrib['arger'] = full_uri # self.logbug('bad url ' + full_uri) # self.logbug(self.preps['res']['url']) web_doc = etree.ElementTree(web_ele) string = str(preps['sheets']['web'](web_doc)) return string def pathstart(): out = '' if 'web' in preps['conf']: if 'pathstart' in preps['conf']['web']: out += preps['conf']['web']['pathstart'] return out