Skip to content

Commit

Permalink
Replace os.path with pathlib (#118)
Browse files Browse the repository at this point in the history
- Replace os.path with pathlib
- Fix TOC blank line bug
  • Loading branch information
alfinkel authored May 6, 2021
1 parent b8ece1a commit 7e57766
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 258 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# [1.18.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.18.0)

- [CHANGED] Now using `pathlib` exclusively for operating system filepath and file functionality.
- [FIXED] README table of contents generation multi-blank line bug is resolved.

# [1.17.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.17.0)

- [ADDED] Locker get_large_files method added to return large files in the locker.
Expand Down
2 changes: 1 addition & 1 deletion compliance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# limitations under the License.
"""Compliance automation package."""

__version__ = '1.17.0'
__version__ = '1.18.0'
21 changes: 9 additions & 12 deletions compliance/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import inspect
import json
import os
from collections import OrderedDict
from copy import deepcopy
from pathlib import Path

from compliance.utils.credentials import Config

Expand Down Expand Up @@ -105,7 +105,7 @@ def load(self, config_file=None):
self._config = self.DEFAULTS.copy()
return
try:
self._config = json.loads(open(config_file).read())
self._config = json.loads(Path(config_file).read_text())
except ValueError as err:
err.args += (config_file, )
raise
Expand Down Expand Up @@ -156,7 +156,7 @@ def add_evidences(self, evidence_list):

def get_template_dir(self, test_obj=None):
"""
Provide the full path to the template directory for the test object.
Provide absolute path to the template directory for the test object.
The associated path will be the first directory found named
``templates`` in the test object absolute path traversed in reverse.
Expand All @@ -166,16 +166,13 @@ def get_template_dir(self, test_obj=None):
:param test_obj: a :class:`compliance.ComplianceTest` object from
where the template directory search will start from.
"""
path = os.path.abspath(os.curdir)
paths = [Path().resolve()] + list(Path().resolve().parents)
if test_obj is not None:
path = inspect.getfile(test_obj.__class__)
while True:
if path == '/':
return None
result = os.path.join(path, 'templates')
if os.path.isdir(result):
return result
path = os.path.dirname(path)
paths = list(Path(inspect.getfile(test_obj.__class__)).parents)
for path in paths[:-1]:
templates = Path(path, 'templates')
if templates.is_dir():
return str(templates)


__config = None
Expand Down
10 changes: 5 additions & 5 deletions compliance/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import copy
import itertools
import json
import os
from collections import defaultdict
from pathlib import Path


class ControlDescriptor(object):
Expand All @@ -33,11 +33,11 @@ def __init__(self, dirs=None):
self._controls = {}
self._paths = []
for d in dirs or ['.']:
json_file = os.path.join(os.path.abspath(d), 'controls.json')
if not os.path.isfile(json_file):
json_file = Path(d, 'controls.json').resolve()
if not json_file.is_file():
continue
self._controls.update(json.loads(open(json_file).read()))
self._paths.append(json_file)
self._controls.update(json.loads(json_file.read_text()))
self._paths.append(str(json_file))

@property
def paths(self):
Expand Down
4 changes: 2 additions & 2 deletions compliance/evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def rootdir(self):

@property
def dir_path(self):
return str(PurePath(self.rootdir).joinpath(self.category))
return str(PurePath(self.rootdir, self.category))

@property
def name(self):
return substitute_config(self._name)

@property
def path(self):
return str(PurePath(self.dir_path).joinpath(self.name))
return str(PurePath(self.dir_path, self.name))

@property
def extension(self):
Expand Down
8 changes: 3 additions & 5 deletions compliance/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# limitations under the License.
"""Compliance fetcher automation module."""

import os
import shutil
import tempfile
import unittest
from pathlib import Path

from compliance.config import get_config
from compliance.utils.http import BaseSession
Expand Down Expand Up @@ -121,10 +121,8 @@ def fetch(url, name):
"""
r = requests.get(url)
r.raise_for_status()
path = os.path.join(tempfile.gettempdir(), name)

with open(path, 'wb') as f:
path = Path(tempfile.gettempdir(), name)
with path.open('wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)

return path
Loading

0 comments on commit 7e57766

Please sign in to comment.