File size: 3,760 Bytes
bb76062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ===== FILE: services/project_manager.py (NEW FILE) =====
import os
import re
from datetime import datetime

class ProjectManager:
    def __init__(self, data_directory):
        """
        Initializes the manager for persistent academic and scientific projects.
        """
        self.base_directory = data_directory
        self.projects_dir = os.path.join(self.base_directory, "Projects")
        os.makedirs(self.projects_dir, exist_ok=True)
        print("Project Manager says: Persistent workspace is online.", flush=True)

    def _sanitize_filename(self, name: str) -> str:
        """
        Sanitizes a user-provided project name into a safe filename.
        """
        # Remove invalid characters
        name = re.sub(r'[\\/*?:"<>|]', "", name)
        # Replace spaces with underscores
        name = name.replace(" ", "_")
        return name

    def list_projects(self) -> list[str]:
        """
        Lists all existing project files in the projects directory.
        """
        try:
            files = [f for f in os.listdir(self.projects_dir) if f.endswith(".txt")]
            # Return the name without the .txt extension
            project_names = [os.path.splitext(f)[0].replace("_", " ") for f in files]
            project_names.sort()
            return project_names
        except Exception as e:
            print(f"Project Manager ERROR: Could not list projects. Reason: {e}", flush=True)
            return []

    def start_project(self, project_name: str) -> str:
        """
        Returns initial template content for a new project.
        Does not save anything to disk until save_project is called.
        """
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
        initial_content = (
            f"# PROJECT: {project_name}\n"
            f"# STARTED: {timestamp}\n"
            f"# AETHERIUS'S WORKSPACE\n"
            f"--------------------------------------------------\n\n"
        )
        return initial_content

    def save_project(self, project_name: str, content: str):
        """
        Saves the content of a project to a text file.
        """
        if not project_name or not project_name.strip():
            print("Project Manager WARNING: Save attempt with empty project name.", flush=True)
            return

        safe_filename = self._sanitize_filename(project_name) + ".txt"
        filepath = os.path.join(self.projects_dir, safe_filename)

        try:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"Project Manager: Successfully saved project '{project_name}' to {filepath}", flush=True)
        except Exception as e:
            print(f"Project Manager ERROR: Could not save project '{project_name}'. Reason: {e}", flush=True)

    def load_project(self, project_name: str) -> str | None:
        """
        Loads the content of a project from a text file.
        Returns None if the project does not exist.
        """
        safe_filename = self._sanitize_filename(project_name) + ".txt"
        filepath = os.path.join(self.projects_dir, safe_filename)

        if not os.path.exists(filepath):
            print(f"Project Manager WARNING: Attempted to load non-existent project '{project_name}'.", flush=True)
            return None

        try:
            with open(filepath, 'r', encoding='utf-8') as f:
                content = f.read()
            print(f"Project Manager: Successfully loaded project '{project_name}'.", flush=True)
            return content
        except Exception as e:
            print(f"Project Manager ERROR: Could not load project '{project_name}'. Reason: {e}", flush=True)
            return f"// ERROR: Could not load project file. Reason: {e} //"