forked from Ahlzen/TopOSM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
53 lines (41 loc) · 1.3 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
"""common.py: Common utility functions for TopOSM"""
import os, time, sys
from os import path
from threading import Lock
import filelock
import influxdb
from env import *
__author__ = "Lars Ahlzen"
__copyright__ = "(c) Lars Ahlzen 2008-2011"
__license__ = "GPLv2"
class ErrorLog:
lock = Lock()
def log(self, message, exception = None):
timestr = time.strftime('[%Y-%m-%d %H:%M:%S]')
ErrorLog.lock.acquire()
try:
file = open(ERRORLOG, 'a')
file.write("%s %s (%s)\n" % (timestr, message, str(exception)))
file.close()
except:
print("Failed to write to the error log:")
print("%s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
finally:
ErrorLog.lock.release()
errorLog = ErrorLog()
# global locking object for file system ops (e.g. creating directories)
fslock = filelock.FileLock('/tmp/lock.TopOSM.fslock')
def ensureDirExists(path):
with fslock:
if not os.path.isdir(path):
os.makedirs(path)
def connect_to_influxdb():
return influxdb.InfluxDBClient(
host=INFLUX_HOST,
port=INFLUX_PORT,
username=INFLUX_USER,
password=INFLUX_PASS,
ssl=INFLUX_SSL,
verify_ssl=True,
database=INFLUX_DB)