Merge pull request #248 from cclauss/modernize-python2-code

Modernize Python 2 code to get ready for Python 3
pull/249/head
Igor Chubin 6 years ago committed by GitHub
commit bbc595fdf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

@ -1,3 +1,4 @@
from __future__ import print_function
from unicodedata import * from unicodedata import *
script_data = { 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.append((int(a, 16), int(b or a, 16), names.index(name), cats.index(cat)))
idx.sort() 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(names), 80)),
'\n'.join(textwrap.wrap(repr(cats), 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))))

@ -1,5 +1,6 @@
# vim: set encoding=utf-8 # vim: set encoding=utf-8
from __future__ import print_function
import gevent import gevent
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from gevent.queue import Queue 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) p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
if p.returncode != 0: 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 'Unable to find any matching weather location to the query submitted' in stderr:
if location != NOT_FOUND_LOCATION: if location != NOT_FOUND_LOCATION:
NOT_FOUND_MESSAGE_HEADER = u"ERROR: %s: %s\n---\n\n" % (get_message('UNKNOWN_LOCATION', lang), location) NOT_FOUND_MESSAGE_HEADER = u"ERROR: %s: %s\n---\n\n" % (get_message('UNKNOWN_LOCATION', lang), location)

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

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

Loading…
Cancel
Save