Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion cdsapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

from __future__ import absolute_import, division, print_function, unicode_literals

from . import api

Expand Down
37 changes: 17 additions & 20 deletions cdsapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

from __future__ import absolute_import, division, print_function, unicode_literals

import json
import logging
Expand All @@ -31,7 +30,7 @@ def bytes_to_string(n):
while n >= 1024:
n /= 1024.0
i += 1
return "%g%s" % (int(n * 10 + 0.5) / 10.0, u[i])
return f"{int(n * 10 + 0.5) / 10.0:g}{u[i]}"


def read_config(path):
Expand Down Expand Up @@ -62,7 +61,7 @@ def toJSON(obj):
return obj


class Result(object):
class Result:
def __init__(self, client, reply):
self.reply = reply

Expand Down Expand Up @@ -135,17 +134,15 @@ def _download(self, url, size, target):
pbar.update(len(chunk))

except requests.exceptions.ConnectionError as e:
self.error("Download interupted: %s" % (e,))
self.error(f"Download interupted: {e}")
finally:
r.close()

if total >= size:
break

self.error(
"Download incomplete, downloaded %s byte(s) out of %s" % (total, size)
)
self.warning("Sleeping %s seconds" % (sleep,))
self.error(f"Download incomplete, downloaded {total} byte(s) out of {size}")
self.warning(f"Sleeping {sleep} seconds")
time.sleep(sleep)
mode = "ab"
total = os.path.getsize(target)
Expand All @@ -154,11 +151,11 @@ def _download(self, url, size, target):
sleep = self.sleep_max
headers = {"Range": "bytes=%d-" % total}
tries += 1
self.warning("Resuming download at byte %s" % (total,))
self.warning(f"Resuming download at byte {total}")

if total != size:
raise Exception(
"Download failed: downloaded %s byte(s) out of %s" % (total, size)
f"Download failed: downloaded {total} byte(s) out of {size}"
)

elapsed = time.time() - start
Expand All @@ -183,7 +180,7 @@ def content_type(self):
return self.reply["content_type"]

def __repr__(self):
return "Result(content_length=%s,content_type=%s,location=%s)" % (
return "Result(content_length={},content_type={},location={})".format(
self.content_length,
self.content_type,
self.location,
Expand All @@ -201,7 +198,7 @@ def check(self):
def update(self, request_id=None):
if request_id is None:
request_id = self.reply["request_id"]
task_url = "%s/tasks/%s" % (self._url, request_id)
task_url = f"{self._url}/tasks/{request_id}"
self.debug("GET %s", task_url)

result = self.robust(self.session.get)(
Expand All @@ -217,7 +214,7 @@ def delete(self):
if "request_id" in self.reply:
rid = self.reply["request_id"]

task_url = "%s/tasks/%s" % (self._url, rid)
task_url = f"{self._url}/tasks/{rid}"
self.debug("DELETE %s", task_url)

delete = self.session.delete(
Expand Down Expand Up @@ -245,7 +242,7 @@ def __del__(self):
print(e)


class Client(object):
class Client:
logger = logging.getLogger("cdsapi")

def __init__(
Expand Down Expand Up @@ -361,7 +358,7 @@ def __init__(
)

def retrieve(self, name, request, target=None):
result = self._api("%s/resources/%s" % (self.url, name), request, "POST")
result = self._api(f"{self.url}/resources/{name}", request, "POST")
if target is not None:
result.download(target)
return result
Expand All @@ -380,7 +377,7 @@ def service(self, name, *args, **kwargs):
request["_cds_metadata"] = self.metadata
request = toJSON(request)
result = self._api(
"%s/tasks/services/%s/clientid-%s" % (self.url, name, uuid.uuid4().hex),
f"{self.url}/tasks/services/{name}/clientid-{uuid.uuid4().hex}",
request,
"PUT",
)
Expand All @@ -392,7 +389,7 @@ def workflow(self, code, *args, **kwargs):
return self.service("tool.toolbox.orchestrator.run_workflow", params)

def status(self, context=None):
url = "%s/status.json" % (self.url,)
url = f"{self.url}/status.json"
r = self.session.get(url, verify=self.verify, timeout=self.timeout)
r.raise_for_status()
return r.json()
Expand Down Expand Up @@ -474,7 +471,7 @@ def _api(self, url, request, method):
self.debug("REPLY %s", reply)

if reply["state"] != self.last_state:
self.info("Request is %s" % (reply["state"],))
self.info(f"Request is {reply['state']}")
self.last_state = reply["state"]

if reply["state"] == "completed":
Expand All @@ -494,7 +491,7 @@ def _api(self, url, request, method):
if sleep > self.sleep_max:
sleep = self.sleep_max

task_url = "%s/tasks/%s" % (self.url, rid)
task_url = f"{self.url}/tasks/{rid}"
self.debug("GET %s", task_url)

result = self.robust(session.get)(
Expand All @@ -521,7 +518,7 @@ def _api(self, url, request, method):
% (reply["error"].get("message"), reply["error"].get("reason"))
)

raise Exception("Unknown API state [%s]" % (reply["state"],))
raise Exception(f"Unknown API state [{reply['state']}]")

def info(self, *args, **kwargs):
if self.info_callback:
Expand Down
4 changes: 2 additions & 2 deletions examples/example-era5-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
while True:
r.update()
reply = r.reply
r.info("Request ID: %s, state: %s" % (reply["request_id"], reply["state"]))
r.info(f"Request ID: {reply['request_id']}, state: {reply['state']}")

if reply["state"] == "completed":
break
Expand All @@ -46,7 +46,7 @@
break
r.error(" %s", n)
raise Exception(
"%s. %s." % (reply["error"].get("message"), reply["error"].get("reason"))
f"{reply['error'].get('message')}. {reply['error'].get('reason')}."
)

r.download("test.nc")
File renamed without changes.
File renamed without changes.