Skip to content

Commit

Permalink
Merge pull request #151 from isogeo/check
Browse files Browse the repository at this point in the history
Checker - Make methods class methods when possible
  • Loading branch information
Guts committed Apr 21, 2020
2 parents 333a0dd + 5461b95 commit 04f15ba
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
"--ignore=E24,E265,E501,W504",
"--verbose"
],
"python.linting.pylintEnabled": true,
"python.pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"-c",
"setup.cfg"
],
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": false,
"python.testing.unittestArgs": [
"-v",
Expand Down
1 change: 1 addition & 0 deletions isogeo_pysdk/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def check_request_parameters(self, parameters: dict = {}):
" Must be one of: {}.".format(in_rel, " | ".join(GEORELATIONS))
)

@classmethod
def check_is_uuid(self, uuid_str: str):
"""Check if it's an Isogeo UUID handling specific form.
Expand Down
13 changes: 9 additions & 4 deletions isogeo_pysdk/samples/related_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
for link in rel_resources:
# only OGC
if link.get("kind") in kind_ogc or (
link.get("type") == "link" and link.get("link").get("kind") in kind_ogc
link.get("type") == "link"
and link.get("link").get("kind") in kind_ogc
):
li_ogc_md.append((link.get("title"), link.get("url")))
md_resources["OGC links"] = len(li_ogc_md)
Expand All @@ -102,7 +103,8 @@

# only Esri
if link.get("kind") in kind_esri or (
link.get("type") == "link" and link.get("link").get("kind") in kind_esri
link.get("type") == "link"
and link.get("link").get("kind") in kind_esri
):
li_esri_md.append((link.get("title"), link.get("url")))
md_resources["Esri links"] = len(li_ogc_md)
Expand All @@ -114,8 +116,11 @@

# downloadable
if (
link.get("kind") == "data" and link.get("actions") == "download" or (
link.get("type") == "link" and link.get("link").get("kind") == "data"
link.get("kind") == "data"
and link.get("actions") == "download"
or (
link.get("type") == "link"
and link.get("link").get("kind") == "data"
)
):
li_dl_md.append((link.get("title"), link.get("url")))
Expand Down
26 changes: 17 additions & 9 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"""
For timestamps about metadata (_created, _modified):
- `2019-05-17T13:01:08.559123+00:00`: sometimes there are 6 digits in milliseconds, as accepted in `standard lib (%f) <https://docs.python.org/fr/3/library/datetime.html#strftime-strptime-behavior>`_
- `2019-05-17T13:01:08.559123+00:00`: sometimes there are 6 digits in milliseconds, \
as accepted in `standard lib (%f) <https://docs.python.org/fr/3/library/datetime.html#strftime-strptime-behavior>`_
- `2019-08-21T16:07:42.9419347+00:00`: sometimes there are more than 6 digits in milliseconds
"""
_dtm_metadata = "%Y-%m-%dT%H:%M:%S.%f+00:00"
Expand Down Expand Up @@ -258,6 +259,13 @@ def convert_octets(cls, octets: int) -> str:
"""Convert a mount of octets in readable size.
:param int octets: mount of octets to convert
:Example:
.. code-block:: python
>>> IsogeoUtils.convert_octets(1024)
"1ko"
"""
# check zero
if octets == 0:
Expand Down Expand Up @@ -475,7 +483,7 @@ def register_webapp(self, webapp_name: str, webapp_args: list, webapp_url: str):

@classmethod
def get_url_base_from_url_token(
self, url_api_token: str = "https://id.api.isogeo.com/oauth/token"
cls, url_api_token: str = "https://id.api.isogeo.com/oauth/token"
) -> str:
"""Returns the Isogeo API root URL (not included into credentials file) from the token or
the auth URL (always included).
Expand All @@ -500,7 +508,7 @@ def get_url_base_from_url_token(
return api_url_base.geturl()

@classmethod
def guess_platform_from_url(self, url: str = "https://api.isogeo.com/") -> str:
def guess_platform_from_url(cls, url: str = "https://api.isogeo.com/") -> str:
"""Returns the Isogeo platform from a given URL.
:param url str: URL string to guess from
Expand Down Expand Up @@ -538,7 +546,7 @@ def guess_platform_from_url(self, url: str = "https://api.isogeo.com/") -> str:

# -- SEARCH --------------------------------------------------------------
@classmethod
def pages_counter(self, total: int, page_size: int = 100) -> int:
def pages_counter(cls, total: int, page_size: int = 100) -> int:
"""Simple helper to handle pagination. Returns the number of pages for a given number of
results.
Expand Down Expand Up @@ -814,7 +822,7 @@ def _duplicate_mng(

# -- API AUTH ------------------------------------------------------------
@classmethod
def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dict:
def credentials_loader(cls, in_credentials: str = "client_secrets.json") -> dict:
"""Loads API credentials from a file, JSON or INI.
:param str in_credentials: path to the credentials file. By default, `./client_secrets.json`
Expand Down Expand Up @@ -880,7 +888,7 @@ def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dic
"scopes": auth_settings.get("scopes", ["resources:read"]),
"uri_auth": auth_settings.get("auth_uri"),
"uri_token": auth_settings.get("token_uri"),
"uri_base": self.get_url_base_from_url_token(
"uri_base": cls.get_url_base_from_url_token(
auth_settings.get("token_uri")
),
"uri_redirect": None,
Expand All @@ -900,7 +908,7 @@ def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dic
"scopes": auth_settings.get("scopes", ["resources:read"]),
"uri_auth": auth_settings.get("auth_uri"),
"uri_token": auth_settings.get("token_uri"),
"uri_base": self.get_url_base_from_url_token(
"uri_base": cls.get_url_base_from_url_token(
auth_settings.get("token_uri")
),
"uri_redirect": auth_settings.get("redirect_uris", None),
Expand Down Expand Up @@ -928,14 +936,14 @@ def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dic
"client_secret": auth_settings.get("CLIENT_SECRET"),
"uri_auth": auth_settings.get("URI_AUTH"),
"uri_token": auth_settings.get("URI_TOKEN"),
"uri_base": self.get_url_base_from_url_token(
"uri_base": cls.get_url_base_from_url_token(
auth_settings.get("URI_TOKEN")
),
"uri_redirect": auth_settings.get("URI_REDIRECT"),
}

# add guessed platform
out_auth["platform"] = self.guess_platform_from_url(out_auth.get("uri_base"))
out_auth["platform"] = cls.guess_platform_from_url(out_auth.get("uri_base"))

# method ending
return out_auth
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pre-commit==2.2.*

# Tests
# -----------------------
python-dotenv==0.12.*
python-dotenv==0.13.*
pytest==5.4.*
pytest-cov==2.8.*
pytest-randomly==3.2.*
Expand Down
1 change: 0 additions & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def test_search_augmented(self):
# redo using existing attribute
search = self.isogeo.search(page_size=0, whole_results=0, augment=1)


def test_search_search_as_application(self):
"""GET :resources/search."""
basic_search = self.isogeo.search()
Expand Down
9 changes: 8 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ def test_pages_counter(self):
self.assertEqual(p_default, 1)
p_default = self.utils.pages_counter(total=50, page_size=10)
self.assertEqual(p_default, 5)
p_default = self.utils.pages_counter(total=156, page_size=22)
# test it also as class method
p_default = IsogeoUtils.pages_counter(total=156, page_size=22)
self.assertEqual(p_default, 8)

# -- Methods helpers
Expand Down Expand Up @@ -409,3 +410,9 @@ def test_helper_datetimes(self):
unrecognized_date = IsogeoUtils.hlpr_datetimes("2014-10-02T00:00:00+00")
self.assertIsInstance(unrecognized_date, datetime)
self.assertEqual(unrecognized_date.year, 2014)

def test_convert_octets(self):
"""Test octets conversion into a readable string."""
result = IsogeoUtils.convert_octets(1024)
self.assertIsInstance(result, str)
self.assertEqual(result, "1.0 Ko")

0 comments on commit 04f15ba

Please sign in to comment.