bfabric-web-apps 0.1.0__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,6 @@
1
+ from objects import (
2
+ BfabricInterface,
3
+ Logger
4
+ )
5
+
6
+ from layouts.layouts import *
@@ -0,0 +1,13 @@
1
+
2
+
3
+ def get_empty_layout():
4
+
5
+ pass
6
+
7
+ def get_layout_with_sidebar():
8
+
9
+ pass
10
+
11
+ def get_layout_without_sidebar():
12
+
13
+ pass
@@ -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
@@ -0,0 +1,14 @@
1
+ [tool.poetry]
2
+ name = "bfabric-web-apps"
3
+ version = "0.1.0"
4
+ description = "A package containing handy boilerplate utilities for developing bfabric web-applications"
5
+ authors = ["Mark Zuber, Griffin White, GWC GmbH"]
6
+
7
+ [tool.poetry.dependencies]
8
+ python = "^3.8"
9
+
10
+ [tool.poetry.dev-dependencies]
11
+
12
+ [build-system]
13
+ requires = ["poetry-core>=1.0.0"]
14
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+ from setuptools import setup
3
+
4
+ packages = \
5
+ ['bfabric_web_apps', 'bfabric_web_apps.layouts', 'bfabric_web_apps.objects']
6
+
7
+ package_data = \
8
+ {'': ['*']}
9
+
10
+ setup_kwargs = {
11
+ 'name': 'bfabric-web-apps',
12
+ 'version': '0.1.0',
13
+ 'description': 'A package containing handy boilerplate utilities for developing bfabric web-applications',
14
+ 'long_description': None,
15
+ 'author': 'Mark Zuber, Griffin White, GWC GmbH',
16
+ 'author_email': None,
17
+ 'maintainer': None,
18
+ 'maintainer_email': None,
19
+ 'url': None,
20
+ 'packages': packages,
21
+ 'package_data': package_data,
22
+ 'python_requires': '>=3.8,<4.0',
23
+ }
24
+
25
+
26
+ setup(**setup_kwargs)