Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various "minor" changes (4) #1435

Merged
merged 21 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement per-entry patches
Instead of one temporary JSON file, contributors now can submit patches in form of separate files per entry, that will be merged by `merge_out.py` without dealing with the potential conflicts to the main `atlas.json`.
  • Loading branch information
Hans5958 committed Jun 20, 2023
commit 978757ce8fa48cea0d9716557a5f012cca80f123
36 changes: 36 additions & 0 deletions tools/create_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import os
import secrets
from pathlib import Path

patches_dir = "../data/patches/"
Path(patches_dir).mkdir(parents=True, exist_ok=True)

entry = None
entry_input = ""

print("Write your submission entry here.")
while entry is None:

entry_input += input("> ")
try:
entry = json.loads(entry_input)
except:
pass
print()
print("Entry received!")
print()
print("Enter your username as the attribution to be shown on the about page.")
print("Leave it empty if you don't want to.")
print("You can use your Reddit username. Do not include the \"u/\" part.")
print("You can also your GitHub username, but add \"gh:\" before your username (e.g. \"gh:octocat\")")
author = input("Author: ")

if author:
entry['_author'] = author

with open(f'{patches_dir}gh-{secrets.token_hex(2)}-{"-".join(entry["name"].split()).lower()}.json', 'w', encoding='utf-8') as out_file:
out_file.write(json.dumps(entry, ensure_ascii=False))

print("Patch created!")
print("You can commit this file directory, after that you can push and create a pull request.")
137 changes: 74 additions & 63 deletions tools/merge_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,98 @@

from scale_back import ScaleConfig

merge_source_file = 'temp-atlas.json'
out_ids = []
atlas_ids = {}
authors = []

with open('../web/all-authors.txt', 'r') as authors_file:
authors = authors_file.read().strip().split()

with open(merge_source_file, 'r', encoding='UTF-8') as f1:
out_json = json.loads(f1.read())
with open('../web/read_ids.txt', 'r') as ids_file:
out_ids = ids_file.read().strip().split()

with open('../web/atlas.json', 'r', encoding='utf-8') as atlas_file:
atlas_data = json.loads(atlas_file.read())

format_all_entries(out_json)
format_all_entries(atlas_file)

base_image_path = os.path.join('..', 'web', '_img', 'canvas', 'place30')
ScaleConfig.image1 = os.path.join(base_image_path, '159.png')
scale_back.swap_source_dest('164', '165', os.path.join(base_image_path, '163_159.png'))
scale_back.scale_back_entries(out_json)
scale_back.scale_back_entries(atlas_file)
scale_back.swap_source_dest('165', '166', os.path.join(base_image_path, '164_159.png'))
scale_back.scale_back_entries(out_json)
scale_back.scale_back_entries(atlas_file)
scale_back.swap_source_dest('166', '167', os.path.join(base_image_path, '165_159.png'))
scale_back.scale_back_entries(out_json)
scale_back.scale_back_entries(atlas_file)

out_ids = set()
out_dupe_ids = set()
atlas_ids = {}

with open('../web/atlas.json', 'r', encoding='utf-8') as atlas_file:
atlas_json = json.loads(atlas_file.read())
last_id = 0

for i, entry in enumerate(atlas_json):
for i, entry in enumerate(atlas_data):
atlas_ids[entry['id']] = i
id = entry['id']
if id.isnumeric() and int(id) > last_id and int(id) - last_id < 100:
last_id = int(id)

last_existing_id = list(atlas_json[-1]['id'])

for entry in out_json:
if entry['id'] == 0 or entry['id'] == '0':
# "Increment" the last ID to derive a new ID.
current_index = -1
while current_index > -(len(last_existing_id)):
current_char = last_existing_id[current_index]

if current_char == 'z':
last_existing_id[current_index] = '0'
current_index -= 1
else:
if current_char == '9':
current_char = 'a'
else:
current_char = chr(ord(current_char) + 1)
last_existing_id[current_index] = current_char
break
entry['id'] = ''.join(last_existing_id)

for entry in out_json:
if entry['id'] in out_ids:
print(f"Entry {entry['id']} has duplicates! Please resolve this conflict. This will be excluded from the merge.")
out_dupe_ids.add(entry['id'])
out_ids.add(entry['id'])

for entry in out_json:
if entry['id'] in out_dupe_ids:
continue

if 'edit' in entry and entry['edit']:
assert entry['id'] in atlas_ids, "Edit failed! ID not found on Atlas."
index = atlas_ids[entry['id']]
patches_dir = "../data/patches/"
if not os.path.exists(patches_dir):
print("Patches folder not found. Exiting.")
exit()

assert index != None, "Edit failed! ID not found on Atlas."
for filename in os.listdir(patches_dir):
f = os.path.join(patches_dir, filename)

print(f"Edited {atlas_json[index]['id']} with {entry['edit']}")
print(f"{filename}: Processing...")

if not os.path.isfile(f) or not f.endswith('json'):
continue

del entry['edit']
atlas_json[index] = entry
elif entry['id'] in atlas_ids:
print(f"Edited {entry['id']} manually.")
atlas_json[atlas_ids[entry['id']]] = entry
else:
print(f"Added {entry['id']}.")
atlas_json.append(entry)
with open(f, 'r', encoding='utf-8') as entry_file:
entry = json.loads(entry_file.read())

if '_reddit_id' in entry:
reddit_id = entry['_reddit_id']
if reddit_id in out_ids:
print(f"{filename}: Submission from {entry['id']} has been included! This will be ignored from the merge.")
continue
out_ids.append(reddit_id)
del entry['_reddit_id']

if '_author' in entry:
author = entry['_author']
if author not in authors:
authors.append(author)
del entry['_author']

if entry['id'] in out_ids:
print(f"{filename}: Submission from {entry['id']} has been included! This will be ignored from the merge.")
continue

if entry['id'] < 1:
last_id += 1
print(f"{filename}: Entry is new, assigned ID {last_id}")
entry['id'] = str(last_id)
else:
out_ids.append(entry['id'])


if entry['id'] in atlas_ids:
index = atlas_ids[entry['id']]
print(f"{filename}: Edited {atlas_data[index]['id']}.")
atlas_data[index] = entry
else:
print(f"{filename}: Added {entry['id']}.")
atlas_data.append(entry)

os.remove(f)

print('Writing...')
with open('../web/atlas.json', 'w', encoding='utf-8') as atlas_file:
per_line_entries(atlas_json, atlas_file)
per_line_entries(atlas_data, atlas_file)

with open('../data/read-ids.txt', 'w', encoding='utf-8') as ids_file:
ids_file.write("\n".join(out_ids) + "\n")

with open('../data/read-ids.txt', 'a', encoding='utf-8') as read_ids_file:
with open('temp-read-ids.txt', 'r+', encoding='utf-8') as read_ids_temp_file:
read_ids_file.writelines(read_ids_temp_file.readlines())
read_ids_temp_file.truncate(0)
with open('../web/all-authors.txt', 'w', encoding='utf-8') as authors_file:
authors_file.write("\n".join(authors) + "\n")

print('All done.')
Loading