-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.py
82 lines (73 loc) · 2.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
import os
import yaml
import logging
from logging.handlers import RotatingFileHandler
from collections import OrderedDict
import json
import sys
import argparse
# Custom Exceptions
class DictError(Exception):
pass
class LoadError(Exception):
pass
# Check needed key in dict
def check_key(key, c_dict):
if not key in c_dict:
raise DictError("No '{0}' key in dict '{1}'".format(key, c_dict))
# Load JSON
def load_json(f, l):
l.info("Loading JSON from file {0}".format(f))
try:
json_dict = json.load(f, object_pairs_hook=OrderedDict)
except:
try:
json_dict = json.load(f)
except:
try:
file_data = f.read()
json_dict = json.loads(file_data)
except:
raise LoadError("Reading JSON from file '{0}' failed".format(f))
return json_dict
# Load YAML
def load_yaml(f, l):
l.info("Loading YAML from file {0}".format(f))
try:
with open(f, 'r') as yaml_file:
yaml_dict = yaml.load(yaml_file, Loader=yaml.SafeLoader)
except:
raise LoadError("Reading YAML from file '{0}' failed".format(f))
return yaml_dict
# Load FILE
def load_file_string(f, l):
l.info("Loading string from file {0}".format(f))
try:
with open(f, 'r') as file_file:
file_string = file_file.read().replace("\n", "")
except:
raise LoadError("Reading string from file '{0}' failed".format(f))
return file_string
# Set logger
def set_logger(console_level, log_dir, log_file):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
if not os.path.isdir(log_dir):
os.mkdir(log_dir, 0o755)
log_handler = RotatingFileHandler("{0}/{1}".format(log_dir, log_file), maxBytes=10485760, backupCount=10, encoding="utf-8")
os.chmod("{0}/{1}".format(log_dir, log_file), 0o600)
log_handler.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(console_level)
formatter = logging.Formatter(fmt='%(asctime)s %(filename)s %(name)s %(process)d/%(threadName)s %(levelname)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S %Z")
log_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
logger.addHandler(log_handler)
logger.addHandler(console_handler)
return logger
def default(x, default):
try:
return x()
except KeyError:
return default