def main(): print_logo() # Initialize the Global config object. # TODO: Acctually use it GlobalConfig.global_config = GlobalConfig.GlobalConfig('config.yaml') db_path = '/home/db/db.db' try: http_config = HttpSchema() http_config.generate_from_db(db_path) #what are you? if GlobalConfig.global_config.vars.EXPAND_SIZES_IN_HTTP_CONFIG: http_config.expand_integer_sizes() http_config.write_schema(db_path) json_schemas = JsonSchemas() json_schemas.generate_from_db(db_path) json_schemas.write_schemas(db_path) main_logger.info("SUCCESS!") except Exception as e: main_logger.exception(e) raise e
def expand_integer_sizes(self): """ This method expand the minimum and maximum of all headers without mjr/min version and status. he call to the func expand_integer_sizes() in StringConfig and MinMax object. """ main_logger.info( "Expanding the sizes of min/max in HTTP Config file to be in integer sizes." ) for header_key, header_val in self.dict_http_config_header.items(): header_val.value.expand_integer_sizes()
def load_from_folder(self, folder_path): main_logger.info("Load schemas from folder {}".format(folder_path)) if not os.path.isdir(folder_path): raise OSError("FOLDER NOT EXIST") for url_folder in self._list_dirs_in_dir(folder_path): for method_folder in self._list_dirs_in_dir(os.path.join(folder_path, url_folder)): with open(os.path.join(folder_path, url_folder, method_folder, self.SCHEMA_JSON_FILES_NAME)) as f: main_logger.info("Load schema about url : {}, and method : {}".format(url_folder, method_folder)) self.schemas[url_folder] = dict() self.schemas[url_folder][method_folder] = dict() self.schemas[url_folder][method_folder]["req"] = SchemaBuilderPlusPlus() self.schemas[url_folder][method_folder]["res"] = SchemaBuilderPlusPlus() schemas_req_and_res = json.loads(f.read()) schemas_req, schema_res = schemas_req_and_res[JsonSchemas.TYPE_OF_BIG_SCHEMA] self.schemas[url_folder][method_folder]["req"].add_schema(schemas_req) self.schemas[url_folder][method_folder]["res"].add_schema(schema_res)
def load_from_pre_config(self, config_path): """ not used!!!!!! config file might look like: Host regex "[a-zA-Z.0-9:]{0,255}" Content-Length numeric "255" "65535" _occurrences 55509 Content-Type enum "application/json; charset=UTF-8" "application/json" :param config_path: path to config file. """ main_logger.info( "Start load pre HTTP Config from file : {}".format(config_path)) curr_header_config = None with open(str(config_path), 'r') as f: line = f.readline() while line: if line.startswith("\n"): line = f.readline() continue if line.startswith(self._COMMENT_SIGN): # if line contain _occurrences of the enum in next line. occurrences = re.findall( pattern=self._REGEX_LINE_COMMENT_ON_ENUMS, string=line) # if have comment that describe _occurrences of the values in next enum: if occurrences: # enum line might look like: # Connection enum "Keep-Alive" "close" # openstack-api-version enum "volume 3.0" "compute 2.1" enum_line = f.readline() # "[^\"]*" in regex mean any character except character \" enum_options = re.findall('\"([^\"]*)\"', enum_line) # In pre examples its will, enum_option will look like: # ["Keep-Alive", "close"] # ["volume 3.0", "compute 2.1"] curr_header_config = StringConfig( enum=enum_options, # because re.findall() return list of strings. occurrences=int(occurrences[0])) header_name = enum_line.split(" ", 1)[0] # if line is not comment. else: header_name, config_type, config_value = line.split(" ", 2) if config_type == "numeric": minimum, maximum = re.findall('"(\d*)"', config_value) curr_header_config = MinMax(minimum=int(minimum), maximum=int(maximum)) elif config_type == "regex": # config value look like: # '"[a-z]{0-9}$"\n' # and we want change it to be # '[a-z]{0-9}$' regex_in_config_value = config_value[1:-2] curr_header_config = StringConfig( regex=regex_in_config_value) elif config_type == "enum": # "[^\"]*" in regex mean any character except character \" enum_options = re.findall('\"([^\"]*)\"', enum_line) curr_header_config = StringConfig(enum=enum_options) else: main_logger.error( "An error occur while try update by pre config\n" "This line made the error {}".format(line)) line = f.readline() continue if curr_header_config: self._help_insert_header_config_into_correct_var( header_name=header_name, header_config=curr_header_config) curr_header_config = None line = f.readline()
def log_running_time(start_running): main_logger.info( "Program run {} until now.".format(datetime.datetime.now() - start_running))