ListArgumentTemplate   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A unfold() 0 2 1
A __init__() 0 2 1
1
import re
2
from collections import OrderedDict
3
4
5
def build_argument_templates_dictionnary():
6
    # Order matter, if some regex is more greedy than another, the it should go after
7
    argument_templates = OrderedDict()
8
    argument_templates[RangeArgumentTemplate.__name__] = RangeArgumentTemplate()
9
    argument_templates[ListArgumentTemplate.__name__] = ListArgumentTemplate()
10
    return argument_templates
11
12
13
class ArgumentTemplate(object):
14
    def __init__(self):
15
        self.regex = ""
16
17
    def unfold(self, match):
18
        raise NotImplementedError("Subclass must implement method `unfold(self, match)`!")
19
20
21
class ListArgumentTemplate(ArgumentTemplate):
22
    def __init__(self):
23
        self.regex = "\[[^]]*\]"
24
25
    def unfold(self, match):
26
        return match[1:-1].split(' ')
27
28
29
class RangeArgumentTemplate(ArgumentTemplate):
30
    def __init__(self):
31
        self.regex = "\[(\d+):(\d+)(?::(\d+))?\]"
32
33
    def unfold(self, match):
34
        groups = re.search(self.regex, match).groups()
35
        start = int(groups[0])
36
        end = int(groups[1])
37
        step = 1 if groups[2] is None else int(groups[2])
38
        return map(str, range(start, end, step))
39
40
41
argument_templates = build_argument_templates_dictionnary()
42