Skip to content

Commit

Permalink
Stop trying to write a 65,000 line python file. Use plain text instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
colons committed Nov 7, 2023
1 parent a9701ce commit 6ffd8a7
Show file tree
Hide file tree
Showing 3 changed files with 64,923 additions and 64,963 deletions.
38 changes: 5 additions & 33 deletions nkdsu/apps/vote/management/commands/update_mastodon_instances.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import subprocess

from django.core.management.base import BaseCommand
import requests
Expand All @@ -13,42 +12,15 @@
}


def fmt(domain: str) -> str:
"""
Break a domain into chunks, so that black is able to combine or rejoin it
as necessary, but only if it's particularly long.
"""

if len(domain) > 100:
return '(\n {}\n )'.format(
'\n '.join(
repr(('' if i == 0 else '.') + chunk)
for i, chunk in enumerate(domain.split('.'))
)
)
else:
return repr(domain)


class Command(BaseCommand):
def write_instances(self, instances: list[str]) -> None:
module_path = os.path.join(
os.path.dirname(vote.__file__), 'mastodon_instances.py'
txt_path = os.path.join(
os.path.dirname(vote.__file__), 'mastodon_instances.txt'
)

with open(module_path, 'wt') as f:
f.write(
"# this file is populated by running `python manage.py"
" update_mastodon_instances`\n# do not edit it by hand\n\n"
)
splitter = ',\n '
f.write(
"MASTODON_INSTANCES: set[str] = {\n"
f" {splitter.join((fmt(i) for i in instances))},\n"
"}"
)

subprocess.check_call(['black', module_path])
with open(txt_path, 'wt') as f:
for instance in instances:
f.write(f"{instance}\n")

def handle(self, *args, **kwargs) -> None:
instances: set[str] = set()
Expand Down
Loading

0 comments on commit 6ffd8a7

Please sign in to comment.