Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/session/storage.py: 76%
45 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from abc import ABC, abstractmethod
4from marshmallow import Schema, fields
7class SessionStorageSchema(Schema):
8 sessionid = fields.UUID()
9 token = fields.Str()
10 last_access = fields.Float()
11 operator = fields.Bool()
12 data = fields.Dict()
15class SessionStorage(ABC):
16 """The abstract session storage interface"""
18 _generate_token = None
20 @staticmethod
21 def set_generate_token(fn):
22 """Set a method from which to generate the session token
24 The function should return a token that can be stored into the session
25 """
26 if not callable(fn):
27 raise AttributeError("Token generation function must be callable")
29 SessionStorage._generate_token = fn
31 @abstractmethod
32 def create(self, data):
33 """Create a new session
35 Args:
36 data (dict): Any data to store along side the basic session info
38 Returns:
39 session: A `SessionStorageSchema` validated dict
40 """
41 pass
43 @abstractmethod
44 def remove(self, sessionid, token=None):
45 """Remove a session
47 Args:
48 sessionid (uuid): The sessionid to remove
50 Kwargs:
51 token (str): The token to remove if sessionid is none
53 Returns:
54 sessionid (uuid): The sessionid removed
56 """
57 pass
59 @abstractmethod
60 def list(self):
61 """List existing sessions
63 Returns:
64 sessions (list(dict)): A list of `SessionStorageSchema` validated dicts
65 """
66 pass
68 @abstractmethod
69 def get(self, sessionid):
70 """Get a session
72 Args:
73 sessionid (uuid): The sessionid
75 Returns:
76 session: A `SessionStorageSchema` validated dict
77 """
78 pass
80 @abstractmethod
81 def exists(self, sessionid):
82 """Check if a session exists
84 Args:
85 sessionid (uuid): The sessionid
87 Returns:
88 True if the session exists, False if not
89 """
90 pass
92 @abstractmethod
93 def update(self, sessionid, **kwargs):
94 """Update a a value in the data dict of a session
96 Updates a data property on the session and updates last_access time
97 Args:
98 sessionid (uuid): The sessionid
100 Kwargs:
101 The properties in the data dict to update
103 """
104 pass
106 @abstractmethod
107 def take_control(self, sessionid):
108 """Set operator=True for the specified session
110 Sets the operator to True for this sessionid and to false for all the other existing
111 sessions
113 Args:
114 sessionid (uuid): The sessionid
115 """
116 pass
118 @abstractmethod
119 def yield_control(self, sessionid):
120 """Set operator=False for the specified session
122 If the current session has control yield it. Return true if control was yielded
124 Args:
125 sessionid (uuid): The sessionid
126 """
127 pass
129 @abstractmethod
130 def has_control(self, sessionid):
131 """Check if this session has control
133 Args:
134 sessionid (uuid): The sessionid
136 Returns:
137 True if sessionid operator=True, False if not
138 """
139 pass
141 @abstractmethod
142 def no_control(self):
143 """Check if someone in the application has control
145 Returns:
146 True if no one has control
147 """
148 pass