diff --git a/python_mommy_venv/__init__.py b/python_mommy_venv/__init__.py index 9f49eea..641a687 100644 --- a/python_mommy_venv/__init__.py +++ b/python_mommy_venv/__init__.py @@ -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) diff --git a/python_mommy_venv/__main__.py b/python_mommy_venv/__main__.py index b523996..7f49976 100644 --- a/python_mommy_venv/__main__.py +++ b/python_mommy_venv/__main__.py @@ -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, ) @@ -130,13 +130,21 @@ def install_pip_hook(path: Path): def mommify_venv(): - compile_config() + compile_local = False + compiled_base_dir = VENV_DIRECTORY if compile_local else CONFIG_DIRECTORY + compiled_config_file = compiled_base_dir / COMPILED_CONFIG_FILE_NAME - v = ".venv" - if len(sys.argv) > 1: - v = sys.argv[1] + 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) - bin_path = Path(v, "bin") + 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 +156,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 +170,3 @@ def mommify_venv(): continue install_pip_hook(path) - - serious_logger.info("") - mommy_logger.info("") diff --git a/python_mommy_venv/responses.py b/python_mommy_venv/responses.py index 571d295..1791547 100644 --- a/python_mommy_venv/responses.py +++ b/python_mommy_venv/responses.py @@ -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 diff --git a/python_mommy_venv/static.py b/python_mommy_venv/static.py index 8e523a5..c265f66 100644 --- a/python_mommy_venv/static.py +++ b/python_mommy_venv/static.py @@ -4,6 +4,7 @@ from pathlib import Path import os import logging from typing import Optional +import sys logger = logging.Logger(__name__) @@ -81,3 +82,19 @@ def get_config_file() -> Optional[Path]: for f in config_files: if f.exists(): return f + + +IS_VENV = sys.prefix != sys.base_prefix +VENV_DIRECTORY = Path(sys.prefix) + +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")