|
22 | 22 | import time |
23 | 23 | import urllib.request |
24 | 24 | import urllib.error |
| 25 | +import configparser |
| 26 | +try: |
| 27 | + import argcomplete |
| 28 | +except ImportError: |
| 29 | + argcomplete = None |
25 | 30 | from guga import __version__ |
26 | 31 |
|
27 | 32 |
|
@@ -124,9 +129,41 @@ def run_command(cmd_args, port, silent, title): |
124 | 129 | sys.exit(exit_code) |
125 | 130 |
|
126 | 131 |
|
| 132 | +# ── Configuration ───────────────────────────────────────────────────────────── |
| 133 | + |
| 134 | +def load_config(): |
| 135 | + """Loads default values from ~/.config/guga/config""" |
| 136 | + config = configparser.ConfigParser() |
| 137 | + config_path = os.path.expanduser("~/.config/guga/config") |
| 138 | + |
| 139 | + defaults = { |
| 140 | + "title": None, |
| 141 | + "port": 6769, |
| 142 | + "silent": False |
| 143 | + } |
| 144 | + |
| 145 | + if os.path.exists(config_path): |
| 146 | + try: |
| 147 | + config.read(config_path) |
| 148 | + if "default" in config: |
| 149 | + section = config["default"] |
| 150 | + if "title" in section: |
| 151 | + defaults["title"] = section["title"] |
| 152 | + if "port" in section: |
| 153 | + defaults["port"] = int(section["port"]) |
| 154 | + if "silent" in section: |
| 155 | + defaults["silent"] = section.getboolean("silent") |
| 156 | + except Exception: |
| 157 | + pass # Ignore invalid config files |
| 158 | + |
| 159 | + return defaults |
| 160 | + |
| 161 | + |
127 | 162 | # ── Entry point ─────────────────────────────────────────────────────────────── |
128 | 163 |
|
129 | 164 | def parse_args(): |
| 165 | + defaults = load_config() |
| 166 | + |
130 | 167 | parser = argparse.ArgumentParser( |
131 | 168 | prog="guga", |
132 | 169 | description="Send notifications to Android, or watch a command and notify on completion.", |
@@ -236,22 +273,27 @@ def parse_args(): |
236 | 273 | parser.add_argument( |
237 | 274 | "--server", |
238 | 275 | type=int, |
239 | | - default=6769, |
| 276 | + default=defaults["port"], |
240 | 277 | metavar="PORT", |
241 | | - help="GuGa server port (default: 6769).", |
| 278 | + help=f"GuGa server port (default: {defaults['port']}).", |
242 | 279 | ) |
243 | 280 | parser.add_argument( |
244 | 281 | "--silent", |
245 | 282 | action="store_true", |
| 283 | + default=defaults["silent"], |
246 | 284 | help="Suppress guga's own output.", |
247 | 285 | ) |
248 | 286 | parser.add_argument( |
249 | | - "--title", |
250 | | - default=None, |
| 287 | + "-f", "--from", "--title", |
| 288 | + dest="title", |
| 289 | + default=defaults["title"], |
251 | 290 | metavar="LABEL", |
252 | 291 | help='Label shown in the notification, e.g. "GPU Server".', |
253 | 292 | ) |
254 | 293 |
|
| 294 | + if argcomplete: |
| 295 | + argcomplete.autocomplete(parser) |
| 296 | + |
255 | 297 | return parser.parse_args() |
256 | 298 |
|
257 | 299 |
|
|
0 commit comments