Exemple #1
0
def routing(path="index"):
    package = request.headers["Host"].split(".")[0]
    try:
        importlib.import_module("controller." + package)
    except ImportError:
        package = "."
    else:
        package = "." + package + "."
    path_list = path.split("/")
    try:
        try:
            module = importlib.import_module("controller" + package + path_list[0] + "_controller")
        except ImportError:
            path_list = ["index", path_list[0]]
            module = importlib.import_module("controller" + package + path_list[0] + "_controller")
        controller_obj = getattr(module, str_utils.snake_case_to_camel_case(path_list[0]) + "Controller")
        if len(path_list) == 1 or path_list[1] == "":
            controller = controller_obj()
        else:
            controller = controller_obj(path_list[1])
        controller.setup()
        return controller.response
    except ImportError as e:
        print(e)
        env = Environment(loader=FileSystemLoader(bc.ROOT_PATH + "/templates"))
        template = env.get_template("error/404.html")
        response = make_response(template.render(), 404)
        return response
Exemple #2
0
 def controller(params):
     if len(params) == 0:
         print("Need controller name parameter.")
         return False
     controller = params[0].split(".")
     if len(controller) == 1:
         if Hokora._yn_confirm("Make Controller Class '{}'. Are you sure?".format(controller[0])):
             base = open(base_config.ROOT_PATH + "/core/controller").read()
             base = base.format(str_utils.snake_case_to_camel_case(controller[0], is_first_upper=True))
             file = open(base_config.ROOT_PATH + "/controller/" + controller[0] + "_controller.py", "w")
             file.write(base)
             file.close()
     elif len(controller) == 2:
         host_path = base_config.ROOT_PATH + "/controller/" + controller[0]
         if not os.path.exists(host_path):
             print("No host found. Make host '{}' ".format(controller[0]))
             return False
         if Hokora._yn_confirm("Make Controller Class '{}'. Are you sure?".format(".".join(controller))):
             base = open(base_config.ROOT_PATH + "/core/controller").read()
             base = base.format(str_utils.snake_case_to_camel_case(controller[1], is_first_upper=True))
             file = open(host_path + "/" + controller[1] + "_controller.py", "w")
             file.write(base)
             file.close()