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
23 def HandlePermissionList(permission_list):
24 """This function is used to handle the permission list and return the string
28 permission_list: the permission list, e.g.["permission1", "permission2"].
31 The string of permissions with ':' as separator.
32 e.g. "permission1:permission2".
34 permissions = list(permission_list)
35 reg_permission = re.compile(r'^[a-zA-Z\.]*$')
36 for permission in permissions:
37 if not reg_permission.match(permission):
38 print('\'Permissions\' field error, only alphabets and '
41 return ':'.join(permissions)
44 def ParseLaunchScreen(ret_dict, launch_screen_dict, orientation):
45 if orientation in launch_screen_dict:
46 sub_dict = launch_screen_dict[orientation]
47 if 'background_color' in sub_dict:
48 ret_dict['launch_screen_background_color_' + orientation] = (
49 sub_dict['background_color'])
50 if 'background_image' in sub_dict:
51 ret_dict['launch_screen_background_image_' + orientation] = (
52 sub_dict['background_image'])
53 if 'image' in sub_dict:
54 ret_dict['launch_screen_image_' + orientation] = (
56 if 'image_border' in sub_dict:
57 ret_dict['launch_screen_image_border_' + orientation] = (
58 sub_dict['image_border'])
61 def PrintDeprecationWarning(item):
62 print ('WARNING: %s is deprecated for Crosswalk. Please follow '
63 'https://www.crosswalk-project.org/#documentation/manifest.' % item)
66 class ManifestJsonParser(object):
67 """ The class is used to parse json-format manifest file, recompose the
68 fields and provide the field interfaces required by the packaging tool.
71 input_path: the full path of the json-format manifest file.
73 def __init__(self, input_path):
74 self.input_path = input_path
75 input_file = open(self.input_path)
77 input_src = input_file.read()
78 self.data_src = json.JSONDecoder().decode(input_src)
79 self.ret_dict = self._output_items()
80 except (TypeError, ValueError, IOError) as error:
81 print('There is a parser error in manifest.json file: %s' % error)
83 except KeyError as error:
84 print('There is a field error in manifest.json file: %s' % error)
89 def _output_items(self):
90 """ The manifest field items are reorganized and returned as a
91 dictionary to support single or multiple values of keys.
94 A dictionary to the corresponding items. the dictionary keys are
95 described as follows, the value is set to "" if the value of the
97 app_name: The application name.
98 version: The version number.
99 icons: An array of icons.
100 app_url: The url of application, e.g. hosted app.
101 description: The description of application.
102 app_root: The root path of the web, this flag allows to package
103 local web application as apk.
104 app_local_path: The relative path of entry file based on app_root,
105 this flag should work with "--app-root" together.
106 permissions: The permission list.
107 required_version: The required crosswalk runtime version.
108 plugin: The plug-in information.
109 orientation The default allowed orientations.
110 fullscreen: The fullscreen flag of the application.
111 launch_screen: The launch screen configuration.
114 if 'name' not in self.data_src:
115 print('Error: no \'name\' field in manifest.json file.')
117 ret_dict['app_name'] = self.data_src['name']
118 if 'version' not in self.data_src:
119 print('Error: no \'version\' field in manifest.json file.')
121 ret_dict['version'] = self.data_src['version']
122 if 'start_url' in self.data_src:
123 app_url = self.data_src['start_url']
124 elif 'launch_path' in self.data_src:
125 PrintDeprecationWarning('launch_path')
126 app_url = self.data_src['launch_path']
127 elif ('app' in self.data_src and
128 'launch' in self.data_src['app'] and
129 'local_path' in self.data_src['app']['launch']):
130 PrintDeprecationWarning('app.launch.local_path')
131 app_url = self.data_src['app']['launch']['local_path']
134 if app_url.lower().startswith(('http://', 'https://')):
137 app_local_path = app_url
139 file_path_prefix = os.path.split(self.input_path)[0]
140 if 'icons' in self.data_src:
141 icons = self.data_src['icons']
142 if type(icons) == dict:
143 PrintDeprecationWarning('icons defined as dictionary form')
144 ret_dict['icons'] = icons
145 elif type(icons) == list:
148 if 'sizes' in icon and 'src' in icon:
149 icons_dict[icon['sizes'].split('x')[0]] = icon['src']
150 ret_dict['icons'] = icons_dict
152 ret_dict['icons'] = {}
154 ret_dict['icons'] = {}
155 app_root = file_path_prefix
156 ret_dict['description'] = ''
157 if 'description' in self.data_src:
158 ret_dict['description'] = self.data_src['description']
159 ret_dict['app_url'] = app_url
160 ret_dict['app_root'] = app_root
161 ret_dict['app_local_path'] = app_local_path
162 ret_dict['permissions'] = ''
163 if 'xwalk_permissions' in self.data_src:
165 permission_list = self.data_src['xwalk_permissions']
166 ret_dict['permissions'] = HandlePermissionList(permission_list)
167 except (TypeError, ValueError, IOError):
168 print('\'Permissions\' field error in manifest.json file.')
170 elif 'permissions' in self.data_src:
171 PrintDeprecationWarning('permissions')
173 permission_list = self.data_src['permissions']
174 ret_dict['permissions'] = HandlePermissionList(permission_list)
175 except (TypeError, ValueError, IOError):
176 print('\'Permissions\' field error in manifest.json file.')
178 ret_dict['required_version'] = ''
179 if 'required_version' in self.data_src:
180 ret_dict['required_version'] = self.data_src['required_version']
181 ret_dict['plugin'] = ''
182 if 'plugin' in self.data_src:
183 ret_dict['plugin'] = self.data_src['plugin']
184 orientation = {'landscape':'landscape',
185 'landscape-primary':'landscape',
186 'landscape-secondary':'reverseLandscape',
187 'portrait':'portrait',
188 'portrait-primary':'portrait',
189 'portrait-secondary':'reversePortrait',
191 'natural':'unspecified'}
192 if 'orientation' in self.data_src:
193 if self.data_src['orientation'] in orientation:
194 ret_dict['orientation'] = orientation[self.data_src['orientation']]
196 ret_dict['orientation'] = 'unspecified'
198 ret_dict['orientation'] = 'unspecified'
199 if 'display' in self.data_src and 'fullscreen' in self.data_src['display']:
200 ret_dict['fullscreen'] = 'true'
202 ret_dict['fullscreen'] = ''
203 if 'xwalk_launch_screen' in self.data_src:
204 launch_screen_dict = self.data_src['xwalk_launch_screen']
205 ParseLaunchScreen(ret_dict, launch_screen_dict, 'default')
206 ParseLaunchScreen(ret_dict, launch_screen_dict, 'portrait')
207 ParseLaunchScreen(ret_dict, launch_screen_dict, 'landscape')
208 elif 'launch_screen' in self.data_src:
209 PrintDeprecationWarning('launch_screen')
210 launch_screen_dict = self.data_src['launch_screen']
211 ParseLaunchScreen(ret_dict, launch_screen_dict, 'default')
212 ParseLaunchScreen(ret_dict, launch_screen_dict, 'portrait')
213 ParseLaunchScreen(ret_dict, launch_screen_dict, 'landscape')
217 """Show the processed results, it is used for command-line
218 internal debugging."""
219 print("app_name: %s" % self.GetAppName())
220 print("version: %s" % self.GetVersion())
221 print("description: %s" % self.GetDescription())
222 print("icons: %s" % self.GetIcons())
223 print("app_url: %s" % self.GetAppUrl())
224 print("app_root: %s" % self.GetAppRoot())
225 print("app_local_path: %s" % self.GetAppLocalPath())
226 print("permissions: %s" % self.GetPermissions())
227 print("required_version: %s" % self.GetRequiredVersion())
228 print("plugins: %s" % self.GetPlugins())
229 print("orientation: %s" % self.GetOrientation())
230 print("fullscreen: %s" % self.GetFullScreenFlag())
231 print('launch_screen.default.background_color: %s' %
232 self.GetLaunchScreenBackgroundColor('default'))
233 print('launch_screen.default.background_image: %s' %
234 self.GetLaunchScreenBackgroundImage('default'))
235 print('launch_screen.default.image: %s' %
236 self.GetLaunchScreenImage('default'))
237 print('launch_screen.default.image_border: %s' %
238 self.GetLaunchScreenImageBorder('default'))
239 print('launch_screen.portrait.background_color: %s' %
240 self.GetLaunchScreenBackgroundColor('portrait'))
241 print('launch_screen.portrait.background_image: %s' %
242 self.GetLaunchScreenBackgroundImage('portrait'))
243 print('launch_screen.portrait.image: %s' %
244 self.GetLaunchScreenImage('portrait'))
245 print('launch_screen.portrait.image_border: %s' %
246 self.GetLaunchScreenImageBorder('portrait'))
247 print('launch_screen.landscape.background_color: %s' %
248 self.GetLaunchScreenBackgroundColor('landscape'))
249 print('launch_screen.landscape.background_image: %s' %
250 self.GetLaunchScreenBackgroundImage('landscape'))
251 print('launch_screen.landscape.image: %s' %
252 self.GetLaunchScreenImage('landscape'))
253 print('launch_screen.landscape.image_border: %s' %
254 self.GetLaunchScreenImageBorder('landscape'))
256 def GetAppName(self):
257 """Return the application name."""
258 return self.ret_dict['app_name']
260 def GetVersion(self):
261 """Return the version number."""
262 return self.ret_dict['version']
265 """Return the icons."""
266 return self.ret_dict['icons']
269 """Return the URL of the application."""
270 return self.ret_dict['app_url']
272 def GetDescription(self):
273 """Return the description of the application."""
274 return self.ret_dict['description']
276 def GetAppRoot(self):
277 """Return the root path of the local web application."""
278 return self.ret_dict['app_root']
280 def GetAppLocalPath(self):
281 """Return the local relative path of the local web application."""
282 return self.ret_dict['app_local_path']
284 def GetPermissions(self):
285 """Return the permissions."""
286 return self.ret_dict['permissions']
288 def GetRequiredVersion(self):
289 """Return the required crosswalk runtime version."""
290 return self.ret_dict['required_version']
292 def GetPlugins(self):
293 """Return the plug-in path and file name."""
294 return self.ret_dict['plugin']
296 def GetOrientation(self):
297 """Return the default allowed orientations"""
298 return self.ret_dict['orientation']
300 def GetFullScreenFlag(self):
301 """Return the set fullscreen flag of the application."""
302 return self.ret_dict['fullscreen']
304 def GetLaunchScreenBackgroundColor(self, orientation):
305 """Return the background color for launch_screen."""
306 key = 'launch_screen_background_color_' + orientation
307 return self.ret_dict.get(key, '')
309 def GetLaunchScreenBackgroundImage(self, orientation):
310 """Return the background image for launch_screen."""
311 key = 'launch_screen_background_image_' + orientation
312 return self.ret_dict.get(key, '')
314 def GetLaunchScreenImage(self, orientation):
315 """Return the image for launch_screen."""
316 key = 'launch_screen_image_' + orientation
317 return self.ret_dict.get(key, '')
319 def GetLaunchScreenImageBorder(self, orientation):
320 """Return the image border for launch_screen."""
321 key = 'launch_screen_image_border_' + orientation
322 return self.ret_dict.get(key, '')
326 """Respond to command mode and show the processed field values."""
327 parser = optparse.OptionParser()
328 info = ('The input json-format file name. Such as: '
329 '--jsonfile=manifest.json')
330 parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
332 opts, _ = parser.parse_args()
336 json_parser = ManifestJsonParser(opts.jsonfile)
337 json_parser.ShowItems()
341 if __name__ == '__main__':
342 sys.exit(main(sys.argv))