1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | """ |
---|
4 | Collection of globally used helper functions. |
---|
5 | """ |
---|
6 | __all__ = ["Property", "Singleton"] |
---|
7 | |
---|
8 | # property wrapper |
---|
9 | def Property(func): |
---|
10 | return property(**func()) |
---|
11 | |
---|
12 | |
---|
13 | class Singleton(type): |
---|
14 | """ |
---|
15 | Meta-class for singletons. |
---|
16 | """ |
---|
17 | |
---|
18 | def __init__(cls, name, bases, dic): |
---|
19 | super(Singleton, cls).__init__(name, bases, dic) |
---|
20 | cls.instance = None |
---|
21 | |
---|
22 | def __call__(cls, *args, **kwargs): |
---|
23 | if cls.instance is None: |
---|
24 | cls.instance = super(Singleton, cls).__call__(*args, **kwargs) |
---|
25 | |
---|
26 | return cls.instance |
---|
27 | |
---|
28 | |
---|
29 | def expandTraceList(trcs, selection): |
---|
30 | """ |
---|
31 | Expand comma separated list of traces into sorted internal trace numbers |
---|
32 | (reverse order). |
---|
33 | """ |
---|
34 | |
---|
35 | if not len(trcs): |
---|
36 | return None |
---|
37 | |
---|
38 | selection = [i.lower() for i in selection.split(",")] |
---|
39 | |
---|
40 | if "all" in selection: |
---|
41 | selected = range(0, len(trcs)) |
---|
42 | selected.reverse() |
---|
43 | return selected |
---|
44 | |
---|
45 | selected = [] |
---|
46 | for s in selection: |
---|
47 | # range |
---|
48 | if "-" in s: |
---|
49 | try: |
---|
50 | start, stop = map(int, s.split("-")) |
---|
51 | except: |
---|
52 | print " skipping '%s'" % s |
---|
53 | continue |
---|
54 | |
---|
55 | if stop < start: |
---|
56 | start, stop = stop, start |
---|
57 | |
---|
58 | selected.extend(range(start - 1, stop)) |
---|
59 | |
---|
60 | else: |
---|
61 | if s.isdigit(): |
---|
62 | selected.append(int(s) - 1) |
---|
63 | else: |
---|
64 | print " skipping '%s'" % s |
---|
65 | |
---|
66 | # unique |
---|
67 | selected = list(set(selected)) |
---|
68 | selected.sort() |
---|
69 | selected.reverse() |
---|
70 | |
---|
71 | return selected |
---|