Skip to content

Commit

Permalink
fix(refactor): clean up timezone deprecations (#1722)
Browse files Browse the repository at this point in the history
* fix(refactor): remove deprecated datetime functionality

Signed-off-by: Chris Butler <[email protected]>


---------

Signed-off-by: Chris Butler <[email protected]>
  • Loading branch information
butler54 authored Oct 14, 2024
1 parent 8744cee commit 7b8b353
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 41 deletions.
20 changes: 10 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0 # Use the ref you want to point at
rev: v5.0.0 # Use the ref you want to point at
hooks:
- id: check-merge-conflict
- id: check-yaml
Expand All @@ -13,13 +14,13 @@ repos:
- id: yapf
args: [--in-place, --parallel, --recursive, --style, .yapf-config]
files: "^(trestle|tests|scripts)"
stages: [commit]
stages: [pre-commit]
additional_dependencies: [toml]
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
rev: 7.1.1
hooks:
- id: flake8
args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028", "--illegal-import-packages=filecmp"]
args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028"]
additional_dependencies:
[
flake8-2020,
Expand All @@ -37,14 +38,13 @@ repos:
flake8-quotes,
flake8-string-format,
flake8-use-fstring,
flake8-illegal-import,
pep8-naming,
]
files: "^(tests|scripts)"
exclude: "(oscal/|third_party)"
stages: [commit]
stages: [pre-commit]
- id: flake8
args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028", "--illegal-import-packages=filecmp"]
args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028"]
additional_dependencies:
[
flake8-2020,
Expand All @@ -62,16 +62,15 @@ repos:
flake8-quotes,
flake8-string-format,
flake8-use-fstring,
flake8-illegal-import,
pep8-naming,
flake8-bandit,
dlint
]
files: "^(trestle)"
exclude: "(oscal/)"
stages: [commit]
stages: [pre-commit]
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
rev: 0.7.17
hooks:
- id: mdformat
exclude: "CHANGELOG.md|docs/mkdocs_code_of_conduct.md|docs/maintainers.md|docs/api_reference|tests/data/author|docs/contributing/mkdocs_contributing.md|tests/data/jinja_markdown_include|tests/data/jinja_cmd/number_captions_data.md|tests/data/jinja_cmd/number_captions_expected_output.md"
Expand All @@ -80,3 +79,4 @@ repos:
- mdformat-config
- mdformat-frontmatter
- mdformat-gfm

8 changes: 4 additions & 4 deletions scripts/flatten_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _replace_ref(self, d, ref_set):

def _replace_refs(self, obj, ref_set):
"""Given an object recurse into it replacing any found ref: defs with what is in def_list."""
if type(obj) == dict:
if isinstance(obj, dict):
# first check if it is a simple $ref line and replace it directly
if len(obj.items()) == 1 and obj.get(self._ref_str, None) is not None:
return self._replace_ref(obj, ref_set)
Expand All @@ -71,9 +71,9 @@ def _replace_refs(self, obj, ref_set):
if changed:
dirty = True
return new_dict, ref_set, dirty
elif type(obj) == str:
elif isinstance(obj, str):
return obj, ref_set, False
elif type(obj) == list:
elif isinstance(obj, list):
n_list = len(obj)
changed = False
dirty = False
Expand All @@ -82,7 +82,7 @@ def _replace_refs(self, obj, ref_set):
if changed:
dirty = True
return obj, ref_set, dirty
elif type(obj) == tuple:
elif isinstance(obj, tuple):
new_val, ref_set, changed = self._replace_refs(obj[1], ref_set)
return (obj[0], new_val), ref_set, changed
if hasattr(obj, '__iter__'):
Expand Down
2 changes: 1 addition & 1 deletion scripts/oscal_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def add_ref_pattern(self, p, line):
new_refs = p.findall(line)
if new_refs:
for r in new_refs:
if type(r) == tuple:
if isinstance(r, tuple):
for s in r:
self.add_ref_if_good(s)
else:
Expand Down
6 changes: 3 additions & 3 deletions scripts/schema_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def recursive_ref(self, ref_key: str, dict_of_interest: Dict[str, Any]) -> List[
for key, value in dict_of_interest.items():
if key == ref_key:
returner.append(value)
elif type(value) == dict:
elif isinstance(value, dict):
returner = returner + self.recursive_ref(ref_key, value)
elif type(value) == list:
elif isinstance(value, list):
for item in value:
if type(item) == dict:
if isinstance(item, dict):
returner = returner + self.recursive_ref(ref_key, item)
elif key == ref_key:
returner.append(value)
Expand Down
4 changes: 2 additions & 2 deletions tests/trestle/core/base_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_is_oscal_base() -> None:
"""Test that the typing information is as expected."""
catalog = simple_catalog()

assert (isinstance(catalog, ospydantic.OscalBaseModel))
assert isinstance(catalog, ospydantic.OscalBaseModel)


def test_no_timezone_exception() -> None:
Expand All @@ -104,7 +104,7 @@ def test_with_timezone() -> None:

popo_json = json.loads(jsoned_catalog)
time = popo_json['metadata']['last-modified']
assert (type(time) == str)
assert isinstance(time, str)
assert ('Z' in time or '+' in time or '-' in time)


Expand Down
2 changes: 1 addition & 1 deletion tests/trestle/core/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_gen_control() -> None:
def test_ensure_optional_exists() -> None:
"""Explicit test to ensure that optional variables are populated."""
my_catalog = gens.generate_sample_model(catalog.Catalog, include_optional=True, depth=-1)
assert type(my_catalog.controls[0]) == catalog.Control
assert isinstance(my_catalog.controls[0], catalog.Control)


def test_gen_party() -> None:
Expand Down
10 changes: 5 additions & 5 deletions tests/trestle/core/remote/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ def test_fetcher_factory(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
as_file_uri('user/oscal_file.json'),
as_file_uri('../user/oscal_file.json')]:
fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri)
assert type(fetcher) == cache.LocalFetcher
assert isinstance(fetcher, cache.LocalFetcher)

# paths with drive letter
for uri in ['C:\\Users\\user\\this.json', 'C:/Users/user/this.json', 'C:file.json']:
if file_utils.is_windows():
fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri)
assert type(fetcher) == cache.LocalFetcher
assert isinstance(fetcher, cache.LocalFetcher)
else:
with pytest.raises(TrestleError):
cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri)
Expand All @@ -291,15 +291,15 @@ def test_fetcher_factory(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
monkeypatch.setenv('myusername', 'user123')
monkeypatch.setenv('mypassword', 'somep4ss')
fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, https_uri)
assert type(fetcher) == cache.HTTPSFetcher
assert isinstance(fetcher, cache.HTTPSFetcher)

sftp_uri = 'sftp://user@hostname:/path/to/file.json'
fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, sftp_uri)
assert type(fetcher) == cache.SFTPFetcher
assert isinstance(fetcher, cache.SFTPFetcher)

sftp_uri = 'sftp://user@hostname:2000/path/to/file.json'
fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, sftp_uri)
assert type(fetcher) == cache.SFTPFetcher
assert isinstance(fetcher, cache.SFTPFetcher)


def test_fetcher_expiration(tmp_trestle_dir: pathlib.Path) -> None:
Expand Down
4 changes: 2 additions & 2 deletions trestle/core/markdown/markdown_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ def compare_keys(
return False
for key in template.keys():
if key in candidate.keys():
if type(template[key]) == dict:
if type(candidate[key]) == dict:
if isinstance(template[key], dict):
if isinstance(candidate[key], dict):
status = cls.compare_keys(template[key], candidate[key], ignore_fields)
if not status:
return status
Expand Down
2 changes: 1 addition & 1 deletion trestle/tasks/cis_xlsx_to_oscal_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

logger = logging.getLogger(__name__)

timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat()
timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()


class XlsxHelper:
Expand Down
3 changes: 1 addition & 2 deletions trestle/tasks/csv_to_oscal_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ def print_info(self) -> None:

def configure(self) -> bool:
"""Configure."""
self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc
).isoformat()
self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()
# config verbosity
self._quiet = self._config.get('quiet', False)
self._verbose = not self._quiet
Expand Down
3 changes: 1 addition & 2 deletions trestle/tasks/ocp4_cis_profile_to_oscal_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None:
config_object: Config section associated with the task.
"""
super().__init__(config_object)
self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc
).isoformat()
self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

def print_info(self) -> None:
"""Print the help string."""
Expand Down
3 changes: 1 addition & 2 deletions trestle/tasks/ocp4_cis_profile_to_oscal_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None:
config_object: Config section associated with the task.
"""
super().__init__(config_object)
self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc
).isoformat()
self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

def set_timestamp(self, timestamp: str) -> None:
"""Set the timestamp."""
Expand Down
2 changes: 1 addition & 1 deletion trestle/tasks/oscal_catalog_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

logger = logging.getLogger(__name__)

timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat()
timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

recurse = True

Expand Down
3 changes: 1 addition & 2 deletions trestle/tasks/xlsx_to_oscal_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None:
"""
super().__init__(config_object)
self.xlsx_helper = XlsxHelper()
self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc
).isoformat()
self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

def set_timestamp(self, timestamp: str) -> None:
"""Set the timestamp."""
Expand Down
3 changes: 1 addition & 2 deletions trestle/tasks/xlsx_to_oscal_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None:
"""
super().__init__(config_object)
self.xlsx_helper = XlsxHelper()
self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc
).isoformat()
self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

def set_timestamp(self, timestamp: str) -> None:
"""Set the timestamp."""
Expand Down
2 changes: 1 addition & 1 deletion trestle/transforms/transformer_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TransformerBase(ABC):
"""Abstract base interface for all transformers."""

# the current time for consistent timestamping
_timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat()
_timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

@staticmethod
def set_timestamp(value: str) -> None:
Expand Down

0 comments on commit 7b8b353

Please sign in to comment.