-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_script.py
executable file
·103 lines (81 loc) · 3.37 KB
/
init_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python3
import os
import toml
import sys
import argparse
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest='command', help='Subcommands')
readme_parser = subparser.add_parser('readme', help='Generate the readme')
add_parser = subparser.add_parser('add', help='Add skeleton')
add_subparsers = add_parser.add_subparsers(dest='add_subcommand', help='Add example to toml or file to solution directory')
add_subparsers.add_parser('toml', help="Add example to toml")
add_subparsers.add_parser('solutions', help="Add missing files to solutions directory")
args = parser.parse_args()
match args.command:
case "readme":
def get_solution(problem_name):
solution_file = f"solutions/{problem_name}.puf"
try:
with open(solution_file, 'r') as f:
return f.read().strip()
except FileNotFoundError:
return "Solution not found"
def generate_markdown_table(data):
markdown_table = "| Problem | Solution |\n|---------|----------|\n"
for puzzle in data['puzzles']:
if puzzle.get('ignore', None): continue
problem_name = puzzle['name']
link = puzzle['links'][0] if puzzle['links'] else ''
example, *_ = puzzle['examples']
solution = get_solution(problem_name).replace('|', '\|')
markdown_table += f"| [{problem_name}]({link}) | `{solution}` |\n"
return markdown_table
# Load the TOML data from file
toml_file = "puzzles.toml" # replace with your TOML file name
with open(toml_file, 'r') as f:
data = toml.load(f)
# Generate the Markdown table
markdown_table = generate_markdown_table(data)
with open("README_TEMPLATE.md") as f:
template = f.read()
with open("README.md", 'w') as f:
f.write(template)
f.write('\n')
f.write(markdown_table)
exit(0)
case "add":
match args.add_subcommand:
case "toml":
with open('puzzles.toml', 'a') as file:
new_example = """
[[puzzles]]
name = ""
links = [""]
[[puzzles.examples]]
args = []
result =
[[puzzles.examples]]
args = []
result =
"""
file.write(new_example)
print("written new example to puzzles.toml")
exit(0)
case "solutions":
# Read the TOML file
with open('puzzles.toml', 'r') as file:
data = toml.load(file)
# Directory where .puf files should be created
solutions_dir = 'solutions/'
# Ensure the solutions directory exists
# os.makedirs(solutions_dir, exist_ok=True)
# Iterate over puzzles in the TOML data
for puzzle in data['puzzles']:
puzzle_name = puzzle['name']
file_name = f"{puzzle_name}.puf"
file_path = os.path.join(solutions_dir, file_name)
# Check if the file exists, if not, create it
if not os.path.exists(file_path):
with open(file_path, 'w') as f:
pass # Create an empty file
print("Missing files have been created.")