forked from afrocheese/MetroAppAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppxMetadata.py
164 lines (139 loc) · 5.22 KB
/
AppxMetadata.py
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
__author__ = 'charles'
import xml.sax
class AppxMetadataSaxHandler ( xml.sax.ContentHandler ):
CATEGORY_IN_PROCESS = 'windows.activatableClass.inProcessServer'
CATEGORY_PROXY_STUB = 'windows.activatableClass.proxyStub'
def __init__(self, show_all=False):
self.show_all = show_all
self.CurrentData = ''
self.CurrentCategory = ''
self.name = ''
self.capabilities = []
self.deviceCapabilities = []
self.type = ''
self.architecture = ''
self.id = ''
self.executable = None
self.protocols = []
self.inProcessServers = []
self.proxyStubs = []
self.fileTypes = []
self.framework = None
self.tileUpdateURI = None
self.tileUpdateFrequency = None
def handleApplication(self, attributes):
self.id = attributes.get('Id')
self.executable = attributes.get('Executable')
if self.executable is None:
self.type = 'WinJS'
else:
self.type = 'Native'
def handleCapability(self, attributes):
self.capabilities.append(attributes.get('Name'))
def handleDeviceCapability(self, attributes):
self.deviceCapabilities.append(attributes.get('Name'))
def handleIdentity(self, attributes):
self.architecture = attributes.get('ProcessorArchitecture')
self.name = attributes.get('Name')
def handleProtocol(self, attributes):
self.protocols.append(attributes.get('Name'))
def handleExtension(self, attributes):
self.CurrentCategory = attributes.get('Category')
def handleTileUpdate(self, attributes):
self.tileUpdateURI = attributes.get('UriTemplate')
self.tileUpdateFrequency = attributes.get('Recurrence')
def characters(self, content):
if len(content.strip()):
if self.CurrentData == 'Path' and self.CurrentCategory == self.CATEGORY_IN_PROCESS:
self.inProcessServers.append(content)
if self.CurrentData == 'Path' and self.CurrentCategory == self.CATEGORY_PROXY_STUB:
self.proxyStubs.append(content)
if self.CurrentData == 'FileType':
self.fileTypes.append(content)
if self.CurrentData == 'Framework':
self.framework = content == 'true'
# Call when an element starts
def startElement(self, tag, attributes):
self.CurrentData = tag
if tag == 'Application':
self.handleApplication(attributes)
elif tag == 'Capability':
self.handleCapability(attributes)
elif tag == 'DeviceCapability':
self.handleDeviceCapability(attributes)
elif tag == 'Identity':
self.handleIdentity(attributes)
elif tag == 'Protocol':
self.handleProtocol(attributes)
elif tag == 'Extension':
self.handleExtension(attributes)
elif tag == 'wb:TileUpdate':
self.handleTileUpdate(attributes)
# elif tag == 'Framework':
# self.handleFramework(attributes)
# Call when an elements ends
def endElement(self, tag):
pass
def show_prop(self, prop, label, joiner=u', ', show_all=False):
value = self.__dict__[prop]
if show_all is False and value is None:
return ''
if isinstance(value, list):
if show_all is False and len(value) == 0:
return ''
value = joiner.join(value)
return '%s: %s' % (label, value)
PROPS = [
('name', 'App'),
'Type',
'Executable',
'Architecture',
'Capabilities',
('deviceCapabilities', 'Device Capabilities'),
'Protocols',
('inProcessServers', 'In Process Servers'),
('proxyStubs', 'Proxy Stubs'),
('fileTypes', 'File Types')
]
def __str__(self):
if self.framework is True:
st = 'Framework: ' + self.name
else:
st = ''
for name in self.PROPS:
if isinstance(name, tuple):
prop = name[0]
name = name[1]
else:
prop = name.lower()
p = self.show_prop(prop, name)
if len(p):
if len(st):
st += '\n\t'
st += p
return st
class AppxMetadataParser:
def __init__(self):
# create an XMLReader
self.parser = xml.sax.make_parser()
# turn off namespaces
self.parser.setFeature(xml.sax.handler.feature_namespaces, 0)
self.urls = []
def parse(self, source, show_all=False):
# override the default ContextHandler
self.Handler = AppxMetadataSaxHandler(show_all=show_all)
self.parser.setContentHandler( self.Handler )
self.parser.parse(source)
return self
def __str__(self):
return str(self.Handler)
if ( __name__ == "__main__"):
# create an XMLReader
parser = xml.sax.make_parser()
# turn off namespaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# override the default ContextHandler
Handler = AppxMetadataSaxHandler()
parser.setContentHandler( Handler )
parser.parse('test/Maps.xml')
print(Handler)