Skip to content

Commit d1ceba1

Browse files
committed
Initial commit
0 parents  commit d1ceba1

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

config_loader/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import io
2+
import json
3+
4+
5+
def get(variable):
6+
try:
7+
with io.open('config.json', "r", encoding='UTF-8') as file:
8+
keys = json.load(file)
9+
if variable in keys:
10+
return keys[variable]
11+
return None
12+
except FileNotFoundError:
13+
return None

logger/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import io
2+
from datetime import datetime
3+
import re
4+
5+
6+
class Logger:
7+
8+
def __init__(self, dateformat, separator, log_to_print=True, log_name='default.log'):
9+
self.dateformat = dateformat
10+
self.separator = separator
11+
self.log_to_print = log_to_print
12+
self.log_name = self._convert(log_name)
13+
14+
@staticmethod
15+
def _convert(name):
16+
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
17+
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
18+
19+
def log(self, severity, *messages):
20+
# Remove whitespace from the messages
21+
messages = [message.strip() for message in messages]
22+
# Convert the messages to one string
23+
message = self.separator.join(messages)
24+
25+
timestamp = self.dateformat.format(datetime.now())
26+
27+
row = timestamp + self.separator +\
28+
severity + self.separator +\
29+
message
30+
if self.log_to_print:
31+
print(row)
32+
else:
33+
with io.open(self.log_name, "a", encoding='UTF-8') as file:
34+
file.write(row + '\n')

setup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from setuptools import setup
2+
3+
setup(name='packages',
4+
version='1.0',
5+
description='Python module for Jönköping University Librarys various helper packages',
6+
url='https://github.com/JonkopingUniversityLibrary/python_packages',
7+
author='Gustav Lindqvist',
8+
author_email='[email protected]',
9+
license='MIT',
10+
packages=[
11+
'logger',
12+
'config_loader',
13+
],
14+
zip_safe=False)

0 commit comments

Comments
 (0)