generated from Hazel/python-project
	feat: config
This commit is contained in:
		| @@ -1,6 +1,9 @@ | |||||||
| [project] | [project] | ||||||
| name = "update-applications" | name = "update-applications" | ||||||
| dependencies = [] | dependencies = [ | ||||||
|  |     "toml~=0.10.2", | ||||||
|  |     "rich~=13.7.1", | ||||||
|  | ] | ||||||
| dynamic = ["version"] | dynamic = ["version"] | ||||||
| authors = [] | authors = [] | ||||||
| description = "If an application like hopscotch is not found in repositories, you can use this to still keep it updated, by either adding a download link for the newest version or a git repository with releases." | description = "If an application like hopscotch is not found in repositories, you can use this to still keep it updated, by either adding a download link for the newest version or a git repository with releases." | ||||||
|   | |||||||
| @@ -1,5 +1,23 @@ | |||||||
|  | import argparse | ||||||
|  | import datetime | ||||||
|  | import subprocess | ||||||
|  | from pathlib import Path | ||||||
|  |  | ||||||
| from .__about__ import __version__ | from .__about__ import __version__ | ||||||
|  | from .utils import (CONFIG_PATH, SETTINGS, SETTINGS_EXISTED, console, | ||||||
|  |                     edit_file, load_config, save_config) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def configure(): | ||||||
|  |     save_config() | ||||||
|  |     edit_file(CONFIG_PATH) | ||||||
|  |     load_config() | ||||||
|  |  | ||||||
|  |  | ||||||
| def cli(): | def cli(): | ||||||
|     print("Hello, world!") |     parser = argparse.ArgumentParser() | ||||||
|  |     parser.add_argument("--config", action=argparse.BooleanOptionalAction, help="Configure the settings") | ||||||
|  |     args = parser.parse_args() | ||||||
|  |      | ||||||
|  |     if not SETTINGS_EXISTED or args.config: | ||||||
|  |         configure() | ||||||
|   | |||||||
							
								
								
									
										62
									
								
								update_applications/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								update_applications/utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | |||||||
|  | import atexit | ||||||
|  | import os | ||||||
|  | import shutil | ||||||
|  | import signal | ||||||
|  | import subprocess | ||||||
|  | from pathlib import Path | ||||||
|  |  | ||||||
|  | import toml | ||||||
|  | from rich.console import Console | ||||||
|  |  | ||||||
|  | from .__about__ import __name__ | ||||||
|  |  | ||||||
|  | console: Console = Console() | ||||||
|  |  | ||||||
|  | SETTINGS_EXISTED = False | ||||||
|  | CONFIG_PATH = Path.home() / ".config" / __name__ / "config.toml" | ||||||
|  | SETTINGS = { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | def load_config(): | ||||||
|  |     global SETTINGS_EXISTED | ||||||
|  |  | ||||||
|  |     if CONFIG_PATH.exists(): | ||||||
|  |         SETTINGS_EXISTED = True | ||||||
|  |         with CONFIG_PATH.open("r", encoding="utf-8") as f: | ||||||
|  |             config = toml.load(f) | ||||||
|  |             SETTINGS.update(config) | ||||||
|  |  | ||||||
|  | load_config() | ||||||
|  |  | ||||||
|  | def save_config(): | ||||||
|  |     CONFIG_PATH.parent.mkdir(exist_ok=True, parents=True) | ||||||
|  |  | ||||||
|  |     with CONFIG_PATH.open("w", encoding="utf-8") as f: | ||||||
|  |         toml.dump(SETTINGS, f) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | atexit.register(save_config) | ||||||
|  | signal.signal(signal.SIGTERM, save_config) | ||||||
|  | signal.signal(signal.SIGINT, save_config) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def edit_file(file_name: Path): | ||||||
|  |     if hasattr(os, "startfile"): | ||||||
|  |         os.startfile(file_name) | ||||||
|  |     elif shutil.which("xdg-open"): | ||||||
|  |         subprocess.call(["xdg-open", file_name]) | ||||||
|  |     elif "EDITOR" in os.environ: | ||||||
|  |         subprocess.call([os.environ["EDITOR"], file_name]) | ||||||
|  |  | ||||||
|  |     # wait till the last changed is different or ctrl+c is pressed | ||||||
|  |     console.print("Waiting till either the file is changed, or ctrl+c is pressed") | ||||||
|  |     try: | ||||||
|  |         last_change = os.path.getmtime(file_name) | ||||||
|  |         while True: | ||||||
|  |             if os.path.getmtime(file_name) != last_change: | ||||||
|  |                 break | ||||||
|  |  | ||||||
|  |             time.sleep(0.1) | ||||||
|  |     except KeyboardInterrupt: | ||||||
|  |         print("No changes were saved") | ||||||
|  |          | ||||||
		Reference in New Issue
	
	Block a user