forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextFile.py
executable file
·162 lines (127 loc) · 5.99 KB
/
TextFile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# TextFile.py
#
# Text file read/edit module
#
# Copyright (C) 2016 Robert Hunt
# Author: Robert Hunt <[email protected]>
# 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.
"""
from gettext import gettext as _
LastModifiedDate = '2016-12-28' # by RJH
ShortProgName = "TextFile"
ProgName = "Text File"
ProgVersion = '0.03'
ProgNameVersion = '{} v{}'.format( ShortProgName, ProgVersion )
ProgNameVersionDate = '{} {} {}'.format( ProgNameVersion, _("last modified"), LastModifiedDate )
debuggingThisModule = False
import os.path, logging
import BibleOrgSysGlobals
class TextFile:
"""
"""
def __init__( self, filepath=None, folderPathname=None, filename=None, encoding=None, autoLoad=True ):
if BibleOrgSysGlobals.debugFlag or debuggingThisModule or BibleOrgSysGlobals.verbosityLevel>2:
print( "TextFile.__init__( {!r}, {!r}, {!r}, {!r}, {} )".format( filepath, folderPathname, filename, encoding, autoLoad ) )
self.encoding = encoding if encoding else 'utf-8'
if folderPathname and filename:
assert filepath is None
self.folderPathname, self.filename = folderPathname, filename
self.filepath = os.path.join( self.folderPathname, self.filename )
elif filepath:
assert folderPathname is None and filename is None
self.filepath = filepath
self.folderPathname, self.filename = os.path.split( self.filepath )
else:
logging.critical( "TextFile.__init__ seems to have too little or too much information: {} {} {}".format( filepath, folderPathname, filename ) )
if autoLoad:
with open( self.filepath, mode='rt', encoding=encoding ) as myFile: # Automatically closes the file when done
self.fileText = myFile.read()
else: self.fileText = None
self.changed = False
# end of TextFile.__init__
#def __enter__( self ):
#return self
# end of TextFile.__enter__
def replace( self, findString, replaceString, replaceCount=None ):
if BibleOrgSysGlobals.debugFlag or debuggingThisModule or BibleOrgSysGlobals.verbosityLevel>2:
print( "TextFile.replace( {!r}, {!r}, {} )".format( findString, replaceString, replaceCount ) )
assert self.fileText is not None
self.originalText = self.fileText
if replaceCount is None: self.fileText = self.fileText.replace( findString, replaceString )
else: self.fileText = self.fileText.replace( findString, replaceString, replaceCount )
if self.fileText!= self.originalText: self.changed = True
# end of TextFile.replace
def save( self ):
if BibleOrgSysGlobals.debugFlag or debuggingThisModule or BibleOrgSysGlobals.verbosityLevel>2:
print( "TextFile.save()" )
assert self.fileText is not None
if self.changed:
with open( self.filepath, mode='wt', encoding=self.encoding ) as myFile: # Automatically closes the file when done
myFile.write( self.fileText )
self.changed = False
# end of TextFile.save
def saveAs( self, filepath=None, folderPathname=None, filename=None, encoding=None ):
if BibleOrgSysGlobals.debugFlag or debuggingThisModule or BibleOrgSysGlobals.verbosityLevel>1:
print( "TextFile.saveAs( {!r}, {!r}, {!r}, {} )".format( filepath, folderPathname, filename, encoding ) )
assert self.fileText is not None
encoding = encoding if encoding else 'utf-8'
if folderPathname and filename:
assert filepath is None
filepath = os.path.join( folderPathname, filename )
elif filepath:
assert folderPathname is None and filename is None
folderPathname, filename = os.path.split( filepath )
else:
logging.critical( "TextFile.saveAs seems to have too little or too much information: {} {} {}".format( filepath, folderPathname, filename ) )
with open( filepath, mode='wt', encoding=encoding ) as myFile: # Automatically closes the file when done
myFile.write( self.fileText )
# end of TextFile.saveAs
def __exit__( self, exc_type, exc_value, traceback ):
if self.changed: self.save()
# end of TextFile.__exit__
def __del__( self ):
if self.changed:
logging.critical( "TextFile: {!r} wasn't saved!!!".format( self.filename ) )
# end of TextFile.__del__
# end of class TextFile
def demo():
"""
Demo program to handle command line parameters and then run what they want.
"""
if BibleOrgSysGlobals.verbosityLevel>0: print( ProgNameVersion )
tf = TextFile( 'TextFile.py' ) # Read myself!
tf.replace( "TextFile", "ABRACADABRA" )
print( tf.fileText )
#tf.saveAs( "/tmp/fred.py" )
tf = TextFile( folderPathname='./', filename='TextFile.py' ) # Read myself!
tf.replace( "TextFile", "ABRACADABRA" )
print( tf.fileText )
#tf.saveAs( "/tmp/fred.py" )
#with TextFile( 'TextFile.py' ) as tf:
#tf.replace( "TextFile", "ABRACADABRA-DOOOOOOOOOOO" )
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = BibleOrgSysGlobals.setup( ProgName, ProgVersion )
BibleOrgSysGlobals.addStandardOptionsAndProcess( parser )
demo()
BibleOrgSysGlobals.closedown( ProgName, ProgVersion )
# end of TextFile.py