class url(Variable): def __getattr__(self, item): return item def __call__(self, *args, **kwargs): return None def __init__(self, *args, **kwargs): if len(args) > 0 or len(kwargs) > 0: if all(args) is not False: self.ul = self.url(*args, **kwargs) else: self.ul = self.url(**kwargs) else: self.ul = self.url(*args) return None def __str__(self): return self.ul def url(self, path="", lang=False): DS, p = "", "" self.Controllers = Controllers() seturl = self.out("HTTP_HOST") + str(":") + str( self.out("SERVER_PORT", '')) if self.out("HTTP_HOST") == "localhost" or self.out( "HTTP_HOST") == "127.0.0.1" else self.out("HTTP_HOST") if self.out("HTTPS", "") == 'on': url = str("https://") + seturl.replace( ":80", "") + "/" + self.Controllers.all_languages.get( self.Controllers._getLanguages(), self.Controllers.languages) if lang == True else str( "https://") + seturl.replace(":80", "") else: url = str("http://") + seturl.replace( ":80", "") + "/" + self.Controllers.all_languages.get( self.Controllers._getLanguages(), self.Controllers.languages) if lang == True else str( "http://") + seturl.replace(":80", "") if path == "": DS = "" else: if path[:1] == "/": p = path[1:] DS = "/" else: p = path DS = "/" return url + DS + p
class App(env, Config, Variable): def __getattr__(self, item): return item def __init__(self, routers="", routes="", getpath="", getrouters=""): self.routers = routers self.routes = routes self.getpath = getpath self.codereponse = 200 self.getrouters = getrouters self.getDB = "" self.Request = Request self.Session = Session self.methodprefix = "" self.actions = "" self.controllers = "" self.controller = "" self.dbDriver = None self.Config = None self.url = "" self.params = "" self.key = "" self.vpathf = "" self.languages = "" self.Driver = "" self.formData = None def getRouters(self): return self.routers def getPath(self): return self.getpath def runs(self, formData=None): self.formData = formData return self.initial() def initial(self): self.default("Framework", Version.AUTHOR) self.default("X-Version", Version.VERSION_TEXT) self.getrouters = self.envrin('route') self.controllers = Controllers() self.methodprefix = self.controllers._getMethodPrefix() self.actions = self.controllers._getActions() self.uri = self.controllers._getUri() self.controller = self.controllers._getControllers() self.routers = self.controllers._getRoutes() self.languages = self.controllers._getLanguages() self.params = self.controllers._getParams() langs = Lang.Lang(self.languages) langs.loadLang() controlUri = [] if Version.PYVERSION_MA >= 3: lrounters = self.getrouters.items() else: lrounters = self.getrouters.iteritems() for k, getRouter in lrounters: if self.controller == k: controlUri = getRouter.split('@') self.routerend() if len(controlUri) != 0: if 'controller' in controlUri[0].lower(): controllersClass = str(controlUri[0].lower()).replace( 'controller', '').capitalize() + 'Controller' else: controllersClass = str( controlUri[0]).capitalize() + 'Controller' if ':' not in controlUri[1]: controllersMethods = str(controlUri[1]) else: getMapPara = controlUri[1].split(':') controllersMethods = str(getMapPara[0]) else: if str(self.controller[0]) == '?': controllersClass = 'IndexController' else: controllersClass = str(self.controller[0].capitalize()) + str( self.controller[1:]) + 'Controller' controllersMethods = str(self.actions) if controllersMethods != "": self.actions = controllersMethods controllers = controllerpath + DS + controllersClass + ".py" if os.path.isfile(controllers) == True: if __name__ == '__main__': spac = "" if os.path.isfile(controllers) == True: if sys.version_info.major <= 2: return self.strClass(controllerpath, controllersClass) else: return self.strClass3(controllerpath, controllersClass) else: Log(controllerpath).error('Controller does not exist ' + str(controllersClass)) return self.errorP('405') def errorP(self, code, replace=""): getErrorP = self.envrin('error') if os.path.isfile(host + DS + "routes.py") == True: sys.path.append(host) import routes as route if len(route.route.getError()) > 0: for i, redirect in enumerate(route.route.getError()): if len(route.route.getError()) > 0: toeroor = route.route.getError()[i] if len(route.route.getErrorControl()) > 0: fromroute = route.route.getErrorControl()[i] if len(route.route.getCode()) > 0: code = route.route.getCode()[i] if len(route.route.getLink()) > 0: link = route.route.getLink()[i] if fromroute == self.controller: return self.redirect(location=toeroor, link=bool(link), code=code) pageCode = "page{code}".format(code=code) if os.path.isdir(os.getcwd() + '/public'): if self.out("SERVER_SOFTWARE") == Version.AUTHOR: re_url = "" errorP = {} if getErrorP != '': errorP = getErrorP if errorP.get(code, '') != '': controllerP = "" if '/' in errorP.get(code, ''): splitP = errorP.get(code, '').split('/') controllerP = controllerpath + DS + \ str(splitP[0]).capitalize() + 'Controller' + ".py" else: controllerP = controllerpath + DS + str( errorP.get(code, '')).capitalize() + 'Controller' + ".py" if code == "500": if os.path.isfile(controllerP) == True: re_url = u.url().url('/' + str(errorP.get(code, ''))) else: re_url = u.url().url("/error") else: if os.path.isfile(controllerP) == True: re_url = u.url().url('/' + str(errorP.get(code, ''))) else: if os.path.isfile( self.error_page_html(code)) == True: re_url = u.url().url( "/error/{code}".format(code=pageCode)) else: if code == "500": re_url = u.url().url("/error") else: if os.path.isfile(self.error_page_html(code)) == True: re_url = u.url().url( "/error/{code}".format(code=pageCode)) self.put(status=code) return code, re_url if getErrorP != '': errorP = getErrorP if errorP.get(code, '') != '': self.put(status=code) if '/' in errorP.get(code, ''): splitP = errorP.get(code, '').split('/') controllerP = controllerpath + DS + \ str(splitP[0]).capitalize() + 'Controller' + ".py" else: controllerP = controllerpath + DS + str( errorP.get(code, '')).capitalize() + 'Controller' + ".py" if os.path.isfile(controllerP) == True: return self.redirect( u.url().url('/' + str(errorP.get(code, '')))) else: if os.path.isfile(self.error_page_html(code)) == True: return self.redirect(u.url().url( "/error/{code}".format(code=pageCode))) else: if os.path.isfile(self.error_page_html(code)) == True: return self.redirect(u.url().url( "/error/{code}".format(code=pageCode))) self.default("REDIRECT_STATUS", 301) self.default("REDIRECT_REDIRECT_STATUS", 301) else: if os.path.isfile(error_page_class) == True: self.default("REDIRECT_STATUS", 301) self.default("REDIRECT_REDIRECT_STATUS", 301) return self.redirect(u.url().url( "/error/{code}".format(code=pageCode))) def envrin(self, key): env = self._e() self.add(env) return self.get(key, '') def DB(self): self.dbDriver = self.envrin('dbConnect') if self.dbDriver.get("driver", '') == "MYSQL": from pytonik.Driver.DB.MYSQL.MYSQL import MYSQL self.getDB = MYSQL(self.dbDriver) self.Driver = "MYSQL" return self.getDB if self.dbDriver.get("driver", '') == "SQLite": from pytonik.Driver.DB.SQLite.SQLite import SQLite self.getDB = SQLite(self.dbDriver) self.Driver = "SQLite" return self.getDB if self.dbDriver.get("driver", '') == "Oracle": from pytonik.Driver.DB.Oracle.Oracle import Oracle self.getDB = Oracle(self.dbDriver) self.Driver = "Oracle" return self.getDB if self.dbDriver.get("driver", '') == "pyPgSQL": from pytonik.Driver.DB.pyPgSQL.pyPgSQL import pyPgSQL self.getDB = pyPgSQL(self.dbDriver) self.Driver = "pyPgSQL" return self.getDB def strClass(self, p=None, c=None): try: sys.path.append(p) ms = str(self.actions) md = importlib.import_module(c) self.default("REDIRECT_REDIRECT_STATUS", 200) self.default("REDIRECT_STATUS", 200) return self.strMethod(p, md, ms) except Exception as err: Log(p + DS + c + '.py').critical(err) return self.errorP('400') def strMethod(self, p, c=None, m=None): Request = self.Request(prform=self.formData) Session = self.Session() try: return getattr(c, m)(Request, Session) except Exception as err: try: return getattr(c, m)(Session, Request) except Exception as err: try: return getattr(c, m)(Request) except Exception as err: try: return getattr(c, m)(Session) except Exception as err: try: return getattr(c, m)() except Exception as err: Log(p + DS + c + '.py').critical(err) return self.errorP('400') def strClass3(self, p=None, c=None): try: sys.path.append(p) ms = str(self.actions) importlib._RELOADING md = importlib.import_module(c, ms) self.default("REDIRECT_REDIRECT_STATUS", 200) self.default("REDIRECT_STATUS", 200) return self.strMethod(p, md, ms) except Exception as err: Log(p + DS + c + '.py').critical(err) return self.errorP('400') def redirect(self, location='/', link=False, code="307"): if self.out("SERVER_SOFTWARE") == Version.AUTHOR: if link == True: location_d = u.url().url(location) else: location_d = location self.put(status=code) self.put(redirect_url=location_d) return code, location_d else: if link == True: location_d = u.url().url(location) else: location_d = location self.default("REDIRECT_STATUS", code) self.default("REDIRECT_REDIRECT_STATUS", code) print("Location: {location}".format(location=location_d)) print() def referer(self, location='/', link=False, code="307"): if self.out("SERVER_SOFTWARE") == Version.AUTHOR: if link == True: location_d = u.url().url(location) else: location_d = location self.put(status=code) self.put(referral=self.out('HTTP_REFERER', location_d)) return code, self.out('HTTP_REFERER', location_d) else: if link == True: location_d = u.url().url(location) else: location_d = location self.default("REDIRECT_STATUS", code) self.default("REDIRECT_REDIRECT_STATUS", code) print("Location: {location}".format( location=self.out('HTTP_REFERER', location_d))) print() @staticmethod def header(p=0, type="text/html"): if Version.PYVERSION_MA <= 2 and Version.PYVERSION_MI <= 7: reload(sys) sys.setdefaultencoding('utf-8') elif Version.PYVERSION_MA == 3 and Version.PYVERSION_MI <= 3: import imp imp.reload() elif Version.PYVERSION_MA >= 3 and Version.PYVERSION_MI >= 4: import importlib importlib.reload(sys) if os.path.isdir(os.getcwd() + '/public') == False: print("Content-type: {type}\r\n".format(type=type)) # \r\n\r\n if p > 0: for x in range(p): print("") else: return def XHreponse(self, dataString): if self.out("SERVER_SOFTWARE") == Version.AUTHOR: return dataString else: self.header() print(dataString) def views(self, pathf="", datag={}, datal={}): if pathf == "": pathf = self.getDefaultViewPath() pathfhtml = host + DS + 'views' + DS + pathf + ".html" html = "" if os.path.isfile(pathfhtml) == False: Log(host + DS + 'views').critical( 'Cannot find file {}'.format(pathf + ".html")) return self.errorP('404') else: if os.path.isdir(os.getcwd() + '/public'): # print(os.environ) return self.read_html(host + DS + 'views' + DS, pathf, datag) else: self.header() print(self.read_html(host + DS + 'views' + DS, pathf, datag)) def read_html(self, template_dir, engine, context=[]): html_file_path = os.path.join(template_dir, "%s.html" % engine) try: with open(html_file_path) as html_file: html = html_file.read() return str('<!-- Pytonik -->\n') + HTMLeditor.Template( html).render(**context) + str('\n<!-- Pytonik {} -->'.format( Version.VERSION_TEXT)) except Exception as err: Log(template_dir + DS + engine + str('.html')).error(err) return def getDefaultViewPath(self): router = self.Router.getRoutes() # Routers.getRoutes() if router == "": rout = "" controllerDirectory = self.Router.getControllers() templateName = str(self.Router.getMethodPrefix()) + \ str(self.Router.getAction()) + '.html' return templateName def error_page_html(self, code): return host + DS + 'views' + DS + str(code) + ".html" def loadmodule(self): path = [ os.path.dirname(__file__) + str("/") + str("Functions"), str(host) + str("/") + "model" ] listpath = [path[0], path[1]] i = 0 lclass = {} for pl in listpath: current_dir = os.path.join(pl) current_module_name = os.path.splitext( os.path.basename(current_dir))[0] for file in glob.glob(os.path.join(current_dir + "/*.py")): name = os.path.splitext(os.path.basename(file))[0] i = i + 1 # Ignore __ files ++i if name.startswith("__init__"): continue if name != "__init__": lclass0 = {name: name} lclass.update(lclass0) if Version.PYVERSION_MA <= 2: item = lclass.iteritems() else: item = lclass.items() result = {} for key, value in item: if value not in result.values(): result[key] = value return result def routerend(self): if os.path.isfile(host + DS + "routes.py") == True: sys.path.append(host) import routes as route toroute, fromroute = "", "" if len(route.route.getRouter()) > 0: for i, redirect in enumerate(route.route.getCode()): if len(route.route.getRediret()) > 0: toroute = route.route.getRediret()[i] if len(route.route.getDespatch()) > 0: fromroute = route.route.getDespatch()[i] if fromroute == self.controller: self.redirect(location=toroute, link=True, code=redirect) for i, route_c in enumerate(route.route.getRouter()): if self.controller == route_c: if len(route.route.getController()) > 0: self.controller = route.route.getController()[i] if len(route.route.getAction()) > 0: self.actions = route.route.getAction()[i] if len(route.route.getRouter()) > 0: self.routers = route.route.getRouter()[i] if len(route.route.getMethod()) > 0: self.method = route.route.getMethod()[i] if self.method != "": if self.method != self.out( "REQUEST_METHOD", ""): Log().error( str(str(self.controller).capitalize()) + "Controller/" + str(self.actions) + " Requires " + self.method) return self.errorP('400')
class Router(env): def args(self, to="", params=[], link=False, code=200): return to, params, link, code def __getattr__(self, item): return item def __init__(self): self.control = Controllers() self._route = [] self._params = {} self._getcontrol = [] self._getaction = [] self._geturi = [] self._redirect = [] self._method = [] self.route_to = [] self._getmethod = [] self._despatch = [] self._code = [] self._error = [] self._geterrocontrol = [] self._link = [] return None def get(self, uri, call=""): if call != "": if isinstance(call, tuple): self._route_(route=uri, call=call, method="GET") else: lcall = [] method_v, control_v = "", "" if len(call.split('@')) > 0: control = call.split('@') if len(control) == 2: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') if len(str(control[1]).split(":")) > 0: method_v = control[1].split(":")[0] param = control[1].split(":")[1:] lcall.append(uri) lcall.append(param) else: method_v = control[1] else: if control[0] == "/": control_v = self.control.default_controllers else: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') method_v = self.control.default_actions lroute = str(control_v)+"/"+str(method_v) self._route_(route=lroute, call=lcall, method="GET") return self def post(self, uri, call=""): if call != "": if isinstance(call, tuple): self._route_(route=uri, call=call, method="POST") else: lcall = [] method_v, control_v = "", "" if len(call.split('@')) > 0: control = call.split('@') if len(control) == 2: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') if len(str(control[1]).split(":")) > 0: method_v = control[1].split(":")[0] param = control[1].split(":")[1:] lcall.append(uri) lcall.append(param) else: method_v = control[1] else: if control[0] == "/": control_v = self.control.default_controllers else: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') method_v = self.control.default_actions lroute = str(control_v)+"/"+str(method_v) self._route_(route=lroute, call=lcall, method="POST") return self def put(self, uri, call=""): if call != "": if isinstance(call, tuple): self._route_(route=uri, call=call, method="POST") else: lcall = [] method_v, control_v = "", "" if len(call.split('@')) > 0: control = call.split('@') if len(control) == 2: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') if len(str(control[1]).split(":")) > 0: method_v = control[1].split(":")[0] param = control[1].split(":")[1:] lcall.append(uri) lcall.append(param) else: method_v = control[1] else: if control[0] == "/": control_v = self.control.default_controllers else: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') method_v = self.control.default_actions lroute = str(control_v)+"/"+str(method_v) self._route_(route=lroute, call=lcall, method="POST") return self def any(self, uri, call=""): if call != "": if isinstance(call, tuple): self._route_(route=uri, call=call) else: lcall = [] method_v, control_v = "", "" if len(call.split('@')) > 0: control = call.split('@') if len(control) == 2: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') if len(str(control[1]).split(":")) > 0: method_v = control[1].split(":")[0] param = control[1].split(":")[1:] lcall.append(uri) lcall.append(param) else: method_v = control[1] else: if control[0] == "/": control_v = self.control.default_controllers else: control_v = str(control[0]).lower().replace( 'Controller', '').replace('controller', '') method_v = self.control.default_actions lroute = str(control_v)+"/"+str(method_v) self._route_(route=lroute, call=lcall) return self def error(self, uri, call="", code=302): if isinstance(call, tuple): if call[0] == "/": replace = self.control.default_controllers else: replace = call[0] code = code if int(call[3]) == int(200) else call[3] else: if call[0] == "/": replace = self.control.default_controllers else: replace = call code = int(code) self._geterrocontrol.append(uri) self._link.append(call[2]) self._error.append(replace) self._code.append(code) return self def redirect(self, uri, call="", code=302): if isinstance(call, tuple): if call[0] == "/": replace = self.control.default_controllers else: replace = call[0] code = code if int(call[3]) == int(200) else call[3] else: if call == "/": replace = self.control.default_controllers else: replace = call code = int(code) self._despatch.append(uri) self._redirect.append(replace) self._code.append(code) return self def permanentRedirect(self, uri, call="", code=301): if isinstance(call, tuple): if call[0] == "/": replace = self.control.default_controllers else: replace = call[0] code = code if int(call[3]) == int(200) else call[3] else: if call == "/": replace = self.control.default_controllers else: replace = call code = int(code) self._despatch.append(uri) self._redirect.append(replace) self._code.append(code) return self def where(self, *args): if len(args) > 0: if isinstance(args[0], dict): if Version.PYVERSION_MA >= 3: params = args[0].items() else: params = args[0].iteritems() for k, v in params: regex = re.compile(v) if regex.match(self._params.get(k, "")): return True else: return False elif isinstance(args, list): regex = re.compile(args[1]) if regex.match(self._params.get(args[0], "")): return True else: return False else: return False def _route_(self, route="", call="", method=""): route = route.split('/') new_para = route parameter = {} replace = "" new_uri = [] list_params = [] params = "" new_paraf = [] if self.control._getLanguages() in new_para: new_paraf = self.control._getUri()[1:] new_paraf.pop(-1) else: new_paraf = self.control._getUri()[2:] if self.control._getControllers() in new_paraf: new_paraf = self.control._getUri() new_paraf.pop(-1) if len(call) > 0: replace = call[0] params = call[1] new_uri = new_paraf for i, para in enumerate(params): if (len(new_uri) - i) > 0: v_para = new_uri[i] else: v_para = "" list_params.append(para) list_params.append(v_para) parameter = Helpers.covert_list_dict(list_params) self._params.update(parameter) if len(route) == 1: ctl = route[0] if route[0] != "" else self.control.default_controllers self._getcontrol.append(ctl) self._getaction.append(self.control.default_actions) elif len(route) > 1: self._getcontrol.append( route[0] if route[0] != "" else self.control.default_controllers) self._getaction.append( route[1] if route[1] != "" else self.control.default_actions) self._route.append(replace) self._method.append(method) def getParams(self): return self._params def getController(self): return self._getcontrol def getAction(self): return self._getaction def getRouter(self): return self._route def getMethod(self): return self._method def getRediret(self): return self._redirect def getDespatch(self): return self._despatch def getCode(self): return self._code def getError(self): return self._error def getLink(self): return self._link def getErrorControl(self): return self._geterrocontrol