cleaned up compiled config file
This commit is contained in:
parent
d1fe9d55d5
commit
be95c352d6
@ -3,8 +3,7 @@ import sys
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from .responses import COMPILED_CONFIG_FILE
|
from .static import colors, get_compiled_config_file
|
||||||
from .static import colors
|
|
||||||
|
|
||||||
|
|
||||||
def get_response_from_situation(situation: str, colorize: Optional[bool] = None):
|
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()
|
colorize = sys.stdout.isatty()
|
||||||
|
|
||||||
# get message
|
# 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())
|
existing_moods = list(config["moods"].keys())
|
||||||
template_options = config["moods"][random.choice(existing_moods)][situation]
|
template_options = config["moods"][random.choice(existing_moods)][situation]
|
||||||
template: str = random.choice(template_options)
|
template: str = random.choice(template_options)
|
||||||
|
@ -3,14 +3,14 @@ from pathlib import Path
|
|||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
import toml
|
|
||||||
|
|
||||||
from . import get_response
|
from . import get_response
|
||||||
from .responses import compile_config
|
from .responses import compile_config
|
||||||
|
from .static import IS_VENV, VENV_DIRECTORY, CONFIG_DIRECTORY, COMPILED_CONFIG_FILE_NAME
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format=' %(message)s',
|
format='%(message)s',
|
||||||
force=True,
|
force=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -130,13 +130,21 @@ def install_pip_hook(path: Path):
|
|||||||
|
|
||||||
|
|
||||||
def mommify_venv():
|
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"
|
compiled = compile_config()
|
||||||
if len(sys.argv) > 1:
|
mommy_logger.info("mommy writes its moods in %s", compiled_config_file)
|
||||||
v = sys.argv[1]
|
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()
|
bin_path = bin_path.resolve()
|
||||||
|
|
||||||
mommy_logger.info("mommy looks in %s to mess your system up~ <33", str(bin_path))
|
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():
|
if path.is_symlink():
|
||||||
# could be python interpreter
|
# could be python interpreter
|
||||||
# check for both just to be more expressive
|
# check for both just to be more expressive
|
||||||
if name.startswith("inner_") or not name.startswith("python"):
|
if name.startswith("inner_"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if subprocess.run([str(path), '-c', '"exit()"']) != 0:
|
if subprocess.run([str(path), '-c', '"exit(0)"']).returncode != 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
wrap_interpreter(path)
|
wrap_interpreter(path)
|
||||||
@ -162,6 +170,3 @@ def mommify_venv():
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
install_pip_hook(path)
|
install_pip_hook(path)
|
||||||
|
|
||||||
serious_logger.info("")
|
|
||||||
mommy_logger.info("")
|
|
||||||
|
@ -9,8 +9,9 @@ import requests
|
|||||||
|
|
||||||
from .static import get_config_file
|
from .static import get_config_file
|
||||||
|
|
||||||
|
mommy_logger = logging.getLogger("mommy")
|
||||||
|
serious_logger = logging.getLogger("serious")
|
||||||
|
|
||||||
logger = logging.Logger(__name__)
|
|
||||||
PREFIX = "MOMMY"
|
PREFIX = "MOMMY"
|
||||||
|
|
||||||
RESPONSES_URL = "https://raw.githubusercontent.com/diamondburned/go-mommy/refs/heads/main/responses.json"
|
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())
|
data = json.loads(RESPONSES_FILE.read_text())
|
||||||
|
|
||||||
if not disable_requests:
|
if not disable_requests:
|
||||||
print("mommy downloads newest responses for her girl~")
|
mommy_logger.info("mommy downloads newest responses for her girl~ %s", RESPONSES_URL)
|
||||||
print(RESPONSES_URL)
|
serious_logger.info("downloading cargo mommy responses: %s", RESPONSES_URL)
|
||||||
print()
|
|
||||||
r = requests.get(RESPONSES_URL)
|
r = requests.get(RESPONSES_URL)
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
|
||||||
@ -103,7 +103,18 @@ def compile_config(disable_requests: bool = False) -> dict:
|
|||||||
for mood in config["mood"]:
|
for mood in config["mood"]:
|
||||||
if mood not in mood_definitions:
|
if mood not in mood_definitions:
|
||||||
supported_moods_str = ", ".join(mood_definitions.keys())
|
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)
|
exit(1)
|
||||||
|
|
||||||
# compile
|
# compile
|
||||||
|
@ -4,6 +4,7 @@ from pathlib import Path
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
logger = logging.Logger(__name__)
|
logger = logging.Logger(__name__)
|
||||||
@ -81,3 +82,19 @@ def get_config_file() -> Optional[Path]:
|
|||||||
for f in config_files:
|
for f in config_files:
|
||||||
if f.exists():
|
if f.exists():
|
||||||
return f
|
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")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user