Skip to content

Commit 4e5fa9e

Browse files
committed
do normalization
1 parent 80f7dbd commit 4e5fa9e

7 files changed

Lines changed: 72 additions & 47 deletions

File tree

dephell_setuptools/_base.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
from pathlib import Path
22
from typing import Union
33

4+
from ._constants import FIELDS
5+
46

57
class BaseReader:
68
def __init__(self, path: Union[str, Path]):
9+
self.path = self._normalize_path(path, default_name='setup.py')
10+
11+
@staticmethod
12+
def _normalize_path(path: Union[str, Path], default_name: str) -> Path:
713
if isinstance(path, str):
814
path = Path(path)
915
if not path.exists():
1016
raise FileNotFoundError(str(path))
1117
if path.is_dir():
12-
path /= 'setup.py'
13-
self.path = path
18+
path /= default_name
19+
return path
20+
21+
@staticmethod
22+
def _clean(data: dict):
23+
result = dict()
24+
for k, v in data.items():
25+
if k not in FIELDS:
26+
continue
27+
if not v or v == 'UNKNOWN':
28+
continue
29+
result[k] = v
30+
31+
# split keywords string by words
32+
if 'keywords' in result:
33+
if isinstance(result['keywords'], str):
34+
result['keywords'] = [result['keywords']]
35+
result['keywords'] = sum((kw.split() for kw in result['keywords']), [])
36+
37+
return result

dephell_setuptools/_cfg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from copy import deepcopy
22
from configparser import ConfigParser
3+
from pathlib import Path
34
from typing import Dict, List, Optional, Union
45

56
from setuptools.config import ConfigOptionsHandler, ConfigMetadataHandler
@@ -9,6 +10,9 @@
910

1011

1112
class CfgReader(BaseReader):
13+
def __init__(self, path: Union[str, Path]):
14+
self.path = self._normalize_path(path, default_name='setup.cfg')
15+
1216
@property
1317
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
1418
path = self.path
@@ -29,8 +33,4 @@ def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
2933
ConfigOptionsHandler(container, options).parse()
3034
ConfigMetadataHandler(container, options).parse()
3135

32-
result = dict()
33-
for k, v in vars(container).items():
34-
if v is not None:
35-
result[k] = v
36-
return result
36+
return self._clean(vars(container))

dephell_setuptools/_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import json
22
import sys
33

4-
from ._manager import ReadersManager
4+
from ._manager import read_setup
55

66

77
def main(argv=None):
88
if argv is None:
99
argv = sys.argv[1:]
10-
result = ReadersManager()(argv[0])
10+
result = read_setup(path=argv[0])
1111
print(json.dumps(result, sort_keys=True, indent=2))
1212
return 0

dephell_setuptools/_cmd.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ def cd(path: Path):
2424
class CommandReader(BaseReader):
2525
@property
2626
def content(self):
27+
# generate a temporary json file which contains the metadata
28+
output_json = NamedTemporaryFile()
29+
cmd = [
30+
sys.executable,
31+
self.path.name,
32+
'-q',
33+
'--command-packages', 'dephell_setuptools',
34+
'distutils_cmd',
35+
'-o', output_json.name,
36+
]
2737
with cd(self.path.parent):
28-
# generate a temporary json file which contains the metadata
29-
output_json = NamedTemporaryFile()
30-
cmd = [
31-
sys.executable,
32-
self.path.name,
33-
'-q',
34-
'--command-packages', 'dephell_setuptools',
35-
'distutils_cmd',
36-
'-o', output_json.name,
37-
]
3838
result = subprocess.run(
3939
cmd,
4040
stderr=subprocess.PIPE,
4141
stdout=subprocess.PIPE,
4242
env={'PYTHONPATH': str(Path(__file__).parent.parent)},
4343
)
44-
if result.returncode != 0:
45-
return None
44+
if result.returncode != 0:
45+
return None
4646

47-
with open(output_json.name) as stream:
48-
return json.load(stream)
47+
with open(output_json.name) as stream:
48+
result = json.load(stream)
49+
return self._clean(result)
4950

5051

5152
class JSONCommand(Command):

dephell_setuptools/_manager.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
from logging import getLogger
22
from pathlib import Path
3+
from typing import Any, Callable, Iterable
34

45
from ._cfg import CfgReader
56
from ._cmd import CommandReader
67
from ._pkginfo import PkgInfoReader
78
from ._static import StaticReader
8-
from ._constants import FIELDS
99

1010

1111
logger = getLogger('dephell_setuptools')
12+
ALL_READERS = (
13+
StaticReader,
14+
CfgReader,
15+
CommandReader,
16+
PkgInfoReader,
17+
)
1218

1319

14-
class ReadersManager:
15-
error_handler = logger.exception
16-
readers = (
17-
StaticReader,
18-
CfgReader,
19-
CommandReader,
20-
PkgInfoReader,
21-
)
22-
23-
def __call__(self, path: Path):
24-
result = dict()
25-
for reader in self.readers:
26-
try:
27-
content = reader(path=path).content
28-
except Exception as e:
29-
self.error_handler(str(e))
30-
else:
31-
result.update(content)
32-
exclude = (None, 0, [])
33-
result = {k: v for k, v in result.items() if k in FIELDS and v not in exclude}
34-
return result
20+
def read_setup(*,
21+
path: Path,
22+
error_handler: Callable[[Exception], Any] = logger.exception,
23+
readers: Iterable = ALL_READERS):
24+
result = dict()
25+
for reader in readers:
26+
try:
27+
content = reader(path=path).content
28+
except Exception as e:
29+
error_handler(e)
30+
else:
31+
result.update(content)
32+
return result

dephell_setuptools/_pkginfo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ def content(self):
1414
result = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
1515
if result.returncode != 0:
1616
return None
17-
return json.loads(result.stdout.decode())
17+
content = json.loads(result.stdout.decode())
18+
return self._clean(content)

dephell_setuptools/_static.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class StaticReader(BaseReader):
1010
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
1111
if not self.call:
1212
return None
13-
return self._get_call_kwargs(self.call)
13+
result = self._get_call_kwargs(self.call)
14+
return self._clean(result)
1415

1516
@cached_property
1617
def tree(self) -> tuple:

0 commit comments

Comments
 (0)