chiark / gitweb /
54b92582bcbfea7b0db9c1d020cf4773ea8546cd
[cura.git] /
1 """
2 String object attributes.
3
4 """
5
6 from __future__ import absolute_import
7
8 from fabmetheus_utilities import euclidean
9
10
11 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
12 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
13 __date__ = '$Date: 2008/02/05 $'
14 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
15
16
17 def _getAccessibleAttribute(attributeName, stringObject):
18         'Get the accessible attribute.'
19         if attributeName in globalNativeFunctionSet:
20                 return getattr(stringObject, attributeName, None)
21         if attributeName in globalGetAccessibleAttributeSet:
22                 stringAttribute = StringAttribute(stringObject)
23                 return getattr(stringAttribute, attributeName, None)
24         return None
25
26
27 class StringAttribute(object):
28         'Class to handle a string.'
29         def __init__(self, stringObject):
30                 'Initialize.'
31                 self.stringObject = stringObject
32
33         def __repr__(self):
34                 "Get the string representation of this StringAttribute."
35                 return self.stringObject
36
37         def add(self, nextString):
38                 'Get the add string, same as append.'
39                 return self.stringObject + nextString
40
41         def append(self, nextString):
42                 'Get the append string.'
43                 return self.stringObject + nextString
44
45         def copy(self):
46                 'Get the copy.'
47                 return self.stringObject[:]
48
49         def delete(self, arguments):
50                 'Get the delete string.'
51                 deleteString = ''
52                 enumeratorSet = set(euclidean.getEnumeratorKeysAlwaysList(self.stringObject, arguments))
53                 for characterIndex, character in enumerate(self.stringObject):
54                         if characterIndex not in enumeratorSet:
55                                 deleteString += character
56                 return deleteString
57
58         def get(self, itemIndex):
59                 'Get value by characterIndex'
60                 return self.stringObject[itemIndex]
61
62         def getExpansion(self, items):
63                 'Get the concatenated copies.'
64                 expansion = ''
65                 for itemIndex in xrange(items):
66                         expansion += self.stringObject
67                 return expansion
68
69         def getIsIn(self, value):
70                 'Determine if the value is in.'
71                 return value in self.stringObject
72
73         def getIsNotIn(self, value):
74                 'Determine if the value is in.'
75                 return not(value in self.stringObject)
76
77         def getLength(self):
78                 'Get the length.'
79                 return len(self.stringObject)
80
81         def getMax(self):
82                 'Get the max.'
83                 return max(self.stringObject)
84
85         def getMin(self):
86                 'Get the min.'
87                 return min(self.stringObject)
88
89         def insert(self, insertIndex, value):
90                 'Get the insert string.'
91                 if insertIndex < 0:
92                         insertIndex += len(self.stringObject)
93                 insertIndex = max(0, insertIndex)
94                 return self.stringObject[: insertIndex] + value + self.stringObject[insertIndex :]
95
96         def keys(self):
97                 'Get the keys.'
98                 return range(len(self.stringObject))
99
100         def length(self):
101                 'Get the length.'
102                 return len(self.stringObject)
103
104         def remove(self, value):
105                 'Get the remove string.'
106                 removeIndex = self.stringObject.find(value)
107                 if removeIndex > -1:
108                         return self.stringObject[: removeIndex] + self.stringObject[removeIndex + len(value) :]
109                 return self.stringObject
110
111         def reverse(self):
112                 'Get the reverse string.'
113                 return self.stringObject[: : -1]
114
115         def set(self, itemIndex, value):
116                 'Set value.'
117                 self.stringObject[itemIndex] = value
118                 return self.stringObject
119
120         def values(self):
121                 'Get the values.'
122                 values = []
123                 for character in self.stringObject:
124                         values.append(character)
125                 return values
126
127
128 globalAccessibleAttributeDictionary = 'add append copy delete get getExpansion getIsIn getIsNotIn getLength getMax getMin'.split()
129 globalAccessibleAttributeDictionary += 'insert keys length remove reverse set values'.split()
130 globalGetAccessibleAttributeSet = set(globalAccessibleAttributeDictionary)
131 globalNativeFunctions = 'capitalize center count decode encode endswith expandtabs find format index isalnum join'.split()
132 globalNativeFunctions += 'isalpha isdigit islower isspace istitle isupper ljust lower lstrip partition replace rfind rindex'.split()
133 globalNativeFunctions += 'rjust rpartition rsplit rstrip split splitlines startswith strip swapcase title translate upper zfill'.split()
134 globalNativeFunctionSet = set(globalNativeFunctions)