import compile
import link
import markdown

import argparse
import imp
import os
import sys


def configure_path():
    configs = {}
    cwd = os.getcwd()
    templar = os.path.abspath(__file__)
    paths = []
    while not templar.startswith(cwd):
        paths.append(cwd)
        cwd = os.path.dirname(cwd)
    paths.append(cwd)
    import config
    for i in range(len(paths)):
        sys.path.insert(0, paths[-i-1])
        imp.reload(config)
        extract_configs(config, configs)
    return configs

def extract_configs(config, configs):
    for dct in ('VARIABLES',):
        if hasattr(config, dct):
            new = getattr(config, dct)
            for k, v in new.items():
                configs[k] = v
    for lst in ('SUBSTITUTIONS', 'TEMPLATE_DIRS'):
        if hasattr(config, lst):
            new = getattr(config, lst)
            configs.setdefault(lst, []).extend(new)
    if hasattr(config, 'table_of_contents') and \
            hasattr(config, 'header_regex') and \
            hasattr(config, 'header_translate'):
        configs['table_of_contents'] = getattr(config, 'table_of_contents')
        configs['header_regex'] = getattr(config, 'header_regex')
        configs['header_translate'] = getattr(config, 'header_translate')


##########################
# Command-line Interface #
##########################

def main():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()

    markdown_parser = subparsers.add_parser('markdown')
    markdown.cmd_options(markdown_parser)
    markdown_parser.set_defaults(func=markdown.main)

    link_parser = subparsers.add_parser('link')
    link.cmd_options(link_parser)
    link_parser.set_defaults(func=link.main)

    compile_parser = subparsers.add_parser('compile')
    compile.cmd_options(compile_parser)
    compile_parser.set_defaults(func=compile.main)

    args = parser.parse_args()

    configs = configure_path()
    args.func(args, configs)

if __name__ == '__main__':
    main()

@@ -1336,16 +1336,747 @@
 ######%0A%0A
+def config_cmd_options(parser):%0A    parser.add_argument('-p', '--path', type=str,%0A                        help=%22Add config.py to specified path%22)%0A%0Adef config_main(args, configs=None):%0A    if args.path:%0A        path = args.path%0A    else:%0A        path = os.getcwd()%0A    path = os.path.join(path, 'config.py')%0A    config = os.path.join(os.path.dirname(__file__), 'config.py')%0A    with open(config, 'r') as f:%0A        template = f.read()%0A    if not os.path.exists(path) or %5C%0A            'y' in input('Remove existing config.py? %5By/n%5D ').lower():%0A        with open(path, 'w') as f:%0A            f.write(template)%0A        print('Copied config.py to', path)%0A        exit(0)%0A    else:%0A        print('config.py not copied')%0A        exit(1)%0A%0A
 def main
@@ -2582,24 +2582,164 @@
 pile.main)%0A%0A
+    config_parser = subparsers.add_parser('config')%0A    config_cmd_options(config_parser)%0A    config_parser.set_defaults(func=config_main)%0A%0A
     args = p

Add option to create config file