def find_page_location(command): """Find the command man page in the pages directory.""" repo_directory = get_config()['repo_directory'] default_platform = get_config()['platform'] with io.open(path.join(repo_directory, 'pages/index.json'), encoding='utf-8') as f: index = json.load(f) command_list = [item['name'] for item in index['commands']] if command not in command_list: sys.exit(("Sorry, we don't support command: {0} right now.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) supported_platforms = index['commands'][command_list.index( command)]['platform'] if default_platform in supported_platforms: platform = default_platform elif 'common' in supported_platforms: platform = 'common' else: platform = '' if not platform: sys.exit(("Sorry, command {0} is not supported on your platform.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) page_path = path.join(path.join(repo_directory, 'pages'), path.join(platform, command + '.md')) return page_path
def find_page_location(command): """Find the command man page in the pages directory.""" repo_directory = get_config()['repo_directory'] default_platform = get_config()['platform'] with io.open(path.join(repo_directory, 'pages/index.json'), encoding='utf-8') as f: index = json.load(f) command_list = [item['name'] for item in index['commands']] if command not in command_list: sys.exit( ("Sorry, we don't support command: {0} right now.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) supported_platforms = index['commands'][ command_list.index(command)]['platform'] if default_platform in supported_platforms: platform = default_platform elif 'common' in supported_platforms: platform = 'common' else: platform = '' if not platform: sys.exit( ("Sorry, command {0} is not supported on your platform.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) page_path = path.join(path.join(repo_directory, 'pages'), path.join(platform, command + '.md')) return page_path
def find_page_location(command): """Find the command man page in the pages directory.""" repo_directory = get_config()["repo_directory"] default_platform = get_config()["platform"] with io.open(path.join(repo_directory, "pages/index.json"), encoding="utf-8") as f: index = json.load(f) command_list = [item["name"] for item in index["commands"]] if command not in command_list: sys.exit( ( "Sorry, we don't support command: {0} right now.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr" ).format(command) ) supported_platforms = index["commands"][command_list.index(command)]["platform"] if default_platform in supported_platforms: platform = default_platform elif "common" in supported_platforms: platform = "common" else: platform = "" if not platform: sys.exit( ( "Sorry, command {0} is not supported on your platform.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr" ).format(command) ) page_path = path.join(path.join(repo_directory, "pages"), path.join(platform, command + ".md")) return page_path
def find_page_location(command, specified_platform): """Find the command man page in the pages directory.""" repo_directory = get_config()['repo_directory'] default_platform = get_config()['platform'] command_platform = ( specified_platform if specified_platform else default_platform) index = get_index() command_list = find_commands() if command not in command_list: sys.exit( ("Sorry, we don't support command: {0} right now.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) supported_platforms = index['commands'][ command_list.index(command)]['platform'] if command_platform in supported_platforms: platform = command_platform elif 'common' in supported_platforms: platform = 'common' else: platform = '' if not platform: sys.exit( ("Sorry, command {0} is not supported on your platform.\n" "You can file an issue or send a PR on github:\n" " https://github.com/tldr-pages/tldr").format(command)) page_path = path.join(path.join(repo_directory, 'pages'), path.join(platform, command + '.md')) return page_path
def build_index(): repo_directory = get_config()['repo_directory'] index_path = path.join(repo_directory, 'pages', 'index.json') page_path = path.join(repo_directory, 'pages') tree_generator = os.walk(page_path) folders = next(tree_generator)[1] commands, new_index = {}, {} for folder in folders: pages = next(tree_generator)[2] for page in pages: command_name = path.splitext(page)[0] if command_name not in commands: commands[command_name] = { 'name': command_name, 'platform': [folder] } else: commands[command_name]['platform'].append(folder) command_list = [ item[1] for item in sorted(commands.items(), key=itemgetter(0)) ] new_index['commands'] = command_list with open(index_path, mode='w') as f: json.dump(new_index, f)
def get_index(): """Retrieve index in the pages directory.""" repo_directory = get_config()['repo_directory'] with io.open(path.join(repo_directory, 'pages/index.json'), encoding='utf-8') as f: index = json.load(f) return index
def update(): """Update to the latest pages.""" repo_directory = get_config()["repo_directory"] os.chdir(repo_directory) click.echo("Check for updates...") local = subprocess.check_output("git rev-parse master".split()).strip() remote = subprocess.check_output("git ls-remote https://github.com/tldr-pages/tldr/ HEAD".split()).split()[0] if local != remote: click.echo("Updating...") subprocess.check_call("git checkout master".split()) subprocess.check_call("git pull --rebase".split()) build_index() click.echo("Update to the latest and rebuild the index.") else: click.echo("No need for updates.")
def update(): """Update to the latest pages.""" repo_directory = get_config()['repo_directory'] os.chdir(repo_directory) click.echo("Check for updates...") local = subprocess.check_output('git rev-parse master'.split()).strip() remote = subprocess.check_output( 'git ls-remote https://github.com/tldr-pages/tldr/ HEAD'.split( )).split()[0] if local != remote: click.echo("Updating...") subprocess.check_call('git checkout master'.split()) subprocess.check_call('git pull --rebase'.split()) click.echo("Updated.") else: click.echo("No need for updates.")
def parse_page(page): """Parse the command man page.""" colors = get_config()['colors'] with io.open(page, encoding='utf-8') as f: lines = f.readlines() output_lines = [] for line in lines: if line.startswith('#'): continue elif line.startswith('>'): output_lines.append(click.style(line.replace('>', ' '), fg=colors['description'])) elif line.startswith('-'): output_lines.append(click.style(line, fg=colors['usage'])) elif line.startswith('`'): output_lines.append(click.style(' ' + line.replace('`', ''), fg=colors['command'])) else: output_lines.append(click.style(line)) return output_lines
def parse_page(page): """Parse the command man page.""" colors = get_config()['colors'] with io.open(page, encoding='utf-8') as f: lines = f.readlines() output_lines = [] for line in lines: if line.startswith('#'): continue elif line.startswith('>'): output_lines.append( click.style(line.replace('>', ' '), fg=colors['description'])) elif line.startswith('-'): output_lines.append(click.style(line, fg=colors['usage'])) elif line.startswith('`'): output_lines.append( click.style(' ' + line.replace('`', ''), fg=colors['command'])) else: output_lines.append(click.style(line)) return output_lines
def build_index(): repo_directory = get_config()["repo_directory"] index_path = path.join(repo_directory, "pages", "index.json") page_path = path.join(repo_directory, "pages") tree_generator = os.walk(page_path) folders = next(tree_generator)[1] commands, new_index = {}, {} for folder in folders: pages = next(tree_generator)[2] for page in pages: command_name = path.splitext(page)[0] if command_name not in commands: commands[command_name] = {"name": command_name, "platform": [folder]} else: commands[command_name]["platform"].append(folder) command_list = [item[1] for item in sorted(commands.items(), key=itemgetter(0))] new_index["commands"] = command_list with open(index_path, mode="w") as f: json.dump(new_index, f)
def parse_page(page): """Parse the command man page.""" colors = get_config()['colors'] with io.open(page, encoding='utf-8') as f: lines = f.readlines() output_lines = [] for line in lines[1:]: if is_headline(line): continue elif is_description(line): output_lines.append(click.style(line.replace('>', ' '), fg=colors['description'])) elif is_old_usage(line): output_lines.append(click.style(line, fg=colors['usage'])) elif is_code_example(line): line = ' ' + line if line.startswith('`') else line[2:] output_lines.append(click.style(line.replace('`', ''), fg=colors['command'])) elif is_line_break(line): output_lines.append(click.style(line)) else: output_lines.append(click.style('- ' + line, fg=colors['usage'])) return output_lines
def build_index(): repo_directory = get_config()['repo_directory'] index_path = path.join(repo_directory, 'pages', 'index.json') page_path = path.join(repo_directory, 'pages') tree_generator = os.walk(page_path) folders = next(tree_generator)[1] commands, new_index = {}, {} for folder in folders: pages = next(tree_generator)[2] for page in pages: command_name = path.splitext(page)[0] if command_name not in commands: commands[command_name] = {'name': command_name, 'platform': [folder]} else: commands[command_name]['platform'].append(folder) command_list = [item[1] for item in sorted(commands.items(), key=itemgetter(0))] new_index['commands'] = command_list with open(index_path, mode='w') as f: json.dump(new_index, f)
def _assert_exception_message(self, expected_message): with self.assertRaises(SystemExit) as error: with mock.patch('os.path.expanduser', return_value=self.repo_dir): get_config() assert error.exception.args[0] == expected_message
def _assert_exception_message(self, expected_message): with self.assertRaises(SystemExit) as error: get_config() assert error.exception.args[0] == expected_message