ddeutil-workflow 0.0.64__py3-none-any.whl → 0.0.65__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ddeutil/workflow/utils.py CHANGED
@@ -6,10 +6,12 @@
6
6
  """Utility function model."""
7
7
  from __future__ import annotations
8
8
 
9
+ import asyncio
9
10
  import stat
10
11
  import time
11
12
  from collections.abc import Iterator
12
13
  from datetime import date, datetime, timedelta
14
+ from functools import wraps
13
15
  from hashlib import md5
14
16
  from inspect import isfunction
15
17
  from itertools import chain, islice, product
@@ -28,6 +30,16 @@ UTC: Final[ZoneInfo] = ZoneInfo("UTC")
28
30
  MARK_NEWLINE: Final[str] = "||"
29
31
 
30
32
 
33
+ def to_train(camel: str) -> str:
34
+ """Convert camel case string to train case.
35
+
36
+ :param camel: (str) A camel case string that want to convert.
37
+
38
+ :rtype: str
39
+ """
40
+ return "".join("-" + i if i.isupper() else i for i in camel).lstrip("-")
41
+
42
+
31
43
  def prepare_newline(msg: str) -> str:
32
44
  """Prepare message that has multiple newline char.
33
45
 
@@ -313,3 +325,24 @@ def dump_all(
313
325
  elif isinstance(value, BaseModel):
314
326
  return value.model_dump(by_alias=by_alias)
315
327
  return value
328
+
329
+
330
+ def awaitable(func):
331
+ """Dynamic function to async or not depend on the called statement."""
332
+
333
+ @wraps(func)
334
+ async def async_wrapper(*args, **kwargs):
335
+ return func(*args, **kwargs)
336
+
337
+ @wraps(func)
338
+ def sync_wrapper(*args, **kwargs):
339
+ return func(*args, **kwargs)
340
+
341
+ def dispatch(*args, **kwargs):
342
+ try:
343
+ asyncio.get_running_loop()
344
+ return async_wrapper(*args, **kwargs)
345
+ except RuntimeError:
346
+ return sync_wrapper(*args, **kwargs)
347
+
348
+ return dispatch