2023-11-24 05:48:03 -08:00
|
|
|
import logging
|
|
|
|
import os
|
2023-11-24 07:01:33 -08:00
|
|
|
import random
|
2023-11-24 05:48:03 -08:00
|
|
|
from enum import Enum
|
|
|
|
from types import ModuleType
|
|
|
|
|
2023-11-24 07:01:33 -08:00
|
|
|
import language
|
2023-11-24 05:48:03 -08:00
|
|
|
|
|
|
|
import typer
|
|
|
|
from rich.logging import RichHandler
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.markdown import Markdown
|
|
|
|
|
|
|
|
app = typer.Typer()
|
|
|
|
|
|
|
|
app_state = {}
|
|
|
|
|
2023-11-24 07:01:33 -08:00
|
|
|
default_language = os.environ.get("FANLANG_DEFAULT_LANGUAGE", "common")
|
|
|
|
language_pack, supported_languages = language.load_language_pack()
|
|
|
|
|
2023-11-24 05:48:03 -08:00
|
|
|
Supported = Enum("Supported", ((k, k) for k in supported_languages.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
def print_sample(lang: str, module: ModuleType) -> None:
|
|
|
|
summary = module.__doc__.replace("\n", " \n")
|
|
|
|
print(f"{summary}")
|
|
|
|
print(f" Text: {module.Language.text(10)}")
|
|
|
|
print(f" Names: {module.Name}")
|
|
|
|
print(f" {module.Name}")
|
|
|
|
print(f" {module.Name}")
|
|
|
|
if module.NobleName != module.Name:
|
|
|
|
print(f" Noble Names: {module.NobleName}")
|
|
|
|
print(f" {module.NobleName}")
|
|
|
|
print(f" {module.NobleName}")
|
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
|
|
@app.callback()
|
|
|
|
def main(
|
2023-11-24 07:01:33 -08:00
|
|
|
language: Supported = typer.Option(
|
|
|
|
default=default_language,
|
|
|
|
help="The language to use."
|
|
|
|
),
|
2023-11-24 05:48:03 -08:00
|
|
|
):
|
|
|
|
app_state["language"] = supported_languages[language.name]
|
|
|
|
|
2023-11-24 07:01:33 -08:00
|
|
|
debug = os.getenv("FANLANG_DEBUG", None)
|
2023-11-24 05:48:03 -08:00
|
|
|
logging.basicConfig(
|
2023-11-24 07:01:33 -08:00
|
|
|
format="%(name)s %(message)s",
|
2023-11-24 05:48:03 -08:00
|
|
|
level=logging.DEBUG if debug else logging.INFO,
|
|
|
|
handlers=[RichHandler(rich_tracebacks=True, tracebacks_suppress=[typer])],
|
|
|
|
)
|
2023-11-24 07:01:33 -08:00
|
|
|
logging.getLogger('markdown_it').setLevel(logging.ERROR)
|
|
|
|
logging.debug(f"Loaded language pack {language_pack}.")
|
|
|
|
logging.debug(f"Default language: {default_language}.")
|
2023-11-24 05:48:03 -08:00
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-11-24 07:01:33 -08:00
|
|
|
def text(count: int = typer.Option(50, help="The number of words to generate.")):
|
|
|
|
|
|
|
|
phrases = []
|
|
|
|
phrase = []
|
|
|
|
for word in app_state["language"].Language.word(count):
|
|
|
|
phrase.append(str(word))
|
|
|
|
if len(phrase) >= random.randint(1, 12):
|
|
|
|
phrases.append(' '.join(phrase))
|
|
|
|
phrase = []
|
|
|
|
if phrase:
|
|
|
|
phrases.append(' '.join(phrase))
|
|
|
|
|
|
|
|
paragraph = phrases[0].capitalize()
|
|
|
|
for phrase in phrases[1:]:
|
|
|
|
if random.choice([0, 0, 1]):
|
|
|
|
paragraph = paragraph + random.choice('?!.') + ' ' + phrase.capitalize()
|
|
|
|
else:
|
|
|
|
paragraph = paragraph + ', ' + phrase
|
|
|
|
paragraph = paragraph + random.choice('?!.')
|
|
|
|
|
|
|
|
console = Console(width=80)
|
|
|
|
console.print(paragraph)
|
2023-11-24 05:48:03 -08:00
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
def names(
|
|
|
|
count: int = typer.Option(50, help="The number of names to generate."),
|
|
|
|
noble: bool = typer.Option(False, help="Generate noble names."),
|
|
|
|
):
|
|
|
|
generator = app_state["language"].Name if not noble else app_state["language"].NobleName
|
|
|
|
for name in generator.name(count):
|
|
|
|
print(name["fullname"])
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
def list():
|
|
|
|
console = Console(width=80)
|
|
|
|
for lang, module in supported_languages.items():
|
|
|
|
console.print(Markdown(module.__doc__))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app()
|