Skip to content

Commit

Permalink
Merge pull request #25 from matthewfeickert/feat/make-python2-python3…
Browse files Browse the repository at this point in the history
…-compatible

feat: Make compatible with Python 2 and Python 3 runtimes
  • Loading branch information
dguest committed Sep 4, 2020
2 parents ba11e8e + ad7f688 commit b49b9da
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pandamon
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ _h_more_info_string = (
_def_user = 'GRID_USER_NAME'
_def_stream = 'OUT'

import urllib2
import urllib
# Attempt to default to Python 3
try:
from urllib.request import urlopen
from urllib.request import Request
from urllib.parse import urlencode
except:
from urllib2 import urlopen
from urllib2 import Request
from urllib import urlencode

import json
import sys, os
import re
Expand Down Expand Up @@ -137,8 +145,8 @@ def get_request(
pars['timestamp'] = datetime.datetime.utcnow().strftime('%H:%M:%S')
if days is not None:
pars['days'] = days
url = 'https://bigpanda.cern.ch/tasks/?' + urllib.urlencode(pars)
return urllib2.Request(url, headers=_headers)
url = 'https://bigpanda.cern.ch/tasks/?' + urlencode(pars)
return Request(url, headers=_headers)


def get_jobid_request(id, days=None, force=False):
Expand All @@ -152,8 +160,8 @@ def get_jobid_request(id, days=None, force=False):
pars['timestamp'] = datetime.datetime.utcnow().strftime('%H:%M:%S')
if days is not None:
pars['days'] = days
url = 'https://bigpanda.cern.ch/jobs/?' + urllib.urlencode(pars)
return urllib2.Request(url, headers=_headers)
url = 'https://bigpanda.cern.ch/jobs/?' + urlencode(pars)
return Request(url, headers=_headers)


_error_ds_re = re.compile('"([^ "]*)"')
Expand Down Expand Up @@ -250,7 +258,7 @@ def format_meta(task, days, force):
jobs = task.get('jobs_metadata', {}).values()
if not jobs and task['status'] in {'done', 'finished'}:
req = get_jobid_request(task['jeditaskid'], days, force)
reply = json.loads(urllib2.urlopen(req).read().decode('utf-8'))
reply = json.loads(urlopen(req).read().decode('utf-8'))
jobs = [j['metastruct'] for j in reply['jobs'] if j]
sum_data = {}
for j in jobs:
Expand All @@ -273,7 +281,7 @@ def stdin_iter(args):
if task[-1] not in '/*':
task = task + '*'
req = get_request(task, args.user, args.days, args.metadata)
for ds in json.loads(urllib2.urlopen(req).read().decode('utf-8')):
for ds in json.loads(urlopen(req).read().decode('utf-8')):
yield ds


Expand All @@ -293,7 +301,7 @@ def run():
if args.print_browser_string:
sys.stdout.write(req.get_full_url() + '\n')
return 0
reply = urllib2.urlopen(req).read().decode('utf-8')
reply = urlopen(req).read().decode('utf-8')
if args.print_json_reply:
sys.stdout.write(reply)
return 0
Expand Down

0 comments on commit b49b9da

Please sign in to comment.