def load_site(self, module_str): if module_str: if isinstance(module_str, str): mod = load(module_str) else: mod = module_str for key, func in vars(mod).items(): if not key.startswith("fetch"): continue self.proxy_methods[key] = partial(exception_wrapper(func), self)
def local_variable(cls): """ 存储配置的可注入的上下文变量及其类型 :return: """ lv = cls._default.copy() lv.update(settings.get("LOCAL_VARIABLE", dict())) var_types = dict() for k, v in lv.items(): var_types[k] = load(v) return var_types
def _cwd(module): """ 切换到模块包所在目录并返回 :param module: :return: """ if module: current_dir = dirname(load(module).__file__) sys.modules.pop(module, None) else: current_dir = "." os.chdir(current_dir) return current_dir
def errors(cls): err = errors.get(cls.__name__) if err is None: # here cannot use super err = cls.__base__.errors err = err or dict() try: # avoid import cycle from apistellar import settings local_errors = load(settings.get("ERROR_DESC")) e = local_errors.get(cls.__name__) if e is not None: err.update(e) except (ImportError, KeyError): pass return err or dict()
def create(self, env, **kwargs): task = kwargs.pop("task") parser = kwargs.pop("parser") name = kwargs.pop("name", [])[0] module = kwargs["module"] location = abspath(join(os.getcwd(), kwargs.pop("location"))) if module: current_dir = dirname(load(module).__file__) sys.modules.pop(module, None) else: current_dir = "." os.chdir(current_dir) painter = DocPainter(current_dir, parser) # 不再提醒是否覆盖 global input input = self._input base_dir_name = join(location, name) indices = OrderedDict() for parent, doc in painter.paint().items(): doc["dirname"] = base_dir_name self.kwargs = doc fn = os.path.join(base_dir_name, doc["file_path"], doc["doc_name"] + ".md.html") indices[fn] = doc["doc_name"] self.copytree(env, task) os.makedirs(base_dir_name, exist_ok=True) with open(os.path.join(base_dir_name, "index.md"), "w") as f: f.write(f"# {name}文档\n\n") for index, (key, val) in enumerate(indices.items()): f.write(f"{index+1}. [{val}]({key})\n") print(f"{name}已创建。") with MarkDownRender("github-markdown.css", base_dir_name) as mk_render: output = mk_render.render() if output: os.system(f"open {output}") else: print("未找到可打开的文档!")
def __init__(self): """ 初始化logger, redis_conn """ self.enrich_parser_arguments() args = self.parser.parse_args() cwd = getcwd() sys.path.insert(0, cwd) self.settings = SettingsLoader().load(args.localsettings, args.settings) self.headers = self.settings.HEADERS self.proxies_check_in_channel = ThreadSafeSet() self.proxies_check_out_channel = TreadSafeDict() self.load_site(proxy_site_spider) self.load_site(args.spider_module) self.redis_conn = Redis(self.settings.get("REDIS_HOST"), self.settings.get_int("REDIS_PORT")) if args.check_method: self.is_anonymous = partial(load(args.check_method), self) super().__init__() self.supervisor.control(log_path=os.path.join(cwd, self.name) + ".log") self.console.init_console() self.my_ip = requests.get("https://httpbin.org/ip").json()["origin"]
def __init__(self, current_dir, parser_path): super(DocPainter, self).__init__(current_dir) self.parser = load(parser_path)()