diff --git a/.gitignore b/.gitignore index f720e08..7136ba0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ coverage.xml build dist .pytest_cache +words.txt diff --git a/README.md b/README.md index a173189..bb3a031 100644 --- a/README.md +++ b/README.md @@ -23,15 +23,18 @@ uvx --from git+https://git.klemek.fr/klemek/cool-domain cool-domain ## Usage ```txt -usage: cool-domain [-h] [--count DOMAIN_COUNT] [--lang {english,french}] [--words WORD_COUNT] [--dig | --no-dig] [--debug | --no-debug] - [--quiet | --no-quiet] [--cache | --no-cache] [--cache-file CACHE_FILE] [--tld-list-uri TLD_LIST_URI] +usage: cool-domain [-h] [--count DOMAIN_COUNT] [--words WORD_COUNT] [--lang {english,french,custom}] [--custom-words CUSTOM_WORDS] + [--dig | --no-dig] [--debug | --no-debug] [--quiet | --no-quiet] [--cache | --no-cache] [--cache-file CACHE_FILE] + [--tld-list-uri TLD_LIST_URI] options: -h, --help show this help message and exit --count DOMAIN_COUNT number of domains to generate (default: 1) - --lang {english,french} - word list lang (default: english) --words WORD_COUNT number of words per domain (default: 1) + --lang {english,french,custom} + word list lang (default: english) + --custom-words CUSTOM_WORDS + custom word list location (default: words.txt) --dig, --no-dig check availability with dig (default: true) --debug, --no-debug --quiet, --no-quiet diff --git a/cool_domain/__main__.py b/cool_domain/__main__.py index 4ac3d49..d0638bd 100644 --- a/cool_domain/__main__.py +++ b/cool_domain/__main__.py @@ -1,6 +1,8 @@ import logging import sys +from cool_domain.word_list import WordList + from . import PKG_NAME, PKG_VERSION from .cache import Cache from .constants import LANGS @@ -14,10 +16,12 @@ def main() -> int: params = parse_parameters(sys.argv[1:]) setup_logs(params) logging.getLogger().info("%s %s", PKG_NAME, PKG_VERSION) + langs = LANGS + langs["custom"] = WordList("custom", params.custom_words) with ( Cache(filepath=params.cache_file, no_load=params.cache) as cache, TLDList(cache=cache, remote_uri=params.tld_list_uri) as tld_list, - LANGS[params.lang] as word_list, + langs[params.lang] as word_list, ): factory = Factory(tld_list=tld_list, word_list=word_list, words=params.words) logging.getLogger().info( diff --git a/cool_domain/constants.py b/cool_domain/constants.py index 4ededee..a163c2d 100644 --- a/cool_domain/constants.py +++ b/cool_domain/constants.py @@ -1,6 +1,6 @@ from .word_list import WordList LANGS: dict[str, WordList] = { - "english": WordList("english", "english", "words_alpha.txt"), - "french": WordList("french", "french", "francais.txt"), + "english": WordList("english", "english/words_alpha.txt"), + "french": WordList("french", "french/francais.txt"), } diff --git a/cool_domain/params.py b/cool_domain/params.py index 2aeec8d..e1fa622 100644 --- a/cool_domain/params.py +++ b/cool_domain/params.py @@ -20,6 +20,7 @@ class Parameters: lang: str = "english" cache_file: str = str(pathlib.Path(tempfile.gettempdir()) / ".cool-domain-cache") tld_list_uri: str = "https://publicsuffix.org/list/public_suffix_list.dat" + custom_words: str = "words.txt" @classmethod def from_namespace(cls, args: argparse.Namespace) -> "Parameters": @@ -98,12 +99,6 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters: default=default_values.count, help_txt="number of domains to generate", ) - parser.add_argument( - "--lang", - default=default_values.lang, - help="word list lang (default: english)", - choices=LANGS.keys(), - ) __add_arg_int( parser, "--words", @@ -111,6 +106,19 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters: default=default_values.words, help_txt="number of words per domain", ) + parser.add_argument( + "--lang", + default=default_values.lang, + help="word list lang (default: english)", + choices=[*list(LANGS.keys()), "custom"], + ) + __add_arg_str( + parser, + "--custom-words", + env_var="CUSTOM_WORDS", + default=default_values.custom_words, + help_txt="custom word list location", + ) __add_arg_bool( parser, "--dig", diff --git a/cool_domain/word_list.py b/cool_domain/word_list.py index 58c8e87..b766434 100644 --- a/cool_domain/word_list.py +++ b/cool_domain/word_list.py @@ -8,10 +8,10 @@ import unidecode class WordList: __slots__ = ["__filepath", "__name", "logger", "words"] - def __init__(self, name: str, path: str, filename: str) -> None: + def __init__(self, name: str, path: str) -> None: self.logger: logging.Logger = logging.getLogger(self.__class__.__name__) self.__name = name - self.__filepath = pathlib.Path(__file__).parent / ".." / path / filename + self.__filepath = pathlib.Path(__file__).parent / ".." / path self.words: list[str] = [] def open(self) -> None: