-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
91 lines (69 loc) · 2.22 KB
/
util.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
83
84
85
86
87
88
89
90
91
#
# Some utilities
#
import __main__
import os
import sys
from configparser import ConfigParser
def write_section(section_name):
"""
Writes section names for configuration files in the format
[section_name]
Args:
section_name: a string representing the section name
Returns:
the string [section_name]\n
"""
return '[{}]\n'.format(section_name)
def write_option(option_name, option_value, last=False):
"""
Writes option and value pairs for configuration files in the
format option=value
Args:
option_name: a string representing the option name
option_value: a string representing the option value
last: a boolean; if True, a new line is added at the end
Returns:
the string option_name=option_value if last is False or
the string option_name=option_value\n if last is True
"""
out = '{}={}\n'.format(option_name, option_value)
if last:
out += '\n'
return out
def get_script_name():
"""
Gets the name of the current script being executed
This function returns the name of the current script being executed
if the Python session is run in non-interactive mode.
Returns:
a string with the name of the script, if the script executed in
non-interactive mode; otherwise, the function returns None
"""
# check whether Python is in interactive mode
interactive = not hasattr(__main__, '__file__')
if interactive:
return None
else:
script_name = os.path.basename(sys.argv[0])
return script_name
def get_config(section, option):
"""
Returns the value of an option stored in a section
of a configuration file
Args:
section: a string corresponding to the name of a section of a
configuration file
option: a string corresponding to an option contained in a section
of a configuration file
Returns:
a string, corresponding to the value of the target option in the
target section
"""
parser = ConfigParser()
cf = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'local.conf'
)
parser.read(cf)
return parser.get(section, option)