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)
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)
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())
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))
def test_open_datalog_datalog_samedir(self): with tempfile.TemporaryDirectory() as tmpdir: teelog = treelog.TeeLog(treelog.DataLog(tmpdir), treelog.DataLog(tmpdir)) with teelog.open('test', 'wb', level=treelog.proto.Level.info) as f: f.write(b'test') with open(os.path.join(tmpdir, 'test'), 'rb') as f: self.assertEqual(f.read(), b'test') with open(os.path.join(tmpdir, 'test-1'), 'rb') as f: self.assertEqual(f.read(), b'test')
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')
def test_open_wo_wo(self): with tempfile.TemporaryDirectory( ) as tmpdir1, tempfile.TemporaryDirectory() as tmpdir2: filenos = set() teelog = treelog.TeeLog(TeeLogTestLog(tmpdir1, False, filenos), TeeLogTestLog(tmpdir2, False, filenos)) with silent(), teelog.open('test', 'wb', level=treelog.proto.Level.info) as f: self.assertNotIn(f.fileno(), filenos) f.write(b'test') with open(os.path.join(tmpdir1, 'test'), 'rb') as f: self.assertEqual(f.read(), b'test') with open(os.path.join(tmpdir2, 'test'), 'rb') as f: self.assertEqual(f.read(), b'test')
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)
def setUp(self): super().setUp() self.enter_context( treelog.set( treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
def setUpContext(self, stack): stack.enter_context( treelog.set( treelog.TeeLog(treelog.StdoutLog(), treelog.LoggingLog())))
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)
def output_tester(self): with DataLog.output_tester(self) as datalog, \ RecordLog.output_tester(self) as recordlog, \ RichOutputLog.output_tester(self) as richoutputlog: yield treelog.TeeLog(richoutputlog, treelog.TeeLog(datalog, recordlog))