-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.py
81 lines (76 loc) · 3.29 KB
/
context.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
from dataclasses import Field
from datetime import datetime
import os
from typing import Optional
from pydantic import BaseModel
from data import ContextData
from storage import ContextStorage
import logging
logger = logging.getLogger(__name__)
class Context():
"""Note: This is a shared contract with the user interface , currently just raycast."""
def __init__(self,storage: ContextStorage):
self.storage = storage
self._current_context = None
def get(self,id: Optional[int]=None, name: Optional[str]=None) -> (ContextData | None):
"""Get a context by id or name"""
return self.storage.get_context(context_id=id,name=name)
def create(self,name: str, id: Optional[int]=None, description: str = "") -> ContextData:
"""Create a new context"""
try:
# Check if context already exists
existing_context = self.storage.get_context(context_id=id,name=name)
if existing_context:
logger.info(f"Context \"{name}\" already exists: {existing_context.id}")
self._current_context = existing_context
return existing_context
else:
logger.info(f"Creating new context: {id} {name} {description}")
context_data = ContextData(id=id,name=name,color="#FF6B6B",last_active=datetime.now(),description=description)
id = self.storage.create_context(context_data)
context_data.id = id
self._current_context = context_data
return context_data
except Exception as e:
logger.error(f"Failed to create context: {e}")
raise e
def _load_current_context(self) -> ContextData:
"""
Load current context from interface file, falling back to defaults if not found
"""
try:
current = self.storage.get_last_active_context()
if current:
self._current_context = ContextData(
id=current.id,
name=current.name,
description=current.description,
color=current.color,
last_active=current.last_active
)
logger.info(f"Loaded context : {current['id']}")
return self._current_context
else:
logger.error("No current context found in DB, using default")
self._current_context = ContextData(
id='work',
name='work',
description='Default context',
color='#FF6B6B',
last_active=datetime.now()
)
logger.debug("Initialized default context: work")
return self._current_context
except Exception as e:
logger.warning(f"Failed to load previous context : {e}")
# Fall back to default if file doesn't exist or no current context found
self._current_context = ContextData(
id='work',
name='work',
color='#FF6B6B',
last_active=datetime.now(),
notes=[],
resources=[]
)
logger.debug("Initialized default context: work")
return self._current_context