#!/usr/bin/env python # -*- coding: utf-8 -*- from os.path import dirname as directory_name from os.path import abspath as path_absolute from os.path import exists as path_exists from re import compile as RECompile from json import loads as json_decode from inspect import stack as get_stack from traceback import format_stack as trace_format_stack from traceback import extract_tb as extract_traceback from time import time as timestamp from base64 import b64encode as base64_encode import datetime if "common" not in globals(): class common:pass class MemWeb: __default_settings = common.DefaultData.settings re_key = RECompile(r'^[a-zA-Z0-9_]+$') re_string_variables = RECompile(r'\{([^\{\}]+)\}') re_print_type = RECompile(r' ?[a-z0-9_]+ ?') re_json = RECompile(r'^(\[(.+|[\r\n]+)*\]|\{(.+|[\r\n]+)*\})$') re_slashes = RECompile(r'[\/\\\\]+') re_break_lines = RECompile(r'\r\n|[\r\n]') re_exception_line = RECompile(r'^\s*File "([^"]+)", line ([0-9]+), in ([^\n]+)(.*|[\r\n]*)*$') def __set_basic_data(self): self.__nulls = self.settings("nulls", None, False, False) self.__default_value = self.settings("default_value", None, None, True) self.__exception_format = self.settings("exception_format") self.__language = self.settings(("language", "default_language")) self.__default_language = self.settings(("default_language", "language")) self.__default_text = self.settings("default_text") self.__print_format = self.settings("print_format") self.__settings_overwrite = self.settings(("settings_overwrite", "overwrite")) self.__debug_print_types = self.settings("debug_print_types") self.__show_settings_add_ok = self.settings(("show_settings_add_ok_message", "show_ok_message")) self.__show_settings_add_error = self.settings(("show_settings_add_error_message", "show_error_message")) self.__show_settings_add_exception = self.settings(("show_settings_add_exception_message", "show_exception_message")) self.__show_fix_path_exception = self.settings(("show_fix_path_exception_message", "show_exception_message")) self.__show_load_file_exception = self.settings(("show_load_file_exception_message", "show_exception_message")) self.__show_add_print_types_exception = self.settings(("show_add_print_types_exception_message", "show_exception_message")) for key in ("default_print_types", "print_types"): self.add_print_types(self.settings(key)) def __init__(self, inputs = None): self.__sentences = common.DefaultData.i18n self.__settings = {} self.__inputs = inputs self.__print_types = [] self.__root_paths = [path_absolute(directory_name(__file__))] self.__slash = '/' if '/' in self.__root_paths[0] else '\\' for i in range(3): self.__root_paths += [self.__root_paths[0 + i] + "/.."] self.__root_paths += [""] self.__root_paths.reverse() self.__set_basic_data() self._print("info", "mem_web_building") self.threads = MemWeb.Threads(self, inputs) self.terminal = MemWeb.Terminal(self, inputs) self.__started = False self._print("info", "mem_web_settings_loading") for key in ("default_settings_files", "settings_files"): self.settings_add(self.settings(key), True) self.__set_basic_data() self._print("ok", "mem_web_settings_loaded") self.__i18n_overwrite = self.settings(("i18n_overwrite", "overwrite")) self.__show_i18n_add_ok = self.settings(("show_i18n_add_ok_message", "show_ok_message")) self.__show_i18n_add_error = self.settings(("show_i18n_add_error_message", "show_error_message")) self.__show_i18n_add_exception = self.settings(("show_i18n_add_exception_message", "show_exception_message")) self.__show_launch_asynchronous_ok = self.settings(("show_launch_asynchronous_ok_message", "show_ok_message")) self.__show_launch_asynchronous_error = self.settings(("show_launch_asynchronous_error_message", "show_error_message")) self.__show_launch_asynchronous_exception = self.settings(("show_launch_asynchronous_exception_message", "show_exception_message")) self.__show_launch_asynchronous_item_exception = self.settings(("show_launch_asynchronous_exception_item_message", "show_exception_message")) self._print("info", "mem_web_i18n_loading") for key in ("default_i18n_files", "i18n_files"): self.i18n_add(self.settings(key), True) self._print("ok", "mem_web_i18n_loaded") self._print("ok", "mem_web_built") self.settings("autostart") and self.start() @staticmethod def reduce(inputs, callback, value = None): error = ( (( 1 << 0 if inputs == None else 1 << 1 if not isinstance(inputs, (list, tuple)) else 0) << 0) | (( 1 << 0 if callback == None else 1 << 1 if not callable(callback) else 0) << 2) | 0) << 1 if not error: try: for i, item in enumerate(inputs): value = callback(value, item, i, inputs) except: error |= 1 << 0 return (value, error) @staticmethod def is_object(item): return not isinstance(item, str) and " object " in str(item) @staticmethod def is_class(item): return not isinstance(item, str) and "