Esempio n. 1
0
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
Esempio n. 2
0
File: cli.py Progetto: canch/tldr.py
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
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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)
Esempio n. 6
0
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
Esempio n. 7
0
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.")
Esempio n. 8
0
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.")
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
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)
Esempio n. 12
0
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
Esempio n. 13
0
File: cli.py Progetto: canch/tldr.py
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)
Esempio n. 14
0
 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
Esempio n. 15
0
 def _assert_exception_message(self, expected_message):
     with self.assertRaises(SystemExit) as error:
         get_config()
     assert error.exception.args[0] == expected_message