bfabric-web-apps 0.1.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- bfabric_web_apps/__init__.py +6 -0
- bfabric_web_apps/layouts/layouts.py +13 -0
- bfabric_web_apps/objects/BfabricInterface.py +16 -0
- bfabric_web_apps/objects/Logger.py +99 -0
- bfabric_web_apps/objects/__init__.py +0 -0
- bfabric_web_apps-0.1.0.dist-info/METADATA +10 -0
- bfabric_web_apps-0.1.0.dist-info/RECORD +8 -0
- bfabric_web_apps-0.1.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
from bfabric import Bfabric
|
2
|
+
|
3
|
+
class BfabricInterface( Bfabric ):
|
4
|
+
|
5
|
+
def __init__():
|
6
|
+
pass
|
7
|
+
|
8
|
+
def token_to_data(self, token):
|
9
|
+
pass
|
10
|
+
|
11
|
+
def token_response_to_bfabric(self, token_response):
|
12
|
+
pass
|
13
|
+
|
14
|
+
def entity_data(self, entity):
|
15
|
+
pass
|
16
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import os
|
2
|
+
import pickle
|
3
|
+
from bfabric import Bfabric
|
4
|
+
from datetime import datetime as dt
|
5
|
+
import base64
|
6
|
+
|
7
|
+
try:
|
8
|
+
from PARAMS import CONFIG_FILE_PATH
|
9
|
+
except ImportError:
|
10
|
+
CONFIG_FILE_PATH = "~/.bfabricpy.yml"
|
11
|
+
|
12
|
+
|
13
|
+
class Logger:
|
14
|
+
"""
|
15
|
+
A Logger class to manage and batch API call logs locally and flush them to the backend when needed.
|
16
|
+
"""
|
17
|
+
def __init__(self, jobid: int, username: str):
|
18
|
+
self.jobid = jobid
|
19
|
+
self.username = username
|
20
|
+
self.power_user_wrapper = self._get_power_user_wrapper()
|
21
|
+
self.logs = []
|
22
|
+
|
23
|
+
def _get_power_user_wrapper(self) -> Bfabric:
|
24
|
+
"""
|
25
|
+
Initializes a B-Fabric wrapper using the power user's credentials.
|
26
|
+
"""
|
27
|
+
power_user_wrapper = Bfabric.from_config(
|
28
|
+
config_path=os.path.expanduser(CONFIG_FILE_PATH)
|
29
|
+
)
|
30
|
+
return power_user_wrapper
|
31
|
+
|
32
|
+
def to_pickle(self):
|
33
|
+
# Pickle the object and then encode it as a base64 string
|
34
|
+
return {"data": base64.b64encode(pickle.dumps(self)).decode('utf-8')}
|
35
|
+
|
36
|
+
@classmethod
|
37
|
+
def from_pickle(cls, pickle_object):
|
38
|
+
# Decode the base64 string back to bytes and then unpickle
|
39
|
+
return pickle.loads(base64.b64decode(pickle_object.get("data").encode('utf-8')))
|
40
|
+
|
41
|
+
def log_operation(self, operation: str, message: str, params = None, flush_logs: bool = True):
|
42
|
+
"""
|
43
|
+
Log an operation either locally (if flush_logs=False) or flush to the backend.
|
44
|
+
Creates well-structured, readable log entries.
|
45
|
+
"""
|
46
|
+
# Define the timestamp format
|
47
|
+
timestamp = dt.now().strftime('%Y-%m-%d %H:%M:%S')
|
48
|
+
|
49
|
+
# Build the base log entry
|
50
|
+
log_entry = (
|
51
|
+
f"[{timestamp}] "
|
52
|
+
f"USER: {self.username} | "
|
53
|
+
f"OPERATION: {operation.upper()} | "
|
54
|
+
f"MESSAGE: {message}"
|
55
|
+
)
|
56
|
+
|
57
|
+
# Add parameters if provided
|
58
|
+
if params is not None:
|
59
|
+
log_entry += f" | PARAMETERS: {params}"
|
60
|
+
|
61
|
+
# Flush or store the log entry
|
62
|
+
if flush_logs:
|
63
|
+
self.logs.append(log_entry) # Temporarily append for flushing
|
64
|
+
self.flush_logs() # Flush all logs, including the new one
|
65
|
+
else:
|
66
|
+
self.logs.append(log_entry) # Append to local logs
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
def flush_logs(self):
|
71
|
+
"""
|
72
|
+
Send all accumulated logs for this job to the backend and clear the local cache.
|
73
|
+
"""
|
74
|
+
if not self.logs:
|
75
|
+
return # No logs to flush
|
76
|
+
|
77
|
+
try:
|
78
|
+
full_log_message = "\n".join(self.logs)
|
79
|
+
self.power_user_wrapper.save("job", {"id": self.jobid, "logthis": full_log_message})
|
80
|
+
self.logs = [] # Clear logs after successful flush
|
81
|
+
except Exception as e:
|
82
|
+
print(f"Failed to save log to B-Fabric: {e}")
|
83
|
+
|
84
|
+
def logthis(self, api_call: callable, *args, params=None , flush_logs: bool = True, **kwargs) -> any:
|
85
|
+
"""
|
86
|
+
Generic logging function to wrap any API call using a Logger instance.
|
87
|
+
"""
|
88
|
+
# Construct a message describing the API call
|
89
|
+
call_args = ', '.join([repr(arg) for arg in args])
|
90
|
+
call_kwargs = ', '.join([f"{key}={repr(value)}" for key, value in kwargs.items()])
|
91
|
+
log_message = f"{api_call.__name__}({call_args}, {call_kwargs})"
|
92
|
+
|
93
|
+
# Execute the actual API call
|
94
|
+
result = api_call(*args, **kwargs)
|
95
|
+
|
96
|
+
# Log the operation
|
97
|
+
self.log_operation(api_call.__name__, log_message, params, flush_logs=flush_logs)
|
98
|
+
|
99
|
+
return result
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: bfabric-web-apps
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: A package containing handy boilerplate utilities for developing bfabric web-applications
|
5
|
+
Author: Mark Zuber, Griffin White, GWC GmbH
|
6
|
+
Requires-Python: >=3.8,<4.0
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
8
|
+
Classifier: Programming Language :: Python :: 3.10
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
@@ -0,0 +1,8 @@
|
|
1
|
+
bfabric_web_apps/__init__.py,sha256=6IFMcIq8biYvtMafM-gELPRBq19k5kHRlEYmRTsXlYI,87
|
2
|
+
bfabric_web_apps/layouts/layouts.py,sha256=6b5_wk2fk1ZLyWhhhKfz2EnsSdZtlPx8aR-pMfPC1t0,135
|
3
|
+
bfabric_web_apps/objects/BfabricInterface.py,sha256=IU1D1Q6ZdAR0oo64Fpcru9LWVj-3Ydmlti6Vy_-VSj4,275
|
4
|
+
bfabric_web_apps/objects/Logger.py,sha256=Sikkqfbkp7NGqEkFZ4cHKtjGX1Bu9Y8sf1PWDLOOjco,3467
|
5
|
+
bfabric_web_apps/objects/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
bfabric_web_apps-0.1.0.dist-info/WHEEL,sha256=y3eDiaFVSNTPbgzfNn0nYn5tEn1cX6WrdetDlQM4xWw,83
|
7
|
+
bfabric_web_apps-0.1.0.dist-info/METADATA,sha256=9GcQM9qWxhYBDHgmq0r0Gu9mAmvvn0Lxy0YJrfFCiPc,429
|
8
|
+
bfabric_web_apps-0.1.0.dist-info/RECORD,,
|