|
5 | 5 | # LICENSE file in the root directory of this source tree. |
6 | 6 |
|
7 | 7 | import unittest |
8 | | -from importlib.metadata import EntryPoint |
| 8 | + |
| 9 | +try: |
| 10 | + from importlib.metadata import EntryPoint |
| 11 | +except ImportError: |
| 12 | + from importlib_metadata import EntryPoint |
| 13 | +from configparser import ConfigParser |
9 | 14 | from typing import Dict |
| 15 | +from typing import List |
10 | 16 | from unittest.mock import MagicMock, patch |
11 | 17 |
|
12 | 18 | from torchx.util.entrypoints import load, load_group |
13 | 19 |
|
14 | 20 |
|
| 21 | +def EntryPoint_from_config(config: ConfigParser) -> List[EntryPoint]: |
| 22 | + # from stdlib, Copyright (c) Python Authors |
| 23 | + return [ |
| 24 | + EntryPoint(name, value, group) |
| 25 | + for group in config.sections() |
| 26 | + for name, value in config.items(group) |
| 27 | + ] |
| 28 | + |
| 29 | + |
| 30 | +def EntryPoint_from_text(text: str) -> List[EntryPoint]: |
| 31 | + # from stdlib, Copyright (c) Python Authors |
| 32 | + config = ConfigParser(delimiters="=") |
| 33 | + config.read_string(text) |
| 34 | + return EntryPoint_from_config(config) |
| 35 | + |
| 36 | + |
15 | 37 | def foobar() -> str: |
16 | 38 | return "foobar" |
17 | 39 |
|
@@ -40,12 +62,11 @@ def barbaz() -> str: |
40 | 62 | [ep.grp.missing.mod.test] |
41 | 63 | baz = torchx.util.test.entrypoints_test.missing_module |
42 | 64 | """ |
43 | | -_ENTRY_POINTS: Dict[str, EntryPoint] = { |
44 | | - # pyre-ignore[16] |
45 | | - "entrypoints.test": EntryPoint._from_text(_EP_TXT), |
46 | | - "ep.grp.test": EntryPoint._from_text(_EP_GRP_TXT), |
47 | | - "ep.grp.missing.attr.test": EntryPoint._from_text(_EP_GRP_IGN_ATTR_TXT), |
48 | | - "ep.grp.missing.mod.test": EntryPoint._from_text(_EP_GRP_IGN_MOD_TXT), |
| 65 | +_ENTRY_POINTS: Dict[str, List[EntryPoint]] = { |
| 66 | + "entrypoints.test": EntryPoint_from_text(_EP_TXT), |
| 67 | + "ep.grp.test": EntryPoint_from_text(_EP_GRP_TXT), |
| 68 | + "ep.grp.missing.attr.test": EntryPoint_from_text(_EP_GRP_IGN_ATTR_TXT), |
| 69 | + "ep.grp.missing.mod.test": EntryPoint_from_text(_EP_GRP_IGN_MOD_TXT), |
49 | 70 | } |
50 | 71 |
|
51 | 72 | _METADATA_EPS: str = "torchx.util.entrypoints.metadata.entry_points" |
|
0 commit comments