-
-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
72 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from ._constants import FIELDS | ||
|
||
|
||
class BaseReader: | ||
def __init__(self, path: Union[str, Path]): | ||
self.path = self._normalize_path(path, default_name='setup.py') | ||
|
||
@staticmethod | ||
def _normalize_path(path: Union[str, Path], default_name: str) -> Path: | ||
if isinstance(path, str): | ||
path = Path(path) | ||
if not path.exists(): | ||
raise FileNotFoundError(str(path)) | ||
if path.is_dir(): | ||
path /= 'setup.py' | ||
self.path = path | ||
path /= default_name | ||
return path | ||
|
||
@staticmethod | ||
def _clean(data: dict): | ||
result = dict() | ||
for k, v in data.items(): | ||
if k not in FIELDS: | ||
continue | ||
if not v or v == 'UNKNOWN': | ||
continue | ||
result[k] = v | ||
|
||
# split keywords string by words | ||
if 'keywords' in result: | ||
if isinstance(result['keywords'], str): | ||
result['keywords'] = [result['keywords']] | ||
result['keywords'] = sum((kw.split() for kw in result['keywords']), []) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import json | ||
import sys | ||
|
||
from ._manager import ReadersManager | ||
from ._manager import read_setup | ||
|
||
|
||
def main(argv=None): | ||
if argv is None: | ||
argv = sys.argv[1:] | ||
result = ReadersManager()(argv[0]) | ||
result = read_setup(path=argv[0]) | ||
print(json.dumps(result, sort_keys=True, indent=2)) | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
from logging import getLogger | ||
from pathlib import Path | ||
from typing import Any, Callable, Iterable | ||
|
||
from ._cfg import CfgReader | ||
from ._cmd import CommandReader | ||
from ._pkginfo import PkgInfoReader | ||
from ._static import StaticReader | ||
from ._constants import FIELDS | ||
|
||
|
||
logger = getLogger('dephell_setuptools') | ||
ALL_READERS = ( | ||
StaticReader, | ||
CfgReader, | ||
CommandReader, | ||
PkgInfoReader, | ||
) | ||
|
||
|
||
class ReadersManager: | ||
error_handler = logger.exception | ||
readers = ( | ||
StaticReader, | ||
CfgReader, | ||
CommandReader, | ||
PkgInfoReader, | ||
) | ||
|
||
def __call__(self, path: Path): | ||
result = dict() | ||
for reader in self.readers: | ||
try: | ||
content = reader(path=path).content | ||
except Exception as e: | ||
self.error_handler(str(e)) | ||
else: | ||
result.update(content) | ||
exclude = (None, 0, []) | ||
result = {k: v for k, v in result.items() if k in FIELDS and v not in exclude} | ||
return result | ||
def read_setup(*, | ||
path: Path, | ||
error_handler: Callable[[Exception], Any] = logger.exception, | ||
readers: Iterable = ALL_READERS): | ||
result = dict() | ||
for reader in readers: | ||
try: | ||
content = reader(path=path).content | ||
except Exception as e: | ||
error_handler(e) | ||
else: | ||
result.update(content) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters