forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlFiles.py
executable file
·149 lines (124 loc) · 5.98 KB
/
ControlFiles.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# ControlFiles.py
# Last modified: 2013-06-24 (also update ProgVersion below)
#
# Control file module
#
# Copyright (C) 2008-2013 Robert Hunt
# Author: Robert Hunt <robert316@users.sourceforge.net>
# License: See gpl-3.0.txt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Module for reading and parsing simple text control files.
"""
ProgName = "ControlFiles"
ProgVersion = "0.05"
ProgNameVersion = "{} v{}".format( ProgName, ProgVersion )
import os, logging
import Globals
def readListFile( folder, filename, outputList, debug=False ):
"""Read a simple list file into a list (checking only for duplicate lines)"""
if debug:
if not isinstance( outputList, list): raise ValueError('List expected here')
oldlen = len( outputList )
if Controls['VerbosityLevel'] > 1: print( ' Loading list file', filename + '...' )
lines = []
with open( os.path.join( folder, filename ), encoding='utf-8' ) as myFile: # Automatically closes the file when done
for line in myFile:
if line[-1]=='\n': line=line[:-1] # Removing trailing newline character
if not line: continue # Just discard blank lines
if line[0]=='#': continue # Just discard comment lines
if line in lines:
logging.warning( 'DUPLICATE LINE IGNORED in list file: ' + line )
continue
lines.append( line )
outputList.append( line )
if debug: print( ' Had', oldlen, 'values, added', len(outputList)-oldlen, 'new list values, now have', len(outputList) )
# end of readListFile
def readControlFile( folder, filename, controls, haveLog=True, debug=False ):
"""Read and parse a control (text) file into the given list."""
displayFolder = folder
if not displayFolder:
displayFolder = 'current folder (' + os.getcwd() + ')'
#if 'VerbosityLevel' in GlobalControls and GlobalControls['VerbosityLevel'] > 1: print( ' Loading control file ' + filename + ' from ' + displayFolder + '...' )
if debug: oldlen = len(controls)
with open( os.path.join( folder, filename ), encoding='utf-8' ) as myFile: # Automatically closes the file when done
for line in myFile:
if line[-1]=='\n': line=line[:-1] # Removing trailing newline character
if not line: continue # Just discard blank lines
if line[0]=='#': continue # Just discard comment lines
if '=' not in line:
if haveLog: logging.error( 'LINE IGNORED: Unknown format for control line: ' + line )
else: print( 'LINE IGNORED: Unknown format for control line: ' + line )
continue
si = line.index('=')
name = line[:si].strip() #Marker is from after backslash and before the equals sign
value = line[si+1:].strip() # All the rest is the text field
if not name:
if haveLog: logging.error( 'LINE IGNORED: Missing control name: ' + line )
else: print( 'LINE IGNORED: Missing control name: ' + line )
continue
if not value:
value = ''
#if haveLog: logging.error( 'LINE IGNORED: Missing control value: ' + line )
#else: print( 'LINE IGNORED: Missing control value: ' + line )
#continue
if name in controls:
if value != controls[name]:
if haveLog: logging.error( 'LINE IGNORED: Duplicate control name: ' + line + ", current value is '" + str(controls[name]) + "'" )
else: print( 'LINE IGNORED: Duplicate control name: ' + line + ", current value is '" + str(controls[name]) + "'" )
else: # New value is same as old one
if haveLog: logging.info( 'LINE IGNORED: Duplicate control name: ' + line )
else: print( 'LINE IGNORED: Duplicate control name: ' + line )
continue
controls[name] = value
if debug: print( ' Added', len(controls)-oldlen, 'new control values.' )
# end of readControlFile
def booleanValue ( value ):
"""Return True or False is value is something sensible.
Else return None."""
ix = value.find( '#' )
if ix != -1: # Could be a comment at the end of the line
value = value[:ix].rstrip()
lcvalue = value.lower()
if lcvalue in ['true','yes', 'on']: return True
if lcvalue in ['false','no','off']: return False
# end of booleanValue
def booleanControl( controlName, controlDict=None ):
"""Return True if the given control name exists in the control dictionary
and has a suitable value such as TRUE or ON."""
if controlDict==None: controlDict = Controls
if controlName not in controlDict:
return False
result = booleanValue( controlDict[controlName] )
if result is None:
logging.error( "Unknown value for controlname '" + controlName + "' = '" + str(controlDict[controlName]) + "'")
return result
# end of booleanControl
def demo():
"""
Demo program to handle command line parameters and then run what they want.
"""
if Globals.verbosityLevel>0: print( ProgNameVersion )
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = Globals.setup( ProgName, ProgVersion )
Globals.addStandardOptionsAndProcess( parser )
demo()
Globals.closedown( ProgName, ProgVersion )
# end of ControlFiles.py