aboutsummaryrefslogtreecommitdiff
path: root/src/utils/parse.py
diff options
context:
space:
mode:
authorGravatar jesopo2019-11-15 13:39:24 +0000
committerGravatar jesopo2019-11-15 13:39:24 +0000
commitbfcf40edd71c5bdd436ab8b535c880fad8781f16 (patch)
treef26b390a444203f03a03be84b475cb83bf907bb0 /src/utils/parse.py
parentonly try to shlex when we know we've found a command hook (diff)
signature
split some stuff out of utils/__init__.py
Diffstat (limited to 'src/utils/parse.py')
-rw-r--r--src/utils/parse.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/utils/parse.py b/src/utils/parse.py
index 7699bd19..d5018441 100644
--- a/src/utils/parse.py
+++ b/src/utils/parse.py
@@ -1,4 +1,4 @@
-import io, typing
+import decimal, io, typing
COMMENT_TYPES = ["#", "//"]
def hashflags(filename: str
@@ -83,3 +83,29 @@ def try_int(s: str) -> typing.Optional[int]:
def line_normalise(s: str) -> str:
lines = list(filter(None, [line.strip() for line in s.split("\n")]))
return " ".join(line.replace(" ", " ") for line in lines)
+
+def parse_number(s: str) -> str:
+ try:
+ decimal.Decimal(s)
+ return s
+ except:
+ pass
+
+ unit = s[-1].lower()
+ number_str = s[:-1]
+ try:
+ number = decimal.Decimal(number_str)
+ except:
+ raise ValueError("Invalid format '%s' passed to parse_number" %
+ number_str)
+
+ if unit == "k":
+ number *= decimal.Decimal("1_000")
+ elif unit == "m":
+ number *= decimal.Decimal("1_000_000")
+ elif unit == "b":
+ number *= decimal.Decimal("1_000_000_000")
+ else:
+ raise ValueError("Unknown unit '%s' given to parse_number" % unit)
+ return str(number)
+