Initial commit
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
import sys
|
||||
import os
|
||||
import glob
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
import re
|
||||
|
||||
def replace_dots(text):
|
||||
# (?m) enables multiline mode so ^ matches start of each line
|
||||
return re.sub(r"(?m)^(\s*.)\.", r"\1)", text)
|
||||
|
||||
|
||||
def format_indices(indices):
|
||||
"""Converts [2, 1] to '2)a)' based on requirements."""
|
||||
if not indices:
|
||||
return ""
|
||||
|
||||
# First level: numeric (1 -> 1))
|
||||
res = f"{indices[0]})"
|
||||
|
||||
# Second level: alpha (1 -> a))
|
||||
if len(indices) > 1:
|
||||
res += f"{chr(96 + indices[1])})"
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def process_directory(directory):
|
||||
# Find the first .tex file in the directory
|
||||
tex_files = glob.glob(os.path.join(directory, "*.tex"))
|
||||
if not tex_files:
|
||||
print(f"No .tex file found in {directory}")
|
||||
return
|
||||
|
||||
tex_file = tex_files[0]
|
||||
|
||||
# Prepare output directories
|
||||
paths = {
|
||||
'Text': os.path.join(directory, "Text"),
|
||||
'Sol': os.path.join(directory, "Sol"),
|
||||
'Persp': os.path.join(directory, "Persp")
|
||||
}
|
||||
for p in paths.values():
|
||||
os.makedirs(p, exist_ok=True)
|
||||
|
||||
labels_file = os.path.join(directory, "labels")
|
||||
current_ex_num = 1
|
||||
|
||||
|
||||
with open(tex_file, 'r', encoding='utf-8') as f_in, \
|
||||
open(labels_file, 'w', encoding='utf-8') as f_labels:
|
||||
for line in f_in:
|
||||
if line.startswith("%%SHEETINFO :"):
|
||||
try:
|
||||
json_str = line.split(":", 1)[1].strip()
|
||||
data = json.loads(json_str)
|
||||
|
||||
# 2. Handle Labels
|
||||
indexes = data.get('indexes', [])
|
||||
if not indexes:
|
||||
f_labels.write(f"Ex {current_ex_num}\n")
|
||||
else:
|
||||
for item in indexes:
|
||||
suffix = format_indices(item['indices'])
|
||||
if suffix != "":
|
||||
f_labels.write(f"Ex {current_ex_num} : {suffix}\n")
|
||||
else:
|
||||
f_labels.write(f"Ex {current_ex_num}\n")
|
||||
|
||||
# Construct 'ids' parameter
|
||||
ex_id = str(data['id'])
|
||||
selection = data.get('select')
|
||||
|
||||
if selection is not None:
|
||||
# Format: "ID.sel1,sel2"
|
||||
sel_s = [i+1 for i in selection]
|
||||
ids = f"{ex_id}.{','.join(map(str, sel_s))}"
|
||||
else:
|
||||
ids = ex_id
|
||||
|
||||
# Construct URL
|
||||
url = f"http://localhost:8080/exercices/emacs/{ids}?pretty=true&all=true&persp=true"
|
||||
|
||||
# Perform GET request
|
||||
with urllib.request.urlopen(url) as response:
|
||||
content = response.read().decode('utf-8')
|
||||
|
||||
# 4. Split and Save content
|
||||
parts = content.split('###')
|
||||
|
||||
# Ensure we have at least 3 parts, pad if necessary to avoid crashes
|
||||
while len(parts) < 3:
|
||||
parts.append("")
|
||||
|
||||
base_filename = f"Ex {current_ex_num}"
|
||||
|
||||
with open(os.path.join(paths['Text'], base_filename), 'w', encoding='utf-8') as f:
|
||||
f.write(replace_dots(parts[0].strip("\n")))
|
||||
|
||||
with open(os.path.join(paths['Sol'], base_filename), 'w', encoding='utf-8') as f:
|
||||
f.write(replace_dots(parts[1].strip("\n")))
|
||||
|
||||
with open(os.path.join(paths['Persp'], base_filename), 'w', encoding='utf-8') as f:
|
||||
f.write(replace_dots(parts[2].strip("\n")))
|
||||
|
||||
current_ex_num += 1
|
||||
|
||||
except json.JSONDecodeError:
|
||||
print(f"Error decoding JSON in line: {line.strip()}")
|
||||
except Exception as e:
|
||||
print(f"Error processing {ids}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python script.py <Dir>")
|
||||
sys.exit(1)
|
||||
|
||||
process_directory(sys.argv[1])
|
||||
Reference in New Issue
Block a user