Пример #1
0
def load_config(filename, **kwargs):
    """
    Read the config.yml file and create the Translator object.
    """
    config = yaml_load(filename, root=MooseDocs.ROOT_DIR)

    # Replace 'default' and 'disable' key in Extensions to allow for recursive_update to accept command line
    for key in config.get('Extensions', dict()).keys():
        if config['Extensions'][key] == 'default':
            config['Extensions'][key] = dict()
        if config['Extensions'][key] == 'disable':
            config['Extensions'][key] = dict(active=False)

    # Apply command-line key value pairs
    recursive_update(config, kwargs)

    extensions = _yaml_load_extensions(config)
    reader = _yaml_load_object('Reader', config, DEFAULT_READER)
    renderer = _yaml_load_object('Renderer', config, DEFAULT_RENDERER)
    content = _yaml_load_content(config, reader.EXTENSIONS)
    executioner = _yaml_load_object('Executioner', config, DEFAULT_EXECUTIONER)
    translator = _yaml_load_object('Translator', config, DEFAULT_TRANSLATOR,
                                   content, reader, renderer, extensions, executioner)

    return translator, config
Пример #2
0
def load_config(filename, **kwargs):
    """
    Read the config.yml file and create the Translator object.
    """
    config = yaml_load(filename, root=MooseDocs.ROOT_DIR)
    recursive_update(config, kwargs)

    extensions = _yaml_load_extensions(config)
    reader = _yaml_load_object('Reader', config, DEFAULT_READER)
    renderer = _yaml_load_object('Renderer', config, DEFAULT_RENDERER)
    content = _yaml_load_content(config, reader.EXTENSIONS)
    executioner = _yaml_load_object('Executioner', config, DEFAULT_EXECUTIONER)
    translator = _yaml_load_object('Translator', config, DEFAULT_TRANSLATOR,
                                   content, reader, renderer, extensions, executioner)

    return translator, config
Пример #3
0
    def _target(self, nodes, barrier, page_attributes, read, tokenize, render, write):
        """Target function for multiprocessing.Process() calls."""

        if read:
            for node in nodes:
                self._page_content[node.uid] = self.read(node)
                page_attributes[node.uid] = node.attributes

            barrier.wait()
            self._updateAttributes(page_attributes)

        if tokenize:
            for node in nodes:
                mooseutils.recursive_update(node.attributes, page_attributes[node.uid])
                self._page_ast[node.uid] = self.tokenize(node, self._page_content[node.uid])
                page_attributes[node.uid] = node.attributes

            barrier.wait()
            self._updateAttributes(page_attributes)

        if render:
            for node in nodes:
                mooseutils.recursive_update(node.attributes, page_attributes[node.uid])
                self._page_result[node.uid] = self.render(node, self._page_ast[node.uid])
                page_attributes[node.uid] = node.attributes

            barrier.wait()
            self._updateAttributes(page_attributes)

        if write:
            for node in nodes:
                mooseutils.recursive_update(node.attributes, page_attributes[node.uid])
                self.write(node, self._page_result[node.uid])
Пример #4
0
    def _target(self, nodes, barrier, page_attributes):
        """Target function for multiprocessing.Process calls."""

        local_content = dict()
        local_ast = dict()
        local_result = dict()

        # READ
        for node in nodes:
            Executioner.setMutable(node, True)
            content = self.read(node)
            page_attributes[node.uid] = node.attributes
            Executioner.setMutable(node, False)
            local_content[node.uid] = content

        barrier.wait()
        self._updateAttributes(page_attributes)

        # TOKENIZE
        for node in nodes:
            content = local_content.pop(node.uid)
            Executioner.setMutable(node, True)
            mooseutils.recursive_update(node.attributes,
                                        page_attributes[node.uid])
            ast = self.tokenize(node, content)
            page_attributes[node.uid] = node.attributes
            Executioner.setMutable(node, False)
            local_ast[node.uid] = ast

        barrier.wait()
        self._updateAttributes(page_attributes)

        # RENDER
        for node in nodes:
            ast = local_ast.pop(node.uid)
            Executioner.setMutable(node, True)
            mooseutils.recursive_update(node.attributes,
                                        page_attributes[node.uid])
            result = self.render(node, ast)
            page_attributes[node.uid] = node.attributes
            Executioner.setMutable(node, False)
            local_result[node.uid] = result

        barrier.wait()
        self._updateAttributes(page_attributes)

        # WRITE
        for node in nodes:
            result = local_result.pop(node.uid)
            Executioner.setMutable(node, True)
            mooseutils.recursive_update(node.attributes,
                                        page_attributes[node.uid])
            result = self.write(node, result)
            Executioner.setMutable(node, False)
Пример #5
0
def main(options):
    """
    Main function for the build command.

    Inputs:
        options[argparse options]: Complete options from argparse, see MooseDocs/main.py
    """
    t = time.time()

    # Infinite nested dict
    tree = lambda: collections.defaultdict(tree)
    kwargs = tree()

    # Setup executioner
    if options.executioner:
        kwargs['Executioner']['type'] = options.executioner

    # Disable extensions
    if options.stable:
        pass
    elif options.fast:
        options.disable += ['MooseDocs.extensions.appsyntax', 'MooseDocs.extensions.navigation',
                            'MooseDocs.extensions.sqa', 'MooseDocs.extensions.civet',
                            'MooseDocs.extensions.gitutils']
    else:
        options.disable += ['MooseDocs.extensions.sqa', 'MooseDocs.extensions.civet',
                            'MooseDocs.extensions.gitutils']

    for name in options.disable:
        kwargs['Extensions'][name] = dict(active=False)

    if options.hide_source:
        kwargs['Extensions']['MooseDocs.extensions.modal']['show_source'] = False

    # Apply Translator settings
    if options.destination:
        kwargs['Translator']['destination'] = mooseutils.eval_path(options.destination)
    if options.profile:
        kwargs['Translator']['profile'] = True

    # Apply '--args' and override anything already set
    if options.args is not None:
        mooseutils.recursive_update(kwargs, options.args)

    # Create translators for the specified configuration files, provide kwargs to override them
    config_files = options.config if isinstance(options.config, list) else [options.config]
    subconfigs = len(config_files) > 1
    LOG.info("Loading configuration file{}".format('s' if subconfigs else ''))
    translators, contents, configurations = common.load_configs(config_files, **kwargs)

    # Initialize the translator objects
    for index, translator in enumerate(translators):
        if subconfigs:
            LOG.info('Initializing translator object loaded from %s', config_files[index])
        translator.init(contents[index])

        # init methods can add pages (e.g., civet.py), but don't add pages from another translator
        contents[index] = [page for page in translator.getPages() if page.translator is translator]

    # Identify the first translator in the list as the "primary" one for convenience
    primary = translators[0]
    pooled = sorted(primary.getPages(), key=(lambda p: p.local))

    # Dump page tree from content pool and syntax list from all translators with AppSyntax
    if options.dump:
        for page in pooled:
            print('{}: {}'.format(page.local, page.source))
        for index, translator in enumerate(translators):
            for extension in translator.extensions:
                if isinstance(extension, MooseDocs.extensions.appsyntax.AppSyntaxExtension):
                    if subconfigs:
                        LOG.info('Building syntax list specified by %s', config_files[index])
                    extension.preExecute()
                    print(extension.syntax)
                    break
        return 0

    # TODO: See `navigation.postExecute`
    #       The navigation "home" should be a markdown file, when all the apps update to this we
    #       can remove this as well as the use of it by CIVET
    home = options.home
    if options.serve and (home is not None) and (not home.endswith('.md')):
        home = 'http://127.0.0.1:{}'.format(options.port)

    if home is not None:
        for ext in primary.extensions:
            if 'home' in ext:
                ext.update(home=home)

    # Set default for --clean: clean when --files is NOT used.
    if options.clean is None:
        options.clean = options.files == []
    else:
        options.clean = options.clean.lower() in ['true', 'yes', '1']

    if options.clean and os.path.exists(primary.destination):
        LOG.info("Cleaning destination %s", primary.destination)
        shutil.rmtree(primary.destination)

    # Update contents lists if only building certain files
    if options.files:
        for index, translator in enumerate(translators):
            func = lambda p: (p in contents[index]
                              and any([p.local.startswith(f) for f in options.files]))
            contents[index] = translator.findPages(func)

    # Execute the read and tokenize methods on all translators
    for index, translator in enumerate(translators):
        if subconfigs:
            LOG.info('Reading content specified by %s', config_files[index])
        translator.execute(contents[index], num_threads=options.num_threads, render=False, write=False)

    # Finally, execute the render and write methods
    for index, translator in enumerate(translators):
        if subconfigs:
            LOG.info('Writing content specified by %s', config_files[index])
        translator.execute(contents[index], num_threads=options.num_threads, read=False, tokenize=False)

    LOG.info('Total Time [%s sec.]', time.time() - t)

    # Run live server and watch for content changes
    if options.serve:
        watcher = MooseDocsWatcher(translators, pooled, configurations, options.num_threads)
        server = livereload.Server(watcher=watcher)
        server.serve(root=primary.destination, host=options.host, port=options.port)

    return 0
Пример #6
0
def main(options):
    """
    Main function for the build command.

    Inputs:
        options[argparse options]: Complete options from argparse, see MooseDocs/main.py
    """
    # Infinite nested dict
    tree = lambda: collections.defaultdict(tree)
    kwargs = tree()

    # Setup executioner
    if options.executioner:
        kwargs['Executioner']['type'] = options.executioner

    # Disable extensions
    if options.fast:
        options.disable += [
            'MooseDocs.extensions.appsyntax',
            'MooseDocs.extensions.navigation', 'MooseDocs.extensions.sqa',
            'MooseDocs.extensions.civet'
        ]
    for name in options.disable:
        kwargs['Extensions'][name] = dict(active=False)

    # Apply '--args' and override anything already set
    if options.args is not None:
        mooseutils.recursive_update(kwargs, options.args)

    # Create translator, provide kwargs to override content of file
    translator, _ = common.load_config(options.config, **kwargs)
    if options.destination:
        translator.update(
            destination=mooseutils.eval_path(options.destination))
    if options.profile:
        translator.executioner.update(profile=True)
    translator.init()

    # Replace "home" with local server
    home = options.home
    if options.serve:
        home = 'http://127.0.0.1:{}'.format(options.port)

    if home is not None:
        for ext in translator.extensions:
            if 'home' in ext:
                ext.update(home=home)

    # Dump page tree
    if options.dump:
        for page in translator.getPages():
            print('{}: {}'.format(page.local, page.source))
        for ext in translator.extensions:
            if isinstance(ext,
                          MooseDocs.extensions.appsyntax.AppSyntaxExtension):
                ext.preExecute()
                print(ext.syntax)
        return 0

    # Set default for --clean: clean when --files is NOT used.
    if options.clean is None:
        options.clean = options.files == []
    else:
        options.clean = options.clean.lower() in ['true', 'yes', '1']

    if options.clean and os.path.exists(translator['destination']):
        log = logging.getLogger('MooseDocs.build')
        log.info("Cleaning destination %s", translator['destination'])
        shutil.rmtree(translator['destination'])

    # Perform build
    if options.files:
        nodes = []
        for filename in options.files:
            nodes += translator.findPages(filename)
        translator.execute(nodes, options.num_threads)
    else:
        translator.execute(None, options.num_threads)

    if options.serve:
        watcher = MooseDocsWatcher(translator, options)
        server = livereload.Server(watcher=watcher)
        server.serve(root=translator['destination'],
                     host=options.host,
                     port=options.port)

    return 0
Пример #7
0
 def _updateAttributes(self, page_attributes):
     """Update the local page objects with attributes gathered from the other processes"""
     for page in self._page_objects:
         mooseutils.recursive_update(page.attributes, page_attributes[page.uid])
Пример #8
0
 def setGlobalAttribute(self, key, value):
     """Set a global attribute to be communicated across processors."""
     with self._lock:
         mooseutils.recursive_update(self._global_attributes, {key: value})