|
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: |
|
|
|
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: |
|
|
|
section_data = load_json(section) |
|
|
|
|
|
if "projects" not in section_data: |
|
section_data["projects"] = [] |
|
|
|
section_data["projects"].append(project_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: |
|
|
|
section_data = load_json(section) |
|
|
|
|
|
if "projects" not in section_data or project_index >= len(section_data["projects"]): |
|
print(f"Invalid project index: {project_index}") |
|
return False |
|
|
|
|
|
section_data["projects"][project_index] = project_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: |
|
|
|
section_data = load_json(section) |
|
|
|
|
|
if "projects" not in section_data or project_index >= len(section_data["projects"]): |
|
print(f"Invalid project index: {project_index}") |
|
return False |
|
|
|
|
|
del section_data["projects"][project_index] |
|
|
|
|
|
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: |
|
|
|
section_data = load_json(section) |
|
|
|
|
|
if "skills" not in section_data or category_index >= len(section_data["skills"]): |
|
print(f"Invalid category index: {category_index}") |
|
return False |
|
|
|
|
|
section_data["skills"][category_index]["items"].append(skill) |
|
|
|
|
|
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: |
|
|
|
section_data = load_json(section) |
|
|
|
|
|
section_data["intro"] = intro_text |
|
|
|
|
|
return save_json(section_data, section) |
|
except Exception as e: |
|
print(f"Error updating intro for {section}: {e}") |
|
return False |