diff options
author | Eddy Comyn-Platt <53045993+EddyCMWF@users.noreply.github.com> | 2023-02-28 12:09:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-28 12:09:54 +0000 |
commit | 85351e4a835a16fff2d64da0f203e0c523432cc1 (patch) | |
tree | fcd71a3e0a05d6d55afcf3a0194fa32f06f4ab17 | |
parent | 503efc0dc7cb261d659cf0d61d979eba5184b330 (diff) |
Code quality tests added (#65)
* code quality checks to github actions
* tox.ini options set
-rw-r--r-- | .github/workflows/check-and-publish.yml | 18 | ||||
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | cdsapi/api.py | 29 | ||||
-rw-r--r-- | docker/retrieve.py | 4 | ||||
-rwxr-xr-x | examples/example-era5-update.py | 1 | ||||
-rw-r--r-- | tests/test_api.py | 7 | ||||
-rw-r--r-- | tox.ini | 8 |
7 files changed, 41 insertions, 28 deletions
diff --git a/.github/workflows/check-and-publish.yml b/.github/workflows/check-and-publish.yml index 9b783de..60e0319 100644 --- a/.github/workflows/check-and-publish.yml +++ b/.github/workflows/check-and-publish.yml @@ -12,7 +12,21 @@ on: jobs: - checks: + quality-checks: + name: Code QA + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: pip install black flake8 isort + - run: black --version + - run: isort --version + - run: flake8 --version + - run: isort --check . + - run: black --check . + - run: flake8 . + + platform-checks: + needs: quality-checks strategy: fail-fast: false matrix: @@ -41,11 +55,11 @@ jobs: pytest deploy: + needs: platform-checks if: ${{ github.event_name == 'release' }} name: Upload to Pypi - needs: checks runs-on: ubuntu-latest @@ -10,7 +10,7 @@ Install via `pip` with:: Configure --------- -Get your UID and API key from the CDS portal at the address https://cds.climate.copernicus.eu/user +Get your user ID (UID) and API key from the CDS portal at the address https://cds.climate.copernicus.eu/user and write it into the configuration file, so it looks like:: $ cat ~/.cdsapirc diff --git a/cdsapi/api.py b/cdsapi/api.py index 17f8e51..27a6b09 100644 --- a/cdsapi/api.py +++ b/cdsapi/api.py @@ -9,10 +9,11 @@ from __future__ import absolute_import, division, print_function, unicode_literals import json -import time -import os import logging +import os +import time import uuid + import requests try: @@ -35,16 +36,15 @@ def bytes_to_string(n): def read_config(path): config = {} with open(path) as f: - for l in f.readlines(): - if ":" in l: - k, v = l.strip().split(":", 1) + for line in f.readlines(): + if ":" in line: + k, v = line.strip().split(":", 1) if k in ("url", "key", "verify"): config[k] = v.strip() return config def toJSON(obj): - to_json = getattr(obj, "toJSON", None) if callable(to_json): return to_json() @@ -63,7 +63,6 @@ def toJSON(obj): class Result(object): def __init__(self, client, reply): - self.reply = reply self._url = client.url @@ -95,7 +94,6 @@ class Result(object): return r def _download(self, url, size, target): - if target is None: target = url.split("/")[-1] @@ -109,7 +107,6 @@ class Result(object): headers = None while tries < self.retry_max: - r = self.robust(self.session.get)( url, stream=True, @@ -211,7 +208,6 @@ class Result(object): self.reply = result.json() def delete(self): - if self._deleted: return @@ -245,7 +241,6 @@ class Result(object): class Client(object): - logger = logging.getLogger("cdsapi") def __init__( @@ -270,9 +265,7 @@ class Client(object): forget=False, session=requests.Session(), ): - if not quiet: - if debug: level = logging.DEBUG else: @@ -326,7 +319,7 @@ class Client(object): self.session = session self.session.auth = tuple(self.key.split(":", 2)) - assert len(self.session.auth)==2, ( + assert len(self.session.auth) == 2, ( "The cdsapi key provided is not the correct format, please ensure it conforms to:\n" "<UID>:<APIKEY>" ) @@ -361,7 +354,7 @@ class Client(object): def service(self, name, *args, **kwargs): self.delete = False # Don't delete results name = "/".join(name.split(".")) - mimic_ui = kwargs.pop('mimic_ui', False) + mimic_ui = kwargs.pop("mimic_ui", False) # To mimic the CDS ui the request should be populated directly with the kwargs if mimic_ui: request = kwargs @@ -409,7 +402,6 @@ class Client(object): pass def _api(self, url, request, method): - self._status(url) session = self.session @@ -435,7 +427,6 @@ class Client(object): result.raise_for_status() reply = result.json() except Exception: - if reply is None: try: reply = result.json() @@ -465,7 +456,6 @@ class Client(object): sleep = 1 while True: - self.debug("REPLY %s", reply) if reply["state"] != self.last_state: @@ -543,7 +533,6 @@ class Client(object): self.logger.debug(*args, **kwargs) def _download(self, results, targets=None): - if isinstance(results, Result): if targets: path = targets.pop(0) @@ -555,7 +544,6 @@ class Client(object): return [self._download(x, targets) for x in results] if isinstance(results, dict): - if "location" in results and "contentLength" in results: reply = dict( location=results["location"], @@ -594,7 +582,6 @@ class Client(object): def robust(self, call): def retriable(code, reason): - if code in [ requests.codes.internal_server_error, requests.codes.bad_gateway, diff --git a/docker/retrieve.py b/docker/retrieve.py index 01ba3f5..a009fba 100644 --- a/docker/retrieve.py +++ b/docker/retrieve.py @@ -1,4 +1,6 @@ -import json, sys, cdsapi +import json + +import cdsapi with open("/input/request.json") as req: request = json.load(req) diff --git a/examples/example-era5-update.py b/examples/example-era5-update.py index 8b56573..5d10c85 100755 --- a/examples/example-era5-update.py +++ b/examples/example-era5-update.py @@ -9,6 +9,7 @@ # does it submit to any jurisdiction. import time + import cdsapi c = cdsapi.Client(debug=True, wait_until_complete=False) diff --git a/tests/test_api.py b/tests/test_api.py index c3f3766..3725f1f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,8 +1,9 @@ -import cdsapi import os -def test_request(): +import cdsapi + +def test_request(): c = cdsapi.Client() r = c.retrieve( @@ -11,7 +12,7 @@ def test_request(): "variable": "2t", "product_type": "reanalysis", "date": "2012-12-01", - "time": "12:00" + "time": "12:00", }, ) @@ -14,3 +14,11 @@ commands = pytest -v --pep8 --mccabe --cov=cdsapi --cov-report=html --cache-clea [testenv:deps] deps = commands = python setup.py test + + +[black] +line_length=120 +[isort] +profile=black +[flake8] +max-line-length = 120
\ No newline at end of file |