-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more ruff lints and convert back to tabs
- Loading branch information
1 parent
ea11d34
commit ba53398
Showing
5 changed files
with
239 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,92 @@ | ||
"""Contains the core functionality of randfacts.""" | ||
|
||
import argparse | ||
import contextlib | ||
import importlib.metadata | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from random import choice | ||
|
||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
dir_path = Path(__file__).resolve().parent | ||
|
||
__version__ = "" | ||
try: | ||
__version__: str = importlib.metadata.version("randfacts") | ||
except Exception: | ||
pass | ||
with contextlib.suppress(Exception): | ||
__version__: str = importlib.metadata.version("randfacts") | ||
|
||
with open(os.path.join(dir_path, "safe.txt"), encoding="utf-8") as f: | ||
safe_facts = [ | ||
fact.rstrip("\r\n ") for fact in f.readlines() if fact.rstrip("\r\n ") != "" | ||
] | ||
with (dir_path / "safe.txt").open(encoding="utf-8") as f: | ||
safe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")] | ||
|
||
with open(os.path.join(dir_path, "unsafe.txt"), encoding="utf-8") as f: | ||
unsafe_facts = [ | ||
fact.rstrip("\r\n ") for fact in f.readlines() if fact.rstrip("\r\n ") != "" | ||
] | ||
with (dir_path / "unsafe.txt").open(encoding="utf-8") as f: | ||
unsafe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")] | ||
|
||
all_facts = safe_facts + unsafe_facts | ||
|
||
|
||
def get_fact(filter_enabled: bool = True, only_unsafe: bool = False) -> str: | ||
"""This function returns a random fact. | ||
Parameters | ||
---------- | ||
filter_enabled : bool | ||
The `filter_enabled` parameter determines if the function will filter | ||
out potentially inappropriate facts. Defaults to True. | ||
"""This function returns a random fact. | ||
only_unsafe : bool | ||
The `only_unsafe` parameter determines if the function will only give | ||
unsafe (NSFW) facts. Takes precedence over the `filter_enabled` argument. | ||
Parameters | ||
---------- | ||
filter_enabled : bool | ||
The `filter_enabled` parameter determines if the function will filter | ||
out potentially inappropriate facts. Defaults to True. | ||
Returns | ||
------ | ||
str | ||
A random fact. | ||
only_unsafe : bool | ||
The `only_unsafe` parameter determines if the function will only give | ||
unsafe (NSFW) facts. Takes precedence over the `filter_enabled` argument. | ||
""" | ||
Returns: | ||
------ | ||
str | ||
A random fact. | ||
if only_unsafe: | ||
return choice(unsafe_facts) | ||
if filter_enabled is False: | ||
return choice(all_facts) | ||
return choice(safe_facts) | ||
""" | ||
if only_unsafe: | ||
return choice(unsafe_facts) | ||
if filter_enabled is False: | ||
return choice(all_facts) | ||
return choice(safe_facts) | ||
|
||
|
||
def _cli_entrypoint() -> None: | ||
"""Entrypoint for execution via command-line.""" | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Generate random facts from the command-line" | ||
) | ||
|
||
parser.add_argument( | ||
"-V", | ||
"--version", | ||
action="store_true", | ||
help="Print the package version and exit", | ||
) | ||
|
||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument( | ||
"-m", "--mixed", action="store_true", help="Include safe and unsafe facts" | ||
) | ||
|
||
group.add_argument( | ||
"-u", "--unsafe", action="store_true", help="Only include unsafe facts" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.version: | ||
print(__version__) | ||
sys.exit(0) | ||
if args.mixed: | ||
print(get_fact(False)) | ||
elif args.unsafe: | ||
print(get_fact(only_unsafe=True)) | ||
else: | ||
print(get_fact()) | ||
"""Entrypoint for execution via command-line.""" | ||
parser = argparse.ArgumentParser( | ||
description="Generate random facts from the command-line", | ||
) | ||
|
||
parser.add_argument( | ||
"-V", | ||
"--version", | ||
action="store_true", | ||
help="Print the package version and exit", | ||
) | ||
|
||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument( | ||
"-m", | ||
"--mixed", | ||
action="store_true", | ||
help="Include safe and unsafe facts", | ||
) | ||
|
||
group.add_argument( | ||
"-u", | ||
"--unsafe", | ||
action="store_true", | ||
help="Only include unsafe facts", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.version: # pyright: ignore[reportAny] | ||
print(__version__) | ||
return | ||
if args.mixed: # pyright: ignore[reportAny] | ||
print(get_fact(filter_enabled=False)) | ||
elif args.unsafe: # pyright: ignore[reportAny] | ||
print(get_fact(only_unsafe=True)) | ||
else: | ||
print(get_fact()) | ||
|
||
|
||
if __name__ == "__main__": | ||
_cli_entrypoint() | ||
_cli_entrypoint() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.