|
1 | 1 | import uuid |
2 | 2 |
|
3 | | -from afw_runtime import * |
| 3 | +from afw_runtime import AFWFuncAbc, AFWResponse |
4 | 4 |
|
5 | | -DEFAULT_RESPONSE = [ |
6 | | - AFWResponse(title="Please enter a UUID like string", arg="", icon=ICON_HELP), |
7 | | - AFWResponse(title="Examples: 1111", arg="", icon=ICON_HELP), |
8 | | -] |
9 | 5 |
|
| 6 | +class AFWFunc(AFWFuncAbc): |
| 7 | + _u: uuid.UUID |
| 8 | + _message_error: str |
10 | 9 |
|
11 | | -def main(args: list[str], logger) -> list[AFWResponse]: |
12 | | - logger.debug("test..") |
13 | | - query = args[0] |
14 | | - logger.debug(query) |
15 | | - |
16 | | - responses = list() |
17 | | - try: |
18 | | - u = uuid.UUID(query) |
19 | | - except ValueError as e: |
20 | | - u = None |
21 | | - v = str(uuid.uuid4()).upper() |
22 | | - responses += [ |
23 | | - AFWResponse(title=v, arg=v, valid=True), |
24 | | - AFWResponse(title=str(e), arg="", icon=ICON_ERROR), |
| 10 | + @staticmethod |
| 11 | + def _make_data_from_uuid(u: uuid.UUID) -> list[AFWResponse]: |
| 12 | + return ( |
| 13 | + (u.hex, "32-character lowercase hexadecimal string."), |
| 14 | + (str(u), "32-character lowercase hexadecimal string with dash."), |
| 15 | + # (str(u).upper(), ""), |
| 16 | + (str(u.int), "The UUID as a 128-bit integer."), |
| 17 | + (str(u.urn), "The UUID as a URN as specified in RFC 4122."), |
| 18 | + ( |
| 19 | + str(u.version), |
| 20 | + "The UUID version number (1 through 5, specified is RFC_4122).", |
| 21 | + ), |
| 22 | + ) |
| 23 | + |
| 24 | + @property |
| 25 | + def _data_defaulte(self) -> list: |
| 26 | + u = uuid.uuid4() |
| 27 | + return self._make_data_from_uuid(u) |
| 28 | + |
| 29 | + @property |
| 30 | + def _data_tips(self) -> list: |
| 31 | + return list() |
| 32 | + |
| 33 | + @property |
| 34 | + def _data_success(self) -> list: |
| 35 | + return self._make_data_from_uuid(self._u) |
| 36 | + |
| 37 | + @property |
| 38 | + def _data_error(self) -> list: |
| 39 | + return [ |
| 40 | + (self._message_error, ""), |
| 41 | + # ("Please enter a UUID like string", ""), |
| 42 | + # ("Examples: 1111", ""), |
25 | 43 | ] |
26 | | - responses += DEFAULT_RESPONSE |
27 | 44 |
|
28 | | - if u is None: |
29 | | - return responses |
| 45 | + def _process(self) -> list[AFWResponse]: |
| 46 | + try: |
| 47 | + if len(self.args) == 0: |
| 48 | + raise ValueError(" Missing argument 'QUERY'") |
30 | 49 |
|
31 | | - return [AFWResponse(title=str(u), arg=str(u), icon=ICON_NOTE, valid=True)] |
| 50 | + self._u = uuid.UUID(self.args[0]) |
| 51 | + return True |
| 52 | + |
| 53 | + except ValueError as e: |
| 54 | + self._message_error = str(e) |
| 55 | + return False |
| 56 | + |
| 57 | + |
| 58 | +def main(args: list[str], logger) -> list[AFWResponse]: |
| 59 | + return AFWFunc(args, logger)() |
0 commit comments