Skip to content

Commit ed65449

Browse files
committed
basic draft
1 parent 38a2415 commit ed65449

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

.flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
extend-ignore = E203

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode/
2+
venv/
3+
__pycache__/

build.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import argparse
2+
import json
3+
import os
4+
import sys
5+
from collections import defaultdict, namedtuple
6+
from datetime import date, datetime
7+
8+
import fastjsonschema
9+
import yaml
10+
from fastjsonschema.exceptions import JsonSchemaException
11+
12+
13+
def school_year_from_date(date: date) -> str:
14+
if date.month < 9:
15+
return "%d_%d" % (date.year - 1, date.year % 100)
16+
return "%d_%d" % (date.year, (date.year + 1) % 100)
17+
18+
19+
ErrorData = namedtuple("ErrorData", ["file", "message"])
20+
ERRORS = []
21+
OUTPUT = defaultdict(lambda: [])
22+
ROOT = os.path.dirname(os.path.abspath(__file__))
23+
24+
parser = argparse.ArgumentParser()
25+
parser.add_argument(
26+
"--dry", action="store_true", help="Only validate, do not build output files."
27+
)
28+
args = parser.parse_args()
29+
30+
31+
with open(os.path.join(ROOT, "schemas", "event.schema.json")) as f:
32+
validate_event = fastjsonschema.compile(json.load(f))
33+
34+
for directory in os.walk(os.path.join(ROOT, "data")):
35+
for file in directory[2]:
36+
name, ext = os.path.splitext(file)
37+
if ext.lower() not in [".yaml", ".yml"]:
38+
continue
39+
path = os.path.join(directory[0], file)
40+
41+
with open(path) as f:
42+
event_data = yaml.safe_load(f)
43+
try:
44+
validate_event(event_data)
45+
if not args.dry:
46+
event_date = datetime.strptime(
47+
event_data["date"]["start"], "%Y-%m-%d"
48+
).date()
49+
OUTPUT[school_year_from_date(event_date)].append(event_data)
50+
print(".", end="", flush=True)
51+
except JsonSchemaException as e:
52+
ERRORS.append(ErrorData(path, e.message))
53+
print("F", end="", flush=True)
54+
55+
print("\n")
56+
57+
if len(ERRORS):
58+
for error in ERRORS:
59+
print("Error validating file %s:\n\t%s" % (error.file, error.message))
60+
sys.exit(1)
61+
62+
if not args.dry:
63+
os.makedirs(os.path.join(ROOT, "build"), exist_ok=True)
64+
for year, events in OUTPUT.items():
65+
print("Writing output for year %s." % (year))
66+
67+
with open(os.path.join(ROOT, "build", "%s.json" % (year)), "w") as f:
68+
json.dump(events, f)

data/.gitkeep

Whitespace-only changes.

example.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Nazov udalosti
2+
name: Naboj JUNIOR
3+
4+
# Vedne obory (1+) pre danu akciu. Mozne hodnoty: mat, fyz, inf a other.
5+
sciences:
6+
- mat
7+
- fyz
8+
9+
# Typ akcie. Moznosti: sutaz, seminar, sustredenie, vikendovka, tabor, olympiada alebo prednasky
10+
type: sutaz
11+
12+
# Informacie k datumom.
13+
date:
14+
# Zaciatok udalosti alebo datum udalosti, ak trva iba den. Format YYYY-MM-DD
15+
start: "2020-01-01"
16+
# Koniec udalosti (optional) YYYY-MM-DD
17+
end: "2020-01-02"
18+
# Text, ktory sa zobrazi miesto datumu. (optional) Vyuzitie ak nie je este znamy presny datum napr.
19+
text: "január/február"
20+
21+
# nazov organizatora alebo zoznam organizatorov
22+
organizers:
23+
- trojsten
24+
- strom
25+
26+
# Odkaz na stranku pre viac info (optional)
27+
link: https://junior.naboj.org/
28+
29+
# Miesto, alebo zoznam miest, kde sa akcia kona
30+
place: Bratislava
31+
32+
# Informacie o sutaziacich.
33+
contestants:
34+
# Minimalny rocnik, ktory sa moze zapojit.
35+
# Mozne hodnoty: zs1, zs2, ..., zs9; ss1, ..., ss4
36+
min: zs5
37+
# Maximalny rocnik, ktory sa moze zapojit.
38+
max: zs9
39+
40+
# Udaje k notifikaciam. (optional)
41+
notifications:
42+
# Pocet dni (alebo zoznam poctu dni), kedy sa ma posielat mail zaujemcom.
43+
remind_days_before: 5

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fastjsonschema==2.14.4
2+
pkg-resources==0.0.0
3+
PyYAML==5.3.1

schemas/event.schema.json

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://kockatykalendar.sk/event.schema.json",
4+
"title": "Event",
5+
"description": "An event in the calendar.",
6+
"type": "object",
7+
"properties": {
8+
"name": {
9+
"description": "Name of the event",
10+
"type": "string"
11+
},
12+
"sciences": {
13+
"description": "Event sciences",
14+
"type": "array",
15+
"items": {
16+
"type": "string",
17+
"enum": ["mat", "fyz", "inf", "other"]
18+
},
19+
"minItems": 1,
20+
"uniqueItems": true
21+
},
22+
"type": {
23+
"description": "Event type",
24+
"type": "string",
25+
"enum": ["sutaz", "seminar", "sustredenie", "vikendovka", "tabor", "olympiada", "prednasky"]
26+
},
27+
"date": {
28+
"description": "Date information",
29+
"type": "object",
30+
"properties": {
31+
"start": {
32+
"description": "Event date or start date if more days",
33+
"type": "string",
34+
"format": "date"
35+
},
36+
"end": {
37+
"description": "End date",
38+
"type": "string",
39+
"format": "date"
40+
},
41+
"text": {
42+
"description": "Event date for user",
43+
"type": "string"
44+
}
45+
},
46+
"required": ["start"]
47+
},
48+
"organizers": {
49+
"description": "Organizer or list of organizers",
50+
"type": ["array", "string"],
51+
"items": {
52+
"type": "string"
53+
}
54+
},
55+
"link": {
56+
"description": "Link to more information",
57+
"type": "string"
58+
},
59+
"place": {
60+
"description": "Place where the event is organized",
61+
"type": ["string", "array"],
62+
"items": {
63+
"type": "string"
64+
}
65+
},
66+
"contestants": {
67+
"description": "Contestant information",
68+
"type": "object",
69+
"properties": {
70+
"min": {
71+
"description": "Min class to attend",
72+
"type": "string",
73+
"pattern": "^(zs[1-9]|ss[1-4])$"
74+
},
75+
"max": {
76+
"description": "Max class to attend",
77+
"type": "string",
78+
"pattern": "^(zs[1-9]|ss[1-4])$"
79+
}
80+
},
81+
"required": ["min"]
82+
},
83+
"notifications": {
84+
"description": "Notification settings",
85+
"type": "object",
86+
"properties": {
87+
"remind_days_before": {
88+
"description": "How many days before should we notify people.",
89+
"type": ["number", "array"],
90+
"items": {
91+
"type": "array"
92+
}
93+
}
94+
},
95+
"required": ["remind_days_before"]
96+
}
97+
},
98+
"required": [ "name", "sciences", "type", "date", "organizers", "place", "contestants" ]
99+
}
100+

0 commit comments

Comments
 (0)