270 lines
10 KiB
Python
270 lines
10 KiB
Python
# project_manager.py
|
|
#
|
|
# Copyright 2025 Pavel Baksy
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
#
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import json
|
|
import uuid
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
from datetime import datetime, timezone
|
|
import gi
|
|
gi.require_version('GLib', '2.0')
|
|
from gi.repository import GLib
|
|
from .models import Project, SavedRequest, HttpRequest, Environment
|
|
|
|
|
|
class ProjectManager:
|
|
"""Manages project and saved request persistence."""
|
|
|
|
def __init__(self):
|
|
# Use XDG data directory (works for both Flatpak and native)
|
|
# Flatpak: ~/.var/app/cz.vesp.roster/data/cz.vesp.roster
|
|
# Native: ~/.local/share/cz.vesp.roster
|
|
self.data_dir = Path(GLib.get_user_data_dir()) / 'cz.vesp.roster'
|
|
self.projects_file = self.data_dir / 'requests.json'
|
|
self._ensure_data_dir()
|
|
|
|
def _ensure_data_dir(self):
|
|
"""Create data directory if it doesn't exist."""
|
|
self.data_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def load_projects(self) -> List[Project]:
|
|
"""Load projects from JSON file."""
|
|
if not self.projects_file.exists():
|
|
return []
|
|
|
|
try:
|
|
with open(self.projects_file, 'r') as f:
|
|
data = json.load(f)
|
|
return [Project.from_dict(p) for p in data.get('projects', [])]
|
|
except Exception as e:
|
|
print(f"Error loading projects: {e}")
|
|
return []
|
|
|
|
def save_projects(self, projects: List[Project]):
|
|
"""Save projects to JSON file."""
|
|
try:
|
|
data = {
|
|
'version': 1,
|
|
'projects': [p.to_dict() for p in projects]
|
|
}
|
|
with open(self.projects_file, 'w') as f:
|
|
json.dump(data, f, indent=2)
|
|
except Exception as e:
|
|
print(f"Error saving projects: {e}")
|
|
|
|
def add_project(self, name: str) -> Project:
|
|
"""Create new project with default environment."""
|
|
projects = self.load_projects()
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
|
|
# Create default environment
|
|
default_env = Environment(
|
|
id=str(uuid.uuid4()),
|
|
name="Default",
|
|
variables={},
|
|
created_at=now
|
|
)
|
|
|
|
project = Project(
|
|
id=str(uuid.uuid4()),
|
|
name=name,
|
|
requests=[],
|
|
created_at=now,
|
|
variable_names=[],
|
|
environments=[default_env]
|
|
)
|
|
projects.append(project)
|
|
self.save_projects(projects)
|
|
return project
|
|
|
|
def update_project(self, project_id: str, new_name: str = None, new_icon: str = None):
|
|
"""Update a project's name and/or icon."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
if new_name is not None:
|
|
p.name = new_name
|
|
if new_icon is not None:
|
|
p.icon = new_icon
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def delete_project(self, project_id: str):
|
|
"""Delete a project and all its requests."""
|
|
projects = self.load_projects()
|
|
projects = [p for p in projects if p.id != project_id]
|
|
self.save_projects(projects)
|
|
|
|
def add_request(self, project_id: str, name: str, request: HttpRequest) -> SavedRequest:
|
|
"""Add request to a project."""
|
|
projects = self.load_projects()
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
saved_request = SavedRequest(
|
|
id=str(uuid.uuid4()),
|
|
name=name,
|
|
request=request,
|
|
created_at=now,
|
|
modified_at=now
|
|
)
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
p.requests.append(saved_request)
|
|
break
|
|
self.save_projects(projects)
|
|
return saved_request
|
|
|
|
def find_request_by_name(self, project_id: str, name: str) -> Optional[SavedRequest]:
|
|
"""Find a request by name within a project. Returns None if not found."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
for req in p.requests:
|
|
if req.name == name:
|
|
return req
|
|
break
|
|
return None
|
|
|
|
def update_request(self, project_id: str, request_id: str, name: str, request: HttpRequest) -> SavedRequest:
|
|
"""Update an existing request."""
|
|
projects = self.load_projects()
|
|
updated_request = None
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
for req in p.requests:
|
|
if req.id == request_id:
|
|
req.name = name
|
|
req.request = request
|
|
req.modified_at = datetime.now(timezone.utc).isoformat()
|
|
updated_request = req
|
|
break
|
|
break
|
|
if updated_request:
|
|
self.save_projects(projects)
|
|
return updated_request
|
|
|
|
def delete_request(self, project_id: str, request_id: str):
|
|
"""Delete a saved request."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
p.requests = [r for r in p.requests if r.id != request_id]
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def add_environment(self, project_id: str, name: str) -> Environment:
|
|
"""Add environment to a project."""
|
|
projects = self.load_projects()
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
environment = None
|
|
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
# Create environment with empty values for all variables
|
|
variables = {var_name: "" for var_name in p.variable_names}
|
|
environment = Environment(
|
|
id=str(uuid.uuid4()),
|
|
name=name,
|
|
variables=variables,
|
|
created_at=now
|
|
)
|
|
p.environments.append(environment)
|
|
break
|
|
|
|
self.save_projects(projects)
|
|
return environment
|
|
|
|
def update_environment(self, project_id: str, env_id: str, name: str = None, variables: dict = None):
|
|
"""Update an environment's name and/or variables."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
for env in p.environments:
|
|
if env.id == env_id:
|
|
if name is not None:
|
|
env.name = name
|
|
if variables is not None:
|
|
env.variables = variables
|
|
break
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def delete_environment(self, project_id: str, env_id: str):
|
|
"""Delete an environment."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
p.environments = [e for e in p.environments if e.id != env_id]
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def add_variable(self, project_id: str, variable_name: str):
|
|
"""Add a variable to the project and all environments."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
if variable_name not in p.variable_names:
|
|
p.variable_names.append(variable_name)
|
|
# Add empty value to all environments
|
|
for env in p.environments:
|
|
env.variables[variable_name] = ""
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def rename_variable(self, project_id: str, old_name: str, new_name: str):
|
|
"""Rename a variable in the project and all environments."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
# Update variable_names list
|
|
if old_name in p.variable_names:
|
|
idx = p.variable_names.index(old_name)
|
|
p.variable_names[idx] = new_name
|
|
# Update all environments
|
|
for env in p.environments:
|
|
if old_name in env.variables:
|
|
env.variables[new_name] = env.variables.pop(old_name)
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def delete_variable(self, project_id: str, variable_name: str):
|
|
"""Delete a variable from the project and all environments."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
# Remove from variable_names
|
|
if variable_name in p.variable_names:
|
|
p.variable_names.remove(variable_name)
|
|
# Remove from all environments
|
|
for env in p.environments:
|
|
env.variables.pop(variable_name, None)
|
|
break
|
|
self.save_projects(projects)
|
|
|
|
def update_environment_variable(self, project_id: str, env_id: str, variable_name: str, value: str):
|
|
"""Update a single variable value in an environment."""
|
|
projects = self.load_projects()
|
|
for p in projects:
|
|
if p.id == project_id:
|
|
for env in p.environments:
|
|
if env.id == env_id:
|
|
env.variables[variable_name] = value
|
|
break
|
|
break
|
|
self.save_projects(projects)
|