<s> import json <EOL> import os <EOL> import pydoc <EOL> from eg import color <EOL> from eg import substitute <EOL> EXAMPLE_FILE_SUFFIX = '<STR_LIT>' <EOL> VERSION = '<STR_LIT>' <EOL> FLAG_ONLY_CUSTOM = '<STR_LIT:+>' <EOL> FLAG_CUSTOM_AND_DEFAULT = '<STR_LIT:*>' <EOL> FLAG_FALLBACK = '<STR_LIT>' <EOL> ALIAS_FILE_NAME = '<STR_LIT>' <EOL> def handle_program ( program , config ) : <EOL> default_file_path = None <EOL> custom_file_path = None <EOL> resolved_program = get_resolved_program ( program , config ) <EOL> if has_default_entry_for_program ( resolved_program , config ) : <EOL> default_file_path = get_file_path_for_program ( <EOL> resolved_program , <EOL> config . examples_dir <EOL> ) <EOL> if has_custom_entry_for_program ( resolved_program , config ) : <EOL> custom_file_path = get_file_path_for_program ( <EOL> resolved_program , <EOL> config . custom_dir <EOL> ) <EOL> if default_file_path is None and custom_file_path is None : <EOL> print ( <EOL> '<STR_LIT>' + <EOL> program + <EOL> '<STR_LIT>' <EOL> ) <EOL> return <EOL> raw_contents = get_contents_from_files ( <EOL> default_file_path , <EOL> custom_file_path <EOL> ) <EOL> formatted_contents = get_formatted_contents ( <EOL> raw_contents , <EOL> use_color = config . use_color , <EOL> color_config = config . color_config , <EOL> squeeze = config . squeeze , <EOL> subs = config . subs <EOL> ) <EOL> page_string ( formatted_contents , config . pager_cmd ) <EOL> def get_file_path_for_program ( program , dir_to_search ) : <EOL> """<STR_LIT>""" <EOL> if dir_to_search is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> else : <EOL> result = os . path . join ( dir_to_search , program + EXAMPLE_FILE_SUFFIX ) <EOL> return result <EOL> def has_default_entry_for_program ( program , config ) : <EOL> """<STR_LIT>""" <EOL> if config . examples_dir : <EOL> file_path = get_file_path_for_program ( <EOL> program , <EOL> config . examples_dir <EOL> ) <EOL> return os . path . isfile ( file_path ) <EOL> else : <EOL> return False <EOL> def has_custom_entry_for_program ( program , config ) : <EOL> """<STR_LIT>""" <EOL> if config . custom_dir : <EOL> custom_path = get_file_path_for_program ( <EOL> program , <EOL> config . custom_dir <EOL> ) <EOL> return os . path . isfile ( custom_path ) <EOL> else : <EOL> return False <EOL> def get_contents_from_files ( default_file_path , custom_file_path ) : <EOL> """<STR_LIT>""" <EOL> file_data = '<STR_LIT>' <EOL> if custom_file_path : <EOL> file_data += _get_contents_of_file ( custom_file_path ) <EOL> if default_file_path : <EOL> file_data += _get_contents_of_file ( default_file_path ) <EOL> return file_data <EOL> def page_string ( str_to_page , pager_cmd ) : <EOL> """<STR_LIT>""" <EOL> use_fallback_page_function = False <EOL> if pager_cmd is None : <EOL> use_fallback_page_function = True <EOL> elif pager_cmd == FLAG_FALLBACK : <EOL> use_fallback_page_function = True <EOL> if use_fallback_page_function : <EOL> pydoc . pager ( str_to_page ) <EOL> else : <EOL> pydoc . pipepager ( str_to_page , cmd = pager_cmd ) <EOL> def _get_contents_of_file ( path ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:r>' ) as f : <EOL> result = f . read ( ) <EOL> return result <EOL> def _is_example_file ( file_name ) : <EOL> """<STR_LIT>""" <EOL> return file_name . endswith ( EXAMPLE_FILE_SUFFIX ) <EOL> def get_list_of_all_supported_commands ( config ) : <EOL> """<STR_LIT>""" <EOL> default_files = [ ] <EOL> custom_files = [ ] <EOL> if config . examples_dir and os . path . isdir ( config . examples_dir ) : <EOL> default_files = os . listdir ( config . examples_dir ) <EOL> if config . custom_dir and os . path . isdir ( config . custom_dir ) : <EOL> custom_files = os . listdir ( config . custom_dir ) <EOL> default_files = [ path for path in default_files if _is_example_file ( path ) ] <EOL> custom_files = [ path for path in custom_files if _is_example_file ( path ) ] <EOL> def get_without_suffix ( file_name ) : <EOL> """<STR_LIT>""" <EOL> return file_name . split ( EXAMPLE_FILE_SUFFIX ) [ <NUM_LIT:0> ] <EOL> default_files = [ get_without_suffix ( f ) for f in default_files ] <EOL> custom_files = [ get_without_suffix ( f ) for f in custom_files ] <EOL> set_default_commands = set ( default_files ) <EOL> set_custom_commands = set ( custom_files ) <EOL> alias_dict = get_alias_dict ( config ) <EOL> both_defined = set_default_commands & set_custom_commands <EOL> only_default = set_default_commands - set_custom_commands <EOL> only_custom = set_custom_commands - set_default_commands <EOL> all_commands = both_defined | only_default | only_custom <EOL> command_to_rep = { } <EOL> for command in all_commands : <EOL> rep = None <EOL> if command in both_defined : <EOL> rep = command + '<STR_LIT:U+0020>' + FLAG_CUSTOM_AND_DEFAULT <EOL> elif command in only_default : <EOL> rep = command <EOL> elif command in only_custom : <EOL> rep = command + '<STR_LIT:U+0020>' + FLAG_ONLY_CUSTOM <EOL> else : <EOL> raise NameError ( '<STR_LIT>' + str ( command ) ) <EOL> command_to_rep [ command ] = rep <EOL> result = [ ] <EOL> all_commands_and_aliases = all_commands . union ( alias_dict . keys ( ) ) <EOL> for command in all_commands_and_aliases : <EOL> if command in alias_dict : <EOL> target = alias_dict [ command ] <EOL> rep_of_target = command_to_rep [ target ] <EOL> result . append ( command + '<STR_LIT>' + rep_of_target ) <EOL> else : <EOL> rep = command_to_rep [ command ] <EOL> result . append ( rep ) <EOL> result . sort ( ) <EOL> return result <EOL> def get_squeezed_contents ( contents ) : <EOL> """<STR_LIT>""" <EOL> line_between_example_code = substitute . Substitution ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> True <EOL> ) <EOL> lines_between_examples = substitute . Substitution ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> lines_between_sections = substitute . Substitution ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> result = contents <EOL> result = line_between_example_code . apply_and_get_result ( result ) <EOL> result = lines_between_examples . apply_and_get_result ( result ) <EOL> result = lines_between_sections . apply_and_get_result ( result ) <EOL> return result <EOL> def get_colorized_contents ( contents , color_config ) : <EOL> """<STR_LIT>""" <EOL> colorizer = color . EgColorizer ( color_config ) <EOL> result = colorizer . colorize_text ( contents ) <EOL> return result <EOL> def get_substituted_contents ( contents , substitutions ) : <EOL> """<STR_LIT>""" <EOL> result = contents <EOL> for sub in substitutions : <EOL> result = sub . apply_and_get_result ( result ) <EOL> return result <EOL> def get_formatted_contents ( <EOL> raw_contents , <EOL> use_color , <EOL> color_config , <EOL> squeeze , <EOL> subs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> result = raw_contents <EOL> if use_color : <EOL> result = get_colorized_contents ( result , color_config ) <EOL> if squeeze : <EOL> result = get_squeezed_contents ( result ) <EOL> if subs : <EOL> result = get_substituted_contents ( result , subs ) <EOL> return result <EOL> def get_resolved_program ( program , config_obj ) : <EOL> """<STR_LIT>""" <EOL> alias_dict = get_alias_dict ( config_obj ) <EOL> if program in alias_dict : <EOL> return alias_dict [ program ] <EOL> else : <EOL> return program <EOL> def get_alias_dict ( config_obj ) : <EOL> """<STR_LIT>""" <EOL> if not config_obj . examples_dir : <EOL> return { } <EOL> alias_file_path = _get_alias_file_path ( config_obj ) <EOL> if not os . path . isfile ( alias_file_path ) : <EOL> return { } <EOL> alias_file_contents = _get_contents_of_file ( alias_file_path ) <EOL> result = json . loads ( alias_file_contents ) <EOL> return result <EOL> def _get_alias_file_path ( config_obj ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( config_obj . examples_dir , ALIAS_FILE_NAME ) </s>
