Compare commits
	
		
			2 Commits
		
	
	
		
			d1fe9d55d5
			...
			a8a1f425cc
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | a8a1f425cc | ||
|  | be95c352d6 | 
| @@ -3,8 +3,7 @@ import sys | ||||
| from typing import Optional | ||||
| import json | ||||
|  | ||||
| from .responses import COMPILED_CONFIG_FILE | ||||
| from .static import colors | ||||
| from .static import colors, get_compiled_config_file | ||||
|  | ||||
|  | ||||
| def get_response_from_situation(situation: str, colorize: Optional[bool] = None): | ||||
| @@ -12,7 +11,7 @@ def get_response_from_situation(situation: str, colorize: Optional[bool] = None) | ||||
|         colorize = sys.stdout.isatty() | ||||
|  | ||||
|     # get message | ||||
|     config = json.loads(COMPILED_CONFIG_FILE.read_text()) | ||||
|     config = json.loads(get_compiled_config_file().read_text()) | ||||
|     existing_moods = list(config["moods"].keys()) | ||||
|     template_options = config["moods"][random.choice(existing_moods)][situation] | ||||
|     template: str = random.choice(template_options) | ||||
|   | ||||
| @@ -3,14 +3,14 @@ from pathlib import Path | ||||
| import stat | ||||
| import subprocess | ||||
| import logging | ||||
|  | ||||
| import toml | ||||
| import json | ||||
|  | ||||
| from . import get_response | ||||
| from .responses import compile_config | ||||
| from .static import IS_VENV, VENV_DIRECTORY, CONFIG_DIRECTORY, COMPILED_CONFIG_FILE_NAME | ||||
|  | ||||
| logging.basicConfig( | ||||
|     format=' %(message)s', | ||||
|     format='%(message)s', | ||||
|     force=True, | ||||
| ) | ||||
|  | ||||
| @@ -82,6 +82,13 @@ PIP_HOOK = """# GENERATED BY MOMMY | ||||
|     sys.exit(code)""" | ||||
|  | ||||
|  | ||||
| def assert_venv(): | ||||
|     if not IS_VENV: | ||||
|         mommy_logger.error("mommy doesn't run in a virtual environment~") | ||||
|         serious_logger.error("this has to run in a virtual environment") | ||||
|         exit(1) | ||||
|  | ||||
|  | ||||
| def wrap_interpreter(path: Path): | ||||
|     mommy_logger.info("mommy found a symlink to an interpreter~ %s", str(path)) | ||||
|     serious_logger.info("interpreter symlink found at %s", str(path)) | ||||
| @@ -130,13 +137,23 @@ def install_pip_hook(path: Path): | ||||
|  | ||||
|  | ||||
| def mommify_venv(): | ||||
|     compile_config() | ||||
|     assert_venv() | ||||
|  | ||||
|     v = ".venv" | ||||
|     if len(sys.argv) > 1: | ||||
|         v = sys.argv[1] | ||||
|     compile_local = False | ||||
|     compiled_base_dir = VENV_DIRECTORY if compile_local else CONFIG_DIRECTORY | ||||
|     compiled_config_file = compiled_base_dir / COMPILED_CONFIG_FILE_NAME | ||||
|  | ||||
|     bin_path = Path(v, "bin") | ||||
|     compiled = compile_config() | ||||
|     mommy_logger.info("mommy writes its moods in %s", compiled_config_file) | ||||
|     serious_logger.info("writing compiled config file to %s", compiled_config_file) | ||||
|     compiled_base_dir.mkdir(parents=True, exist_ok=True) | ||||
|     with compiled_config_file.open("w") as f: | ||||
|         json.dump(compiled, f, indent=4) | ||||
|  | ||||
|     serious_logger.info("") | ||||
|     mommy_logger.info("") | ||||
|  | ||||
|     bin_path = VENV_DIRECTORY / "bin" | ||||
|     bin_path = bin_path.resolve() | ||||
|  | ||||
|     mommy_logger.info("mommy looks in %s to mess your system up~ <33", str(bin_path)) | ||||
| @@ -148,10 +165,10 @@ def mommify_venv(): | ||||
|         if path.is_symlink(): | ||||
|             # could be python interpreter | ||||
|             # check for both just to be more expressive | ||||
|             if name.startswith("inner_") or not name.startswith("python"): | ||||
|             if name.startswith("inner_"): | ||||
|                 continue | ||||
|              | ||||
|             if subprocess.run([str(path), '-c', '"exit()"']) != 0: | ||||
|             if subprocess.run([str(path), '-c', '"exit(0)"']).returncode != 0: | ||||
|                 continue | ||||
|  | ||||
|             wrap_interpreter(path) | ||||
| @@ -162,6 +179,3 @@ def mommify_venv(): | ||||
|                 continue | ||||
|  | ||||
|             install_pip_hook(path) | ||||
|  | ||||
|         serious_logger.info("") | ||||
|         mommy_logger.info("") | ||||
|   | ||||
| @@ -9,8 +9,9 @@ import requests | ||||
|  | ||||
| from .static import get_config_file | ||||
|  | ||||
| mommy_logger = logging.getLogger("mommy") | ||||
| serious_logger = logging.getLogger("serious") | ||||
|  | ||||
| logger = logging.Logger(__name__) | ||||
| PREFIX = "MOMMY" | ||||
|  | ||||
| RESPONSES_URL = "https://raw.githubusercontent.com/diamondburned/go-mommy/refs/heads/main/responses.json" | ||||
| @@ -68,9 +69,8 @@ def compile_config(disable_requests: bool = False) -> dict: | ||||
|     data = json.loads(RESPONSES_FILE.read_text()) | ||||
|      | ||||
|     if not disable_requests: | ||||
|         print("mommy downloads newest responses for her girl~") | ||||
|         print(RESPONSES_URL) | ||||
|         print() | ||||
|         mommy_logger.info("mommy downloads newest responses for her girl~ %s", RESPONSES_URL) | ||||
|         serious_logger.info("downloading cargo mommy responses: %s", RESPONSES_URL) | ||||
|         r = requests.get(RESPONSES_URL) | ||||
|         data = r.json() | ||||
|  | ||||
| @@ -103,7 +103,18 @@ def compile_config(disable_requests: bool = False) -> dict: | ||||
|     for mood in config["mood"]: | ||||
|         if mood not in mood_definitions: | ||||
|             supported_moods_str = ", ".join(mood_definitions.keys()) | ||||
|             print(f"{random.choice(config['role'])} doesn't know how to feel {mood}... {random.choice(config['pronoun'])} moods are {supported_moods_str}") | ||||
|             mommy_logger.error( | ||||
|                 "%s doesn't know how to feel %s... %s moods are %s", | ||||
|                 random.choice(config['role']), | ||||
|                 mood, | ||||
|                 random.choice(config['pronoun']), | ||||
|                 supported_moods_str, | ||||
|             ) | ||||
|             serious_logger.error( | ||||
|                 "mood '%s' doesn't exist. moods are %s", | ||||
|                 mood, | ||||
|                 supported_moods_str, | ||||
|             ) | ||||
|             exit(1) | ||||
|  | ||||
|     # compile | ||||
|   | ||||
| @@ -4,6 +4,7 @@ from pathlib import Path | ||||
| import os | ||||
| import logging | ||||
| from typing import Optional | ||||
| import sys | ||||
|  | ||||
|  | ||||
| logger = logging.Logger(__name__) | ||||
| @@ -72,12 +73,36 @@ def _get_xdg_config_dir() -> Path: | ||||
| CONFIG_DIRECTORY = _get_xdg_config_dir() / "mommy" | ||||
| COMPILED_CONFIG_FILE_NAME = "compiled-mommy.json" | ||||
|  | ||||
| IS_VENV = sys.prefix != sys.base_prefix | ||||
| VENV_DIRECTORY = Path(sys.prefix) | ||||
|  | ||||
| def get_config_file() -> Optional[Path]: | ||||
|     config_files = [ | ||||
|     config_files = [] | ||||
|     if IS_VENV: | ||||
|         config_files.extend([ | ||||
|             VENV_DIRECTORY / "python-mommy.toml", | ||||
|             VENV_DIRECTORY / "mommy.toml", | ||||
|         ]) | ||||
|     config_files.extend([ | ||||
|         CONFIG_DIRECTORY / "python-mommy.toml", | ||||
|         CONFIG_DIRECTORY / "mommy.toml", | ||||
|     ] | ||||
|     ]) | ||||
|  | ||||
|     for f in config_files: | ||||
|         if f.exists(): | ||||
|             return f | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| def get_compiled_config_file() -> Path: | ||||
|     compiled_config_files = [ | ||||
|         VENV_DIRECTORY / "compiled-mommy.json", | ||||
|         CONFIG_DIRECTORY / "compiled-mommy.json", | ||||
|     ] | ||||
|  | ||||
|     for f in compiled_config_files: | ||||
|         if f.exists(): | ||||
|             return f | ||||
|          | ||||
|     raise Exception("couldn't find compiled config file") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user