-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_manager.py
More file actions
111 lines (94 loc) · 3.92 KB
/
tutorial_manager.py
File metadata and controls
111 lines (94 loc) · 3.92 KB
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
103
104
105
106
107
108
109
110
111
import json
from pathlib import Path
import shutil
import os
class TutorialManager:
def __init__(self):
self.tutorial_dir = Path("tutorials")
self.tutorial_dir.mkdir(exist_ok=True)
def save_tutorial(self, name, tutorial_data):
"""Save a tutorial to an individual JSON file."""
tutorial_path = self.tutorial_dir / f"{name}.json"
with open(tutorial_path, 'w') as f:
json.dump(tutorial_data, f, indent=4)
def load_tutorials(self):
"""Load all tutorials from individual JSON files."""
tutorials = {}
# Get all JSON files in the tutorial directory
for tutorial_file in self.tutorial_dir.glob("*.json"):
try:
name = tutorial_file.stem # Filename without extension
tutorial_data = self.get_tutorial(name)
if tutorial_data:
tutorials[name] = tutorial_data
except json.JSONDecodeError:
print(f"Error loading tutorial file: {tutorial_file}")
continue
return tutorials
def get_tutorial(self, tutorial_name):
"""Get a specific tutorial by name by loading its file."""
tutorial_path = self.tutorial_dir / f"{tutorial_name}.json"
if tutorial_path.exists():
try:
with open(tutorial_path, 'r') as f:
return json.load(f)
except json.JSONDecodeError:
print(f"Error loading tutorial file: {tutorial_path}")
return None
return None
def add_tutorial(self, name, title, steps):
"""Add a new tutorial as an individual file."""
tutorial_data = {
"title": title,
"steps": steps
}
self.save_tutorial(name, tutorial_data)
def delete_tutorial(self, tutorial_name):
"""Delete a tutorial by removing its file."""
tutorial_path = self.tutorial_dir / f"{tutorial_name}.json"
if tutorial_path.exists():
tutorial_path.unlink()
return True
return False
def export_tutorial(self, tutorial_name, export_path):
"""Export a tutorial to a specified location.
Args:
tutorial_name: Name of the tutorial to export
export_path: Path where the tutorial will be exported
Returns:
bool: True if export was successful, False otherwise
"""
tutorial_path = self.tutorial_dir / f"{tutorial_name}.json"
if not tutorial_path.exists():
return False
try:
shutil.copy(tutorial_path, export_path)
return True
except Exception as e:
print(f"Error exporting tutorial: {str(e)}")
return False
def import_tutorial(self, import_path):
"""Import a tutorial from a specified location.
Args:
import_path: Path to the tutorial JSON file to import
Returns:
bool: True if import was successful, False otherwise
"""
if not os.path.exists(import_path):
return False
try:
# Validate that it's a proper tutorial file by loading it
with open(import_path, 'r') as f:
tutorial_data = json.load(f)
# Check if it has the required fields
if not all(key in tutorial_data for key in ['title', 'steps']):
return False
# Get the tutorial name from the file name
tutorial_name = Path(import_path).stem
# Copy the file to the tutorials directory
destination = self.tutorial_dir / f"{tutorial_name}.json"
shutil.copy(import_path, destination)
return True
except Exception as e:
print(f"Error importing tutorial: {str(e)}")
return False