Skip to content

Commit

Permalink
#78 use logging module instead of print() in occurrence download methods
Browse files Browse the repository at this point in the history
- this allows users to set the loggin level
- and allows users to turn off or turn on logging messages
- bump patch version for the change
  • Loading branch information
sckott committed Jan 28, 2021
1 parent bcd2379 commit 83500bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pygbif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@
from .gbifissues import occ_issues_lookup
from .utils import *
from .caching import caching

# Set default logging handler to avoid "No handler found" warnings.
import logging
from logging import NullHandler

logging.getLogger(__name__).addHandler(NullHandler())
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
28 changes: 23 additions & 5 deletions pygbif/occurrences/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .. import package_metadata, occurrences
from ..gbifutils import is_not_none, is_none, stop, gbif_GET, gbif_GET_write

import logging

def _parse_args(x):
tmp = re.split("\s", x)
Expand Down Expand Up @@ -126,6 +126,14 @@ def download(queries, user=None, pwd=None, email=None, pred_type="and"):
# Repratriated data for Costa Rica
occ.download(['country = CR', 'repatriated = true'])
# turn off logging
import logging
logger = logging.getLogger()
logger.disabled = True
z = occ.download('elevation >= 95000')
logger.disabled = False
w = occ.download('elevation >= 10000')
"""

user = _check_environ("GBIF_USER", user)
Expand Down Expand Up @@ -304,7 +312,7 @@ def post_download(self, user=None, pwd=None):
)
else:
self.request_id = r.text
print("Your download key is ", self.request_id)
logging.info("Your download key is " + self.request_id)
return self.request_id

def get_status(self):
Expand Down Expand Up @@ -387,18 +395,28 @@ def download_get(key, path=".", **kwargs):
Usage::
from pygbif import occurrences as occ
occ.download_get("0000066-140928181241064")
x=occ.download_get("0000066-140928181241064")
occ.download_get("0003983-140910143529206")
# turn off logging
import logging
logger = logging.getLogger()
logger.disabled = True
x = occ.download_get("0000066-140928181241064")
# turn back on
logger.disabled = False
x = occ.download_get("0000066-140928181241064")
"""
meta = occurrences.download_meta(key)
if meta["status"] != "SUCCEEDED":
raise Exception('download "%s" not of status SUCCEEDED' % key)
else:
print("Download file size: %s bytes" % meta["size"])
logging.info("Download file size: %s bytes" % meta["size"])
url = "http://api.gbif.org/v1/occurrence/download/request/" + key
path = "%s/%s.zip" % (path, key)
gbif_GET_write(url, path, **kwargs)
print("On disk at " + path)
logging.info("On disk at " + path)
return {"path": path, "size": meta["size"], "key": key}


Expand Down
2 changes: 1 addition & 1 deletion pygbif/package_metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.5.0"
__version__ = "0.5.1"
__title__ = "pygbif"
__author__ = "Scott Chamberlain"
__license__ = "MIT"

0 comments on commit 83500bb

Please sign in to comment.