pycodb 0.1.0__py3-none-any.whl
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.
- pycodb/__init__.py +2 -0
- pycodb/entities/__init__.py +0 -0
- pycodb/entities/basetable.py +37 -0
- pycodb/entities/database.py +43 -0
- pycodb/entities/dataproxy.py +29 -0
- pycodb-0.1.0.dist-info/METADATA +20 -0
- pycodb-0.1.0.dist-info/RECORD +8 -0
- pycodb-0.1.0.dist-info/WHEEL +4 -0
pycodb/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
|
|
3
|
+
class BaseTable:
|
|
4
|
+
def __init__(self, domain, headers, view_id, table_id):
|
|
5
|
+
self.view_id = view_id
|
|
6
|
+
self.headers = headers
|
|
7
|
+
self.endpoint = f'http://{domain}/api/v2/tables/{table_id}/records'
|
|
8
|
+
|
|
9
|
+
def _get(self, fields=None, **kwargs):
|
|
10
|
+
url_query = f'{self.endpoint}?viewId={self.view_id}&limit=1000&shuffle=0'
|
|
11
|
+
|
|
12
|
+
if fields is not None:
|
|
13
|
+
fields = ','.join(fields)
|
|
14
|
+
url_query += f'&fields={fields}'
|
|
15
|
+
|
|
16
|
+
if kwargs:
|
|
17
|
+
expressions = '&'.join([f'{operator}={kwargs[operator]}' for operator in kwargs.keys()])
|
|
18
|
+
url_query += f'&{expressions}'
|
|
19
|
+
try:
|
|
20
|
+
response = requests.get(url_query, headers=self.headers)
|
|
21
|
+
data = response.json()
|
|
22
|
+
if isinstance(data, dict):
|
|
23
|
+
return data.get('list', [])
|
|
24
|
+
elif isinstance(data, list):
|
|
25
|
+
return data
|
|
26
|
+
else:
|
|
27
|
+
raise Exception(f'Unexpected response structure: {data}')
|
|
28
|
+
except Exception as e:
|
|
29
|
+
raise Exception(f'_Get is broken: {e}')
|
|
30
|
+
|
|
31
|
+
def _append(self, target: dict):
|
|
32
|
+
try:
|
|
33
|
+
response = requests.post(self.endpoint, headers=self.headers, json=target)
|
|
34
|
+
data = response.json()
|
|
35
|
+
return data
|
|
36
|
+
except Exception as e:
|
|
37
|
+
raise Exception(f'_Append is broken: {e}')
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from .dataproxy import DataProxy
|
|
2
|
+
|
|
3
|
+
class DataBase:
|
|
4
|
+
DOMAIN = ''
|
|
5
|
+
NOCODB_API_KEY = ''
|
|
6
|
+
|
|
7
|
+
def __init__(self, table_name: str):
|
|
8
|
+
table = getattr(self.__class__, table_name)
|
|
9
|
+
|
|
10
|
+
self._objects = table(
|
|
11
|
+
domain=DataBase.DOMAIN,
|
|
12
|
+
headers={
|
|
13
|
+
'xc-token': DataBase.NOCODB_API_KEY,
|
|
14
|
+
'accept': 'application/json'
|
|
15
|
+
},
|
|
16
|
+
view_id=table.view_id,
|
|
17
|
+
table_id=table.table_id
|
|
18
|
+
)
|
|
19
|
+
self._data_cache = None
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def data(self):
|
|
23
|
+
if self._data_cache is None:
|
|
24
|
+
raw_data = self._objects._get()
|
|
25
|
+
self._data_cache = DataProxy(raw_data)
|
|
26
|
+
return self._data_cache
|
|
27
|
+
|
|
28
|
+
def values(self, fields=None):
|
|
29
|
+
if fields is not None:
|
|
30
|
+
raw_data = self._objects._get(fields=fields)
|
|
31
|
+
self._data_cache = DataProxy(raw_data)
|
|
32
|
+
return self._data_cache
|
|
33
|
+
return self.data
|
|
34
|
+
|
|
35
|
+
def filter(self, **kwargs):
|
|
36
|
+
if kwargs:
|
|
37
|
+
raw_data = self._objects._get(**kwargs)
|
|
38
|
+
self._data_cache = DataProxy(raw_data)
|
|
39
|
+
return self._data_cache
|
|
40
|
+
return self.data
|
|
41
|
+
|
|
42
|
+
def append(self, target: dict):
|
|
43
|
+
return self._objects._append(target)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class DataProxy:
|
|
2
|
+
def __init__(self, data_list):
|
|
3
|
+
self._objects = [self._dict_to_obj(item) for item in data_list]
|
|
4
|
+
|
|
5
|
+
def _dict_to_obj(self, data):
|
|
6
|
+
cls = type('DataItem', (), {})
|
|
7
|
+
obj = cls()
|
|
8
|
+
for key, value in data.items():
|
|
9
|
+
setattr(obj, key, value)
|
|
10
|
+
|
|
11
|
+
def repr_str(self):
|
|
12
|
+
attrs = ', '.join(f'{k}={v}' for k, v in self.__dict__.items())
|
|
13
|
+
return f"<DataItem {attrs}>"
|
|
14
|
+
|
|
15
|
+
cls.__repr__ = repr_str
|
|
16
|
+
|
|
17
|
+
return obj
|
|
18
|
+
|
|
19
|
+
def __iter__(self):
|
|
20
|
+
return iter(self._objects)
|
|
21
|
+
|
|
22
|
+
def __getitem__(self, index):
|
|
23
|
+
return self._objects[index]
|
|
24
|
+
|
|
25
|
+
def __len__(self):
|
|
26
|
+
return len(self._objects)
|
|
27
|
+
|
|
28
|
+
def __repr__(self):
|
|
29
|
+
return f'DataProxy({self._objects})'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycodb
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple module for working with NocoDB
|
|
5
|
+
Author: Helpmap
|
|
6
|
+
Author-email: helpmap.online@gmail.com
|
|
7
|
+
Requires-Python: >=3.6
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pycodb/__init__.py,sha256=Nq0DZxMmwmE1USuBPqRPqH2PSxwvEeZ18FYozQZY0Ak,93
|
|
2
|
+
pycodb/entities/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
pycodb/entities/basetable.py,sha256=jweiCVigP6iXJux9Xsc8RxUQ2RSDr7A7tAVh2iDCKEE,1363
|
|
4
|
+
pycodb/entities/database.py,sha256=wTeKt2n1vKGxT8eUJDv7Nvus5c8PC7oDnQUlCKKloCY,1232
|
|
5
|
+
pycodb/entities/dataproxy.py,sha256=yXLjeYf1RA-7YEO7aQupuT3TPXprAF6nvqgSzccVVb0,752
|
|
6
|
+
pycodb-0.1.0.dist-info/METADATA,sha256=X-t7Ec8ehx0Y72XgNz_hG31RYzUGSF_-1o5Qt_NqI38,722
|
|
7
|
+
pycodb-0.1.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
|
|
8
|
+
pycodb-0.1.0.dist-info/RECORD,,
|