def shell_search(args: argparse.Namespace) -> None: if args.shell_name != "" and args.all is True: logger.error("Can't use both shell_name and --all") raise CommandFailedError() if args.shell_name == "" and args.all is False: logger.error("Required shell_name arguments. See xim shell search -h") raise CommandFailedError() url: str = get_shells_repository_content_api_url() try: files: List[GithubFile] = fetch_files_on_server(url) except RequestResponseError as e: logger.error("Response {} Status failed.".format(e.status_code)) raise CommandFailedError() except RequestError as e: logger.error("Request failed. URL: {}".format(e.url)) logger.error(str(e.error)) raise CommandFailedError() provider: GithubContentsInfoProvider = GithubContentsInfoProvider(files) if not args.all: match_shell_names: List[str] = provider.search(args.shell_name) else: match_shell_names: List[str] = provider.list() number_matched = len(match_shell_names) if number_matched > 0: logger.info("Matched {} shell file.".format(number_matched)) for name in match_shell_names: logger.info(remove_extension(name)) else: logger.error("Not matched shell file")
def current(args) -> None: stream = ConfigJSONFileStream(config_path) config: Config = stream.load() current_proxy: str = config["current_proxy"] if current_proxy == un_setting_current_proxy: logger.info("haven't set proxy yet") return logger.info("current set proxy: {}".format(current_proxy))
def shell_uninstall(args: argparse.Namespace) -> None: file_name: str = args.shell_name + get_shell_extension() file_system: ShellFileSystem = ShellFileSystem(shells_path) if file_system.exists(file_name): file_system.delete(file_name) logger.info("Successfully uninstall") else: logger.error("Not found shell file") raise CommandFailedError()
def proxy_delete(args: argparse.Namespace) -> None: name: str = args.proxy_name stream: ProxiesJSONFileStream = ProxiesJSONFileStream(proxies_path) proxies: Proxies = stream.load() if proxies.exists(name): proxies.delete(name) else: logger.error("not found this name") stream.save(proxies) logger.info("add inputted proxy")
def proxy_add(args: argparse.Namespace) -> None: stream: ProxiesJSONFileStream = ProxiesJSONFileStream(proxies_path) proxies: Proxies = stream.load() name: str = args.proxy_name if name == "": logger.error("Empty name is invalid") raise CommandFailedError() if name == un_setting_current_proxy: logger.error( "Sorry, this name is already used by system. put a else name") raise CommandFailedError() if proxies.exists(name): logger.error("This name is already used") raise CommandFailedError() proxy: Proxy = create_proxy_from_input(name) proxies.add(proxy) stream.save(proxies) logger.info("add inputted proxy")
def shell_list(args: argparse.Namespace) -> None: file_system: ShellFileSystem = ShellFileSystem(shells_path) files: List[str] = file_system.list() if len(files) > 0: logger.info("Installed {} shell file.".format(len(files))) for file in files: logger.info(remove_extension(file)) else: logger.info("No installed shell file")
def shell_install(args: argparse.Namespace) -> None: for name in args.shell_name.split(","): logger.info("Install {}...".format(name)) file_name: str = name + get_shell_extension() file_system = ShellFileSystem(shells_path) if file_system.exists(file_name): logger.error("{} shell file already exits.".format(name)) raise CommandFailedError() url: str = get_shells_repository_content_api_url() try: files: List[GithubFile] = fetch_files_on_server(url) except RequestResponseError as e: logger.error("Request Status {} failed.".format(e.status_code)) raise CommandFailedError() except RequestError as e: logger.error("Request failed. URL: {}".format(e.url)) logger.error(str(e.error)) raise CommandFailedError() provider: GithubContentsInfoProvider = GithubContentsInfoProvider( files) if provider.exists(file_name): try: content_url: str = get_raw_shell_file_api_url( ) + "/" + file_name except RequestResponseError as e: logger.error("Request Status {} failed.".format(e.status_code)) raise CommandFailedError() except RequestError as e: logger.error("Request failed. URL: {}".format(e.url)) logger.error(str(e.error)) raise CommandFailedError() content: str = fetch_file_content(content_url) file_system.save(file_name, content) logger.info("Install success") else: logger.error("Not found shell file")
def init(): config_stream: ConfigJSONFileStream = ConfigJSONFileStream(config_path) proxies_stream: ProxiesJSONFileStream = ProxiesJSONFileStream(proxies_path) if not config_stream.exists(): logger.info("Can't find xim.config.json. Create default setting") config_stream.save(config_default) if not proxies_stream.exists(): logger.info("Can't find xim_proxies.json. Create default setting") proxies_stream.save(proxies_default) if not os.path.exists(shells_path): logger.info("Can't find shell directory. Create directory") os.mkdir(shells_path)
def proxy_set(args: argparse.Namespace) -> None: proxies_stream: ProxiesJSONFileStream = ProxiesJSONFileStream(proxies_path) proxies: Proxies = proxies_stream.load() if not proxies.exists(args.proxy_name): logger.error("Not found given name proxy") raise CommandFailedError() proxy: Proxy = proxies.get(args.proxy_name) shell_system: ShellFileSystem = ShellFileSystem(shells_path) shell_names: List[str] = shell_system.list() if len(shell_names) == 0: logger.error("No installed shell file") raise CommandFailedError() for name in shell_names: logger.info("Setting {}...".format(remove_extension(name))) try: proc: subprocess.CompletedProcess = shell_system.run( name, proxy.url_list()) proc.check_returncode() except subprocess.CalledProcessError as e: logger.error("Shell script did not exit successfully") logger.info("Exit Status: {}".format(e.returncode)) raise CommandFailedError() except subprocess.TimeoutExpired as e: logger.error("Timeout shell script") raise CommandFailedError() except KeyboardInterrupt as e: logger.error("Keyboard interrupt") raise CommandFailedError() logger.info("Done") logger.info("set proxy successfully") config_stream: ConfigJSONFileStream = ConfigJSONFileStream(config_path) config: Config = config_stream.load() config["current_proxy"] = args.proxy_name config_stream.save(config)