Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "usf"
version = "0.1.2"
version = "0.2.0"
description = "A lightweight Universal Schedule Format (USF) parser and generator."
authors = [{ name = "USF-org", email = "USF-org@outlook.com" }]
license = { text = "MIT" }
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jsonschema==4.0.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="usf",
version="0.1.2",
version="0.2.0",
packages=find_packages(),
install_requires=[],
python_requires=">=3.6",
Expand Down
38 changes: 36 additions & 2 deletions tests/test_usf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def setUp(self):
self.generator.add_schedule(1, "all", "Physics", 2) # 星期一,第二节,物理

# 生成 USF 数据
self.usf_data = self.generator.generate_usf_data()
self.usf_data = self.generator.generate()

def test_usf_generation(self):
"""测试 USF 生成"""
Expand All @@ -38,7 +38,10 @@ def test_usf_generation(self):

def test_usf_parser(self):
"""测试 USF 解析"""
# 将生成的 USF 数据转为 JSON 字符串
json_data = json.dumps(self.usf_data, indent=2)

# 使用解析器解析数据
parser = USFParser(json_data)

self.assertEqual(len(parser.get_subjects()), 2)
Expand All @@ -63,7 +66,38 @@ def test_usf_validation(self):
"subjects": self.usf_data["subjects"],
"periods": self.usf_data["periods"]
}
self.assertFalse(validator.validate(invalid_data))

# 这里我们添加了额外的错误处理来捕获无效数据的校验错误
with self.assertRaises(jsonschema.exceptions.ValidationError):
validator.validate(invalid_data)

def test_invalid_period(self):
"""测试无效时间段"""
invalid_periods_data = {
"version": 1,
"subjects": self.usf_data["subjects"],
"periods": [["invalid", "format"]],
"timetable": self.usf_data["timetable"]
}

# 使用校验器验证无效的时间段数据
validator = USFValidator()
with self.assertRaises(jsonschema.exceptions.ValidationError):
validator.validate(invalid_periods_data)

def test_invalid_subject(self):
"""测试无效科目"""
invalid_subjects_data = {
"version": 1,
"subjects": {"InvalidSubject": {}},
"periods": self.usf_data["periods"],
"timetable": self.usf_data["timetable"]
}

# 使用校验器验证无效的科目数据
validator = USFValidator()
with self.assertRaises(jsonschema.exceptions.ValidationError):
validator.validate(invalid_subjects_data)

if __name__ == "__main__":
unittest.main()
18 changes: 16 additions & 2 deletions usf/generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re

class USFGenerator:
def __init__(self, version=1):
Expand All @@ -23,6 +24,9 @@ def add_subject(self, name, simplified_name=None, teacher=None, room=None):
teacher (str, optional): Teacher's name.
room (str, optional): Classroom.
"""
if name in self.subjects:
raise ValueError(f"Subject '{name}' already exists.")

self.subjects[name] = {
"simplified_name": simplified_name or name,
"teacher": teacher or "",
Expand All @@ -40,6 +44,10 @@ def add_period(self, start_time, end_time):
Returns:
int: Index of the added period (1-based).
"""
time_pattern = "^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$"
if not re.match(time_pattern, start_time) or not re.match(time_pattern, end_time):
raise ValueError(f"Invalid time format. Must be 'HH:MM:SS'.")

self.periods.append([start_time, end_time])
return len(self.periods) # Return 1-based index

Expand All @@ -57,6 +65,9 @@ def add_schedule(self, day, week_type, subject, period_index):
raise ValueError(f"Subject '{subject}' is not defined.")
if period_index < 1 or period_index > len(self.periods):
raise ValueError(f"Invalid period index {period_index}.")
if week_type not in ["all", "even", "odd"]:
raise ValueError(f"Invalid week type '{week_type}'. Must be 'all', 'even', or 'odd'.")

self.timetable.append([day, week_type, subject, period_index])

def generate(self):
Expand All @@ -80,5 +91,8 @@ def save_to_file(self, filename):
Args:
filename (str): Output filename.
"""
with open(filename, "w", encoding="utf-8") as f:
json.dump(self.generate(), f, indent=2, ensure_ascii=False)
try:
with open(filename, "w", encoding="utf-8") as f:
json.dump(self.generate(), f, indent=2, ensure_ascii=False)
except Exception as e:
raise IOError(f"Failed to save USF data to {filename}: {e}")
9 changes: 7 additions & 2 deletions usf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ def __init__(self, filename):
Args:
filename (str): Path to USF JSON file.
"""
with open(filename, "r", encoding="utf-8") as f:
self.data = json.load(f)
try:
with open(filename, "r", encoding="utf-8") as f:
self.data = json.load(f)
except FileNotFoundError:
raise FileNotFoundError(f"File '{filename}' not found.")
except json.JSONDecodeError:
raise ValueError(f"Failed to decode JSON from the file '{filename}'.")

def get_subjects(self):
"""Return the subjects dictionary."""
Expand Down
17 changes: 12 additions & 5 deletions usf/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ def __init__(self, schema_file="schema.json"):
Args:
schema_file (str, optional): Path to JSON Schema. Defaults to "schema.json".
"""
with open(schema_file, "r", encoding="utf-8") as f:
self.schema = json.load(f)
try:
with open(schema_file, "r", encoding="utf-8") as f:
self.schema = json.load(f)
except FileNotFoundError:
raise FileNotFoundError(f"Schema file '{schema_file}' not found.")
except json.JSONDecodeError:
raise ValueError(f"Failed to decode JSON from the schema file '{schema_file}'.")

def validate(self, data):
"""
Expand All @@ -23,6 +28,8 @@ def validate(self, data):
Returns:
bool: True if valid, raises an error if invalid.
"""
validate(instance=data, schema=self.schema)
return True

try:
validate(instance=data, schema=self.schema)
except jsonschema.exceptions.ValidationError as e:
raise ValueError(f"USF data validation failed: {e.message}")
return True