-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPBXGenericObject.py
More file actions
182 lines (140 loc) · 5.8 KB
/
Copy pathPBXGenericObject.py
File metadata and controls
182 lines (140 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import re
import uuid
from pbxproj.PBXKey import PBXKey
class PBXGenericObject(object):
"""
Generic class that creates internal attributes to match the structure of the tree used to create the element.
Also, prints itself using the openstep format. Extensions might be required to insert comments on right places.
"""
_VALID_KEY_REGEX = '[a-zA-Z0-9\\._/]*'
def __init__(self, parent=None):
self._parent = parent
def get_parent(self):
return self._parent
def parse(self, value):
if isinstance(value, dict):
return self._parse_dict(value)
if isinstance(value, basestring):
return self._parse_string(value)
if isinstance(value, list):
return self._parse_list(value)
return value
def _parse_dict(self, obj):
# all top level objects are added as variables to this object
for key, value in obj.iteritems():
if value is None:
continue
key = self._parse_string(key)
setattr(self, key, self._get_instance(key, value))
return self
def _parse_list(self, obj):
ret = []
for item in obj:
ret.append(self.parse(item))
return ret
def _parse_string(self, obj):
if re.match('([0-9A-F]{24})', obj) is not None:
return PBXKey(obj, self)
return obj
def _get_instance(self, class_type, content):
# check if the key maps to a kind of object
return self._get_class_reference(class_type)(self).parse(content)
@classmethod
def _get_class_reference(cls, class_type):
module = __import__(u'pbxproj')
if hasattr(module, class_type):
class_ = getattr(module, class_type)
return class_
return PBXGenericObject
def __repr__(self):
return self._print_object()
def _print_object(self, indentation_depth=u'', entry_separator=u'\n', object_start=u'\n',
indentation_increment=u'\t'):
ret = u'{' + object_start
for key in self.get_keys():
value = self._format(getattr(self, key), indentation_depth, entry_separator, object_start,
indentation_increment)
# use key decorators, could simplify the generation of the comments.
ret += indentation_depth + u'{3}{0} = {1};{2}'.format(PBXGenericObject._escape(key), value, entry_separator,
indentation_increment)
ret += indentation_depth + u'}'
return ret
def _print_list(self, obj, indentation_depth=u'', entry_separator=u'\n', object_start=u'\n',
indentation_increment=u'\t'):
ret = u'(' + object_start
for item in obj:
value = self._format(item, indentation_depth, entry_separator, object_start, indentation_increment)
ret += indentation_depth + u'{1}{0},{2}'.format(value, indentation_increment, entry_separator)
ret += indentation_depth + u')'
return ret
def _format(self, value, indentation_depth=u'', entry_separator=u'\n', object_start=u'\n',
indentation_increment=u'\t'):
if hasattr(value, u'_print_object'):
value = value._print_object(indentation_depth + indentation_increment,
entry_separator,
object_start,
indentation_increment)
elif isinstance(value, list):
value = self._print_list(value, indentation_depth + indentation_increment,
entry_separator,
object_start,
indentation_increment)
elif isinstance(value, PBXKey):
value = value.__repr__()
else:
value = PBXGenericObject._escape(value.__str__())
return value
def get_keys(self):
fields = list([x for x in dir(self) if not x.startswith(u'_') and not hasattr(getattr(self, x), '__call__')])
if u'isa' in fields:
fields.remove(u'isa')
fields = sorted(fields)
fields.insert(0, u'isa')
else:
fields = sorted(fields)
return fields
def __getitem__(self, key):
if hasattr(self, key):
return getattr(self, key)
return None
def __setitem__(self, key, value):
if type(value) == list:
if value.__len__() == 1:
value = value[0]
if value.__len__() == 0:
delattr(self, key)
return
setattr(self, key, value)
def __delitem__(self, key):
delattr(self, key)
def __contains__(self, item):
return hasattr(self, item)
def _resolve_comment(self, key):
parent = self.get_parent()
if key in self:
return self[key]._get_comment()
if parent is None:
return None
return parent._resolve_comment(key)
def get_id(self):
return self['_id']
def _get_comment(self):
if hasattr(self, u'name'):
return self.name
if hasattr(self, u'path'):
return self.path
return None
@classmethod
def _generate_id(cls):
return ''.join(str(uuid.uuid4()).upper().split('-')[1:])
@classmethod
def _escape(cls, item):
if item.__len__() == 0 or re.match(cls._VALID_KEY_REGEX, item).group(0) != item:
escaped = item.replace(u'\\', u'\\\\')\
.replace(u'\n', u'\\n')\
.replace(u'\"', u'\\"')\
.replace(u'\0', u'\\0')\
.replace(u'\'', u'\\\'')\
.replace(u'\t', u'\\\t')
return u'"{0}"'.format(escaped)
return item