Skip to content

Cross compatible #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ docs/_build/

# PyBuilder
target/

# Backup files from conversion with six and future
*.bak

# VSCode Configuration
.vscode
47 changes: 28 additions & 19 deletions api/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# from __future__ import absolute_import
# from __future__ import print_function
# from builtins import input
# from builtins import str
# from builtins import range
import pprint
import base64
import urllib
import urllib2
import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error
import six.moves.urllib.request, six.moves.urllib.error, six.moves.urllib.parse
import json
import shelve
from six.moves import range
from six.moves import input
__version__ = '1.1'
'''
Docket Alarm Python API Client
Expand Down Expand Up @@ -42,7 +49,8 @@ def call(call, method="GET", **kwargs):


if PRESS_KEY_BEFORE_CALL:
raw_input("(press enter to continue)")
# eval(input("(press enter to continue)"))
input("(press enter to continue)")

# Prepare the URL and arguments
if USE_LOCAL:
Expand All @@ -65,20 +73,20 @@ def call(call, method="GET", **kwargs):
kwargs['login_token'] = ''

# Sort the keywords so they are applied consistently.
sorted_kw = sorted(kwargs.items(), key = lambda val: val[0])
urlargs = urllib.urlencode(sorted_kw, doseq=True)
sorted_kw = sorted(list(kwargs.items()), key = lambda val: val[0])
urlargs = six.moves.urllib.parse.urlencode(sorted_kw, doseq=True)

if method == "GET":
url = url + "?" + urlargs

# Allow for debug printing
if DEBUG:
print("%s: %s"%(method, url))
print(("%s: %s"%(method, url)))
if method == "POST":
print("ARGUMENTS: %s"%pprint.pformat(urlargs))
print(("ARGUMENTS: %s"%pprint.pformat(urlargs)))

# Add an authorization header if provided.
req = urllib2.Request(url)
req = six.moves.urllib.request.Request(url)
if username and password:
auth = base64.encodestring('%s:%s' % (username, password)).strip()
req.add_header("Authorization", "Basic %s" % auth)
Expand All @@ -87,20 +95,21 @@ def call(call, method="GET", **kwargs):
if _INTERNAL_TESTING:
out = _INTERNAL_TESTING(method, url, urlargs)
elif method == "GET":
out = urllib2.urlopen(req, timeout = TIMEOUT).read()
out = six.moves.urllib.request.urlopen(req, timeout = TIMEOUT).read()
else:
out = urllib2.urlopen(req, urlargs, timeout = TIMEOUT).read()
# Encoding specified manually for urlargs with six. Not specifying breaks cross-compatibility.
out = six.moves.urllib.request.urlopen(req, six.b(urlargs), timeout = TIMEOUT).read()

try:
out = json.loads(out)
except:
raise Exception("Not JSON: " + out)

if DEBUG and out and out.get('error'):
print "Error: %s"%out['error']
print("Error: %s"%out['error'])

if PRESS_KEY_AFTER_CALL:
raw_input("API Call Complete (press enter to continue)")
input("API Call Complete (press enter to continue)")
print("")

return out
Expand All @@ -109,7 +118,7 @@ def call(call, method="GET", **kwargs):
################################################################################
# Utilities and Time Saving Helper Functions
import time, logging
from Queue import Empty
from six.moves.queue import Empty
from multiprocessing import Process
from multiprocessing import Pool as MultiProcessPool
from multiprocessing import Queue as ProcessQueue
Expand Down Expand Up @@ -160,7 +169,7 @@ def _dl_worker(username, password, client_matter, cached, dlqueue, docketqueue):

def getdocket_parallel(username, password, client_matter, docket_list,
cached = False, num_workers = 15,
save_progress = None, async = False):
save_progress = None, _async = False):
'''
Download a list of dockets in parallel by launching many processes.

Expand All @@ -171,7 +180,7 @@ def getdocket_parallel(username, password, client_matter, docket_list,
async If True, we get data asyncrhonously.
'''
if save_progress != None:
if async:
if _async:
raise NotImplementedError("Cannot save progress and async.")
save_progress = shelve.open(save_progress, 'c')

Expand Down Expand Up @@ -242,7 +251,7 @@ def iterator(sleep_time = 1.0):
pool.close()
pool.terminate()

if async:
if _async:
return iterator

for new_i, new_docket in enumerate(iterator()):
Expand Down Expand Up @@ -332,7 +341,7 @@ def search_parallel(username, password, client_matter, q,

# Put all of the search ranges into the result queue
dlqueue = ProcessQueue()
for i in xrange(num_first_page, num_results, SEARCH_RESULTS_AT_ONCE):
for i in range(num_first_page, num_results, SEARCH_RESULTS_AT_ONCE):
limit = min(num_results, i+SEARCH_RESULTS_AT_ONCE) - i
logging.info("Added: %s --> %s"%(i, i+limit))
dlqueue.put((i, limit))
Expand All @@ -357,7 +366,7 @@ def search_parallel(username, password, client_matter, q,
start, end, num_results))
got += 1
except Empty:
left = len(results) - len(filter(None, results))
left = len(results) - len([_f for _f in results if _f])
if left <= 0:
break
logging.info("Got %d, %d results. Waiting for %d more."%(
Expand All @@ -378,7 +387,7 @@ def search_parallel(username, password, client_matter, q,

for i, r in enumerate(results):
if not r:
print "Missing Result %s"%(i+1)
print("Missing Result %s"%(i+1))


return {
Expand Down
12 changes: 6 additions & 6 deletions api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
code corresponding to this program.

"""

from six.moves import input
import pprint
import os
import sys
import urllib
import urllib2
import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error
import six.moves.urllib.request, six.moves.urllib.error, six.moves.urllib.parse
import logging

# This if statemetn is only really used internally or if you're using it in a
Expand Down Expand Up @@ -94,15 +94,15 @@ def do_print(out):
def print_title(msg):
global part_no
print("\n\n---------------------------------")
print("Part %d: %s"%(part_no, msg))
print(("Part %d: %s"%(part_no, msg)))
part_no += 1

if __name__ == "__main__":
print(msg)
if not username or password:
print("Enter your Docket Alarm username (your email) and password.")
username = raw_input("Username: ")
password = raw_input("Password: ")
username = input("Username: ")
password = input("Password: ")

do_getdocket, do_search, do_track_test = True, True, False
# do_getdocket, do_search, do_track_test = False, False, True
Expand Down