Exemplo n.º 1
0
def load_plugins():
    """
    Call the load function on all plugins in the appropriate order. Loads the following
    [option](%/Modules/options.html):
     - `plugins -plg` List all currently loaded plugins
    """
    options.add_option('plugins', '-plg', 'List all currently loaded plugins', False, bool)
    plugins = [plugin[:-3] for plugin in os.listdir(config.plugin_directory) if plugin.endswith('.py')]
    # Put logging at the start of list
    plugins.insert(0, plugins.pop(plugins.index('_logging')))
    # Put options at the end of the list
    plugins.append(plugins.pop(plugins.index('_options')))
    # Attempt to load plugins
    for plugin in plugins:
        try:
            exec('import plugins.{}; load(plugins.{}.load())'.format(plugin, plugin))
            logging.info('Loaded ' + plugin)
        except AttributeError:
            raise PluginError('Failed to load ' + plugin + ': plugin has no load function')
        except TypeError:
            raise PluginError('Failed to load ' + plugin + ': plugin returned an invalid plugin signature or was None')
    # Display plugin list and exit
    options.post('Successfully loaded', len(_plugins), 'plugins.')
    if options.state.plugins():
        for plugin in _plugins.values():
            print('\t' + plugin['name'] + ' by ' + plugin['author'] + '\n\t\t' + plugin['description'])
        exit()
Exemplo n.º 2
0
def load():
    """
    Adds two dummy formats to the formatter, `pdf` and `dvi`. Hooks `pdf_bypass` to after `post_status` in the
    [process_list](%/Modules/process_list.html) to ensure that arguments from the command line and from the template
    file are both loaded. The bypass function checks to see if either PDF or DVI formats have been requested. Loads the
    following [options](%/Modules/options.html):
     - `recomps -r` The number of LaTeX recompilations

    :return: plugin signature
    """
    parser_composer.add_format(
        name='pdf',
        extensions=['pdf'],
        description='PDF export support',
        format={}
    )
    parser_composer.add_format(
        name='dvi',
        extensions=['dvi'],
        description='DVI export support',
        format={}
    )
    process_list.run_after('post_status', pdf_bypass)
    options.add_option('recomps', '-r', 'The number of LaTeX recompilations',  config.recomps, int)
    return signature
Exemplo n.º 3
0
def load():
    """
    Loads the following [options](%/Modules/options.html):
     - `gitdocs -gdocs` Builed Pyxam's documentation for use on Github
     - `docs -docs` Build Pyxam's documentation for use locally

    If this option is supplied Pyxam's docs are rebuilt. Docs are built by:
     - Finding the paths to the documentation source, module files, and plugin files
     - Copying all docstrings from module files and plugin files to the documentation build directory
     - Copying all files from the documentation source to documentation build directory
     - Converting all markdown files in the documentation build directory to HTML
    """
    global url, nav
    options.add_option('docs', '-docs', 'Build Pyxam\'s documentation for use locally', False, bool)
    options.add_option('gitdocs', '-gdocs', 'Build Pyxam\'s documentation for use on Github', False, bool)
    if options.state.docs() or options.state.gitdocs():
        if options.state.gitdocs():
            url = config.git_docs
        else:
            url = config.local_docs
        # Get paths, use the full path each time in case abspath changes / to \\ on windows
        plugins = os.path.abspath(__file__.replace('docs.py', ''))
        modules = os.path.abspath(__file__.replace('docs.py', '') + '..')
        docs = os.path.join(os.path.abspath(__file__.replace('docs.py', '')), '..', '..', 'docs', 'source')
        build = os.path.abspath(docs.replace('source', 'build'))
        load_docs(docs)
        load_source('Modules', modules, build)
        load_source('Plugins', plugins, build)
        # Compile docs
        compile_docs(build)
        # Exit
        exit('Docs successfully recompiled')
    return signature
Exemplo n.º 4
0
def load():
    """
    Loads the following [option](%/Modules/options.html):
     - `htmltemplate -htt` Specify an HTML template file

    Loads the HTML format. The template file is used as a wrapper for converted content.
    """
    options.add_option(
        'htmltemplate', '-htt',
        'Specify an HTML template file',
        os.path.abspath(os.path.dirname(__file__) + '/../templates/exam.html'),
        str
    )
    parser_composer.add_format(
        name='html',
        extensions=['html'],
        description=signature[2],
        composer_preprocessor=composer_preprocessor,
        composer_postprocessor=composer_postprocessor,
        left_paren='<div',
        right_paren='</div>',
        # Use an OrderedDict to preserve token order
        format=collections.OrderedDict([
            ('comment', ['<!--', (), '-->', '.']),
            ('questions', ['<div class="questions">', (), '</div>', '.']),
            ('$$', [' $$', (), '$$ ', '.']),
            ('$', [' $', (), '$ ', '.']),
            ('title', ['<div class="title">', (), '</div>', '.']),
            ('solution', ['<div class="solution">', (), '</div>', '.']),
            ('img', ['<img alt="Embedded Image" src="data:image/png;base64,', (), '">', '.']),
            ('choice', ['<div class="choice">', (), '</div>', '.']),
            ('correctchoice', ['<div class="correctchoice">', (), '</div>', '.']),
            ('tolerance', [' tolerance ', (), '.']),
            ('verbatim', ['<pre class="verb-block">', (), '</pre>', '.']),
            ('true', ['<div class=True>True</div>', '.']),
            ('false', ['<div class=False>False</div>', '.']),
            ('h3', ['<h3>', (), '</h3>', '.']),
            ('h2', ['<h2>', (), '</h2>', '.']),
            ('h1', ['<h1>', (), '</h1>', '.']),
            ('list', ['<ul>', (), '</ul>', '.']),
            ('listitem', ['<li>', (), '</li>']),
            ('hr', ['<hr />', '.']),
            ('center', ['<div class="center">', (), '</div>', '.']),
            ('emphasis3', ['<i><b>', (), '</b></i>', '.']),
            ('emphasis2', ['<br /><b>', (), '</b>', '.']),
            ('emphasis1', ['<i>', (), '</i>', '.']),
            ('verbython', ['<pre class="verb-python">', (), '</pre>', '.']),
            ('verbhtml', ['<div></div>', (), '<div></div>', '.']),
            ('verbblock', ['<pre class="verb-block">', (), '</pre>', '.']),
            ('verbquote', ['<div class="verb-quote">', (), '</div>', '.']),
            ('verbexpr', ['<pre class="verb-expr">', (), '</pre>', '.']),
            ('newline', ['<br />', '.'])
        ])
    )
    return signature
Exemplo n.º 5
0
def load():
    """
    Loads the following [option](%/Modules/options.html):
     - `htmltemplate -htt` Specify an HTML template file

    Loads the HTML format. The template file is used as a wrapper for converted content.
    """
    options.add_option(
        'htmltemplate', '-htt', 'Specify an HTML template file',
        os.path.abspath(os.path.dirname(__file__) + '/../templates/exam.html'),
        str)
    parser_composer.add_format(
        name='html',
        extensions=['html'],
        description=signature[2],
        composer_preprocessor=composer_preprocessor,
        composer_postprocessor=composer_postprocessor,
        left_paren='<div',
        right_paren='</div>',
        # Use an OrderedDict to preserve token order
        format=collections.OrderedDict([
            ('comment', ['<!--', (), '-->', '.']),
            ('questions', ['<div class="questions">', (), '</div>', '.']),
            ('$$', [' $$', (), '$$ ', '.']), ('$', [' $', (), '$ ', '.']),
            ('title', ['<div class="title">', (), '</div>', '.']),
            ('solution', ['<div class="solution">', (), '</div>', '.']),
            ('img', [
                '<img alt="Embedded Image" src="data:image/png;base64,', (),
                '">', '.'
            ]), ('choice', ['<div class="choice">', (), '</div>', '.']),
            ('correctchoice',
             ['<div class="correctchoice">', (), '</div>', '.']),
            ('tolerance', [' tolerance ', (), '.']),
            ('verbatim', ['<pre class="verb-block">', (), '</pre>', '.']),
            ('true', ['<div class=True>True</div>', '.']),
            ('false', ['<div class=False>False</div>', '.']),
            ('h3', ['<h3>', (), '</h3>', '.']),
            ('h2', ['<h2>', (), '</h2>', '.']),
            ('h1', ['<h1>', (), '</h1>', '.']),
            ('list', ['<ul>', (), '</ul>', '.']),
            ('listitem', ['<li>', (), '</li>']), ('hr', ['<hr />', '.']),
            ('center', ['<div class="center">', (), '</div>', '.']),
            ('emphasis3', ['<i><b>', (), '</b></i>', '.']),
            ('emphasis2', ['<br /><b>', (), '</b>', '.']),
            ('emphasis1', ['<i>', (), '</i>', '.']),
            ('verbython', ['<pre class="verb-python">', (), '</pre>', '.']),
            ('verbhtml', ['<div></div>', (), '<div></div>', '.']),
            ('verbblock', ['<pre class="verb-block">', (), '</pre>', '.']),
            ('verbquote', ['<div class="verb-quote">', (), '</div>', '.']),
            ('verbexpr', ['<pre class="verb-expr">', (), '</pre>', '.']),
            ('newline', ['<br />', '.'])
        ]))
    return signature
Exemplo n.º 6
0
def load():
    """
    Loads the following [option](%/Modules/options.html):
     - `logging -l` Set the logging level for pyxam
    Sets logging.basicConfig with a default format and sets it to the option value.

    :return: plugin signature
    """
    options.add_option('logging', '-l', description, config.default_logging, int)
    # Set logging configuration
    logging.basicConfig(format=config.logging_format, level=options.state.logging())
    # Log configuration
    logging.info('Logging configured with level {}'.format(logging.getLevelName(options.state.logging())))
    # Return signature
    return signature
Exemplo n.º 7
0
def start(args, api=True):
    """
    Start Pyxam with a set of [options](%/Modules/options.html). Start adds the following processes to the
    [process_list](%/Modules/process_list.html):
     - `load_options` loads the command line options
     - `welcome` displays a welcome message
     - `load_plugins` loads all available plugins
     - `build_files` fixes paths and builds all necessary files
     - `run_commands` prepossesses commands in the template (see [bang](%/Modules/bang.html))
     - `post_status` posts the current state of all options
     - `mix` runs any inline code within the template
     - `parse` reads the template document into an intermediate format (see [formatter](%/Modules/parser_composer.py))
     - `compose` converts the intermediate format into the output format
     - `export` moves files from the tmp directory to the final out directory
     - `cleanup` removes all temporary files
     - `unload_plugins` unloads all available plugins
     - `goodbye` display a goodbye message
    Once added these processes are consumed until there are no more processes.

    :param args: A list of options provided in command line syntax
    :param api: A flag indicating if Pyxam is being called as an api
    """
    # Clear last session data
    store_args(args)
    options.clear()
    process_list.clear()
    # Add api option
    options.add_option('api', '-api', 'Run Pyxam in api mode', True, bool, value=api)
    process_list.append([
        options.load_options,
        welcome,
        plugin_loader.load_plugins,
        options.load_template,
        fileutil.build_files,
        mixer.setup,
        options.post_status,
        parser_composer.parse,
        parser_composer.compose,
        fileutil.export,
        fileutil.cleanup,
        plugin_loader.unload_plugins,
        goodbye])
    while process_list.ready():
        args = process_list.consume(args)
Exemplo n.º 8
0
def start(args, api=True):
    """
    Start Pyxam with a set of [options](%/Modules/options.html). Start adds the following processes to the
    [process_list](%/Modules/process_list.html):
     - `load_options` loads the command line options
     - `welcome` displays a welcome message
     - `load_plugins` loads all available plugins
     - `build_files` fixes paths and builds all necessary files
     - `run_commands` prepossesses commands in the template (see [bang](%/Modules/bang.html))
     - `post_status` posts the current state of all options
     - `mix` runs any inline code within the template
     - `parse` reads the template document into an intermediate format (see [formatter](%/Modules/parser_composer.py))
     - `compose` converts the intermediate format into the output format
     - `export` moves files from the tmp directory to the final out directory
     - `cleanup` removes all temporary files
     - `unload_plugins` unloads all available plugins
     - `goodbye` display a goodbye message
    Once added these processes are consumed until there are no more processes.

    :param args: A list of options provided in command line syntax
    :param api: A flag indicating if Pyxam is being called as an api
    """
    # Clear last session data
    store_args(args)
    options.clear()
    process_list.clear()
    # Add api option
    options.add_option('api',
                       '-api',
                       'Run Pyxam in api mode',
                       True,
                       bool,
                       value=api)
    process_list.append([
        options.load_options, welcome, plugin_loader.load_plugins,
        options.load_template, fileutil.build_files, mixer.setup,
        options.post_status, parser_composer.parse, parser_composer.compose,
        fileutil.export, fileutil.cleanup, plugin_loader.unload_plugins,
        goodbye
    ])
    while process_list.ready():
        args = process_list.consume(args)
Exemplo n.º 9
0
def load():
    """
    Adds two dummy formats to the formatter, `pdf` and `dvi`. Hooks `pdf_bypass` to after `post_status` in the
    [process_list](%/Modules/process_list.html) to ensure that arguments from the command line and from the template
    file are both loaded. The bypass function checks to see if either PDF or DVI formats have been requested. Loads the
    following [options](%/Modules/options.html):
     - `recomps -r` The number of LaTeX recompilations

    :return: plugin signature
    """
    parser_composer.add_format(name='pdf',
                               extensions=['pdf'],
                               description='PDF export support',
                               format={})
    parser_composer.add_format(name='dvi',
                               extensions=['dvi'],
                               description='DVI export support',
                               format={})
    process_list.run_after('post_status', pdf_bypass)
    options.add_option('recomps', '-r', 'The number of LaTeX recompilations',
                       config.recomps, int)
    return signature
Exemplo n.º 10
0
class L1DCache(L1Cache):
    """Simple L1 data cache with default values"""

    # Set the default size
    size = '32kB'
    assoc = 8

    options.add_option('--replacement_policy',
                       help="L1 cache replacement policy. [PLRU,NMRU,Random]")

    options.add_option('--l1d_size',
                       help="L1 data cache size. Default: %s" % size)

    options.add_option('--l1d_assoc',
                       help="L1 data cache associativity. Default: %s" % assoc)

    def __init__(self, opts=None):
        super(L1DCache, self).__init__(opts)
        if not opts:
            return

        if opts.l1d_size:
            self.size = opts.l1d_size

        if opts.l1d_assoc:
            self.assoc = opts.l1d_assoc

        if opts.replacement_policy == "PLRU":
            from m5.objects import PLRU
            self.tags = PLRU()
        elif opts.replacement_policy == "NMRU":
            from m5.objects import NMRU
            self.tags = NMRU()
        elif opts.replacement_policy == "Random":
            from m5.objects import RandomRepl
            self.tags = RandomRepl()
        elif opts.replacement_policy:
            fatal("Unsupported replacement policy: %s" %
                  opts.replacement_policy)
Exemplo n.º 11
0
class L1ICache(L1Cache):
    """Simple L1 instruction cache with default values"""

    # Set the default size
    size = '16kB'

    options.add_option('--l1i_size',
                       help="L1 instruction cache size. Default: %s" % size)

    def __init__(self, opts=None):
        super(L1ICache, self).__init__(opts)
        if not opts or not opts.l1i_size:
            return
        self.size = opts.l1i_size
Exemplo n.º 12
0
def build_files():
    """
    Finds the absolute file path for the template, tmp directory, and out directory and updates their options to point
    to their absolute path. Also creates the tmp and out directories if necessary. Warns user if tmp is going to be
    overridden as this will delete any leftover files. Finally changes the current working directory to tmp.
    """
    # Get absolute paths
    options.state.template(os.path.abspath(options.state.template()))
    options.add_option('cwd', '', 'The original CWD', os.getcwd(), str)
    os.chdir(os.path.abspath(os.path.dirname(options.state.template())))
    if os.path.abspath(options.state.out()) != options.state.out():
        options.state.out(options.state.cwd() + '/' + options.state.out())
    if os.path.abspath(options.state.tmp()) != options.state.tmp():
        options.state.tmp(options.state.cwd() + '/' + options.state.tmp())
    if os.path.abspath(options.state.figure()) != options.state.figure():
        options.state.figure(options.state.tmp() + '/' + options.state.figure())
    logging.info('Fixed paths')
    # Overwrite warning
    if os.path.isdir(options.state.tmp()) and not options.state.api() and \
            input('Temporary directory already exists. Continue anyways? (y/n)') != 'y':
        exit('Cancelling operation.')
    else:
        remove(options.state.tmp())
    # Build tmp directory
    if not os.path.exists(options.state.tmp()):
        os.mkdir(options.state.tmp())
    else:
        cleanup()
        if not os.path.exists(options.state.tmp()):
            os.mkdir(options.state.tmp())
    # Build out directory
    if not os.path.exists(options.state.out()):
        os.mkdir(options.state.out())
    # Change current working directory
    options.state.cwd(options.state.tmp())
    logging.info('Built directories')
Exemplo n.º 13
0
class L2Cache(BaseCache):
    """Simple L2 Cache with default values"""

    # Default parameters
    size = '256kB'
    assoc = 8
    hit_latency = 20
    response_latency = 20
    mshrs = 20
    tgts_per_mshr = 12

    options.add_option('--l2_size', help="L2 cache size. Default: %s" % size)

    def __init__(self, opts=None):
        super(L2Cache, self).__init__()
        if not opts or not opts.l2_size:
            return
        self.size = opts.l2_size

    def connectCPUSideBus(self, bus):
        self.cpu_side = bus.master

    def connectMemSideBus(self, bus):
        self.mem_side = bus.slave
Exemplo n.º 14
0
def load():
    """
    Loads the following [options](%/Modules/options.html):
     - `out -o` Set the output directory
     - `tmp -tmp` Set the temporary directory
     - `figure -fig` Set the figure directory
     - `number -n` Set the number of exams to generate
     - `title -t` Set the title of the exam
     - `format -f` Set the format of the exam
     - `shell -shl` Set the shell used to weave the exam
     - `method -m` Set the selection method for population mixing
     - `population -p` Set the class list
     - `alphabetize -a` Enable lettered versioning
     - `noweave -w` Disable pweave
     - `debug -d` Disable file cleanup
     - `solutions -s` Enable solutions
     - `version -v` Show the version number
     - `list -ls` List all available formats
     - `help -h` Show a help message
     Manages the `solutions`, `version`, `list`, and `help` commands as explained above.

    :return: plugin signature
    """
    #                   NAME            FLAG    DESCRIPTION                           DEFAULT            TYPE
    options.add_option('out', '-o', 'Set the output directory', config.out,
                       str)
    options.add_option('tmp', '-tmp', 'Set the temporary directory',
                       config.tmp, str)
    options.add_option('figure', '-fig', 'Set the figure directory',
                       config.fig, str)
    options.add_option('number', '-n', 'Set the number of exams to generate',
                       config.number, int)
    options.add_option('title', '-t', 'Set the title of the exam',
                       config.title, str)
    options.add_option('format', '-f', 'Set export format', config.format, str)
    options.add_option('shell', '-shl', 'Set shell used to weave the exam',
                       config.shell, str)
    options.add_option('method', '-m', 'Set selection method for CSVs',
                       config.method, str)
    options.add_option('population', '-p', 'Set class list', None, str)
    options.add_option('alphabetize', '-a', 'Enable lettered versioning',
                       config.alphabetize, bool)
    options.add_option('noweave', '-w', 'Disable pweave', config.noweave, bool)
    options.add_option('debug', '-d', 'Disable file cleanup', config.debug,
                       bool)
    options.add_option('solutions', '-s', 'Enable soultions', config.solutions,
                       bool)
    options.add_option('version', '-v', 'Show the version number', False, bool)
    options.add_option('list', '-ls', 'List all available formats', False,
                       bool)
    options.add_option('help', '-h', 'Show a help message', False, bool)
    # Run once and produce solutions then run again widthout solutions
    if options.state.solutions():
        options.state.population('')
        process_list.run_before('goodbye', rerun_without_solutions)
    # Display version number via the welcome message then exit
    if options.state.version():
        if options.state.api():
            pyxam.welcome()
    # Display a list of all available formats then exit
    elif options.state.list():
        formats = set(fmt['extensions'][0]
                      for key, fmt in parser_composer.formats.items())
        for fmt in set(formats):
            print(parser_composer.formats[fmt]['extensions'][0] + ':\n\t' +
                  parser_composer.formats[fmt]['description'])
    # Display a help message then exit
    elif options.state.help():
        div = '-' * max(len(line) for line in options.get_help().split('\n'))
        print('\n'.join([
            div, 'Pyxam Options'.center(len(div)), div,
            options.get_help(), div
        ]))
    # Return signature
    else:
        return signature
    # Exit
    exit()
Exemplo n.º 15
0
 def add_option(self,names):
     #Call the add_option function to implement options in the model
     options.add_option(self,names)
Exemplo n.º 16
0
def load():
    """
    Loads the following [options](%/Modules/options.html):
     - `out -o` Set the output directory
     - `tmp -tmp` Set the temporary directory
     - `figure -fig` Set the figure directory
     - `number -n` Set the number of exams to generate
     - `title -t` Set the title of the exam
     - `format -f` Set the format of the exam
     - `shell -shl` Set the shell used to weave the exam
     - `method -m` Set the selection method for population mixing
     - `population -p` Set the class list
     - `alphabetize -a` Enable lettered versioning
     - `noweave -w` Disable pweave
     - `debug -d` Disable file cleanup
     - `solutions -s` Enable solutions
     - `version -v` Show the version number
     - `list -ls` List all available formats
     - `help -h` Show a help message
     Manages the `solutions`, `version`, `list`, and `help` commands as explained above.

    :return: plugin signature
    """
    #                   NAME            FLAG    DESCRIPTION                           DEFAULT            TYPE
    options.add_option('out',          '-o',   'Set the output directory',            config.out,         str)
    options.add_option('tmp',          '-tmp', 'Set the temporary directory',         config.tmp,         str)
    options.add_option('figure',       '-fig', 'Set the figure directory',            config.fig,         str)
    options.add_option('number',       '-n',   'Set the number of exams to generate', config.number,      int)
    options.add_option('title',        '-t',   'Set the title of the exam',           config.title,       str)
    options.add_option('format',       '-f',   'Set export format',                   config.format,      str)
    options.add_option('shell',        '-shl', 'Set shell used to weave the exam',    config.shell,       str)
    options.add_option('method',       '-m',   'Set selection method for CSVs',       config.method,      str)
    options.add_option('population',   '-p',   'Set class list',                      None,               str)
    options.add_option('alphabetize',  '-a',   'Enable lettered versioning',          config.alphabetize, bool)
    options.add_option('noweave',      '-w',   'Disable pweave',                      config.noweave,     bool)
    options.add_option('debug',        '-d',   'Disable file cleanup',                config.debug,       bool)
    options.add_option('solutions',    '-s',   'Enable soultions',                    config.solutions,   bool)
    options.add_option('version',      '-v',  'Show the version number',             False,               bool)
    options.add_option('list',         '-ls', 'List all available formats',          False,               bool)
    options.add_option('help',         '-h',  'Show a help message',                 False,               bool)
    # Run once and produce solutions then run again widthout solutions
    if options.state.solutions():
        options.state.population('')
        process_list.run_before('goodbye', rerun_without_solutions)
    # Display version number via the welcome message then exit
    if options.state.version():
        if options.state.api():
            pyxam.welcome()
    # Display a list of all available formats then exit
    elif options.state.list():
        formats = set(fmt['extensions'][0] for key, fmt in parser_composer.formats.items())
        for fmt in set(formats):
            print(parser_composer.formats[fmt]['extensions'][0] + ':\n\t' + parser_composer.formats[fmt]['description'])
    # Display a help message then exit
    elif options.state.help():
        div = '-' * max(len(line) for line in options.get_help().split('\n'))
        print('\n'.join([div, 'Pyxam Options'.center(len(div)), div, options.get_help(), div]))
    # Return signature
    else:
        return signature
    # Exit
    exit()