File size: 4,942 Bytes
8ae67aa |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
import json
import base64
import os
def load_json(filename, folder="content"):
"""Load JSON data from specified folder"""
try:
with open(f"{folder}/{filename}.json", "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
print(f"Error loading {filename}.json: {e}")
return {}
def save_json(data, filename, folder="content"):
"""Save JSON data to specified folder"""
try:
# Create folder if it doesn't exist
if not os.path.exists(folder):
os.makedirs(folder)
with open(f"{folder}/{filename}.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
return True
except Exception as e:
print(f"Error saving {filename}.json: {e}")
return False
def file_to_data_uri(filepath, mime_type="application/pdf"):
"""Convert file to data URI"""
try:
with open(filepath, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode("utf-8")
return f"data:{mime_type};base64,{b64}"
except Exception as e:
print(f"Error converting file to data URI: {e}")
return None
def image_to_data_uri(filepath, mime_type="image/jpeg"):
"""Convert image to data URI"""
try:
with open(filepath, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode("utf-8")
return f"data:{mime_type};base64,{b64}"
except Exception as e:
print(f"Error converting image to data URI: {e}")
return None
def add_project(section, project_data):
"""Add a new project to a specific section"""
try:
# Load the current data
section_data = load_json(section)
# Add the new project
if "projects" not in section_data:
section_data["projects"] = []
section_data["projects"].append(project_data)
# Save the updated data
return save_json(section_data, section)
except Exception as e:
print(f"Error adding project to {section}: {e}")
return False
def update_project(section, project_index, project_data):
"""Update an existing project in a specific section"""
try:
# Load the current data
section_data = load_json(section)
# Check if the index is valid
if "projects" not in section_data or project_index >= len(section_data["projects"]):
print(f"Invalid project index: {project_index}")
return False
# Update the project
section_data["projects"][project_index] = project_data
# Save the updated data
return save_json(section_data, section)
except Exception as e:
print(f"Error updating project in {section}: {e}")
return False
def delete_project(section, project_index):
"""Delete a project from a specific section"""
try:
# Load the current data
section_data = load_json(section)
# Check if the index is valid
if "projects" not in section_data or project_index >= len(section_data["projects"]):
print(f"Invalid project index: {project_index}")
return False
# Delete the project
del section_data["projects"][project_index]
# Save the updated data
return save_json(section_data, section)
except Exception as e:
print(f"Error deleting project from {section}: {e}")
return False
def update_profile(profile_data):
"""Update the profile information"""
try:
return save_json(profile_data, "profile")
except Exception as e:
print(f"Error updating profile: {e}")
return False
def add_skill(section, category_index, skill):
"""Add a new skill to a specific category in a section"""
try:
# Load the current data
section_data = load_json(section)
# Check if the category index is valid
if "skills" not in section_data or category_index >= len(section_data["skills"]):
print(f"Invalid category index: {category_index}")
return False
# Add the skill
section_data["skills"][category_index]["items"].append(skill)
# Save the updated data
return save_json(section_data, section)
except Exception as e:
print(f"Error adding skill to {section}: {e}")
return False
def update_section_intro(section, intro_text):
"""Update the introduction text of a section"""
try:
# Load the current data
section_data = load_json(section)
# Update the intro
section_data["intro"] = intro_text
# Save the updated data
return save_json(section_data, section)
except Exception as e:
print(f"Error updating intro for {section}: {e}")
return False |