Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'archinstall/lib/utils')
-rw-r--r--archinstall/lib/utils/__init__.py0
-rw-r--r--archinstall/lib/utils/singleton.py15
-rw-r--r--archinstall/lib/utils/util.py51
3 files changed, 66 insertions, 0 deletions
diff --git a/archinstall/lib/utils/__init__.py b/archinstall/lib/utils/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/archinstall/lib/utils/__init__.py
diff --git a/archinstall/lib/utils/singleton.py b/archinstall/lib/utils/singleton.py
new file mode 100644
index 00000000..55be70eb
--- /dev/null
+++ b/archinstall/lib/utils/singleton.py
@@ -0,0 +1,15 @@
+from typing import Dict, Any
+
+
+class _Singleton(type):
+ """ A metaclass that creates a Singleton base class when called. """
+ _instances: Dict[Any, Any] = {}
+
+ def __call__(cls, *args, **kwargs):
+ if cls not in cls._instances:
+ cls._instances[cls] = super().__call__(*args, **kwargs)
+ return cls._instances[cls]
+
+
+class Singleton(_Singleton('SingletonMeta', (object,), {})): # type: ignore
+ pass
diff --git a/archinstall/lib/utils/util.py b/archinstall/lib/utils/util.py
new file mode 100644
index 00000000..2e42b3cf
--- /dev/null
+++ b/archinstall/lib/utils/util.py
@@ -0,0 +1,51 @@
+from pathlib import Path
+from typing import Any, TYPE_CHECKING, Optional, List
+
+from ..output import FormattedOutput
+from ..output import info
+
+if TYPE_CHECKING:
+ _: Any
+
+
+def prompt_dir(text: str, header: Optional[str] = None) -> Path:
+ if header:
+ print(header)
+
+ while True:
+ path = input(text).strip(' ')
+ dest_path = Path(path)
+ if dest_path.exists() and dest_path.is_dir():
+ return dest_path
+ info(_('Not a valid directory: {}').format(dest_path))
+
+
+def is_subpath(first: Path, second: Path):
+ """
+ Check if _first_ a subpath of _second_
+ """
+ try:
+ first.relative_to(second)
+ return True
+ except ValueError:
+ return False
+
+
+def format_cols(items: List[str], header: Optional[str] = None) -> str:
+ if header:
+ text = f'{header}:\n'
+ else:
+ text = ''
+
+ nr_items = len(items)
+ if nr_items <= 4:
+ col = 1
+ elif nr_items <= 8:
+ col = 2
+ elif nr_items <= 12:
+ col = 3
+ else:
+ col = 4
+
+ text += FormattedOutput.as_columns(items, col)
+ return text