Skip to content

Commit

Permalink
Parametrizo timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslavandeira committed May 28, 2019
1 parent 2b8abf3 commit f7ab1b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions pydatajson/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REQUESTS_TIMEOUT = 30
14 changes: 9 additions & 5 deletions pydatajson/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pydatajson.response_formatters import format_response
from pydatajson.validation import Validator, \
DEFAULT_CATALOG_SCHEMA_FILENAME, ABSOLUTE_SCHEMA_DIR
from . import documentation
from . import documentation, constants
from . import helpers
from . import indicators
from . import readers
Expand Down Expand Up @@ -53,7 +53,8 @@ class DataJson(dict):

def __init__(self, catalog=None, schema_filename=None, schema_dir=None,
default_values=None, catalog_format=None,
validator_class=Validator, verify_ssl=False):
validator_class=Validator, verify_ssl=False,
requests_timeout=constants.REQUESTS_TIMEOUT):
"""Lee un catálogo y crea un objeto con funciones para manipularlo.
Salvo que se indique lo contrario, se utiliza como default el schema
Expand All @@ -79,15 +80,16 @@ def __init__(self, catalog=None, schema_filename=None, schema_dir=None,
}
"""
self.verify_ssl = verify_ssl

self.requests_timeout = requests_timeout
# se construye el objeto DataJson con la interfaz de un dicconario
if catalog:

# lee representaciones de un catálogo hacia un diccionario
catalog = readers.read_catalog(catalog,
default_values=default_values,
catalog_format=catalog_format,
verify=self.verify_ssl)
verify=self.verify_ssl,
timeout=self.requests_timeout)

# copia todos los atributos del diccionario hacia el objeto
for key, value in iteritems(catalog):
Expand Down Expand Up @@ -1097,7 +1099,9 @@ def make_catalogs_backup(self, catalogs=None,
pass

def _read_catalog(self, catalog):
return readers.read_catalog(catalog, verify=self.verify_ssl)
return readers.read_catalog(catalog,
verify=self.verify_ssl,
timeout=self.requests_timeout)


def main():
Expand Down
22 changes: 14 additions & 8 deletions pydatajson/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import pydatajson
from . import custom_exceptions as ce
from . import helpers
from . import helpers, constants
from .ckan_reader import read_ckan_catalog

import urllib3
Expand All @@ -53,7 +53,7 @@ def read_catalog_obj(catalog):


def read_catalog(catalog, default_values=None, catalog_format=None,
verify=False):
verify=False, timeout=constants.REQUESTS_TIMEOUT):
"""Toma una representación cualquiera de un catálogo, y devuelve su
representación interna (un diccionario de Python con su metadata.)
Expand Down Expand Up @@ -87,13 +87,17 @@ def read_catalog(catalog, default_values=None, catalog_format=None,
catalog_format = catalog_format or suffix
if catalog_format == "xlsx":
try:
catalog_dict = read_xlsx_catalog(catalog, verify=verify)
catalog_dict = read_xlsx_catalog(catalog,
verify=verify,
timeout=timeout)
except openpyxl_exceptions + \
(ValueError, AssertionError, IOError, BadZipfile) as e:
raise ce.NonParseableCatalog(catalog, str(e))
elif catalog_format == "json":
try:
catalog_dict = read_json(catalog, verify=verify)
catalog_dict = read_json(catalog,
verify=verify,
timeout=timeout)
except(ValueError, TypeError, IOError) as e:
raise ce.NonParseableCatalog(catalog, str(e))
elif catalog_format == "ckan":
Expand Down Expand Up @@ -183,7 +187,8 @@ def _set_default_value(dict_obj, keys, value):
variable[keys[-1]] = value


def read_json(json_path_or_url, verify=False):
def read_json(json_path_or_url, verify=False,
timeout=constants.REQUESTS_TIMEOUT):
"""Toma el path a un JSON y devuelve el diccionario que representa.
Se asume que el parámetro es una URL si comienza con 'http' o 'https', o
Expand All @@ -201,7 +206,7 @@ def read_json(json_path_or_url, verify=False):

parsed_url = urlparse(json_path_or_url)
if parsed_url.scheme in ["http", "https"]:
res = requests.get(json_path_or_url, verify=verify)
res = requests.get(json_path_or_url, verify=verify, timeout=timeout)
json_dict = json.loads(res.content, encoding='utf-8')

else:
Expand All @@ -219,7 +224,8 @@ def read_json(json_path_or_url, verify=False):
return json_dict


def read_xlsx_catalog(xlsx_path_or_url, logger=None, verify=False):
def read_xlsx_catalog(xlsx_path_or_url, logger=None, verify=False,
timeout=constants.REQUESTS_TIMEOUT):
"""Toma el path a un catálogo en formato XLSX y devuelve el diccionario
que representa.
Expand All @@ -239,7 +245,7 @@ def read_xlsx_catalog(xlsx_path_or_url, logger=None, verify=False):

parsed_url = urlparse(xlsx_path_or_url)
if parsed_url.scheme in ["http", "https"]:
res = requests.get(xlsx_path_or_url, verify=verify)
res = requests.get(xlsx_path_or_url, verify=verify, timeout=timeout)
tmpfilename = ".tmpfile.xlsx"
with io.open(tmpfilename, 'wb') as tmpfile:
tmpfile.write(res.content)
Expand Down

0 comments on commit f7ab1b3

Please sign in to comment.