diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2014-12-31 11:24:54 -0500 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-12-31 11:24:54 -0500 |
| commit | 6deadf03439b91582206605f0ce3a35d9da46e09 (patch) | |
| tree | 44edb2679ba3d7e4cd34a27f625a37ada0dbc5af | |
| parent | 032ff62dd86be10a2580389f4f91f82ec20fd47b (diff) | |
| download | python-setuptools-git-6deadf03439b91582206605f0ce3a35d9da46e09.tar.gz | |
Extract pattern and entry point parsing from EntryPoint.parse.
| -rw-r--r-- | pkg_resources/__init__.py | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 8854de86..572f8459 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -2272,6 +2272,14 @@ class EntryPoint(object): items = working_set.resolve(reqs, env, installer) list(map(working_set.add, items)) + pattern = re.compile( + r'(?P<name>\w+)\s*' + r'=\s*' + r'(?P<module>[\w.]+)\s*' + r'(:\s*(?P<attr>[\w.]+))?\s*' + r'(?P<extras>\[.*\])?\s*$' + ) + @classmethod def parse(cls, src, dist=None): """Parse a single entry point from string `src` @@ -2283,24 +2291,23 @@ class EntryPoint(object): The entry name and module name are required, but the ``:attrs`` and ``[extras]`` parts are optional """ - try: - attrs = extras = () - name, value = src.split('=', 1) - if '[' in value: - value, extras = value.split('[', 1) - req = Requirement.parse("x[" + extras) - if req.specs: - raise ValueError - extras = req.extras - if ':' in value: - value, attrs = value.split(':', 1) - if not MODULE(attrs.rstrip()): - raise ValueError - attrs = attrs.rstrip().split('.') - except ValueError: + m = cls.pattern.match(src) + if not m: msg = "EntryPoint must be in 'name=module:attrs [extras]' format" raise ValueError(msg, src) - return cls(name.strip(), value.strip(), attrs, extras, dist) + res = m.groupdict() + extras = cls._parse_extras(res['extras']) + attrs = (res['attr'] or '').split('.') + return cls(res['name'], res['module'], attrs, extras, dist) + + @classmethod + def _parse_extras(cls, extras_spec): + if not extras_spec: + return () + req = Requirement.parse('x' + extras_spec) + if req.specs: + raise ValueError() + return req.extras @classmethod def parse_group(cls, group, lines, dist=None): |
