Skip to content

Commit

Permalink
reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
lunakv committed Jun 26, 2022
1 parent f3933da commit 731a8e9
Show file tree
Hide file tree
Showing 27 changed files with 389 additions and 360 deletions.
36 changes: 15 additions & 21 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from .utils.remove422 import remove_422s
from .utils.scheduler import Scheduler

logging.basicConfig(
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(message)s", datefmt="%Y-%m-%d %H:%M:%S")

description = """
This API provides information about rules resources for Magic: the Gathering. It was primarily created to serve the
Expand All @@ -26,25 +24,20 @@
description=description,
openapi_tags=[
{"name": "Rules", "description": "Resources pertaining to the parsed representation of the current CR."}
]
],
)

app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_methods=['*'],
allow_headers=['*']
)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])


app.mount('/static', StaticFiles(directory="app/static"), name="static")
app.include_router(admin.router, prefix='/admin')
app.include_router(glossary.router, prefix='/glossary')
app.include_router(link.router, prefix='/link')
app.include_router(diff.router, prefix='/diff')
app.include_router(rawfile.router, prefix='/file')
app.include_router(metadata.router, prefix='/metadata')
app.include_router(pending.router, prefix='/pending')
app.mount("/static", StaticFiles(directory="app/static"), name="static")
app.include_router(admin.router, prefix="/admin")
app.include_router(glossary.router, prefix="/glossary")
app.include_router(link.router, prefix="/link")
app.include_router(diff.router, prefix="/diff")
app.include_router(rawfile.router, prefix="/file")
app.include_router(metadata.router, prefix="/metadata")
app.include_router(pending.router, prefix="/pending")
app.include_router(rule.router)

app.openapi = remove_422s(app)
Expand All @@ -54,7 +47,7 @@
@app.on_event("startup")
async def seed():
await seeder.seed()
print('x')
print("x")


@app.exception_handler(RequestValidationError)
Expand All @@ -65,5 +58,6 @@ async def validation_exception_handler(request, exc):
@app.get("/", include_in_schema=False, status_code=400)
def root():
return {
'detail': 'This is the root of the Academy Ruins API. You can find the documentation at '
'https://api.academyruins.com/docs'}
"detail": "This is the root of the Academy Ruins API. You can find the documentation at "
"https://api.academyruins.com/docs"
}
22 changes: 11 additions & 11 deletions app/parsing/cr_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
from ..utils import db
import logging

rules_page_uri = 'https://magic.wizards.com/en/rules/'
rules_page_uri = "https://magic.wizards.com/en/rules/"


def is_txt_link(tag):
return tag.name == 'a' and tag.has_attr('href') and re.search(r'.*\.txt', tag['href'])
return tag.name == "a" and tag.has_attr("href") and re.search(r".*\.txt", tag["href"])


async def scrape_rules_page():
pending = await db.get_pending('cr')
pending = await db.get_pending("cr")
if pending:
logging.debug('New CR redirect already pending, skipping scrape')
logging.debug("New CR redirect already pending, skipping scrape")
return

response = requests.get(rules_page_uri)
if response.status_code != requests.codes.ok:
notify_scrape_error(f"Couldn't fetch rules page (code {response.status_code})")
return

soup = BeautifulSoup(response.text, 'html.parser')
soup = BeautifulSoup(response.text, "html.parser")
txt_links = soup.find_all(is_txt_link)
if len(txt_links) != 1:
notify_scrape_error(f'Wrong number of TXT links found! (expected 1, got {len(txt_links)})')
notify_scrape_error(f"Wrong number of TXT links found! (expected 1, got {len(txt_links)})")
return

href = txt_links[0]['href']
href = href.replace(' ', '%20') # the last path segment sometimes has a space (kinda hacky, but whatever)
href = txt_links[0]["href"]
href = href.replace(" ", "%20") # the last path segment sometimes has a space (kinda hacky, but whatever)

current = await db.get_redirect('cr')
current = await db.get_redirect("cr")
if href != current:
await db.set_pending('cr', href)
await db.set_pending("cr", href)
notify_new_cr(href)


if __name__ == '__main__':
if __name__ == "__main__":
scrape_rules_page()
29 changes: 13 additions & 16 deletions app/parsing/difftool/diff_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ def diff_cr(orig_cr, dest_cr):
output_comparison = diff_utils.diff_rules(old, new)
if output_comparison:
rules.append(output_comparison)
if output_comparison['old'] and output_comparison['new']:
if (output_comparison['old']['ruleNum'] !=
output_comparison['new']['ruleNum']):
changes.append({'old':
output_comparison['old']['ruleNum'],
'new':
output_comparison['new']['ruleNum']})

if output_comparison['old'] and not output_comparison['new']:
deletions.append({'removed':
output_comparison['old']['ruleNum']})
if output_comparison["old"] and output_comparison["new"]:
if output_comparison["old"]["ruleNum"] != output_comparison["new"]["ruleNum"]:
changes.append(
{"old": output_comparison["old"]["ruleNum"], "new": output_comparison["new"]["ruleNum"]}
)

if output_comparison["old"] and not output_comparison["new"]:
deletions.append({"removed": output_comparison["old"]["ruleNum"]})

return rules
# finished_changes = {'lastUpdate':
Expand All @@ -38,20 +35,20 @@ def diff_cr(orig_cr, dest_cr):
# out_changes.write(json.dumps(finished_changes, indent=4))


if __name__ == '__main__':
if __name__ == "__main__":
# Arg list looks like this:
# old CR file, ~~old file set name (e.g. "Guilds of Ravnica")~~,
# new CR file, ~~new file set name~~
# orig_cr, dest_cr = sys.argv[1], sys.argv[2]
cr_dir = '../../static/raw_docs/cr/'
orig_path = cr_dir + 'cr-2021-07-23-AFR.txt'
dest_path = cr_dir + 'cr-2021-09-24-MID.txt'
cr_dir = "../../static/raw_docs/cr/"
orig_path = cr_dir + "cr-2021-07-23-AFR.txt"
dest_path = cr_dir + "cr-2021-09-24-MID.txt"
# This is explicitly done because I have complete control over the file
# naming convention I use. This is N O T the best way to do this, but
# I didn't want to futz with regex here.
# orig_cr_set_code = orig_cr[-7:-4]
# dest_cr_set_code = dest_cr[-7:-4]
with open(orig_path, 'r') as orig_file, open(dest_path, 'r') as dest_file:
with open(orig_path, "r") as orig_file, open(dest_path, "r") as dest_file:
orig = orig_file.read()
dest = dest_file.read()
diff_cr(orig, dest)
50 changes: 20 additions & 30 deletions app/parsing/difftool/diff_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_readable_header(first_file, second_file):
from_name = Path(first_file).stem
to_name = Path(second_file).stem

return {'old': from_name, 'new': to_name}
return {"old": from_name, "new": to_name}


def wrap_slice(rule_slice, status):
Expand All @@ -37,17 +37,14 @@ def wrap_slice(rule_slice, status):
status -- whether the slice belongs to an 'old' rule or a 'new' rule.
"""
if not rule_slice:
return ''
if re.match('^^(?:rules? )?'
'\d{3}(?:\.\d+[a-z]*)*'
'(?:–\d{3}(?:\.\d+[a-z]?)?)?\)?\.?',
' '.join(rule_slice)):
return ""
if re.match("^^(?:rules? )?" "\d{3}(?:\.\d+[a-z]*)*" "(?:–\d{3}(?:\.\d+[a-z]?)?)?\)?\.?", " ".join(rule_slice)):
return rule_slice

if status == 'old':
return ['old_start', *rule_slice, 'old_end']
if status == "old":
return ["old_start", *rule_slice, "old_end"]
else:
return ['new_start', *rule_slice, 'new_end']
return ["new_start", *rule_slice, "new_end"]


def diff_rules(old_rule, new_rule):
Expand All @@ -66,7 +63,7 @@ def diff_rules(old_rule, new_rule):
new_rule -- the new rule to compare to
"""
rules_comparison = {}
rules_comparison['old'], rules_comparison['new'] = [], []
rules_comparison["old"], rules_comparison["new"] = [], []

old_rule_num, new_rule_num = old_rule[0], new_rule[0]
old_rule_text, new_rule_text = old_rule[1:], new_rule[1:]
Expand All @@ -81,19 +78,15 @@ def diff_rules(old_rule, new_rule):
for o, n, i in matches:
if len(matches) == 1: # A rule doesn't have a partner

if o > n: # Old rule was deleted
rules_comparison['old'] = {
'ruleNum': old_rule_num,
'ruleText': ' '.join(old_rule_text)}
if o > n: # Old rule was deleted
rules_comparison["old"] = {"ruleNum": old_rule_num, "ruleText": " ".join(old_rule_text)}

rules_comparison['new'] = None
rules_comparison["new"] = None

elif o < n: # New rule was added
rules_comparison['old'] = None
elif o < n: # New rule was added
rules_comparison["old"] = None

rules_comparison['new'] = {
'ruleNum': new_rule_num,
'ruleText': ' '.join(new_rule_text)}
rules_comparison["new"] = {"ruleNum": new_rule_num, "ruleText": " ".join(new_rule_text)}

return rules_comparison

Expand All @@ -103,21 +96,18 @@ def diff_rules(old_rule, new_rule):
elif len(matches) == 2 and old_rule_text == new_rule_text:
return None
else:
modded_old.extend(wrap_slice(old_rule_text[old_offset:o], 'old'))
modded_old.extend(old_rule_text[o:o + i])
modded_old.extend(wrap_slice(old_rule_text[old_offset:o], "old"))
modded_old.extend(old_rule_text[o : o + i])
old_offset = o + i

modded_new.extend(wrap_slice(new_rule_text[new_offset:n], 'new'))
modded_new.extend(new_rule_text[n:n + i])
modded_new.extend(wrap_slice(new_rule_text[new_offset:n], "new"))
modded_new.extend(new_rule_text[n : n + i])
new_offset = n + i

if ('old_start' not in ' '.join(modded_old) and
'new_start' not in ' '.join(modded_new)):
if "old_start" not in " ".join(modded_old) and "new_start" not in " ".join(modded_new):
# the only changes were to rule numbers, so we can get out
return None

rules_comparison['old'] = {'ruleNum': old_rule_num,
'ruleText': ' '.join(modded_old)}
rules_comparison['new'] = {'ruleNum': new_rule_num,
'ruleText': ' '.join(modded_new)}
rules_comparison["old"] = {"ruleNum": old_rule_num, "ruleText": " ".join(modded_old)}
rules_comparison["new"] = {"ruleNum": new_rule_num, "ruleText": " ".join(modded_new)}
return rules_comparison
50 changes: 27 additions & 23 deletions app/parsing/difftool/rules_manip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@


def ensure_real_match(rule, some_list):
keyword_list = ['attacking', 'attackers.',
'blocking', 'blockers.']
problem_children = ['716.1a', '716.1b', '716.1c', '716.1d', '716.1e',
'716.1f', '716.2a', '716.2b', '716.2c', '716.2d',
'716.2e', '716.2f']
keyword_list = ["attacking", "attackers.", "blocking", "blockers."]
problem_children = [
"716.1a",
"716.1b",
"716.1c",
"716.1d",
"716.1e",
"716.1f",
"716.2a",
"716.2b",
"716.2c",
"716.2d",
"716.2e",
"716.2f",
]
for index, comparison in enumerate(some_list):
if len(rule) == len(comparison):
difference = list(set(rule[1:])
.symmetric_difference(set(comparison[1:])))
if (len(difference) > 0 and
all(word in keyword_list for word in difference)):
difference = list(set(rule[1:]).symmetric_difference(set(comparison[1:])))
if len(difference) > 0 and all(word in keyword_list for word in difference):
return some_list[1]
if (rule[0] in problem_children):
if (rule[0][3:] == comparison[0][3:]):
if rule[0] in problem_children:
if rule[0][3:] == comparison[0][3:]:
return some_list[index]
return some_list[0]

Expand All @@ -34,9 +42,9 @@ def extract_rules(entire_doc):
"""
# Handle editorial snafus on WotC's end
# TODO centralize these replacements
entire_doc = entire_doc.replace(" \"", " “")
entire_doc = entire_doc.replace("(\"", "(“")
entire_doc = entire_doc.replace("\"", "”")
entire_doc = entire_doc.replace(' "', " “")
entire_doc = entire_doc.replace('("', "(“")
entire_doc = entire_doc.replace('"', "”")
entire_doc = entire_doc.replace("'", "’")
entire_doc = entire_doc.replace(" ’", " ‘")
entire_doc = entire_doc.replace("-", "—")
Expand All @@ -45,12 +53,11 @@ def extract_rules(entire_doc):
entire_doc = entire_doc.replace("(r)", "®")
entire_doc = re.sub(r"\n\s{4,}(\w)", r" \1", entire_doc)

extracted_rules = re.findall(r'^\d{3}[^a-zA-Z\n]{2}.*[“"”.) :]$',
entire_doc, re.MULTILINE)
extracted_rules = re.findall(r'^\d{3}[^a-zA-Z\n]{2}.*[“"”.) :]$', entire_doc, re.MULTILINE)
rules_list = []
for rule in extracted_rules:
rule_normalized = rule.split()
rule_normalized[0] = rule_normalized[0].rstrip('.')
rule_normalized[0] = rule_normalized[0].rstrip(".")
rules_list.append(rule_normalized)

return rules_list
Expand All @@ -76,7 +83,7 @@ def aggregate_rule_nums(first_rules, second_rules):
unique_rules = set(first_rule_numbers) ^ set(second_rule_numbers)

for index in unique_rules:
placeholder = [index, '']
placeholder = [index, ""]

if index not in first_rule_numbers:
first_rules.append(placeholder)
Expand Down Expand Up @@ -107,10 +114,7 @@ def align_matches(some_list, match_list):
"""
homeless_rules = []
for index, rule in enumerate(some_list):
best = difflib.get_close_matches(
rule,
match_list,
cutoff=.4)
best = difflib.get_close_matches(rule, match_list, cutoff=0.4)
try:
if len(best) == 0:
raise IndexError
Expand All @@ -123,7 +127,7 @@ def align_matches(some_list, match_list):
# Can't swap in place because it might alienate
# rules later in the list
homeless_rules.append((swap_index, rule))
placeholder = [rule[0], '']
placeholder = [rule[0], ""]
some_list[some_list.index(rule)] = placeholder

for index, rule in homeless_rules:
Expand Down
14 changes: 6 additions & 8 deletions app/parsing/difftool/sort_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ def is_after(left, right):
left -- rule to compare from
right -- rule to compare to
"""
split_left = re.match('^([0-9]{3,})(?:\.([0-9]+)([a-z]*))\.?$',
left)
split_right = re.match('^([0-9]{3,})(?:\.([0-9]+)([a-z]*))\.?$',
right)
split_left = re.match(r"^([0-9]{3,})(?:\.([0-9]+)([a-z]*))\.?$", left)
split_right = re.match(r"^([0-9]{3,})(?:\.([0-9]+)([a-z]*))\.?$", right)

# e.g. 100.1a splits into 100, 1, and a
# First two are ints, so treat them as such
Expand All @@ -32,7 +30,7 @@ def is_after(left, right):
if l_rule > r_rule:
return True
elif l_rule == r_rule:
if (len(str(l_sub)) > len(str(r_sub))):
if len(str(l_sub)) > len(str(r_sub)):
return True
if l_sub > r_sub:
return True
Expand All @@ -50,7 +48,7 @@ def insertion_sort(input_List):
cur = input_List[i]
pos = i

while pos > 0 and is_after(input_List[pos-1][0], cur[0]):
input_List[pos] = input_List[pos-1]
pos = pos-1
while pos > 0 and is_after(input_List[pos - 1][0], cur[0]):
input_List[pos] = input_List[pos - 1]
pos = pos - 1
input_List[pos] = cur
Loading

0 comments on commit 731a8e9

Please sign in to comment.