diff options
| author | 2024-05-28 20:00:07 +0200 | |
|---|---|---|
| committer | 2024-05-31 18:14:55 +0100 | |
| commit | 6c3865e40cbcbce4b57edbef16fa5335449e1826 (patch) | |
| tree | a3cfd3b3d728fe1fc6cf1f083c056ab4df63a2a0 | |
| parent | Swap from custom trashcan emoji to :wastebasket: (diff) | |
Add support for parsing system information
This commit adds support for parsing miscellaneous system file formats,
allowing us to further incorporate parsing for files such as
``/proc/PID/status`` for future command purposes.
| -rw-r--r-- | arthur/apis/systems/__init__.py | 1 | ||||
| -rw-r--r-- | arthur/apis/systems/lib9front.py | 99 | ||||
| -rw-r--r-- | pyproject.toml | 5 |
3 files changed, 105 insertions, 0 deletions
diff --git a/arthur/apis/systems/__init__.py b/arthur/apis/systems/__init__.py new file mode 100644 index 0000000..302f80c --- /dev/null +++ b/arthur/apis/systems/__init__.py @@ -0,0 +1 @@ +"""Functionality for working with various operating systems.""" diff --git a/arthur/apis/systems/lib9front.py b/arthur/apis/systems/lib9front.py new file mode 100644 index 0000000..e499147 --- /dev/null +++ b/arthur/apis/systems/lib9front.py @@ -0,0 +1,99 @@ +"""Library functions for working with 9front systems and file formats.""" + +import random + + +def generate_blog_comment(blogcom: str) -> str: + """ + Generate a blog comment out of the ``blogcom`` file contents that are passed in. + + The blogcom file can be retrieved at ``/lib/blogcom`` in 9front. + + This function is implemented according to the highest standards in + performance and security, and to be able to generate properly random texts, + it utilizes a 623-dimensionally equidistributed uniform pseudorandom number + generator as described by Makoto Matsumoto and Takuji Nishimura. In other + words, for our purposes, this function matches or even exceeds Python + Discord's security requirements. + """ + # Complete output buffer + out = [] + # Options of the current branch, of which one will be selected at random + options = [] + # Character buffer of the current choice + choice_buf = [] + # Whether we are in a {block|of|options} at the moment + in_block = False + + for char in blogcom: + if char == "{": + in_block = True + elif in_block and char == "|": + options.append("".join(choice_buf)) + choice_buf.clear() + elif in_block and char == "}": + options.append("".join(choice_buf)) + choice_buf.clear() + out.append(random.choice(options)) + options.clear() + in_block = False + elif in_block: + choice_buf.append(char) + else: + out.append(char) + + return "".join(out) + + +def generate_buzzwords(bullshit: str) -> str: + """ + Generates buzzwords to describe a random product of the ``bullshit`` file contents that are passed in. + + The bullshit file can be retrieved at ``/lib/bullshit`` in 9front. + + This function underlies the same security guarantees as ``generate_blog_comment``. + """ + # line markers + # nothing -> word + # ^ -> start + # * -> protocol + # % -> suffix + # | -> adjectives + # $ -> end + words = [] + starters = [] + protocols = [] + suffixes = [] + adjectives = [] + endings = [] + + # Parsing + for line in bullshit.splitlines(): + if " " not in line: + words.append(line) + else: + word, qualifier = line.split() + if qualifier == "^": + starters.append(word) + elif qualifier == "*": + protocols.append(word) + elif qualifier == "%": + suffixes.append(word) + elif qualifier == "|": + adjectives.append(word) + elif qualifier == "$": + endings.append(word) + + # Generating + response = [] + for _ in range(random.randint(1, 2)): + response.append(random.choice(starters)) + for _ in range(random.randint(1, 2)): + response.append(random.choice(adjectives)) + for _ in range(random.randint(1, 2)): + response.append(random.choice(words) + random.choice(suffixes) * random.randint(0, 1)) + if random.random() > 0.5: + response.append("over " + random.choice(protocols)) + if random.random() > 0.3: + response.append(random.choice(endings)) + return " ".join(response) diff --git a/pyproject.toml b/pyproject.toml index b5653b8..b286606 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,6 +67,11 @@ ignore = [ "COM812", "D206", "E111", "E114", "E117", "E501", "ISC001", "Q000", "Q001", "Q002", "Q003", "W191", ] +[tool.ruff.lint.per-file-ignores] +# McCabe is not supported on Linux systems +"arthur/apis/systems/*.py" = ["C901", "PLR0912", "PERF401", "PLR2004"] + + [tool.ruff.lint.isort] order-by-type = false case-sensitive = true |