diff options
author | Mykyta Holubakha <hilobakho@gmail.com> | 2017-07-20 06:03:59 +0300 |
---|---|---|
committer | Mykyta Holubakha <hilobakho@gmail.com> | 2017-07-20 06:03:59 +0300 |
commit | 353a2e82f5f81d00279ce4c087be4625966f7db6 (patch) | |
tree | 76f63e9fd2b22575053f188c9997b9238424c0ba /pomu/util | |
parent | Implemented user changes integration (diff) | |
download | pomu-353a2e82f5f81d00279ce4c087be4625966f7db6.tar.gz pomu-353a2e82f5f81d00279ce4c087be4625966f7db6.tar.bz2 pomu-353a2e82f5f81d00279ce4c087be4625966f7db6.zip |
Added bugz package source module
package: allow merging files using direct binary data
stopped using a temporary file in the URL handler
added utility helpers to extract URLs from text and parse numeric ranges
Diffstat (limited to 'pomu/util')
-rw-r--r-- | pomu/util/misc.py | 46 |
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) + |