예제 #1
0
def main(verbosity: str, rich: bool, path: Path, template: str):
    if rich:
        logger = RichOutputLog(sys.stdout)
    else:
        logger = log.TeeLog(
            log.FilterLog(log.StdoutLog(), maxlevel=log.proto.Level.user),
            log.FilterLog(log.StderrLog(), minlevel=log.proto.Level.warning),
        )
    log.current = log.FilterLog(logger,
                                minlevel=getattr(log.proto.Level, verbosity))

    blocks = [
        'summary',
        'experience',
        'education',
        'honors',
        'publications',
        'presentations',
        'projects',
        'committees',
    ]

    try:
        Resume(path, template).render({'blocks': blocks}, verbosity == 'debug')
    except Exception as err:
        if verbosity == 'debug':
            traceback.print_exc()
        else:
            print(err)
            sys.exit(1)
예제 #2
0
 def setUp(self):
     super().setUp()
     self.enter_context(
         treelog.set(
             treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
     self.enter_context(_builtin_warnings.catch_warnings())
     _builtin_warnings.simplefilter('error', warnings.NutilsWarning)
예제 #3
0
def setup(scriptname: str,
          kwargs: typing.List[typing.Tuple[str, str, str]],
          outrootdir: str = '~/public_html',
          outdir: typing.Optional[str] = None,
          cachedir: str = 'cache',
          cache: bool = False,
          nprocs: int = 1,
          matrix: str = 'auto',
          richoutput: typing.Optional[bool] = None,
          outrooturi: typing.Optional[str] = None,
          outuri: typing.Optional[str] = None,
          verbose: typing.Optional[int] = 4,
          pdb: bool = False,
          gracefulexit: bool = True,
          **unused):
    '''Set up compute environment.'''

    from . import cache as _cache, parallel as _parallel, matrix as _matrix

    for name in unused:
        warnings.warn(
            'ignoring unused configuration variable {!r}'.format(name))

    if outdir is None:
        outdir = os.path.join(os.path.expanduser(outrootdir), scriptname)
        if outrooturi is None:
            outrooturi = pathlib.Path(
                outrootdir).expanduser().resolve().as_uri()
        outuri = outrooturi.rstrip('/') + '/' + scriptname
    elif outuri is None:
        outuri = pathlib.Path(outdir).resolve().as_uri()

    if richoutput is None:
        richoutput = sys.stdout.isatty()

    consolellog = treelog.RichOutputLog() if richoutput else treelog.StdoutLog(
    )
    if verbose is not None:
        consolellog = treelog.FilterLog(consolellog,
                                        minlevel=tuple(Level)[5 - verbose])
    htmllog = _htmllog(outdir, scriptname, kwargs)

    if nprocs == 1:
        os.environ['MKL_THREADING_LAYER'] = 'SEQUENTIAL'

    with htmllog, \
         _status(outuri+'/'+htmllog.filename, richoutput), \
         treelog.set(treelog.TeeLog(consolellog, htmllog)), \
         _traceback(richoutput=richoutput, postmortem=pdb, exit=gracefulexit), \
         warnings.via(treelog.warning), \
         _cache.enable(os.path.join(outdir, cachedir)) if cache else _cache.disable(), \
         _parallel.maxprocs(nprocs), \
         _matrix.backend(matrix), \
         _signal_handler(signal.SIGINT, functools.partial(_breakpoint, richoutput)):

        treelog.info('nutils v{}'.format(_version()))
        treelog.info('start', time.ctime())
        yield
        treelog.info('finish', time.ctime())
예제 #4
0
class ConsoleDirective(docutils.parsers.rst.Directive):

    has_content = True
    required_arguments = 0
    options_arguments = 0

    info = treelog.proto.Level.info if hasattr(treelog, 'proto') else 1
    _console_log = treelog.FilterLog(treelog.StdoutLog(), minlevel=info)

    def run(self):
        document = self.state.document
        env = document.settings.env
        nodes = []

        indent = min(len(line) - len(line.lstrip()) for line in self.content)
        code = ''.join(line[indent:] + '\n' for line in self.content)
        code_wo_spread = nutils.testing.FloatNeighborhoodOutputChecker.re_spread.sub(
            lambda m: m.group(0).split('±', 1)[0], code)

        literal = docutils.nodes.literal_block(code_wo_spread,
                                               code_wo_spread,
                                               classes=['console'])
        literal['language'] = 'python3'
        literal['linenos'] = False
        sphinx.util.nodes.set_source_info(self, literal)
        nodes.append(literal)

        import matplotlib.testing
        matplotlib.testing.setup()
        import matplotlib.pyplot
        parser = doctest.DocTestParser()
        runner = doctest.DocTestRunner(
            checker=nutils.testing.FloatNeighborhoodOutputChecker(),
            optionflags=doctest.ELLIPSIS)
        globs = getattr(document, '_console_globs', {})
        test = parser.get_doctest(code, globs, 'test', env.docname,
                                  self.lineno)
        with treelog.set(self._console_log):
            failures, tries = runner.run(test, clear_globs=False)
        for fignum in matplotlib.pyplot.get_fignums():
            fig = matplotlib.pyplot.figure(fignum)
            with io.BytesIO() as f:
                fig.savefig(f, format='svg')
                name = hashlib.sha1(f.getvalue()).hexdigest() + '.svg'
                uri = 'data:image/svg+xml;base64,{}'.format(
                    base64.b64encode(f.getvalue()).decode())
                nodes.append(
                    docutils.nodes.image('',
                                         uri=uri,
                                         alt='image generated by matplotlib'))
        matplotlib.pyplot.close('all')
        if failures:
            document.reporter.warning('doctest failed', line=self.lineno)
        document._console_globs = test.globs

        return nodes
예제 #5
0
파일: __main__.py 프로젝트: akva2/badger
def main(ctx, verbosity, rich):
    if rich:
        logger = RichOutputLog(sys.stdout)
    else:
        logger = log.TeeLog(
            log.FilterLog(log.StdoutLog(), maxlevel=log.proto.Level.user),
            log.FilterLog(log.StderrLog(), minlevel=log.proto.Level.warning),
        )
    log.current = log.FilterLog(logger,
                                minlevel=getattr(log.proto.Level, verbosity))
예제 #6
0
 def test_open_rw_devnull(self):
     with tempfile.TemporaryDirectory() as tmpdir:
         filenos = set()
         teelog = treelog.TeeLog(TeeLogTestLog(tmpdir, True, filenos),
                                 treelog.StdoutLog())
         with silent(), teelog.open('test',
                                    'wb',
                                    level=treelog.proto.Level.info) as f:
             self.assertIn(f.fileno(), filenos)
             f.write(b'test')
         with open(os.path.join(tmpdir, 'test'), 'rb') as f:
             self.assertEqual(f.read(), b'test')
예제 #7
0
 def output_tester(self):
     with capture() as captured:
         yield treelog.StdoutLog()
     self.assertEqual(
         captured.stdout, 'my message\n'
         'test.dat\n'
         'my context > iter 1 > a\n'
         'my context > iter 2 > b\n'
         'my context > iter 3 > c\n'
         'my context > multiple..\n'
         '  ..lines\n'
         'my context > generating\n'
         'my context > test.dat\n'
         'generate_test > test.dat\n'
         'context step=0 > foo\n'
         'context step=1 > bar\n'
         'same.dat\n'
         'dbg.dat\n'
         'dbg\n'
         'warn\n')
     self.assertEqual(captured.stderr, '')
예제 #8
0
파일: __main__.py 프로젝트: TheBB/SISO
def convert(ctx, verbosity, rich, infile, fmt, outfile, **kwargs):
    # Set up logging
    if rich:
        logger = RichOutputLog(sys.stdout)
    else:
        logger = log.TeeLog(
            log.FilterLog(log.StdoutLog(), maxlevel=log.proto.Level.user),
            log.FilterLog(log.StderrLog(), minlevel=log.proto.Level.warning),
        )
    log.current = log.FilterLog(logger,
                                minlevel=getattr(log.proto.Level, verbosity))

    # Print potential warnings
    if '--global' in sys.argv:
        log.warning(f"--global is deprecated; use --coords geocentric instead")
    if '--local' in sys.argv:
        log.warning(f"--local is deprecated; use --coords local instead")
    if '--geometry' in sys.argv or '-g' in sys.argv:
        log.warning(f"--geometry is deprecated; use --coords instead")
    if '--endianness' in sys.argv:
        log.warning(f"--endianness is deprecated; use --in-endianness instead")

    # Convert to pathlib
    infile = Path(infile)
    outfile = Path(outfile) if outfile else None

    # Determine filename of output
    if outfile and not fmt:
        fmt = outfile.suffix[1:].lower()
    elif not outfile:
        fmt = fmt or 'pvd'
        outfile = Path(infile.name).with_suffix(f'.{fmt}')

    # Handle default values of multi-valued options that should be
    # distinguished from empty, as well as comma splitting and other
    # transformations
    explicit = {
        click.core.ParameterSource.COMMANDLINE,
        click.core.ParameterSource.ENVIRONMENT
    }
    kwargs['input_coords'] = dict(kwargs['input_coords'])
    for k in ['field_filter', 'only_bases']:
        kwargs[k] = tuple(split_commas(kwargs[k]))
    if kwargs['no_fields']:
        kwargs['field_filter'] = []
    elif ctx.get_parameter_source('field_filter') not in explicit:
        kwargs['field_filter'] = None
    else:
        kwargs['field_filter'] = tuple(f.lower()
                                       for f in kwargs['field_filter'])
    if isinstance(kwargs['timestep_index'], int):
        n = kwargs['timestep_index']
        kwargs['timestep_slice'] = f'{n}:{n+1}:1'
        config.require(multiple_timesteps=False, reason="--time is set")

    # Remove meta-options
    for k in ['timestep_index', 'no_fields']:
        kwargs.pop(k)

    try:
        # The config can influence the choice of readers or writers,
        # so apply it first.  Since kwargs may include options that
        # are not explicity set by the user, we set the source to
        # Default, and later use the upgrade_source method.
        with config(source=ConfigSource.Default, **kwargs):
            for name in kwargs:
                if ctx.get_parameter_source(name) in explicit:
                    config.upgrade_source(name, ConfigSource.User)
            if not infile.exists():
                raise IOError(f"File or directory does not exist: {infile}")
            ReaderClass = Reader.find_applicable(infile)
            WriterClass = Writer.find_applicable(fmt)
            with ReaderClass(infile) as reader, WriterClass(outfile) as writer:
                reader.validate()
                writer.validate()
                pipeline(reader, writer)

    except Exception as e:
        if verbosity == 'debug':
            # In debug mode, allow exceptions to filter through in raw form
            traceback.print_exc()
        else:
            log.error(f"Error: {e}")
            log.error("More information may be available with --debug")
        sys.exit(1)
예제 #9
0
파일: cli.py 프로젝트: scdivi/nutils
def call(func, kwargs, scriptname, funcname=None):
    '''set up compute environment and call function'''

    outdir = config.outdir or os.path.join(
        os.path.expanduser(config.outrootdir), scriptname)

    with contextlib.ExitStack() as stack:

        stack.enter_context(
            cache.enable(os.path.join(outdir, config.cachedir)) if config.
            cache else cache.disable())
        stack.enter_context(matrix.backend(config.matrix))
        stack.enter_context(
            log.set(
                log.FilterLog(log.RichOutputLog()
                              if config.richoutput else log.StdoutLog(),
                              minlevel=5 - config.verbose)))
        if config.htmloutput:
            html = stack.enter_context(
                log.HtmlLog(
                    outdir,
                    title=scriptname,
                    htmltitle='<a href="http://www.nutils.org">{}</a> {}'.
                    format(SVGLOGO, scriptname),
                    favicon=FAVICON))
            uri = (config.outrooturi.rstrip('/') + '/' +
                   scriptname if config.outrooturi else pathlib.Path(
                       outdir).resolve().as_uri()) + '/' + html.filename
            if config.richoutput:
                t0 = time.perf_counter()
                bar = lambda running: '{0} [{1}] {2[0]}:{2[1]:02d}:{2[2]:02d}'.format(
                    uri, 'RUNNING'
                    if running else 'STOPPED', _hms(time.perf_counter() - t0))
                stack.enter_context(stickybar.activate(bar, update=1))
            else:
                log.info('opened log at', uri)
            html.write(
                '<ul style="list-style-position: inside; padding-left: 0px; margin-top: 0px;">{}</ul>'
                .format(''.join(
                    '<li>{}={} <span style="color: gray;">{}</span></li>'.
                    format(param.name, kwargs.get(param.name, param.default),
                           param.annotation)
                    for param in inspect.signature(func).parameters.values())),
                level=1,
                escape=False)
            stack.enter_context(log.add(html))
        stack.enter_context(warnings.via(log.warning))
        stack.callback(signal.signal, signal.SIGINT,
                       signal.signal(signal.SIGINT, _sigint_handler))

        log.info('nutils v{}'.format(_version()))
        log.info('start', time.ctime())
        try:
            func(**kwargs)
        except (KeyboardInterrupt, SystemExit, pdb.bdb.BdbQuit):
            log.error('killed by user')
            return 1
        except:
            log.error(traceback.format_exc())
            if config.pdb:
                print(
                    _mkbox('YOUR PROGRAM HAS DIED. The Python debugger',
                           'allows you to examine its post-mortem state',
                           'to figure out why this happened. Type "h"',
                           'for an overview of commands to get going.'))
                pdb.post_mortem()
            return 2
        else:
            log.info('finish', time.ctime())
            return 0
예제 #10
0
 def setUp(self):
     super().setUp()
     self.enter_context(
         treelog.set(
             treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
예제 #11
0
import doctest as _doctest, unittest, importlib, os, tempfile, pathlib, functools, warnings, subprocess, sys, treelog
import nutils.testing

_doctestlog = treelog.FilterLog(treelog.StdoutLog(), minlevel=1)


class DocTestCase(nutils.testing.ContextTestCase, _doctest.DocTestCase):
    def __init__(self, test, *, requires=None, **kwargs):
        self.__test = test
        self.__requires = tuple(requires) if requires else ()
        super().__init__(test, **kwargs)

    def setUpContext(self, stack):
        lines = self.__test.docstring.splitlines()
        indent = min((len(line) - len(line.lstrip())
                      for line in lines[1:] if line.strip()),
                     default=0)
        blank = True
        requires = list(self.__requires)
        for line in lines:
            if blank and line[indent:].startswith('.. requires:: '):
                requires.extend(name.strip()
                                for name in line[indent + 13:].split(','))
            blank = not line.strip()
        missing = tuple(filter(nutils.testing._not_has_module, requires))
        if missing:
            self.skipTest('missing module{}: {}'.format(
                's' if len(missing) > 1 else '', ','.join(missing)))

        if 'matplotlib' in requires:
            import matplotlib.testing
예제 #12
0
 def setUpContext(self, stack):
     stack.enter_context(
         treelog.set(
             treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
예제 #13
0
 def test_open_devnull_devnull(self):
     teelog = treelog.TeeLog(treelog.StdoutLog(), treelog.StdoutLog())
     with silent(), teelog.open('test',
                                'wb',
                                level=treelog.proto.Level.info) as f:
         self.assertEqual(f.name, os.devnull)