|
3 | 3 | import sys |
4 | 4 |
|
5 | 5 |
|
6 | | -def get_config(sources=("file", "env", "args"), env_prefix="MRQ_", defaults=None): |
| 6 | +def get_config(sources=("file", "env", "args"), env_prefix="MRQ_"): |
7 | 7 |
|
8 | 8 | parser = argparse.ArgumentParser(description='Starts an RQ worker') |
9 | 9 |
|
@@ -89,46 +89,53 @@ def get_config(sources=("file", "env", "args"), env_prefix="MRQ_", defaults=None |
89 | 89 | parser.add_argument('queues', nargs='*', default=["default"], |
90 | 90 | help='The queues to listen on (default: \'default\')') |
91 | 91 |
|
92 | | - if "args" in sources: |
93 | | - from_args = parser.parse_args() |
94 | | - else: |
95 | | - from_args = parser.parse_args([]) |
96 | | - |
97 | | - # Get defaults |
98 | | - merged_config = from_args.__dict__ |
99 | | - if defaults is not None: |
100 | | - merged_config.update(defaults) |
101 | | - |
102 | | - # If a mrq-config.py file is in the current directory, use it! |
103 | | - default_config_file = os.path.join(os.getcwd(), "mrq-config.py") |
104 | | - |
105 | | - if merged_config["config"] is None and os.path.isfile(default_config_file): |
106 | | - # print "Using config file at %s" % default_config_file |
107 | | - merged_config["config"] = default_config_file |
108 | | - |
109 | | - config_module = None |
110 | | - if "file" in sources and merged_config["config"]: |
111 | | - sys.path.append(os.path.dirname(merged_config["config"])) |
112 | | - config_module = __import__(os.path.basename(merged_config["config"].replace(".py", ""))) |
113 | | - sys.path.pop(-1) |
114 | | - merged_config.update({k.lower(): v for k, v in config_module.__dict__.iteritems() if k[0].isupper()}) |
| 92 | + default_config = parser.parse_args([]).__dict__ |
115 | 93 |
|
116 | 94 | # Keys that can't be passed from the command line |
117 | | - merged_config["tasks"] = {} |
118 | | - merged_config["scheduled_tasks"] = {} |
| 95 | + default_config["tasks"] = {} |
| 96 | + default_config["scheduled_tasks"] = {} |
| 97 | + |
| 98 | + # Only keep values different from config, actually passed on the command line |
| 99 | + from_args = {} |
| 100 | + if "args" in sources: |
| 101 | + for k, v in parser.parse_args().__dict__.iteritems(): |
| 102 | + if default_config[k] != v: |
| 103 | + from_args[k] = v |
119 | 104 |
|
| 105 | + # If we were given another config file, use it |
| 106 | + if from_args.get("config"): |
| 107 | + config_file = from_args.get("config") |
| 108 | + # If a mrq-config.py file is in the current directory, use it! |
| 109 | + elif os.path.isfile(os.path.join(os.getcwd(), "mrq-config.py")): |
| 110 | + config_file = os.path.join(os.getcwd(), "mrq-config.py") |
| 111 | + else: |
| 112 | + config_file = None |
| 113 | + |
| 114 | + from_file = {} |
| 115 | + if config_file and "file" in sources: |
| 116 | + sys.path.insert(0, os.path.dirname(config_file)) |
| 117 | + config_module = __import__(os.path.basename(config_file.replace(".py", ""))) |
| 118 | + sys.path.pop(0) |
| 119 | + for k, v in config_module.__dict__.iteritems(): |
| 120 | + |
| 121 | + # We only keep variables starting with an uppercase character. |
| 122 | + if k[0].isupper(): |
| 123 | + default_config[k.lower()] = v |
| 124 | + if k.lower() not in default_config: |
| 125 | + default_config[k.lower()] = v |
| 126 | + |
| 127 | + # Merge the config in the order given by the user |
| 128 | + merged_config = default_config |
120 | 129 | for part in sources: |
121 | 130 | for name, arg_value in merged_config.iteritems(): |
122 | 131 |
|
123 | | - value = None |
124 | 132 | if part == "env": |
125 | 133 | value = os.environ.get(env_prefix + name.upper()) |
126 | | - elif part == "args": |
127 | | - value = arg_value |
128 | | - elif part == "file": |
129 | | - value = getattr(config_module, name.upper(), None) |
130 | | - |
131 | | - if value is not None: |
132 | | - merged_config[name] = value |
| 134 | + if value: |
| 135 | + merged_config[name] = value |
| 136 | + elif part == "args" and name in from_args: |
| 137 | + merged_config[name] = from_args[name] |
| 138 | + elif part == "file" and name in from_file: |
| 139 | + merged_config[name] = from_file[name] |
133 | 140 |
|
134 | 141 | return merged_config |
0 commit comments