comparison env/lib/python3.9/site-packages/galaxy/util/script.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Utilities for Galaxy scripts
2 """
3 import argparse
4 import logging
5 import os
6 import sys
7
8 from galaxy.util.properties import find_config_file, load_app_properties
9
10 DESCRIPTION = None
11 ACTIONS = None
12 ARGUMENTS = None
13 DEFAULT_ACTION = None
14
15 ARG_HELP_CONFIG_FILE = """
16 Galaxy config file (defaults to $GALAXY_ROOT/config/galaxy.yml if that file exists
17 or else to ./config/galaxy.ini if that exists). If this isn't set on the
18 command line it can be set with the environment variable GALAXY_CONFIG_FILE.
19 """
20
21 # ARG_HELP_CONFIG_SECTION = """
22 # Section containing application configuration in the target config file specified with
23 # -c/--config-file. This defaults to 'galaxy' for YAML/JSON configuration files and 'main'
24 # with 'app:' prepended for INI. If this isn't set on the command line it can be set with
25 # the environment variable GALAXY_CONFIG_SECTION.
26 # """
27
28
29 def main_factory(description=None, actions=None, arguments=None, default_action=None):
30 global DESCRIPTION, ACTIONS, ARGUMENTS, DEFAULT_ACTION
31 DESCRIPTION = description
32 ACTIONS = actions or {}
33 ARGUMENTS = arguments or []
34 DEFAULT_ACTION = default_action
35 return main
36
37
38 def main(argv=None):
39 """Entry point for conversion process."""
40 if argv is None:
41 argv = sys.argv[1:]
42 args = _arg_parser().parse_args(argv)
43 kwargs = app_properties_from_args(args)
44 action = args.action
45 action_func = ACTIONS[action]
46 action_func(args, kwargs)
47
48
49 def app_properties_from_args(args, legacy_config_override=None, app=None):
50 config_file = config_file_from_args(args, legacy_config_override=legacy_config_override, app=app)
51 config_section = getattr(args, "config_section", None)
52 app_properties = load_app_properties(config_file=config_file, config_section=config_section)
53 return app_properties
54
55
56 def config_file_from_args(args, legacy_config_override=None, app=None):
57 app = app or getattr(args, "app", "galaxy")
58 config_file = legacy_config_override or args.config_file or find_config_file(app)
59 return config_file
60
61
62 def populate_config_args(parser):
63 # config and config-file respected because we have used different arguments at different
64 # time for scripts.
65
66 # Options (e.g. option_name) not found in this file can have their defaults overridden
67 # set setting GALAXY_CONFIG_OPTION_NAME where OPTION_NAME is option_name converted to upper case.
68 # Options specified in that file can be overridden for this program set setting
69 # GALAXY_CONFIG_OVERRIDE_OPTION_NAME to a new value.
70 parser.add_argument("-c", "--config-file", "--config",
71 default=os.environ.get('GALAXY_CONFIG_FILE', None),
72 help=ARG_HELP_CONFIG_FILE)
73 parser.add_argument("--config-section",
74 default=os.environ.get('GALAXY_CONFIG_SECTION', None),
75 help=argparse.SUPPRESS) # See ARG_HELP_CONFIG_SECTION comment above for unsuppressed details.
76
77
78 def _arg_parser():
79 parser = argparse.ArgumentParser(description=DESCRIPTION)
80 parser.add_argument('action', metavar='ACTION', type=str,
81 choices=list(ACTIONS.keys()),
82 default=DEFAULT_ACTION,
83 nargs='?' if DEFAULT_ACTION is not None else None,
84 help='action to perform')
85 populate_config_args(parser)
86 parser.add_argument("--app",
87 default=os.environ.get('GALAXY_APP', 'galaxy'))
88 for argument in ARGUMENTS:
89 parser.add_argument(*argument[0], **argument[1])
90 return parser
91
92
93 def set_log_handler(filename=None, stream=None):
94 if filename:
95 handler = logging.FileHandler(filename)
96 else:
97 handler = logging.StreamHandler(stream=stream)
98 return handler