Skip to content

Commit

Permalink
python3 port (#4)
Browse files Browse the repository at this point in the history
* port to python3 (#3)
* initial 2to3 run
* remove supervisor from required (not python3 compatible yet)
* format to PEP8 spec
* initial 2to3 run; update dependencies
* remediate CVE-2015-8857 and CVE-2015-8858
* pasteurize
* remove imports not yet installed via install_requires
* require future
* bump version
  • Loading branch information
pymonger authored Mar 20, 2019
1 parent 8a02d93 commit 482b293
Show file tree
Hide file tree
Showing 36 changed files with 951 additions and 466 deletions.
9 changes: 9 additions & 0 deletions grq2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
from flask import Flask

app = Flask(__name__)
Expand All @@ -10,13 +16,16 @@
# services blueprints
from grq2.services.main import mod as mainModule
app.register_blueprint(mainModule)

from grq2.services.query import mod as queryModule
app.register_blueprint(queryModule)

from grq2.services.geonames import mod as geonamesModule
app.register_blueprint(geonamesModule)

# rest API blueprints
from grq2.services.api_v01 import services as api_v01Services
app.register_blueprint(api_v01Services)

from grq2.services.api_v02 import services as api_v02Services
app.register_blueprint(api_v02Services)
35 changes: 25 additions & 10 deletions grq2/lib/dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import json, traceback, re, requests, types
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import str
from future import standard_library
standard_library.install_aliases()
import json
import traceback
import re
import requests
import types
from flask import jsonify, Blueprint, request
from pprint import pformat
from elasticsearch import Elasticsearch
Expand Down Expand Up @@ -78,24 +89,28 @@ def update(update_json):
# add closest continent
lon, lat = update_json['center']['coordinates']
continents = get_continents(lon, lat)
update_json['continent'] = continents[0]['name'] if len(continents) > 0 else None
update_json['continent'] = continents[0]['name'] if len(
continents) > 0 else None

# set temporal_span
if update_json.get('starttime', None) is not None and \
update_json.get('endtime', None) is not None:

if isinstance(update_json['starttime'], types.StringTypes) and \
isinstance(update_json['endtime'], types.StringTypes):
update_json['temporal_span'] = get_ts(update_json['starttime'], update_json['endtime'])

if isinstance(update_json['starttime'], str) and \
isinstance(update_json['endtime'], str):
update_json['temporal_span'] = get_ts(
update_json['starttime'], update_json['endtime'])

#app.logger.debug("update_json:\n%s" % json.dumps(update_json, indent=2))

# update in elasticsearch
try:
es = Elasticsearch(hosts=[app.config['ES_URL']])
ret = es.index(index=index, doc_type=doctype, id=update_json['id'], body=update_json)
except Exception, e:
message = "Got exception trying to index dataset: %s\n%s" % (str(e), traceback.format_exc())
ret = es.index(index=index, doc_type=doctype,
id=update_json['id'], body=update_json)
except Exception as e:
message = "Got exception trying to index dataset: %s\n%s" % (
str(e), traceback.format_exc())
app.logger.debug(message)
return jsonify({
'success': False,
Expand All @@ -116,7 +131,7 @@ def update(update_json):
"actions": actions
})
#app.logger.debug("alias_ret: %s" % json.dumps(alias_ret, indent=2))
except Exception, e:
except Exception as e:
app.logger.debug("Got exception trying to add aliases to index: %s\n%s\nContinuing on." %
(str(e), traceback.format_exc()))

Expand Down
16 changes: 13 additions & 3 deletions grq2/lib/geo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import json, requests, types
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
import json
import requests
import types
from pprint import pformat
from shapely.geometry import Polygon, MultiPolygon
import shapely.wkt
Expand All @@ -18,8 +26,10 @@ def get_center(bbox):
tgt_proj = src_proj
for point in bbox:
if point[0] == -180. or point[0] == 180.:
if point[1] > 0: tgt_proj = ccrs.RotatedPole(0., 90.)
else: tgt_proj = ccrs.RotatedPole(0., -90.)
if point[1] > 0:
tgt_proj = ccrs.RotatedPole(0., 90.)
else:
tgt_proj = ccrs.RotatedPole(0., -90.)
break
multipolygons = tgt_proj.project_geometry(poly, src_proj)
multipolygons = multipolygons.simplify(10.)
Expand Down
18 changes: 14 additions & 4 deletions grq2/lib/geonames.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import json, requests, types
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
import json
import requests
import types
from pprint import pformat

from grq2 import app
Expand Down Expand Up @@ -113,7 +121,8 @@ def get_cities(polygon, pop_th=1000000, size=20, multipolygon=False):
# query for results
es_url = app.config['ES_URL']
index = app.config['GEONAMES_INDEX']
r = requests.post('%s/%s/_search' % (es_url, index), data=json.dumps(query))
r = requests.post('%s/%s/_search' %
(es_url, index), data=json.dumps(query))
app.logger.debug("get_cities(): %s" % json.dumps(query, indent=2))
if r.status_code != 200:
raise RuntimeError("Failed to get cities: %s" % pformat(r.json()))
Expand Down Expand Up @@ -181,7 +190,7 @@ def get_continents(lon, lat):
"sort": [
{
"_geo_distance": {
"location": [ lon, lat ],
"location": [lon, lat],
"order": "asc",
"unit": "km"
}
Expand All @@ -195,7 +204,8 @@ def get_continents(lon, lat):
# query for results
es_url = app.config['ES_URL']
index = app.config['GEONAMES_INDEX']
r = requests.post('%s/%s/_search' % (es_url, index), data=json.dumps(query))
r = requests.post('%s/%s/_search' %
(es_url, index), data=json.dumps(query))
app.logger.debug("get_continents(): %s" % json.dumps(query, indent=2))
if r.status_code != 200:
raise RuntimeError("Failed to get cities: %s" % pformat(r.json()))
Expand Down
72 changes: 48 additions & 24 deletions grq2/lib/time_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import map
from future import standard_library
standard_library.install_aliases()
import time as _time
from datetime import tzinfo, timedelta, datetime
import re, time
import re
import time

ZERO = timedelta(0)
HOUR = timedelta(hours=1)

# A UTC class.


class UTC(tzinfo):
"""UTC"""

Expand All @@ -18,17 +28,19 @@ def tzname(self, dt):
def dst(self, dt):
return ZERO


utc = UTC()

# A class building tzinfo objects for fixed-offset time zones.
# Note that FixedOffset(0, "UTC") is a different way to build a
# UTC tzinfo object.


class FixedOffset(tzinfo):
"""Fixed offset in minutes east from UTC."""

def __init__(self, offset, name):
self.__offset = timedelta(minutes = offset)
self.__offset = timedelta(minutes=offset)
self.__name = name

def utcoffset(self, dt):
Expand All @@ -42,16 +54,16 @@ def dst(self, dt):

# A class capturing the platform's idea of local time.

import time as _time

STDOFFSET = timedelta(seconds = -_time.timezone)
STDOFFSET = timedelta(seconds=-_time.timezone)
if _time.daylight:
DSTOFFSET = timedelta(seconds = -_time.altzone)
DSTOFFSET = timedelta(seconds=-_time.altzone)
else:
DSTOFFSET = STDOFFSET

DSTDIFF = DSTOFFSET - STDOFFSET


class LocalTimezone(tzinfo):

def utcoffset(self, dt):
Expand All @@ -77,6 +89,7 @@ def _isdst(self, dt):
tt = _time.localtime(stamp)
return tt.tm_isdst > 0


Local = LocalTimezone()


Expand Down Expand Up @@ -114,6 +127,7 @@ def first_sunday_on_or_after(dt):
DSTSTART_1967_1986 = datetime(1, 4, 24, 2)
DSTEND_1967_1986 = DSTEND_1987_2006


class USTimeZone(tzinfo):

def __init__(self, hours, reprname, stdname, dstname):
Expand Down Expand Up @@ -164,50 +178,60 @@ def dst(self, dt):
else:
return ZERO

Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
Central = USTimeZone(-6, "Central", "CST", "CDT")

Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
Central = USTimeZone(-6, "Central", "CST", "CDT")
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")


def getFormattedDate(dt):
"""Return formatted date string."""

return dt.strftime("%A, %B %d %Y %I:%M%p")


def getPSTFromUTC(dt):
"""Return PST datetime object from unaware UTC datetime object."""

return dt.replace(tzinfo=utc).astimezone(Pacific)


def getMDY(t):
"""Return MMM DD, YYYY."""

tm = time.strptime(t, "%Y-%m-%d %H:%M:%S")
return time.strftime("%b %d, %Y", tm)


def getTimeElementsFromString(dtStr):
"""Return tuple of (year,month,day,hour,minute,second) from date time string."""

match = re.match(r'^(\d{4})[/-](\d{2})[/-](\d{2})[\s*T](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?Z?$',dtStr)
if match: (year,month,day,hour,minute,second) = map(int,match.groups())
else:
match = re.match(r'^(\d{4})[/-](\d{2})[/-](\d{2})$',dtStr)
if match:
(year,month,day) = map(int,match.groups())
(hour,minute,second) = (0,0,0)
else: raise(RuntimeError("Failed to recognize date format: %s" % dtStr))
return (year,month,day,hour,minute,second)
"""Return tuple of (year,month,day,hour,minute,second) from date time string."""

match = re.match(
r'^(\d{4})[/-](\d{2})[/-](\d{2})[\s*T](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?Z?$', dtStr)
if match:
(year, month, day, hour, minute, second) = list(map(int, match.groups()))
else:
match = re.match(r'^(\d{4})[/-](\d{2})[/-](\d{2})$', dtStr)
if match:
(year, month, day) = list(map(int, match.groups()))
(hour, minute, second) = (0, 0, 0)
else:
raise RuntimeError
return (year, month, day, hour, minute, second)


def getDatetimeFromString(dtStr, dayOnly=False):
"""Return datetime object from date time string."""
(year,month,day,hour,minute,second) = getTimeElementsFromString(dtStr)

(year, month, day, hour, minute, second) = getTimeElementsFromString(dtStr)
if dayOnly:
return datetime(year=year, month=month, day=day)
else:
return datetime(year=year, month=month, day=day, hour=hour,
minute=minute, second=second)


def getTemporalSpanInDays(dt1, dt2):
"""Return temporal timespan in days."""

Expand Down
11 changes: 10 additions & 1 deletion grq2/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import os, sys, json
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import open
from future import standard_library
standard_library.install_aliases()
import os
import sys
import json

from grq2 import app

Expand Down
Loading

0 comments on commit 482b293

Please sign in to comment.