Skip to content

Commit

Permalink
Added pre-commit to repo, pyproject, flake8 and ran linting
Browse files Browse the repository at this point in the history
  • Loading branch information
eXamadeus committed Sep 19, 2023
1 parent 5b00a3e commit 73895b9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 56 deletions.
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

default_language_version:
python: python3.11

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VENV_BIN ?= python3 -m venv
VENV_BIN ?= python3.11 -m venv
VENV_DIR ?= venv
PIP_CMD ?= pip3

Expand All @@ -23,7 +23,13 @@ venv: $(VENV_ACTIVATE) ## Create a new (empty) virtual environment
freeze: ## Run pip freeze -l in the virtual environment
@$(VENV_RUN); pip freeze -l

install: install-lib install-dev ## Install full dependencies into venv
pre-commit: ## Install pre-commit hooks
@pre-commit install > /dev/null

install: ## Install full dependencies into venv
make install-lib
make install-dev
@make pre-commit

install-dev: venv ## Install requirements for development into venv
@$(VENV_RUN); $(PIP_CMD) install -r requirements-dev.txt
Expand All @@ -50,4 +56,4 @@ clean-dist: ## Clean up python distribution directories
rm -rf dist/
rm -rf *.egg-info

.PHONY: usage freeze install install-dev install-lib test lint format clean clean-dist
.PHONY: usage freeze pre-commit install install-dev install-lib test lint format clean clean-dist
5 changes: 3 additions & 2 deletions examples/update_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Partial imports
from godaddypy import Client, Account
from godaddypy.client import BadResponse

domain = "example.com"
a_record = "www"
Expand All @@ -23,6 +24,6 @@
print("Update ended with no Exception.")
else:
print("No DNS update needed.")
except:
print(sys.exc_info()[1])
except ValueError | BadResponse as e:
print(e)
sys.exit()
35 changes: 8 additions & 27 deletions godaddypy/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals

import logging
import sys
from enum import Enum

import requests
Expand Down Expand Up @@ -70,9 +69,7 @@ def _build_record_url(self, domain, record_type=None, name=None):
elif name is not None and record_type is None:
raise ValueError("If name is specified, type must also be specified")
else:
url += self.RECORDS_TYPE_NAME.format(
domain=domain, type=record_type, name=name
)
url += self.RECORDS_TYPE_NAME.format(domain=domain, type=record_type, name=name)

return url

Expand All @@ -83,9 +80,7 @@ def _get_json_from_response(self, url, json=None, **kwargs):
return self._request_submit(requests.get, url=url, json=json, **kwargs).json()

def _log_response_from_method(self, req_type, resp):
self.logger.debug(
"[{req_type}] response: {resp}".format(resp=resp, req_type=req_type.upper())
)
self.logger.debug("[{req_type}] response: {resp}".format(resp=resp, req_type=req_type.upper()))
self.logger.debug("Response data: {}".format(resp.content))

def _patch(self, url, json=None, **kwargs):
Expand Down Expand Up @@ -207,9 +202,7 @@ def get_records(self, domain, record_type=None, name=None, offset=1, limit=500):
"""

url = self._build_record_url(domain, record_type=record_type, name=name)
data = self._get_json_from_response(
url, params=dict(limit=limit, offset=offset)
)
data = self._get_json_from_response(url, params=dict(limit=limit, offset=offset))
self.logger.debug("Retrieved {} record(s) from {}.".format(len(data), domain))

return data
Expand Down Expand Up @@ -252,13 +245,8 @@ def update_ip(self, ip, record_type="A", domains=None, subdomains=None):

if domains is None:
domains = self.get_domains()
elif sys.version_info < (3, 0):
# noinspection PyUnresolvedReferences
if isinstance(domains, (str, unicode)):
domains = [domains]
elif sys.version_info >= (3, 0):
if isinstance(domains, str):
domains = [domains]
elif isinstance(domains, str):
domains = [domains]
else:
# we have a tuple, set, or something else, try to convert it to a list
domains = list(domains)
Expand All @@ -273,10 +261,7 @@ def update_ip(self, ip, record_type="A", domains=None, subdomains=None):
# noinspection PyUnresolvedReferences
if (
subdomains is None
or (
isinstance(subdomains, (unicode, str))
and r_name == subdomains
)
or (isinstance(subdomains, str) and r_name == subdomains)
or r_name in subdomains
):
record.update(data=str(ip))
Expand Down Expand Up @@ -315,14 +300,10 @@ def update_record(self, domain, record, record_type=None, name=None):
if name is None:
name = record["name"]

url = self.API_TEMPLATE + self.RECORDS_TYPE_NAME.format(
domain=domain, type=record_type, name=name
)
url = self.API_TEMPLATE + self.RECORDS_TYPE_NAME.format(domain=domain, type=record_type, name=name)
self._put(url, json=[record])
self.logger.info(
"Updated record. Domain {} name {} type {}".format(
domain, str(record["name"]), str(record["type"])
)
"Updated record. Domain {} name {} type {}".format(domain, str(record["name"]), str(record["type"]))
)

# If we didn't get any exceptions, return True to let the user know
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.black]
line-length = 120
target-version = ['py38']

[tool.flake8]
max-line-length = 120
max-complexity = 10
extend-ignore = ["E203"]
exclude = ["venv"]
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ callee>=0.2.1
coverage>=4.4.1
mock>=2.0.0
pif==0.8.2
pre-commit==3.4.0
pyproject-flake8==5.0.4
pytest==7.4.2
33 changes: 9 additions & 24 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

# noinspection PyPackageRequirements
import callee
from callee import EndsWith

# noinspection PyPackageRequirements
from mock import patch
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_get_domain_a_record_with_bad_response(self, mock):
def test_update_record(self, put_mock):
self.client.update_record("test.com", self.fake_records[0])
put_mock.assert_called_once_with(
"https://api.ote-godaddy.com/v1/domains/test.com/records/A/test1",
EndsWith("/v1/domains/test.com/records/A/test1"),
json=[self.fake_records[0]],
)

Expand Down Expand Up @@ -89,9 +89,7 @@ def test_delete_records(self, get_mock, delete_mock):

self.client.delete_records(fake_domain, "test2")

delete_mock.assert_called_once_with(
url="https://api.ote-godaddy.com/v1/domains/apple.com/records/A/test2"
)
delete_mock.assert_called_once_with(url=EndsWith("/v1/domains/apple.com/records/A/test2"))

def test_account_without_delegate(self):
_PRIVATE_KEY = "blahdeyblah"
Expand All @@ -109,9 +107,7 @@ def test_account_with_delegate(self):
_PRIVATE_KEY = "blahdeyblah"
_PUBLIC_KEY = "hooeybalooooooeryasdfasdfsdfs"

acct = Account(
api_key=_PUBLIC_KEY, api_secret=_PRIVATE_KEY, delegate=_DELEGATE_ID
)
acct = Account(api_key=_PUBLIC_KEY, api_secret=_PRIVATE_KEY, delegate=_DELEGATE_ID)

assert "X-Shopper-Id" in acct.get_headers()
assert "Authorization" in acct.get_headers()
Expand All @@ -127,33 +123,22 @@ def test_build_record_url_happy_path(self):

expected = [
self.client.API_TEMPLATE
+ self.client.RECORDS_TYPE_NAME.format(
domain=domains[0], name=names[0], type=types[0]
),
self.client.API_TEMPLATE
+ self.client.RECORDS_TYPE.format(domain=domains[1], type=types[1]),
+ self.client.RECORDS_TYPE_NAME.format(domain=domains[0], name=names[0], type=types[0]),
self.client.API_TEMPLATE + self.client.RECORDS_TYPE.format(domain=domains[1], type=types[1]),
self.client.API_TEMPLATE
+ self.client.RECORDS_TYPE_NAME.format(
domain=domains[2], name=names[2], type=types[2]
),
+ self.client.RECORDS_TYPE_NAME.format(domain=domains[2], name=names[2], type=types[2]),
self.client.API_TEMPLATE + self.client.RECORDS.format(domain=domains[3]),
]

urls = list()

if len(domains) == len(names) == len(types) == len(expected):
for i, val in enumerate(domains):
urls.append(
self.client._build_record_url(
val, name=names[i], record_type=types[i]
)
)
urls.append(self.client._build_record_url(val, name=names[i], record_type=types[i]))
assert urls[i] == expected[i]
else:
raise ValueError(
"The test {} has invalid internal parameters!".format(
self.test_build_record_url_happy_path.__name__
)
"The test {} has invalid internal parameters!".format(self.test_build_record_url_happy_path.__name__)
)

def test_build_record_url_raise_value_error(self):
Expand Down

0 comments on commit 73895b9

Please sign in to comment.