Modernize Python 2 code to get ready for Python 3

This commit is contained in:
cclauss 2018-12-02 09:41:29 +01:00
parent c597191892
commit 7c47cd7d0c
7 changed files with 25 additions and 19 deletions

View File

@ -8,6 +8,7 @@ It caches the answers and handles various data sources transforming their
answers into format supported by the wttr.in service.
"""
from __future__ import print_function
from gevent.pywsgi import WSGIServer
from gevent.monkey import patch_all
@ -90,7 +91,7 @@ def translate(text, lang):
"""
translated = TRANSLATIONS.get(lang, {}).get(text, text)
if text.encode('utf-8') == translated:
print "%s: %s" % (lang, text)
print("%s: %s" % (lang, text))
return translated
def cyr(to_translate):
@ -111,9 +112,9 @@ def add_translations(content, lang):
try:
d = json.loads(content) # pylint: disable=invalid-name
except ValueError as exception:
print "---"
print exception
print "---"
print("---")
print(exception)
print("---")
try:
weather_condition = d['data']['current_condition'][0]['weatherDesc'][0]['value']
@ -159,7 +160,7 @@ def add_translations(content, lang):
content = json.dumps(d)
except (IndexError, ValueError) as exception:
print exception
print(exception)
return content
@APP.route("/<path:path>")
@ -176,7 +177,7 @@ def proxy(path):
if content is None:
srv = _find_srv_for_query(path, query_string)
url = '%s/%s?%s' % (srv, path, query_string)
print url
print(url)
attempts = 5
while attempts:

View File

@ -1,6 +1,7 @@
"""
global configuration of the project
"""
from __future__ import print_function
import logging
import os
@ -76,7 +77,7 @@ def error(text):
"log error `text` and raise a RuntimeError exception"
if not text.startswith('Too many queries'):
print text
print(text)
logging.error("ERROR %s", text)
raise RuntimeError(text)
@ -84,7 +85,7 @@ def log(text):
"log error `text` and do not raise any exceptions"
if not text.startswith('Too many queries'):
print text
print(text)
logging.info(text)
def debug_log(text):

View File

@ -7,6 +7,7 @@ and basing on this information generates
precise location description.
"""
from __future__ import print_function
import os
import json
@ -67,7 +68,7 @@ def geolocator(location):
try:
geo = requests.get('%s/%s' % (GEOLOCATOR_SERVICE, location)).text
except requests.exceptions.ConnectionError as exception:
print "ERROR: %s" % exception
print("ERROR: %s" % exception)
return None
if geo == "":
@ -77,7 +78,7 @@ def geolocator(location):
answer = json.loads(geo.encode('utf-8'))
return answer
except ValueError as exception:
print "ERROR: %s" % exception
print("ERROR: %s" % exception)
return None
return None

View File

@ -1,3 +1,4 @@
from __future__ import print_function
from unicodedata import *
script_data = {
@ -599,7 +600,7 @@ def _compile_scripts_txt():
idx.append((int(a, 16), int(b or a, 16), names.index(name), cats.index(cat)))
idx.sort()
print 'script_data = {\n"names":%s,\n"cats":%s,\n"idx":[\n%s\n]}' % (
print('script_data = {\n"names":%s,\n"cats":%s,\n"idx":[\n%s\n]}' % (
'\n'.join(textwrap.wrap(repr(names), 80)),
'\n'.join(textwrap.wrap(repr(cats), 80)),
'\n'.join(textwrap.wrap(', '.join('(0x%x,0x%x,%d,%d)' % c for c in idx), 80)))
'\n'.join(textwrap.wrap(', '.join('(0x%x,0x%x,%d,%d)' % c for c in idx), 80))))

View File

@ -1,5 +1,6 @@
# vim: set encoding=utf-8
from __future__ import print_function
import gevent
from gevent.pywsgi import WSGIServer
from gevent.queue import Queue
@ -119,7 +120,7 @@ def get_wetter(location, ip, html=False, lang=None, query=None, location_name=No
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
print "ERROR: location not found: %s" % location
print("ERROR: location not found: %s" % location)
if 'Unable to find any matching weather location to the query submitted' in stderr:
if location != NOT_FOUND_LOCATION:
NOT_FOUND_MESSAGE_HEADER = u"ERROR: %s: %s\n---\n\n" % (get_message('UNKNOWN_LOCATION', lang), location)

View File

@ -163,7 +163,7 @@ def wttr(location, request):
try:
LIMITS.check_ip(ip_addr)
except RuntimeError, exception:
except RuntimeError as exception:
return str(exception)
png_filename = None
@ -249,7 +249,7 @@ def wttr(location, request):
output += '\n' + get_message('FOLLOW_ME', lang).encode('utf-8') + '\n'
return output
except RuntimeError, exception:
except RuntimeError as exception:
if 'Malformed response' in str(exception) \
or 'API key has reached calls per day allowed limit' in str(exception):
if html_output:

View File

@ -1,6 +1,7 @@
#!/usr/bin/python
#vim: encoding=utf-8
from __future__ import print_function
import sys
import os
import re
@ -287,9 +288,9 @@ def make_wttr_in_png(png_name, options=None):
"""
parsed = parse_wttrin_png_name(png_name)
print "------"
print parsed
print "------"
print("------")
print(parsed)
print("------")
# if location is MyLocation it should be overriden
# with autodetected location (from options)
@ -301,7 +302,7 @@ def make_wttr_in_png(png_name, options=None):
if key not in parsed:
parsed[key] = val
url = make_wttrin_query(parsed)
print "URL = ", url
print("URL = ", url)
timestamp = time.strftime("%Y%m%d%H", time.localtime())
cached_basename = url[14:].replace('/','_')