aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'pomu/util/misc.py')
-rw-r--r--pomu/util/misc.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/pomu/util/misc.py b/pomu/util/misc.py
index a6ebb1b..7fa6fd1 100644
--- a/pomu/util/misc.py
+++ b/pomu/util/misc.py
@@ -1,4 +1,5 @@
"""Miscellaneous utility functions"""
+import re
def list_add(dst, src):
"""
@@ -20,3 +21,48 @@ def pivot(string, idx, keep_pivot=False):
return (string[:idx], string[idx:])
else:
return (string[:idx], string[idx+1:])
+
+def extract_urls(text):
+ """Extracts URLs from arbitrary text"""
+ schemas = ['http://', 'https://', 'ftp://', 'ftps://']
+ words = list(filter(lambda x: any(y in x for y in schemas), text.split()))
+ maxshift = lambda x: max(x.find(y) for y in schemas)
+ links = [x[maxshift(x):].rstrip('\'")>.,') for x in words]
+ return links
+
+def parse_range(text, max_num=None):
+ """Parses a numeric range (e.g. 1-2,5-16)"""
+ text = re.sub('\s*-\s*', '-', text)
+ subranges = [x.strip() for x in text.split(',')]
+ subs = []
+ maxint = -1
+ for sub in subranges:
+ l, _, r = sub.partition('-')
+ if not l and not _:
+ continue
+ if (l and not l.isdigit()) or (r and not r.isdigit()):
+ return Result.Err('Invalid subrange: {}'.format(sub))
+ if l:
+ l = int(l)
+ maxint = max(l, maxint)
+ if r:
+ r = int(r)
+ maxint = max(r, maxint)
+ if _:
+ subs.append((l if l else 1, r if r else -1))
+ else:
+ subs.append((l, None))
+ if max_num:
+ maxint = max_num
+ res = set()
+ add = lambda x: res.add(x) if x <= maxint else None
+ for l, r in subs:
+ if not r:
+ add(l)
+ continue
+ if r == -1:
+ r = maxint
+ for x in range(l, r + 1):
+ add(x)
+ return Result.Ok(res)
+