]>
Commit | Line | Data |
---|---|---|
02b800ce RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: strutils.py | |
3 | # Purpose: String Utilities | |
4 | # | |
5 | # Author: Morgan Hua | |
6 | # | |
7 | # Created: 11/3/05 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | ||
14 | def caseInsensitiveCompare(s1, s2): | |
15 | """ Method used by sort() to sort values in case insensitive order """ | |
16 | s1L = s1.lower() | |
17 | s2L = s2.lower() | |
18 | if s1L == s2L: | |
19 | return 0 | |
20 | elif s1L < s2L: | |
21 | return -1 | |
22 | else: | |
23 | return 1 | |
aca310e5 RD |
24 | |
25 | def multiSplit(stringList, tokenList=[" "]): | |
26 | """Splits strings in stringList by tokens, returns list of string.""" | |
27 | if not stringList: return [] | |
28 | if isinstance(tokenList, basestring): | |
29 | tokenList = [tokenList] | |
30 | if isinstance(stringList, basestring): | |
31 | stringList = [stringList] | |
32 | rtnList = stringList | |
33 | for token in tokenList: | |
34 | rtnList = rtnList[:] | |
35 | for string in rtnList: | |
36 | if string.find(token) > -1: | |
37 | rtnList.remove(string) | |
38 | names = string.split(token) | |
39 | for name in names: | |
40 | name = name.strip() | |
41 | if name: | |
42 | rtnList.append(name) | |
43 | return rtnList | |
44 | ||
45 | QUOTES = ("\"", "'") | |
46 | ||
47 | def _findArgStart(argStr): | |
48 | i = -1 | |
49 | for c in argStr: | |
50 | i += 1 | |
51 | if (c == " "): | |
52 | continue | |
53 | elif (c == ","): | |
54 | continue | |
55 | return i | |
56 | return None | |
57 | ||
58 | def _findArgEnd(argStr): | |
59 | quotedArg = True | |
60 | argEndChar = argStr[0] | |
61 | if (not argEndChar in QUOTES): | |
62 | argEndChar = "," | |
63 | quotedArg = False | |
64 | i = -1 | |
65 | firstChar = True | |
66 | for c in argStr: | |
67 | i+= 1 | |
68 | if (firstChar): | |
69 | firstChar = False | |
70 | if (quotedArg): | |
71 | continue | |
72 | if (c == argEndChar): | |
73 | if (quotedArg): | |
74 | return min(i+1, len(argStr)) | |
75 | else: | |
76 | return i | |
77 | return i | |
78 | ||
79 | def parseArgs(argStr, stripQuotes=False): | |
80 | """ | |
81 | Given a str representation of method arguments, returns list arguments (as | |
82 | strings). | |
83 | ||
84 | Input: "('[a,b]', 'c', 1)" -> Output: ["'[a,b]'", "'c'", "1"]. | |
85 | ||
86 | If stripQuotes, removes quotes from quoted arg. | |
87 | """ | |
88 | if (argStr.startswith("(")): | |
89 | argStr = argStr[1:] | |
90 | if (argStr.endswith(")")): | |
91 | argStr = argStr[:-1] | |
92 | else: | |
93 | raise AssertionError("Expected argStr to end with ')'") | |
94 | ||
95 | rtn = [] | |
96 | argsStr = argStr.strip() | |
97 | while (True): | |
98 | startIndex = _findArgStart(argStr) | |
99 | if (startIndex == None): | |
100 | break | |
101 | argStr = argStr[startIndex:] | |
102 | endIndex = _findArgEnd(argStr) | |
103 | if (endIndex == len(argStr) - 1): | |
104 | rtn.append(argStr.strip()) | |
105 | break | |
106 | t = argStr[:endIndex].strip() | |
107 | if (stripQuotes and t[0] in QUOTES and t[-1] in QUOTES): | |
108 | t = t[1:-1] | |
109 | rtn.append(t) | |
110 | argStr = argStr[endIndex:] | |
111 | return rtn |