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