-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
86 lines (70 loc) · 2.58 KB
/
utils.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
import yaml
from typing import Optional, Any
def read_yaml_file(file_path: str) -> Optional[Any]:
"""
Read YAML data from a file and return it as Python objects.
Args:
file_path (str): Path to the YAML file to be read.
Returns:
Optional[Any]: Python objects representing the YAML data, or None if there was an error.
Raises:
FileNotFoundError: If the specified file_path does not exist.
yaml.YAMLError: If there is an error in parsing the YAML file.
Example:
If you have a YAML file 'example.yaml' with contents:
```
key1: value1
key2: value2
```
You can read it and print its contents as follows:
```python
yaml_file_path = 'example.yaml'
yaml_data = read_yaml_file(yaml_file_path)
if yaml_data:
print("YAML data:")
print(yaml_data)
```
"""
try:
with open(file_path, 'r') as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
return yaml_data
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
return None
except yaml.YAMLError as e:
print(f"Error reading YAML file {file_path}: {e}")
return None
def write_html_css_js_to_file(plan: str, html: str, css: str, js: str) -> Optional[str]:
"""
Write HTML, CSS, JS, and a plan to respective files.
Args:
plan (str): Content of the plan to write to 'plan.md'.
html (str): Content of the HTML file to write to 'index.html'.
css (str): Content of the CSS file to write to 'style.css'.
js (str): Content of the JavaScript file to write to 'script.js'.
Returns:
Optional[str]: A success message if files are created successfully, None if an error occurs.
Example:
plan_content = "This is the plan."
html_content = "<html><body><h1>Hello, World!</h1></body></html>"
css_content = "body { background-color: lightblue; }"
js_content = "console.log('Hello, World!');"
result = write_html_css_js_to_file(plan_content, html_content, css_content, js_content)
if result:
print(result)
"""
file_contents = {
'plan.md': plan,
'index.html': html,
'style.css': css,
'script.js': js
}
try:
for filename, content in file_contents.items():
with open(filename, 'w') as f:
f.write(content)
except IOError as e:
print(f"Error writing to file: {e}")
return None
return "Files created successfully"