aboutsummaryrefslogtreecommitdiff
blob: de297afadb5c031802948a0f6a2d454dfd5f1142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Miscellaneous utility functions"""
import re

from pomu.util.result import Result

def list_add(dst, src):
    """
    Extends the target list with a scalar, or contents of the given list
    """
    if isinstance(src, list):
        dst.extend(src)
    else:
        dst.append(src)


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.
    Alternatively, it is omitted.
    """
    if keep_pivot:
        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)