aboutsummaryrefslogtreecommitdiff
path: root/pomu/util
diff options
context:
space:
mode:
Diffstat (limited to 'pomu/util')
-rw-r--r--pomu/util/pkg.py6
-rw-r--r--pomu/util/query.py78
-rw-r--r--pomu/util/str.py2
3 files changed, 60 insertions, 26 deletions
diff --git a/pomu/util/pkg.py b/pomu/util/pkg.py
index 7ff3a7c..7544e42 100644
--- a/pomu/util/pkg.py
+++ b/pomu/util/pkg.py
@@ -24,19 +24,19 @@ def cpv_split(pkg):
# openssl-0.9.8z_p8-r100
m = re.search(r'-r\d+$', pkg) # revision is optional
if m:
- pkg, rev = pivot(pkg, m.start(0), False)
+ pkg, rev = pivot(pkg, m.start(0))
else:
rev = None
# openssl-0.9.8z_p8
m = re.search(r'_({})(\d*)$'.format('|'.join(suffixes)), pkg)
if m:
- pkg, suff = pivot(pkg, m.start(0), False)
+ pkg, suff = pivot(pkg, m.start(0))
else:
suff = None
# openssl-0.9.8z
m = re.search(r'-(\d+(\.\d+)*)([a-z])?$', pkg)
if m:
- pkg, vernum = pivot(pkg, m.start(0), False)
+ pkg, vernum = pivot(pkg, m.start(0))
else:
vernum = None
# openssl
diff --git a/pomu/util/query.py b/pomu/util/query.py
index d9dc09e..872e0a3 100644
--- a/pomu/util/query.py
+++ b/pomu/util/query.py
@@ -3,25 +3,59 @@ A module to (non)interactively query the user for impure values
"""
from pomu.util.result import Result
-def query(name, prompt=None, default=None):
- """
- Queries the impure world for name
- Parameters:
- name - the name
- prompt - prompt text
- default - default value used for errors, forced non-interactive etc.
- TODO: non-interactive
- """
- if not prompt:
- prompt = 'Please enter ' + name
- if default: prompt += ' ({})'.format(default)
- prompt += ' > '
- res = None
- try:
- res = input(prompt)
- except EOFError: pass
- if not res:
- res = default
- if not res:
- return Result.Err('No {} or default provided'.format(name))
- return Result.Ok()
+class _query:
+ def __init__(self):
+ self.map = {}
+ self.interactive = True
+
+ def __call__(self, name, prompt=None, default=None):
+ """
+ Queries the impure world for name
+ Parameters:
+ name - the name
+ prompt - prompt text
+ default - default value used for errors, forced non-interactive etc.
+ TODO: non-interactive
+ """
+ if name in self.map:
+ return Result.Ok(self.map[name])
+ if not prompt:
+ prompt = 'Please enter ' + name
+ if default: prompt += ' ({})'.format(default)
+ prompt += ' > '
+ res = None
+ if self.interactive:
+ try:
+ res = input(prompt)
+ except EOFError: pass
+ if not res:
+ res = default
+ if not res:
+ return Result.Err('No {} or default provided'.format(name))
+ return Result.Ok(res)
+
+ def set(self, name, val):
+ old = None
+ if name in self.map:
+ old = self.map[name]
+ if val is None:
+ self.unset(name)
+ self.map[name] = val
+ return old
+
+ def unset(self, name):
+ if name in self.map:
+ del self.map[name]
+
+query = _query()
+
+class QueryContext:
+ def __init__(self, **kwargs):
+ self.map = kwargs
+
+ def __enter__(self):
+ self.map_old = {x: query.set(x, self.map[x]) for x in self.map}
+
+ def __exit__(self):
+ for x, y in self.map_old.items():
+ query.set(x, y)
diff --git a/pomu/util/str.py b/pomu/util/str.py
index 96a7c81..11fc514 100644
--- a/pomu/util/str.py
+++ b/pomu/util/str.py
@@ -1,5 +1,5 @@
"""String processing utilities"""
-def pivot(string, idx, keep_pivot=True):
+def pivot(string, idx, keep_pivot=False):
"""
A function to split a string in two, pivoting at string[idx].
If keep_pivot is set, the pivot character is included in the second string.