-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeleton.py
More file actions
executable file
·123 lines (111 loc) · 3.31 KB
/
skeleton.py
File metadata and controls
executable file
·123 lines (111 loc) · 3.31 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# base import {{
import os
from os.path import expanduser
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import argparse
import yaml
import re
import logging
import inspect
from deepmerge import always_merger
# }}
# local import {{
# }}
# base {{
# default vars
scriptName = os.path.basename(sys.argv[0]).split('.')[0]
homeDir = expanduser("~")
defaultConfigFiles = [
'/etc/' + scriptName + '/config.yaml',
homeDir + '/.' + scriptName + '.yaml',
'./.config.yaml',
]
cfg = {
'logFile': '/var/log/' + scriptName + '/' + scriptName + '.log',
'logFile': 'stdout',
'logLevel': 'info',
}
# parse args
parser = argparse.ArgumentParser( description = '''
default config files: %s
''' % ', '.join(defaultConfigFiles),
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
'-c',
'--config',
help = 'path to config file',
)
args = parser.parse_args()
argConfigFile = args.config
# get settings
if argConfigFile:
if os.path.isfile(argConfigFile):
try:
with open(argConfigFile, 'r') as ymlfile:
#cfg.update(yaml.load(ymlfile,Loader=yaml.Loader))
cfg = always_merger.merge(cfg,yaml.load(ymlfile,Loader=yaml.Loader))
except Exception as e:
logging.error("main: failed load config file: '%s', error: '%s'", argConfigFile, e)
exit(1)
else:
for configFile in defaultConfigFiles:
if os.path.isfile(configFile):
try:
with open(configFile, 'r') as ymlfile:
try:
#cfg.update(yaml.load(ymlfile,Loader=yaml.Loader))
cfg = always_merger.merge(cfg,yaml.load(ymlfile,Loader=yaml.Loader))
except Exception as e:
logging.warning("main: skipping load load config file: '%s', error '%s'", configFile, e)
continue
except:
continue
# fix logDir
cfg['logDir'] = os.path.dirname(cfg['logFile'])
if cfg['logDir'] == '':
cfg['logDir'] = '.'
# }}
# defs
def testbytest():
defName = inspect.stack()[0][3]
logging.info("%s: 1111" % (defName))
if __name__ == "__main__":
# basic config {{
for dirPath in [
cfg['logDir'],
]:
try:
os.makedirs(dirPath)
except OSError:
if not os.path.isdir(dirPath):
raise
# choice loglevel
if re.match(r"^(warn|warning)$", cfg['logLevel'], re.IGNORECASE):
logLevel = logging.WARNING
elif re.match(r"^debug$", cfg['logLevel'], re.IGNORECASE):
logLevel = logging.DEBUG
else:
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("requests").setLevel(logging.WARNING)
logLevel = logging.INFO
if cfg['logFile'] == 'stdout':
logging.basicConfig(
level = logLevel,
format = '%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s',
datefmt = '%Y-%m-%dT%H:%M:%S',
)
else:
logging.basicConfig(
filename = cfg['logFile'],
level = logLevel,
format = '%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s',
datefmt = '%Y-%m-%dT%H:%M:%S',
)
# }}
defName = "main"
testbytest()