memory-oc 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- memory-oc-1.0.0/PKG-INFO +19 -0
- memory-oc-1.0.0/README.md +2 -0
- memory-oc-1.0.0/memory/__init__.py +270 -0
- memory-oc-1.0.0/memory/__main__.py +1 -0
- memory-oc-1.0.0/memory_oc.egg-info/PKG-INFO +19 -0
- memory-oc-1.0.0/memory_oc.egg-info/SOURCES.txt +11 -0
- memory-oc-1.0.0/memory_oc.egg-info/dependency_links.txt +1 -0
- memory-oc-1.0.0/memory_oc.egg-info/requires.txt +3 -0
- memory-oc-1.0.0/memory_oc.egg-info/top_level.txt +1 -0
- memory-oc-1.0.0/memory_oc.egg-info/zip-safe +1 -0
- memory-oc-1.0.0/setup.cfg +7 -0
- memory-oc-1.0.0/setup.py +30 -0
memory-oc-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: memory-oc
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A session library using redis
|
|
5
|
+
Home-page: https://ouroboroscoding.com/memory/
|
|
6
|
+
Author: Chris Nasr - Ouroboros Coding Inc.
|
|
7
|
+
Author-email: chris@ouroboroscoding.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Documentation, https://ouroboroscoding.com/memory/
|
|
10
|
+
Project-URL: Source, https://github.com/ouroboroscoding/memory-python
|
|
11
|
+
Project-URL: Tracker, https://github.com/ouroboroscoding/memory-python/issues
|
|
12
|
+
Keywords: session,redis
|
|
13
|
+
Platform: UNKNOWN
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# memory-python
|
|
18
|
+
A python session library using redis
|
|
19
|
+
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# coding=utf8
|
|
2
|
+
""" Memory
|
|
3
|
+
|
|
4
|
+
Handles internal sessions shared across requests
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
__author__ = "Chris Nasr"
|
|
9
|
+
__copyright__ = "Ouroboros Coding Inc."
|
|
10
|
+
__email__ = "chris@ouroboroscoding.com"
|
|
11
|
+
__created__ = "2023-03-15"
|
|
12
|
+
|
|
13
|
+
# Limit exports
|
|
14
|
+
__all__ = ['create', 'init', 'load']
|
|
15
|
+
|
|
16
|
+
# Ouroboros imports
|
|
17
|
+
from config import config
|
|
18
|
+
from jobject import jobject
|
|
19
|
+
import jsonb
|
|
20
|
+
from nredis import nr
|
|
21
|
+
|
|
22
|
+
# Python imports
|
|
23
|
+
import uuid
|
|
24
|
+
|
|
25
|
+
# Open redis connection
|
|
26
|
+
_moRedis = nr(config.memory.redis('session'))
|
|
27
|
+
|
|
28
|
+
def create(key: str = None, ttl: int = 0) -> _Memory:
|
|
29
|
+
"""Create
|
|
30
|
+
|
|
31
|
+
Returns a brand new session using the key given, else a UUID is generated
|
|
32
|
+
|
|
33
|
+
Arguments:
|
|
34
|
+
key (str): The key to use for the session
|
|
35
|
+
ttl (uint): Time to live, a specific expiry time in seconds
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
_Memory
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
# Init the data with the expires time
|
|
42
|
+
dData = { '__ttl': ttl }
|
|
43
|
+
|
|
44
|
+
# Create a new Memory using the passed key, or a new UUID
|
|
45
|
+
return _Memory(key and key or uuid.uuid4().hex, dData)
|
|
46
|
+
|
|
47
|
+
def load(key: str) -> _Memory:
|
|
48
|
+
"""Load
|
|
49
|
+
|
|
50
|
+
Loads an existing session from the cache
|
|
51
|
+
|
|
52
|
+
Arguments:
|
|
53
|
+
key (str): The unique id of an existing session
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
_Memory
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
# Fetch from Redis
|
|
60
|
+
s = _moRedis.get(key)
|
|
61
|
+
|
|
62
|
+
# If there's no session or it expired
|
|
63
|
+
if s == None: return None
|
|
64
|
+
|
|
65
|
+
# Make sure we have a string, not a set of bytes
|
|
66
|
+
try: s = s.decode()
|
|
67
|
+
except (UnicodeDecodeError, AttributeError): pass
|
|
68
|
+
|
|
69
|
+
# Create a new instance with the decoded data
|
|
70
|
+
return _Memory(key, jsonb.decode(s))
|
|
71
|
+
|
|
72
|
+
class _Memory(object):
|
|
73
|
+
"""Memory
|
|
74
|
+
|
|
75
|
+
A wrapper for the session data
|
|
76
|
+
|
|
77
|
+
Extends:
|
|
78
|
+
object
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, key: str, data: dict = {}):
|
|
82
|
+
"""Constructor
|
|
83
|
+
|
|
84
|
+
Intialises the instance, which is just setting up the dict
|
|
85
|
+
|
|
86
|
+
Arguments:
|
|
87
|
+
key (str): The key used to access or store the session
|
|
88
|
+
data (dict): The data in the session
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
_Memory
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
# Store the key and data
|
|
95
|
+
object.__setattr__(self, '__key', key)
|
|
96
|
+
object.__setattr__(self, '__store', jobject(data))
|
|
97
|
+
|
|
98
|
+
def __contains__(self, key: str):
|
|
99
|
+
"""__contains__
|
|
100
|
+
|
|
101
|
+
True if the key exists in the session
|
|
102
|
+
|
|
103
|
+
Arguments:
|
|
104
|
+
key (str): The field to check for
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
bool
|
|
108
|
+
"""
|
|
109
|
+
return object.__getattribute__(self, '__store').__contains__(key)
|
|
110
|
+
|
|
111
|
+
def __delitem__(self, k):
|
|
112
|
+
"""__delete__
|
|
113
|
+
|
|
114
|
+
Removes a key from a session
|
|
115
|
+
|
|
116
|
+
Arguments:
|
|
117
|
+
k (str): The key to remove
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
None
|
|
121
|
+
"""
|
|
122
|
+
del object.__getattribute__(self, '__store')[k]
|
|
123
|
+
|
|
124
|
+
def __getattr__(self, a: str) -> any:
|
|
125
|
+
"""__getattr__
|
|
126
|
+
|
|
127
|
+
Gives object notation access to get the internal dict keys
|
|
128
|
+
|
|
129
|
+
Arguments:
|
|
130
|
+
a (str): The attribute to get
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
AttributeError
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
any
|
|
137
|
+
"""
|
|
138
|
+
try:
|
|
139
|
+
return object.__getattribute__(self, '__store')[a]
|
|
140
|
+
except KeyError:
|
|
141
|
+
raise AttributeError(a, '%s not in Memory instance' % a)
|
|
142
|
+
|
|
143
|
+
def __getitem__(self, k):
|
|
144
|
+
"""__getitem__
|
|
145
|
+
|
|
146
|
+
Returns the given key
|
|
147
|
+
|
|
148
|
+
Arguments:
|
|
149
|
+
k (str): The key to return
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
any
|
|
153
|
+
"""
|
|
154
|
+
return object.__getattribute__(self, '__store').__getitem__(k)
|
|
155
|
+
|
|
156
|
+
def __iter__(self):
|
|
157
|
+
"""__iter__
|
|
158
|
+
|
|
159
|
+
Returns an iterator for the internal dict
|
|
160
|
+
|
|
161
|
+
Returns:
|
|
162
|
+
iterator
|
|
163
|
+
"""
|
|
164
|
+
return object.__getattribute__(self, '__store').__iter__()
|
|
165
|
+
|
|
166
|
+
def __len__(self):
|
|
167
|
+
"""__len__
|
|
168
|
+
|
|
169
|
+
Return the length of the internal dict
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
uint
|
|
173
|
+
"""
|
|
174
|
+
return object.__getattribute__(self, '__store').__len__()
|
|
175
|
+
|
|
176
|
+
def __setattr__(self, a: str, v: any) -> None:
|
|
177
|
+
"""__setattr__
|
|
178
|
+
|
|
179
|
+
Gives object notation access to set the internal dict keys
|
|
180
|
+
|
|
181
|
+
Arguments:
|
|
182
|
+
a (str): The key in the dict to set
|
|
183
|
+
v (any): The value to set on the key
|
|
184
|
+
"""
|
|
185
|
+
object.__getattribute__(self, '__store').__setitem__(a, v)
|
|
186
|
+
|
|
187
|
+
def __setitem__(self, k, v):
|
|
188
|
+
"""__setitem__
|
|
189
|
+
|
|
190
|
+
Sets the given key
|
|
191
|
+
|
|
192
|
+
Arguments:
|
|
193
|
+
k (str): The key to set
|
|
194
|
+
v (any): The value for the key
|
|
195
|
+
"""
|
|
196
|
+
object.__getattribute__(self, '__store').__setitem__(k, v)
|
|
197
|
+
|
|
198
|
+
def __str__(self):
|
|
199
|
+
"""__str__
|
|
200
|
+
|
|
201
|
+
Returns a string representation of the internal dict
|
|
202
|
+
|
|
203
|
+
Returns:
|
|
204
|
+
str
|
|
205
|
+
"""
|
|
206
|
+
return object.__getattribute__(self, '__store').__str__()
|
|
207
|
+
|
|
208
|
+
def close(self):
|
|
209
|
+
"""Close
|
|
210
|
+
|
|
211
|
+
Deletes the session from the cache
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
None
|
|
215
|
+
"""
|
|
216
|
+
_moRedis.delete(object.__getattribute__(self, '__key'))
|
|
217
|
+
|
|
218
|
+
def extend(self):
|
|
219
|
+
"""Extend
|
|
220
|
+
|
|
221
|
+
Keep the session alive by extending it's expire time by the internally \
|
|
222
|
+
set expire value, or else by the global one set for the module
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
None
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
# If the expire time is 0, do nothing
|
|
229
|
+
if object.__getattribute__(self, '__store')['__ttl'] == 0:
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
# Extend the session in Redis
|
|
233
|
+
_moRedis.expire(
|
|
234
|
+
object.__getattribute__(self, '__key'),
|
|
235
|
+
object.__getattribute__(self, '__store')['__ttl']
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
def key(self):
|
|
239
|
+
"""Key
|
|
240
|
+
|
|
241
|
+
Returns the key of the session
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
str
|
|
245
|
+
"""
|
|
246
|
+
return object.__getattribute__(self, '__key')
|
|
247
|
+
|
|
248
|
+
def save(self):
|
|
249
|
+
"""Save
|
|
250
|
+
|
|
251
|
+
Saves the current session data in the cache
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
None
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
# If we have no expire time, set forever
|
|
258
|
+
if object.__getattribute__(self, '__store')['__ttl'] == 0:
|
|
259
|
+
_moRedis.set(
|
|
260
|
+
object.__getattribute__(self, '__key'),
|
|
261
|
+
jsonb.encode(object.__getattribute__(self, '__store'))
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
# Else, set to expire
|
|
265
|
+
else:
|
|
266
|
+
_moRedis.setex(
|
|
267
|
+
object.__getattribute__(self, '__key'),
|
|
268
|
+
object.__getattribute__(self, '__store')['__ttl'],
|
|
269
|
+
jsonb.encode(object.__getattribute__(self, '__store'))
|
|
270
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
print('I compile, but I don\'t necessarily run')
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: memory-oc
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A session library using redis
|
|
5
|
+
Home-page: https://ouroboroscoding.com/memory/
|
|
6
|
+
Author: Chris Nasr - Ouroboros Coding Inc.
|
|
7
|
+
Author-email: chris@ouroboroscoding.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Documentation, https://ouroboroscoding.com/memory/
|
|
10
|
+
Project-URL: Source, https://github.com/ouroboroscoding/memory-python
|
|
11
|
+
Project-URL: Tracker, https://github.com/ouroboroscoding/memory-python/issues
|
|
12
|
+
Keywords: session,redis
|
|
13
|
+
Platform: UNKNOWN
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# memory-python
|
|
18
|
+
A python session library using redis
|
|
19
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.cfg
|
|
3
|
+
setup.py
|
|
4
|
+
memory/__init__.py
|
|
5
|
+
memory/__main__.py
|
|
6
|
+
memory_oc.egg-info/PKG-INFO
|
|
7
|
+
memory_oc.egg-info/SOURCES.txt
|
|
8
|
+
memory_oc.egg-info/dependency_links.txt
|
|
9
|
+
memory_oc.egg-info/requires.txt
|
|
10
|
+
memory_oc.egg-info/top_level.txt
|
|
11
|
+
memory_oc.egg-info/zip-safe
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
memory
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
memory-oc-1.0.0/setup.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
with open('README.md', 'r') as oF:
|
|
4
|
+
long_description=oF.read()
|
|
5
|
+
|
|
6
|
+
setup(
|
|
7
|
+
name='memory-oc',
|
|
8
|
+
version='1.0.0',
|
|
9
|
+
description='A session library using redis',
|
|
10
|
+
long_description=long_description,
|
|
11
|
+
long_description_content_type='text/markdown',
|
|
12
|
+
url='https://ouroboroscoding.com/memory/',
|
|
13
|
+
project_urls={
|
|
14
|
+
'Documentation': 'https://ouroboroscoding.com/memory/',
|
|
15
|
+
'Source': 'https://github.com/ouroboroscoding/memory-python',
|
|
16
|
+
'Tracker': 'https://github.com/ouroboroscoding/memory-python/issues'
|
|
17
|
+
},
|
|
18
|
+
keywords=['session','redis'],
|
|
19
|
+
author='Chris Nasr - Ouroboros Coding Inc.',
|
|
20
|
+
author_email='chris@ouroboroscoding.com',
|
|
21
|
+
license='MIT',
|
|
22
|
+
packages=['memory'],
|
|
23
|
+
python_requires='>=3.10',
|
|
24
|
+
install_requires=[
|
|
25
|
+
'jobject>=1.0.2,<1.1',
|
|
26
|
+
'jsonb>=1.0.0,<1.1',
|
|
27
|
+
'namedredis>=1.0.1,<1.1'
|
|
28
|
+
],
|
|
29
|
+
zip_safe=True
|
|
30
|
+
)
|