Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/tidypy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import shutil
import sys
import pathlib

from hashlib import sha512

Expand Down Expand Up @@ -295,11 +296,10 @@ def get_local_config(project_path, use_cache=True):
:type use_cache: bool
:rtype: dict
"""
pyproject_path = pathlib.Path(project_path) / 'pyproject.toml'

pyproject_path = os.path.join(project_path, 'pyproject.toml')

if os.path.exists(pyproject_path):
with open(pyproject_path, 'r') as config_file:
if pyproject_path.exists():
with pyproject_path.open() as config_file:
config = toml.load(config_file)

config = config.get('tool', {}).get('tidypy', {})
Expand All @@ -326,7 +326,12 @@ def get_project_config(project_path, use_cache=True):
:type use_cache: bool
:rtype: dict
"""

path = pathlib.Path(project_path)
if path.is_file():
project_path = pathlib.Path.cwd()
else:
project_path = path

return get_local_config(project_path, use_cache=use_cache) \
or get_user_config(project_path, use_cache=use_cache) \
or get_default_config()
Expand Down
44 changes: 27 additions & 17 deletions src/tidypy/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ class Finder(object):
be analyzed.
"""

def __init__(self, base_path, config):
def __init__(self, path, config):
"""
:param base_path: the path to the base of the project
:type base_path: str
:param path: the path to the base of the project or a python file
:type path: str
:param config: the configuration to use when searching the project
:type config: dict
"""

self.base_path = Path(base_path).resolve()

path = Path(path).resolve()
if path.is_file():
self.base_path = Path.cwd()
else:
self.base_path = path
self.excludes = compile_masks(config['exclude'])

self._found = dict()
self._find(self.base_path)
self._find(path)
self._found = dict([
(dirname, files)
for dirname, files in self._found.items()
Expand All @@ -61,17 +65,23 @@ def relative_to_project(self, filepath):
return str(Path(filepath).relative_to(self.base_path))

def _find(self, path):
for subpath in path.iterdir():
if subpath.is_dir():
if not self.is_excluded_dir(subpath):
self._found[str(subpath)] = []
self._find(subpath)

elif subpath.is_file():
if not self.is_excluded(subpath):
if str(path) not in self._found:
self._found[str(path)] = []
self._found[str(path)].append(str(subpath))
if path.is_file():
if not self.is_excluded(path):
if str(path) not in self._found:
self._found[str(self.base_path)] = []
self._found[str(self.base_path)].append(str(path))
else:
for subpath in path.iterdir():
if subpath.is_dir():
if not self.is_excluded_dir(subpath):
self._found[str(subpath)] = []
self._find(subpath)

elif subpath.is_file():
if not self.is_excluded(subpath):
if str(path) not in self._found:
self._found[str(path)] = []
self._found[str(path)].append(str(subpath))

def is_excluded(self, path):
"""
Expand Down
13 changes: 8 additions & 5 deletions src/tidypy/reports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ class Report(object):
The base class for TidyPy issue reporters.
"""

def __init__(self, config, base_path, output_file=None):
def __init__(self, config, path, output_file=None):
"""
:param config:
the configuration used during the analysis of the project
:type config: dict
:param base_path: the path to the project base directory
:type base_path: str
:param path: the path to the project base directory or file
:type path: str
"""

self.config = config
self.base_path = base_path
path = Path(path)
if path.is_file():
self.base_path = Path.cwd()
else:
self.base_path = path
self._output_needs_closing = False
if output_file:
self.output_file = output_file
Expand Down Expand Up @@ -57,7 +61,6 @@ def relative_filename(self, filename):
:type filename: str
:rtype: str
"""

return Path(filename).relative_to(self.base_path).as_posix()

def output(self, msg, newline=True):
Expand Down