Example #1
0
def do_config(where, previous_context):
    if not os.path.exists(previous_context["_configfile_name"]):
        return copy.deepcopy(previous_context)

    with open(previous_context["_configfile_name"]) as f:
        try:
            return dictmerge(previous_context, json.load(f))
        except Exception as e:
            logging.error("Bad JSON in file:" + previous_context["_configfile_name"])
            logging.error(str(e.message))
            exit(1)
Example #2
0
def do_dir(where, previous_context):
    return_to = getcwd()
    chdir(where)
    context = do_config(where, previous_context)

    if not "_base_path" in context:
        context["_base_path"] = ""
    else:
        context["_base_path"] += "../"

    context["_output_dir"] = context.pop("_output_dir", os.path.join(context["_parent_output_dir"], where))

    if os.path.exists("_config.py"):
        sys.path.insert(0, os.getcwd())

        # Make _plugins/ directories behave as modules:
        # temp variables to make the next if nicer:
        plugin_dir = _HIDE_ME_PREFIX + "plugins"
        plugin_modulator = os.path.join(plugin_dir, "__init__.py")

        if os.path.isdir(plugin_dir) and not os.path.exists(plugin_modulator):
            open(plugin_modulator, "wb").close()

        try:

            def do_plugin(filename):
                # global context
                x = __import__(filename, fromlist=True)
                # Here is a list of plugins we import:
                # TODO: functionise/DRY this:
                if "_context" in dir(x):
                    context.update(x._context)
                if "_file_handlers" in dir(x):
                    context["_file_handlers"].update(x._file_handlers)
                if "_tag_plugins" in dir(x):
                    [markdown_handler.register_tag_plugin(tag, func) for tag, func in x._tag_plugins.items()]
                if "_post_plugins" in dir(x):
                    [markdown_handler.register_post_plugin(tag, func) for tag, func in x._post_plugins.items()]

            execfile("_config.py")
        except Exception as e:
            logging.error("Error with _config.py in " + os.getcwd())
            [logging.error(l) for l in traceback.format_exc().splitlines()]
            exit(1)

    if not os.path.exists(context["_cache_dir"]):
        os.makedirs(context["_cache_dir"])

    if not os.path.exists(context["_output_dir"]):
        os.makedirs(context["_output_dir"])
    elif not os.path.isdir(context["_output_dir"]):
        os.rename(context["_output_dir"], context["_output_dir"] + ".prev")
        os.makedirs(context["_output_dir"])

    files_list = []
    for filename in filter(lambda x: exclude_test(x, _HIDE_ME_PREFIX), listdir(".")):

        # Do files after subdirectories...
        if os.path.isfile(filename):
            files_list.append(filename)

        if os.path.isdir(filename):
            # TODO - think.  Is this the best place for this?  I kind of
            # think it should be in do_config ?? I dunno...
            my_parent_output_dir = context["_parent_output_dir"]
            my_output_dir = context["_parent_output_dir"] = context.pop("_output_dir")

            do_dir(filename, context)

            context["_parent_output_dir"] = my_parent_output_dir
            context["_output_dir"] = my_output_dir

    [do_file(filename, context) for filename in files_list]
    ##############################
    # MULTIPROCESSING: (TODO)
    #     pool = Pool(5)
    #     pool.map(_do_file_wrapped, [(filename, context) for filename in files_list])
    ##############################

    # return to where we came from before leaving...
    # it's only polite.
    chdir(return_to)