clockwork 0.1.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Zachary Einck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.1
2
+ Name: clockwork
3
+ Version: 0.1.0
4
+ Summary: Toolkit for time-related operations including scheduling, logging, date manipulation, and more.
5
+ Home-page: https://github.com/zteinck/clockwork
6
+ License: MIT
7
+ Author: Zachary Einck
8
+ Author-email: zacharyeinck@gmail.com
9
+ Requires-Python: >=3.8,<4.0
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: holidays
18
+ Requires-Dist: iterlab
19
+ Requires-Dist: pathpilot
20
+ Requires-Dist: schedule
21
+ Requires-Dist: textwrap3
22
+ Project-URL: Repository, https://github.com/zteinck/clockwork
23
+ Description-Content-Type: text/markdown
24
+
25
+ # clockwork
26
+ `clockwork` is a library that provides a multitude of time-related functionalities, facilitating tasks such as scheduling, logging, date manipulation, and more.
@@ -0,0 +1,2 @@
1
+ # clockwork
2
+ `clockwork` is a library that provides a multitude of time-related functionalities, facilitating tasks such as scheduling, logging, date manipulation, and more.
@@ -0,0 +1,4 @@
1
+ from .clockwork import *
2
+
3
+ __version__ = '0.1.0'
4
+ __author__ = 'Zachary Einck <zacharyeinck@gmail.com>'
@@ -0,0 +1,97 @@
1
+ import logging
2
+ import datetime
3
+ from pathpilot import Folder
4
+
5
+
6
+ class CustomLogFormatter(logging.Formatter):
7
+ ''' the default implementation of logging.Formatter does not allow timestamps to be formatted how I want '''
8
+
9
+ converter = datetime.datetime.fromtimestamp
10
+
11
+ def formatTime(self, record, datefmt=None):
12
+ if datefmt is not None: raise TypeError('datefmt argument must be None')
13
+ return self.converter(record.created).strftime('%Y-%m-%d %I:%M:%S.{} %p').format('%03d' % record.msecs)
14
+
15
+
16
+
17
+ class Logger(object):
18
+
19
+ instances = {}
20
+
21
+ @classmethod
22
+ def load(cls, name, *args, **kwargs):
23
+ if name not in cls.instances:
24
+ cls.instances[name] = cls(name, *args, **kwargs)
25
+ return cls.instances[name]
26
+
27
+
28
+ def __init__(self, name, clear=False, stream_handler=False):
29
+ self.name = name
30
+
31
+ # create logger
32
+ logger = logging.getLogger(name)
33
+ logger.setLevel(logging.DEBUG)
34
+
35
+ # create custom formatter
36
+ # https://docs.python.org/3/library/logging.html#logrecord-attributes
37
+ formatter = CustomLogFormatter(fmt='%(asctime)s %(levelname)s %(message)s')
38
+ #formatter = CustomLogFormatter(fmt='%(asctime)s %(name)s %(levelname)s %(message)s')
39
+
40
+ # create file handler which logs even debug messages
41
+ self.file = Folder().parent.join('Data', 'Logger', read_only=False).join(f'{name}.log').path
42
+ if clear: self.clear()
43
+ fh = logging.FileHandler(self.file)
44
+ fh.setLevel(logging.DEBUG)
45
+ fh.setFormatter(formatter)
46
+ logger.addHandler(fh) # add handler to the logger
47
+
48
+ # create console handler with a higher log level
49
+ if stream_handler:
50
+ ch = logging.StreamHandler()
51
+ ch.setLevel(logging.ERROR)
52
+ ch.setFormatter(formatter)
53
+ logger.addHandler(ch) # add handler to the logger
54
+
55
+ # disable logging to console
56
+ logger.propagate = False
57
+
58
+ self.logger = logger
59
+
60
+
61
+ def __getitem__(self, name):
62
+ return self.__dict__[name].logger
63
+
64
+ def __repr__(self):
65
+ return self.file
66
+
67
+ def __str__(self):
68
+ return self.file
69
+
70
+ def clear(self):
71
+ open(self.file, 'w').close()
72
+
73
+
74
+
75
+ def log(logger=None):
76
+ ''' Logs decorated function using the passed logging.Logger object.
77
+ If None, a logger object is created (or loaded if already exists)
78
+ using the decorated function's name. '''
79
+ def decorator(func):
80
+ def wrapper(*args, **kwargs):
81
+ nonlocal logger
82
+ logger = logger or Logger.load(func.__name__).logger
83
+ logger.info('start')
84
+ try:
85
+ out = func(*args, **kwargs)
86
+ logger.info('complete')
87
+ return out
88
+ except Exception as e:
89
+ logger.exception('exception')
90
+ return e
91
+ return wrapper
92
+ return decorator
93
+
94
+
95
+
96
+ if __name__ == '__main__':
97
+ pass