Skip to content

Commit

Permalink
Merge pull request #160 from cgat-developers/fix-missing-header
Browse files Browse the repository at this point in the history
Fix missing header
  • Loading branch information
AndreasHeger authored May 18, 2022
2 parents a0cd954 + dc12961 commit 9198312
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 150 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ _test_commandline.yaml
.mr.developer.cfg
.project
.pydevproject


# sample workflow
means.txt
sample*
242 changes: 93 additions & 149 deletions cgatcore/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,88 @@ def get_args():
return global_args


def _setup_seed(options):
if options.random_seed is not None:
random.seed(options.random_seed)


def _setup_pipes(options, add_pipe_options):
if add_pipe_options:
if options.stdout != sys.stdout:
options.stdout = open_file(options.stdout, "w")
if options.stderr != sys.stderr:
if options.stderr == "stderr":
options.stderr = options.stderr
else:
options.stderr = open_file(
options.stderr, "w")
if options.stdlog != sys.stdout:
options.stdlog = open_file(options.stdlog, "a")
if options.stdin != sys.stdin:
options.stdin = open_file(options.stdin, "r")
else:
options.stderr = sys.stderr
options.stdout = sys.stdout
options.stdlog = sys.stdout
options.stdin = sys.stdin


def _setup_logging(options, logger_callback):
# reset log_config_filename if logging.yml does not exist
if options.log_config_filename == "logging.yml" and not os.path.exists(
options.log_config_filename):
options.log_config_filename = None

if options.log_config_filename:
if os.path.exists(options.log_config_filename):
# configure logging from filename
with open(options.log_config_filename) as inf:
dict_yaml = yaml.safe_load(inf)
logging.config.dictConfig(dict_yaml)
else:
raise OSError("file {} with logging configuration does not exist".format(
options.log_config_filename))
else:
# configure logging from command line options
# map from 0-10 to logging scale
# 0: quiet
# 1: little verbositiy
# >1: increased verbosity
if options.loglevel == 0:
lvl = logging.ERROR
elif options.loglevel == 1:
lvl = logging.INFO
else:
lvl = logging.DEBUG

if options.stdout == options.stdlog:
logformat = '# %(asctime)s %(levelname)s %(message)s'
else:
logformat = '%(asctime)s %(levelname)s %(message)s'

logging.basicConfig(
level=lvl,
format=logformat,
stream=options.stdlog)

# set up multi-line logging
# Note that .handlers is not part of the API, might change
# Solution is to configure handlers explicitely.
for handler in logging.getLogger().handlers:
handler.setFormatter(MultiLineFormatter(logformat))

if logger_callback:
logger = logger_callback(options)
else:
logger = logging.getLogger("cgatcore")
return logger


def _setup_tracing(options):
if options.tracing == "function":
sys.settrace(trace_calls)


def start(parser=None,
argv=None,
quiet=False,
Expand Down Expand Up @@ -979,80 +1061,7 @@ def start(parser=None,
if not no_parsing:
(global_options, global_args) = parser.parse_args(argv[1:])

if global_options.random_seed is not None:
random.seed(global_options.random_seed)

if add_pipe_options:
if global_options.stdout != sys.stdout:
global_options.stdout = open_file(global_options.stdout, "w")
if global_options.stderr != sys.stderr:
if global_options.stderr == "stderr":
global_options.stderr = global_options.stderr
else:
global_options.stderr = open_file(
global_options.stderr, "w")
if global_options.stdlog != sys.stdout:
global_options.stdlog = open_file(global_options.stdlog, "a")
if global_options.stdin != sys.stdin:
global_options.stdin = open_file(global_options.stdin, "r")
else:
global_options.stderr = sys.stderr
global_options.stdout = sys.stdout
global_options.stdlog = sys.stdout
global_options.stdin = sys.stdin

# reset log_config_filename if logging.yml does not exist
if global_options.log_config_filename == "logging.yml" and not os.path.exists(
global_options.log_config_filename):
global_options.log_config_filename = None

if global_options.log_config_filename:
if os.path.exists(global_options.log_config_filename):
# configure logging from filename
with open(global_options.log_config_filename) as inf:
dict_yaml = yaml.safe_load(inf)
logging.config.dictConfig(dict_yaml)
else:
raise OSError("file {} with logging configuration does not exist".format(
global_options.log_config_filename))
else:
# configure logging from command line options
# map from 0-10 to logging scale
# 0: quiet
# 1: little verbositiy
# >1: increased verbosity
if global_options.loglevel == 0:
lvl = logging.ERROR
elif global_options.loglevel == 1:
lvl = logging.INFO
else:
lvl = logging.DEBUG

if global_options.stdout == global_options.stdlog:
logformat = '# %(asctime)s %(levelname)s %(message)s'
else:
logformat = '%(asctime)s %(levelname)s %(message)s'

logging.basicConfig(
level=lvl,
format=logformat,
stream=global_options.stdlog)

# set up multi-line logging
# Note that .handlers is not part of the API, might change
# Solution is to configure handlers explicitely.
for handler in logging.getLogger().handlers:
handler.setFormatter(MultiLineFormatter(logformat))

if logger_callback:
logger = logger_callback(global_options)
else:
logger = logging.getLogger("cgatcore")

if global_options.tracing == "function":
sys.settrace(trace_calls)

return global_options, global_args
opt1, opt2 = global_options, global_args

# Argparse options
else:
Expand Down Expand Up @@ -1209,85 +1218,20 @@ def start(parser=None,
if not no_parsing:
global_args, unknown = parser.parse_known_args(argv[1:])

if global_args.random_seed is not None:
random.seed(global_args.random_seed)

if add_pipe_options:
if global_args.stdout != sys.stdout:
global_args.stdout = open_file(global_args.stdout, "w")
if global_args.stderr != sys.stderr:
if global_args.stderr == "stderr":
global_args.stderr = global_args.stderr
else:
global_args.stderr = open_file(global_args.stderr, "w")
if global_args.stdlog != sys.stdout:
global_args.stdlog = open_file(global_args.stdlog, "a")
if global_args.stdin != sys.stdin:
global_args.stdin = open_file(global_args.stdin, "r")
else:
global_args.stderr = sys.stderr
global_args.stdout = sys.stdout
global_args.stdlog = sys.stdout
global_args.stdin = sys.stdin

# reset log_config_filename if logging.yml does not exist
if global_args.log_config_filename == "logging.yml" and not os.path.exists(
global_args.log_config_filename):
global_args.log_config_filename = None

if global_args.log_config_filename:
if os.path.exists(global_args.log_config_filename):
# configure logging from filename
with open(global_args.log_config_filename) as inf:
dict_yaml = yaml.safe_load(inf)
logging.config.dictConfig(dict_yaml)
else:
raise OSError("file {} with logging configuration does not exist".format(
global_args.log_config_filename))
else:
# configure logging from command line options
# map from 0-10 to logging scale
# 0: quiet
# 1: little verbositiy
# >1: increased verbosity
if global_args.loglevel == 0:
lvl = logging.ERROR
elif global_args.loglevel == 1:
lvl = logging.INFO
else:
lvl = logging.DEBUG

if global_args.stdout == global_args.stdlog:
logformat = '# %(asctime)s %(levelname)s %(message)s'
else:
logformat = '%(asctime)s %(levelname)s %(message)s'

logging.basicConfig(
level=lvl,
format=logformat,
stream=global_args.stdlog)

# set up multi-line logging
# Note that .handlers is not part of the API, might change
# Solution is to configure handlers explicitely.
for handler in logging.getLogger().handlers:
handler.setFormatter(MultiLineFormatter(logformat))

if logger_callback:
logger = logger_callback(global_args)
if unknowns:
opt1, opt2 = global_args, unknown
else:
logger = logging.getLogger("cgatcore")
opt1, opt2 = global_args, None

logger.info(get_header())
logger.info(get_params(global_args))
_setup_seed(opt1)
_setup_pipes(opt1, add_pipe_options)
_setup_tracing(opt1)
logger = _setup_logging(opt1, logger_callback)

if global_args.tracing == "function":
sys.settrace(trace_calls)
logger.info(get_header())
logger.info(get_params(opt1))

if unknowns:
return global_args, unknown
else:
return global_args
return opt1, opt2


def stop(logger=None):
Expand Down
2 changes: 1 addition & 1 deletion cgatcore/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.11"
__version__ = "0.6.12"
2 changes: 2 additions & 0 deletions conda_requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ ruffus
pep8
yaml
apsw
pyyaml
sqlalchemy
19 changes: 19 additions & 0 deletions tests/test_experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cgatcore.experiment as E


def test_start_and_stop_are_logged_with_optparse():
statement = (
f"python -c 'import cgatcore.experiment as E; options, args = E.start(parser=E.OptionParser()); E.stop()'")

stdout = E.run(statement, return_stdout=True)
assert "job started" in stdout
assert "job finished" in stdout


def test_start_and_stop_are_logged_with_argparse():
statement = (
f"python -c 'import cgatcore.experiment as E; options, args = E.start(parser=E.ArgumentParser()); E.stop()'")

stdout = E.run(statement, return_stdout=True)
assert "job started" in stdout
assert "job finished" in stdout

0 comments on commit 9198312

Please sign in to comment.