33from typing import get_type_hints , get_origin , get_args , TypeVar , Tuple , Type
44from types import UnionType , NoneType
55from . import io_safe
6+ from . sessions import SessionFileFull , SessionFileKey , SessionFileWhere , SessionDirFull , SessionDirWhere
67
78
89
@@ -67,40 +68,46 @@ class FileDictModel(ABC):
6768 __item_model__ : T = None
6869
6970 @classmethod
70- def get_by_key (cls , key ) -> T :
71+ def get_at_key (cls , key ) -> T :
7172 """
7273 Gets an item by key.
7374 The data is partially read from the __file__.
7475 """
7576 return cls .__item_model__ .partial_read_by_key (key )
7677
78+ @classmethod
79+ def session_at_key (cls , key ):
80+ return cls .__item_model__ .session (key )
81+
7782 @classmethod
7883 def items (cls ) -> list [Tuple [str , T ]]:
7984 """
80- Gets all items.
85+ Gets all items as a list of tuples (key, ORM model of value) .
8186 """
8287 data = io_safe .read (cls .__file__ )
83- return [cls .__item_model__ (key , value ) for key , value in data .items ()]
88+ return [( k , cls .__item_model__ (k , v )) for k , v in data .items ()]
8489
8590 @classmethod
8691 def session (cls ):
87- ...
88-
89-
90- @classmethod
91- def session_at_key (cls , key ):
92- ...
93-
94-
92+ """
93+ Enter a session with the file as (session, data) where data is a dict of
94+ <key>: <ORM model of value> pairs.
95+ """
96+ def make_session_obj_from_dict (data ):
97+ sess_obj = {}
98+ for k , v in data .items ():
99+ sess_obj [k ] = cls .__item_model__ .from_key_value (k , v )
100+ return sess_obj
101+ return SessionFileFull (cls .__file__ , make_session_obj_from_dict )
95102
96103
97104 @classmethod
98- def get_where (cls , where : callable ) -> list [T ]:
105+ def get_where (cls , where : callable ) -> list [Tuple [ str , T ] ]:
99106 """
100107 Gets all items where the where function returns True.
101108 The where function takes an object of type __item_model__.
102109 """
103- return [item for item in cls .get_all () if where (item )]
110+ return [( k , v ) for k , v in cls .items () if where (v )]
104111
105112 # Not Implemented:
106113 # - select by filter callback (file_where): Not implemented because no performance advantage.
@@ -121,6 +128,11 @@ def read_by_key(cls, key) -> T:
121128 data = io_safe .partial_read (cls .__file__ , key )
122129 return cls .from_key_value (key , data )
123130
131+ @classmethod
132+ def session (cls , key ):
133+ def partial_func (x ):
134+ return cls .from_key_value (key , x )
135+ return SessionFileKey (cls .__file__ , key , partial_func )
124136
125137
126138
@@ -131,7 +143,6 @@ def from_dict(cls, data) -> DictModel:
131143 obj = cls ()
132144 return fill_object_from_dict_using_type_hints (obj , cls , data )
133145
134-
135146 def to_dict (self ) -> dict :
136147 res = {}
137148 for var_name in get_type_hints (self ).keys ():
0 commit comments