Skip to content

Commit

Permalink
Merge pull request #497 from goelakash/python3
Browse files Browse the repository at this point in the history
Python3 compatibility changes
  • Loading branch information
henrykironde committed Jun 7, 2016
2 parents 3607409 + d95103f commit 8ae610e
Show file tree
Hide file tree
Showing 42 changed files with 245 additions and 158 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ retriever.egg-info
apidocs
.DS_Store
.cache/*
*.bak
4 changes: 3 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
download published ecological data, and store the data in a database.
"""
from __future__ import print_function
from builtins import str

import os
import sys
Expand Down Expand Up @@ -37,7 +39,7 @@
pw = pwd.getpwnam(os.getenv("SUDO_USER"))
os.chown(dir, pw.pw_uid, pw.pw_gid)
except OSError:
print "The Retriever lacks permission to access the ~/.retriever/ directory."
print("The Retriever lacks permission to access the ~/.retriever/ directory.")
raise
SCRIPT_SEARCH_PATHS = [
"./",
Expand Down
18 changes: 10 additions & 8 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
The main() function can be used for bootstrapping.
"""
from __future__ import print_function
from builtins import str

import os
import platform
Expand Down Expand Up @@ -79,9 +81,9 @@ def main():

# If scripts have never been downloaded there is nothing to list
if not script_list:
print "No scripts are currently available. Updating scripts now..."
print("No scripts are currently available. Updating scripts now...")
check_for_updates()
print "\n\nScripts downloaded.\n"
print("\n\nScripts downloaded.\n")
script_list = SCRIPT_LIST()

all_scripts = []
Expand All @@ -105,7 +107,7 @@ def main():

all_scripts = sorted(all_scripts, key=lambda s: s.lower())

print "Available datasets : {}\n".format(len(all_scripts))
print("Available datasets : {}\n".format(len(all_scripts)))

if args.l==None:
import lscolumns
Expand All @@ -127,19 +129,19 @@ def main():
scripts = name_matches(script_list, args.dataset)
if scripts:
for dataset in scripts:
print "=> Installing", dataset.name
print("=> Installing", dataset.name)
try:
dataset.download(engine, debug=debug)
dataset.engine.final_cleanup()
except KeyboardInterrupt:
pass
except Exception as e:
print e
print(e)
if debug: raise
print "Done!"
print("Done!")
else:
print "The dataset {} isn't currently available in the Retriever".format(args.dataset)
print "Run 'retriever ls to see a list of currently available datasets"
print("The dataset {} isn't currently available in the Retriever".format(args.dataset))
print("Run 'retriever ls to see a list of currently available datasets")

if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions compile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import print_function
from retriever import MODULE_LIST


def compile():
print "Compiling retriever scripts..."
print("Compiling retriever scripts...")
MODULE_LIST(force_compile=True)
print "done."
print("done.")

if __name__ == "__main__":
compile()
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
from retriever import VERSION,COPYRIGHT
from retriever.lib.repository import check_for_updates
from retriever import SCRIPT_LIST
Expand Down
5 changes: 4 additions & 1 deletion engines/csv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from builtins import str
from builtins import object
from builtins import range
import os
import platform
from retriever.lib.models import Engine, no_cleanup
from retriever import DATA_DIR


class DummyConnection:
class DummyConnection(object):

def cursor(self):
pass
Expand Down
4 changes: 3 additions & 1 deletion engines/download_only.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function
from builtins import object
import os
import platform
import shutil
Expand All @@ -8,7 +10,7 @@
from retriever import DATA_DIR, HOME_DIR


class DummyConnection:
class DummyConnection(object):

def cursor(self):
pass
Expand Down
5 changes: 4 additions & 1 deletion engines/jsonengine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Engine for writing data to a JSON file"""
from builtins import zip
from builtins import object
from builtins import range

import os
import json
Expand All @@ -7,7 +10,7 @@
from retriever import DATA_DIR
from collections import OrderedDict

class DummyConnection:
class DummyConnection(object):
def cursor(self):
pass

Expand Down
6 changes: 4 additions & 2 deletions engines/msaccess.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function
from builtins import str
import os
from retriever.lib.models import Engine, no_cleanup
from retriever import DATA_DIR, current_platform
Expand Down Expand Up @@ -89,7 +91,7 @@ def insert_data_from_file(self, filename):
newfilename = filename + "_new"

if not os.path.isfile(newfilename):
print "Adding index to " + os.path.abspath(newfilename) + "..."
print("Adding index to " + os.path.abspath(newfilename) + "...")
read = open(filename, "rb")
write = open(newfilename, "wb")
to_write = ""
Expand Down Expand Up @@ -118,7 +120,7 @@ def insert_data_from_file(self, filename):
try:
self.execute(statement)
except:
print "Couldn't bulk insert. Trying manual insert."
print("Couldn't bulk insert. Trying manual insert.")
self.connection.rollback()

self.table.record_id -= add_to_record_id
Expand Down
2 changes: 2 additions & 0 deletions engines/mysql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function
from builtins import str
import os
import platform
from retriever.lib.models import Engine, no_cleanup
Expand Down
1 change: 1 addition & 0 deletions engines/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import range
import os
import platform
from retriever.lib.models import Engine, no_cleanup
Expand Down
5 changes: 4 additions & 1 deletion engines/xmlengine.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from builtins import str
from builtins import object
from builtins import range
import os

from retriever.lib.models import Engine
from retriever import DATA_DIR


class DummyConnection:
class DummyConnection(object):

def cursor(self):
pass
Expand Down
3 changes: 2 additions & 1 deletion lib/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import object
def floatable(value):
"""Check if a value can be converted to a float"""
try:
Expand All @@ -23,7 +24,7 @@ def no_cleanup(value, args):
return value


class Cleanup:
class Cleanup(object):
"""This class represents a custom cleanup function and a dictionary of
arguments to be passed to that function."""

Expand Down
11 changes: 6 additions & 5 deletions lib/compile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
script_templates = {
"default": """#retriever
from retriever.lib.templates import BasicTextTemplate
Expand Down Expand Up @@ -94,7 +95,7 @@ def compile_script(script_file):
# general script attributes
values[key] = '"' + value + '"'

if 'shortname' not in values.keys():
if 'shortname' not in list(values.keys()):
try:
values['shortname'] = values['name']
except:
Expand All @@ -108,10 +109,10 @@ def get_value(key):
return ""

table_desc = "{"
for (key, value) in tables.items():
for (key, value) in list(tables.items()):
table_desc += "'" + key + "': Table('" + key + "', "
table_desc += ','.join([key + "=" + str(value)
for key, value, in value.items()])
for key, value, in list(value.items())])
table_desc += "),"
if table_desc != '{':
table_desc = table_desc[:-1]
Expand All @@ -120,14 +121,14 @@ def get_value(key):
values['tables'] = table_desc

script_desc = []
for key, value in values.items():
for key, value in list(values.items()):
if key == "url":
key = "ref"
if key not in keys_to_ignore:
script_desc.append(key + "=" + str(value))
script_desc = (',\n' + ' ' * 27).join(script_desc)

if 'template' in values.keys():
if 'template' in list(values.keys()):
template = values["template"]
else:
template = "default"
Expand Down
14 changes: 8 additions & 6 deletions lib/download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""A function to begin dataset downloads in a separate thread."""
from __future__ import print_function
from builtins import object

import sys
from time import time
Expand Down Expand Up @@ -41,7 +43,7 @@ def download_script(self):

start = time()

class download_stdout:
class download_stdout(object):

def write(self, s):
if s and s != '\n':
Expand All @@ -51,24 +53,24 @@ def write(self, s):

sys.stdout = download_stdout()

print "Connecting to database..."
print("Connecting to database...")

# Connect
try:
engine.get_cursor()
except Exception as e:
print "<b><font color='red'>Error: There was an error with your database connection.<br />" + e.__str__() + "</font></b>"
print("<b><font color='red'>Error: There was an error with your database connection.<br />" + e.__str__() + "</font></b>")
return

# Download script
error = False

print "<b><font color='blue'>Downloading. . .</font></b>"
print("<b><font color='blue'>Downloading. . .</font></b>")
try:
script.download(engine)
except Exception as e:
error = True
print "<b><font color='red'>Error: " + e.__str__() + "</font></b>"
print("<b><font color='red'>Error: " + e.__str__() + "</font></b>")

if not error:
finish = time()
Expand All @@ -89,4 +91,4 @@ def write(self, s):
if len(s.split('.')[0]) < 2:
s = "0" + s

print "<b>Done!</b> <i>Elapsed time: %02d:%02d:%s</i>" % (h, m, s)
print("<b>Done!</b> <i>Elapsed time: %02d:%02d:%s</i>" % (h, m, s))

0 comments on commit 8ae610e

Please sign in to comment.