<s> from __future__ import absolute_import <EOL> import io <EOL> import json <EOL> from operator import itemgetter <EOL> import os <EOL> from os import path <EOL> import subprocess <EOL> import sys <EOL> import click <EOL> import yaml <EOL> from tldr import __version__ <EOL> from tldr . config import get_config <EOL> from tldr . parser import parse_page <EOL> def parse_man_page ( command ) : <EOL> """<STR_LIT>""" <EOL> page_path = find_page_location ( command ) <EOL> output_lines = parse_page ( page_path ) <EOL> return output_lines <EOL> def find_page_location ( command ) : <EOL> """<STR_LIT>""" <EOL> repo_directory = get_config ( ) [ '<STR_LIT>' ] <EOL> default_platform = get_config ( ) [ '<STR_LIT>' ] <EOL> with io . open ( path . join ( repo_directory , '<STR_LIT>' ) , <EOL> encoding = '<STR_LIT:utf-8>' ) as f : <EOL> index = json . load ( f ) <EOL> command_list = [ item [ '<STR_LIT:name>' ] for item in index [ '<STR_LIT>' ] ] <EOL> if command not in command_list : <EOL> sys . exit ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( command ) ) <EOL> supported_platforms = index [ '<STR_LIT>' ] [ <EOL> command_list . index ( command ) ] [ '<STR_LIT>' ] <EOL> if default_platform in supported_platforms : <EOL> platform = default_platform <EOL> elif '<STR_LIT>' in supported_platforms : <EOL> platform = '<STR_LIT>' <EOL> else : <EOL> platform = '<STR_LIT>' <EOL> if not platform : <EOL> sys . exit ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( command ) ) <EOL> page_path = path . join ( path . join ( repo_directory , '<STR_LIT>' ) , <EOL> path . join ( platform , command + '<STR_LIT>' ) ) <EOL> return page_path <EOL> def build_index ( ) : <EOL> repo_directory = get_config ( ) [ '<STR_LIT>' ] <EOL> index_path = path . join ( repo_directory , '<STR_LIT>' , '<STR_LIT>' ) <EOL> page_path = path . join ( repo_directory , '<STR_LIT>' ) <EOL> tree_generator = os . walk ( page_path ) <EOL> folders = next ( tree_generator ) [ <NUM_LIT:1> ] <EOL> commands , new_index = { } , { } <EOL> for folder in folders : <EOL> pages = next ( tree_generator ) [ <NUM_LIT:2> ] <EOL> for page in pages : <EOL> command_name = path . splitext ( page ) [ <NUM_LIT:0> ] <EOL> if command_name not in commands : <EOL> commands [ command_name ] = { '<STR_LIT:name>' : command_name , <EOL> '<STR_LIT>' : [ folder ] } <EOL> else : <EOL> commands [ command_name ] [ '<STR_LIT>' ] . append ( folder ) <EOL> command_list = [ item [ <NUM_LIT:1> ] for item in <EOL> sorted ( commands . items ( ) , key = itemgetter ( <NUM_LIT:0> ) ) ] <EOL> new_index [ '<STR_LIT>' ] = command_list <EOL> with open ( index_path , mode = '<STR_LIT:w>' ) as f : <EOL> json . dump ( new_index , f ) <EOL> @ click . group ( context_settings = { '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> @ click . version_option ( __version__ , '<STR_LIT>' , '<STR_LIT>' , message = '<STR_LIT>' ) <EOL> def cli ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ cli . command ( ) <EOL> @ click . argument ( '<STR_LIT>' ) <EOL> def find ( command ) : <EOL> """<STR_LIT>""" <EOL> output_lines = parse_man_page ( command ) <EOL> click . echo ( '<STR_LIT>' . join ( output_lines ) ) <EOL> @ cli . command ( ) <EOL> def update ( ) : <EOL> """<STR_LIT>""" <EOL> repo_directory = get_config ( ) [ '<STR_LIT>' ] <EOL> os . chdir ( repo_directory ) <EOL> click . echo ( "<STR_LIT>" ) <EOL> local = subprocess . check_output ( '<STR_LIT>' . split ( ) ) . strip ( ) <EOL> remote = subprocess . check_output ( <EOL> '<STR_LIT>' . split ( ) <EOL> ) . split ( ) [ <NUM_LIT:0> ] <EOL> if local != remote : <EOL> click . echo ( "<STR_LIT>" ) <EOL> subprocess . check_call ( '<STR_LIT>' . split ( ) ) <EOL> subprocess . check_call ( '<STR_LIT>' . split ( ) ) <EOL> build_index ( ) <EOL> click . echo ( "<STR_LIT>" ) <EOL> else : <EOL> click . echo ( "<STR_LIT>" ) <EOL> @ cli . command ( ) <EOL> def init ( ) : <EOL> """<STR_LIT>""" <EOL> default_config_path = path . join ( <EOL> ( os . environ . get ( '<STR_LIT>' ) or path . expanduser ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' ) <EOL> if path . exists ( default_config_path ) : <EOL> click . echo ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> repo_path = click . prompt ( "<STR_LIT>" ) <EOL> if not path . exists ( repo_path ) : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> platform = click . prompt ( "<STR_LIT>" ) <EOL> if platform not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> colors = { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> config = { <EOL> "<STR_LIT>" : repo_path , <EOL> "<STR_LIT>" : colors , <EOL> "<STR_LIT>" : platform <EOL> } <EOL> with open ( default_config_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( yaml . safe_dump ( config , default_flow_style = False ) ) <EOL> click . echo ( "<STR_LIT>" . format ( <EOL> default_config_path ) ) <EOL> @ cli . command ( ) <EOL> def reindex ( ) : <EOL> """<STR_LIT>""" <EOL> build_index ( ) <EOL> click . echo ( '<STR_LIT>' ) <EOL> @ cli . command ( ) <EOL> @ click . argument ( '<STR_LIT>' ) <EOL> def locate ( command ) : <EOL> """<STR_LIT>""" <EOL> location = find_page_location ( command ) <EOL> click . echo ( location ) </s>
