Skip to content

Commit

Permalink
do normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Nov 13, 2019
1 parent 80f7dbd commit 4e5fa9e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 47 deletions.
28 changes: 26 additions & 2 deletions dephell_setuptools/_base.py
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
10 changes: 5 additions & 5 deletions dephell_setuptools/_cfg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy
from configparser import ConfigParser
from pathlib import Path
from typing import Dict, List, Optional, Union

from setuptools.config import ConfigOptionsHandler, ConfigMetadataHandler
Expand All @@ -9,6 +10,9 @@


class CfgReader(BaseReader):
def __init__(self, path: Union[str, Path]):
self.path = self._normalize_path(path, default_name='setup.cfg')

@property
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
path = self.path
Expand All @@ -29,8 +33,4 @@ def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
ConfigOptionsHandler(container, options).parse()
ConfigMetadataHandler(container, options).parse()

result = dict()
for k, v in vars(container).items():
if v is not None:
result[k] = v
return result
return self._clean(vars(container))
4 changes: 2 additions & 2 deletions dephell_setuptools/_cli.py
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
29 changes: 15 additions & 14 deletions dephell_setuptools/_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@ def cd(path: Path):
class CommandReader(BaseReader):
@property
def content(self):
# generate a temporary json file which contains the metadata
output_json = NamedTemporaryFile()
cmd = [
sys.executable,
self.path.name,
'-q',
'--command-packages', 'dephell_setuptools',
'distutils_cmd',
'-o', output_json.name,
]
with cd(self.path.parent):
# generate a temporary json file which contains the metadata
output_json = NamedTemporaryFile()
cmd = [
sys.executable,
self.path.name,
'-q',
'--command-packages', 'dephell_setuptools',
'distutils_cmd',
'-o', output_json.name,
]
result = subprocess.run(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
env={'PYTHONPATH': str(Path(__file__).parent.parent)},
)
if result.returncode != 0:
return None
if result.returncode != 0:
return None

with open(output_json.name) as stream:
return json.load(stream)
with open(output_json.name) as stream:
result = json.load(stream)
return self._clean(result)


class JSONCommand(Command):
Expand Down
42 changes: 20 additions & 22 deletions dephell_setuptools/_manager.py
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
3 changes: 2 additions & 1 deletion dephell_setuptools/_pkginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ def content(self):
result = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if result.returncode != 0:
return None
return json.loads(result.stdout.decode())
content = json.loads(result.stdout.decode())
return self._clean(content)
3 changes: 2 additions & 1 deletion dephell_setuptools/_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class StaticReader(BaseReader):
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
if not self.call:
return None
return self._get_call_kwargs(self.call)
result = self._get_call_kwargs(self.call)
return self._clean(result)

@cached_property
def tree(self) -> tuple:
Expand Down

0 comments on commit 4e5fa9e

Please sign in to comment.