3 # Copyright (c) 2013, 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
8 Parse JSON-format manifest configuration file and
9 provide the specific fields, which have to be integrated with
10 packaging tool(e.g. make_apk.py) to generate xml-format manifest file.
12 Sample usage from shell script:
13 python manifest_json_parser.py --jsonfile=/path/to/manifest.json
22 def HandlePermissionList(permission_list):
23 """This function is used to handle the permission list and return the string
27 permission_list: the permission list, e.g.["permission1", "permission2"].
30 The string of permissions with ':' as separator.
31 e.g. "permission1:permission2".
33 permissions = list(permission_list)
34 reg_permission = re.compile(r'^[a-zA-Z\.]*$')
35 for permission in permissions:
36 if not reg_permission.match(permission):
37 print '\'Permissions\' field error, only alphabets and \'.\' are allowed.'
39 return ':'.join(permissions)
42 class ManifestJsonParser(object):
43 """ The class is used to parse json-format manifest file, recompose the fields
44 and provide the field interfaces required by the packaging tool.
47 input_path: the full path of the json-format manifest file.
49 def __init__(self, input_path):
50 self.input_path = input_path
51 input_file = file(self.input_path)
53 input_src = input_file.read()
54 self.data_src = json.JSONDecoder().decode(input_src)
55 self.ret_dict = self._output_items()
56 except (TypeError, ValueError, IOError):
57 print 'There is a parser error in manifest.json file.'
60 print 'There is a field error in manifest.json file.'
65 def _output_items(self):
66 """ The manifest field items are reorganized and returned as a
67 dictionary to support single or multiple values of keys.
70 A dictionary to the corresponding items. the dictionary keys are
71 described as follows, the value is set to "" if the value of the
73 app_name: The application name.
74 version: The version number.
75 icons: An array of icons.
76 app_url: The url of application, e.g. hosted app.
77 description: The description of application.
78 app_root: The root path of the web, this flag allows to package
79 local web application as apk.
80 app_local_path: The relative path of entry file based on app_root,
81 this flag should work with "--app-root" together.
82 permissions: The permission list.
83 required_version: The required crosswalk runtime version.
84 plugin: The plug-in information.
85 fullscreen: The fullscreen flag of the application.
88 if not self.data_src.has_key('name'):
89 print 'Error: no \'name\' field in manifest.json file.'
91 ret_dict['app_name'] = self.data_src['name']
92 if not self.data_src.has_key('version'):
93 print 'Error: no \'version\' field in manifest.json file.'
95 ret_dict['version'] = self.data_src['version']
96 if self.data_src.has_key('launch_path'):
97 app_url = self.data_src['launch_path']
98 elif (self.data_src.has_key('app') and
99 self.data_src['app'].has_key('launch') and
100 self.data_src['app']['launch'].has_key('local_path')):
101 app_url = self.data_src['app']['launch']['local_path']
104 if app_url.lower().startswith(('http', 'https')):
107 app_local_path = app_url
109 file_path_prefix = os.path.split(self.input_path)[0]
110 if self.data_src.has_key('icons'):
111 ret_dict['icons'] = self.data_src['icons']
113 ret_dict['icons'] = ''
114 app_root = file_path_prefix
115 ret_dict['description'] = ''
116 if self.data_src.has_key('description'):
117 ret_dict['description'] = self.data_src['description']
118 ret_dict['app_url'] = app_url
119 ret_dict['app_root'] = app_root
120 ret_dict['app_local_path'] = app_local_path
121 ret_dict['permissions'] = ''
122 if self.data_src.has_key('permissions'):
124 permission_list = self.data_src['permissions']
125 ret_dict['permissions'] = HandlePermissionList(permission_list)
126 except (TypeError, ValueError, IOError):
127 print '\'Permissions\' field error in manifest.json file.'
129 ret_dict['required_version'] = ''
130 if self.data_src.has_key('required_version'):
131 ret_dict['required_version'] = self.data_src['required_version']
132 ret_dict['plugin'] = ''
133 if self.data_src.has_key('plugin'):
134 ret_dict['plugin'] = self.data_src['plugin']
135 if self.data_src.has_key('fullscreen'):
136 ret_dict['fullscreen'] = self.data_src['fullscreen']
138 ret_dict['fullscreen'] = 'False'
142 """Show the processed results, it is used for command-line
143 internal debugging."""
144 print "app_name: %s" % self.GetAppName()
145 print "version: %s" % self.GetVersion()
146 print "description: %s" % self.GetDescription()
147 print "icons: %s" % self.GetIcons()
148 print "app_url: %s" % self.GetAppUrl()
149 print "app_root: %s" % self.GetAppRoot()
150 print "app_local_path: %s" % self.GetAppLocalPath()
151 print "permissions: %s" % self.GetPermissions()
152 print "required_version: %s" % self.GetRequiredVersion()
153 print "plugins: %s" % self.GetPlugins()
154 print "fullscreen: %s" % self.GetFullScreenFlag()
156 def GetAppName(self):
157 """Return the application name."""
158 return self.ret_dict['app_name']
160 def GetVersion(self):
161 """Return the version number."""
162 return self.ret_dict['version']
165 """Return the icons."""
166 return self.ret_dict['icons']
169 """Return the URL of the application."""
170 return self.ret_dict['app_url']
172 def GetDescription(self):
173 """Return the description of the application."""
174 return self.ret_dict['description']
176 def GetAppRoot(self):
177 """Return the root path of the local web application."""
178 return self.ret_dict['app_root']
180 def GetAppLocalPath(self):
181 """Return the local relative path of the local web application."""
182 return self.ret_dict['app_local_path']
184 def GetPermissions(self):
185 """Return the permissions."""
186 return self.ret_dict['permissions']
188 def GetRequiredVersion(self):
189 """Return the required crosswalk runtime version."""
190 return self.ret_dict['required_version']
192 def GetPlugins(self):
193 """Return the plug-in path and file name."""
194 return self.ret_dict['plugin']
196 def GetFullScreenFlag(self):
197 """Return the set fullscreen flag of the application."""
198 return self.ret_dict['fullscreen']
202 """Respond to command mode and show the processed field values."""
203 parser = optparse.OptionParser()
204 info = ('The input json-format file name. Such as: '
205 '--jsonfile=manifest.json')
206 parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
208 opts, _ = parser.parse_args()
212 json_parser = ManifestJsonParser(opts.jsonfile)
213 json_parser.ShowItems()
217 if __name__ == '__main__':
218 sys.exit(main(sys.argv))