chg: using 'with open' for files

rewrite
Emilio J. Rodríguez-Posada 10 years ago
parent 10d269deb0
commit 30c153ce1f

@ -328,10 +328,9 @@ def getUserAgent():
def logerror(config={}, text=''): def logerror(config={}, text=''):
""" Log error in file """ """ Log error in file """
if text: if text:
f = open('%s/errors.log' % (config['path']), 'a') with open('%s/errors.log' % (config['path']), 'a') as outfile:
output = u'%s: %s\n' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), text) output = u'%s: %s\n' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), text)
f.write(output.encode('utf-8')) outfile.write(output.encode('utf-8'))
f.close()
def getXMLPageCore(headers={}, params={}, config={}, session=None): def getXMLPageCore(headers={}, params={}, config={}, session=None):
""" """ """ """
@ -753,21 +752,19 @@ def loadConfig(config={}, configfilename=''):
""" Load config file """ """ Load config file """
try: try:
f = open('%s/%s' % (config['path'], configfilename), 'r') with open('%s/%s' % (config['path'], configfilename), 'r') as infile:
config = cPickle.load(infile)
except: except:
print 'There is no config file. we can\'t resume. Start a new dump.' print 'There is no config file. we can\'t resume. Start a new dump.'
sys.exit() sys.exit()
config = cPickle.load(f)
f.close()
return config return config
def saveConfig(config={}, configfilename=''): def saveConfig(config={}, configfilename=''):
""" Save config file """ """ Save config file """
f = open('%s/%s' % (config['path'], configfilename), 'w') with open('%s/%s' % (config['path'], configfilename), 'w') as outfile:
cPickle.dump(config, f) cPickle.dump(config, outfile)
f.close()
def welcome(): def welcome():
message = '' message = ''
@ -1181,9 +1178,8 @@ def saveSpecialVersion(config={}, session=None):
raw = r.text raw = r.text
delay(config=config, session=session) delay(config=config, session=session)
raw = removeIP(raw=raw) raw = removeIP(raw=raw)
f = open('%s/Special:Version.html' % (config['path']), 'w') with open('%s/Special:Version.html' % (config['path']), 'w') as outfile:
f.write(raw.encode('utf-8')) outfile.write(raw.encode('utf-8'))
f.close()
def saveIndexPHP(config={}, session=None): def saveIndexPHP(config={}, session=None):
""" Save index.php as .html, to preserve license details available at the botom of the page """ """ Save index.php as .html, to preserve license details available at the botom of the page """
@ -1196,9 +1192,8 @@ def saveIndexPHP(config={}, session=None):
raw = r.text raw = r.text
delay(config=config, session=session) delay(config=config, session=session)
raw = removeIP(raw=raw) raw = removeIP(raw=raw)
f = open('%s/index.html' % (config['path']), 'w') with open('%s/index.html' % (config['path']), 'w') as outfile:
f.write(raw.encode('utf-8')) outfile.write(raw.encode('utf-8'))
f.close()
def saveSiteInfo(config={}, session=None): def saveSiteInfo(config={}, session=None):
""" Save a file with site info """ """ Save a file with site info """
@ -1211,9 +1206,8 @@ def saveSiteInfo(config={}, session=None):
r = session.post(url=config['api'], data = {'action': 'query', 'meta': 'siteinfo', 'format': 'json'}) r = session.post(url=config['api'], data = {'action': 'query', 'meta': 'siteinfo', 'format': 'json'})
result = json.loads(r.text) result = json.loads(r.text)
delay(config=config, session=session) delay(config=config, session=session)
f = open('%s/siteinfo.json' % (config['path']), 'w') with open('%s/siteinfo.json' % (config['path']), 'w') as outfile:
f.write(json.dumps(result, indent=4, sort_keys=True)) outfile.write(json.dumps(result, indent=4, sort_keys=True))
f.close()
def avoidWikimediaProjects(config={}, other={}): def avoidWikimediaProjects(config={}, other={}):
""" Skip Wikimedia projects and redirect to the dumps website """ """ Skip Wikimedia projects and redirect to the dumps website """

Loading…
Cancel
Save