|
6 | 6 |
|
7 | 7 | # pyre-strict |
8 | 8 |
|
| 9 | +import abc |
9 | 10 | import asyncio |
10 | 11 | import copy |
11 | 12 | import inspect |
|
17 | 18 | import shutil |
18 | 19 | import typing |
19 | 20 | import warnings |
| 21 | +from abc import abstractmethod |
20 | 22 | from dataclasses import asdict, dataclass, field |
21 | 23 | from datetime import datetime |
22 | 24 | from enum import Enum, IntEnum |
|
39 | 41 | Union, |
40 | 42 | ) |
41 | 43 |
|
| 44 | +import parse |
| 45 | + |
42 | 46 | from torchx.util.types import to_dict |
43 | 47 |
|
44 | 48 | _APP_STATUS_FORMAT_TEMPLATE = """AppStatus: |
@@ -877,11 +881,86 @@ def __init__(self, status: AppStatus, *args: object) -> None: |
877 | 881 | self.status = status |
878 | 882 |
|
879 | 883 |
|
| 884 | +U = TypeVar("U", bound="StructuredRunOpt") |
| 885 | + |
| 886 | + |
| 887 | +class StructuredRunOpt(abc.ABC): |
| 888 | + """ |
| 889 | + StructuredRunOpt is a class that represents a structured run option. |
| 890 | + This is to allow for more complex types than currently supported. |
| 891 | +
|
| 892 | + Usage |
| 893 | +
|
| 894 | + .. code-block:: python |
| 895 | +
|
| 896 | + @dataclass |
| 897 | + class Ulimit(StructuredRunOpt): |
| 898 | + name: str |
| 899 | + hard: int |
| 900 | + soft: int |
| 901 | +
|
| 902 | + def template(self) -> str: |
| 903 | + return "{name},{soft:d},{hard:d}" |
| 904 | +
|
| 905 | + """ |
| 906 | + |
| 907 | + @abstractmethod |
| 908 | + def template(self) -> str: |
| 909 | + """ |
| 910 | + Returns the template string for the StructuredRunOpt. |
| 911 | + These are mapped to the field names of the StructuredRunOpt object. |
| 912 | +
|
| 913 | + Usage |
| 914 | +
|
| 915 | + .. code-block:: python |
| 916 | +
|
| 917 | + @dataclass |
| 918 | + class Ulimit(StructuredRunOpt): |
| 919 | + name: str |
| 920 | + hard: int |
| 921 | + soft: int |
| 922 | +
|
| 923 | + def template(self) -> str: |
| 924 | + # The template string should contain the field names of the Ulimit object. |
| 925 | + # Template strings also may need types as below where `:d` is for integer type. |
| 926 | + return "{name},{soft:d},{hard:d}" |
| 927 | +
|
| 928 | + opts = runopts() |
| 929 | + opts.add("ulimit", type_=self.Ulimit, help="ulimits for the container") |
| 930 | +
|
| 931 | + # .from_repr() is used to create a Ulimit object from a string representation that is the template. |
| 932 | + cfg = opts.resolve( |
| 933 | + { |
| 934 | + "ulimit": self.Ulimit.from_repr( |
| 935 | + "test,50,100", |
| 936 | + ) |
| 937 | + } |
| 938 | + ) |
| 939 | +
|
| 940 | + """ |
| 941 | + pass |
| 942 | + |
| 943 | + def __repr__(self) -> str: |
| 944 | + return self.template().format(**asdict(self)) |
| 945 | + |
| 946 | + def __eq__(self, other: object) -> bool: |
| 947 | + return isinstance(other, type(self)) and asdict(self) == asdict(other) |
| 948 | + |
| 949 | + @classmethod |
| 950 | + def from_repr(cls: Type[U], repr: str) -> U: |
| 951 | + """ |
| 952 | + Parses the repr string and returns a StructuredRunOpt object |
| 953 | + """ |
| 954 | + tmpl = cls.__new__(cls).template() |
| 955 | + result = parse.parse(tmpl, repr) |
| 956 | + return cls(**result.named) |
| 957 | + |
| 958 | + |
880 | 959 | # valid run cfg values; only support primitives (str, int, float, bool, List[str], Dict[str, str]) |
881 | 960 | # TODO(wilsonhong): python 3.9+ supports list[T] in typing, which can be used directly |
882 | 961 | # in isinstance(). Should replace with that. |
883 | 962 | # see: https://docs.python.org/3/library/stdtypes.html#generic-alias-type |
884 | | -CfgVal = Union[str, int, float, bool, List[str], Dict[str, str], None] |
| 963 | +CfgVal = Union[str, int, float, bool, List[str], Dict[str, str], StructuredRunOpt, None] |
885 | 964 |
|
886 | 965 |
|
887 | 966 | T = TypeVar("T") |
|
0 commit comments