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(deprecated_items):
62 if len(deprecated_items) > 0:
63 print (' Warning: The following fields have been deprecated for '
65 ', '.join([str(item) for item in deprecated_items]))
66 print (' Please follow: https://www.crosswalk-project.org/#documentation/'
70 class ManifestJsonParser(object):
71 """ The class is used to parse json-format manifest file, recompose the
72 fields and provide the field interfaces required by the packaging tool.
75 input_path: the full path of the json-format manifest file.
77 def __init__(self, input_path):
78 self.input_path = input_path
79 input_file = open(self.input_path)
81 input_src = input_file.read()
82 self.data_src = json.JSONDecoder().decode(input_src)
83 self.ret_dict = self._output_items()
84 except (TypeError, ValueError, IOError) as error:
85 print('There is a parser error in manifest.json file: %s' % error)
87 except KeyError as error:
88 print('There is a field error in manifest.json file: %s' % error)
93 def _output_items(self):
94 """ The manifest field items are reorganized and returned as a
95 dictionary to support single or multiple values of keys.
98 A dictionary to the corresponding items. the dictionary keys are
99 described as follows, the value is set to "" if the value of the
101 app_name: The application name.
102 version: The version number.
103 icons: An array of icons.
104 app_url: The url of application, e.g. hosted app.
105 description: The description of application.
106 app_root: The root path of the web, this flag allows to package
107 local web application as apk.
108 app_local_path: The relative path of entry file based on app_root,
109 this flag should work with "--app-root" together.
110 permissions: The permission list.
111 orientation The default allowed orientations.
112 fullscreen: The fullscreen flag of the application.
113 launch_screen: The launch screen configuration.
115 print ("Checking manifest file")
117 deprecated_items = []
118 if 'name' not in self.data_src:
119 print('Error: no \'name\' field in manifest.json file.')
121 ret_dict['app_name'] = self.data_src['name']
122 ret_dict['version'] = ''
123 if 'version' in self.data_src and 'xwalk_version' in self.data_src:
124 print('WARNING: the value in "version" will be ignored and support '
125 'for it will be removed in the future.')
126 ret_dict['version'] = self.data_src['xwalk_version']
127 elif 'xwalk_version' in self.data_src:
128 ret_dict['version'] = self.data_src['xwalk_version']
129 elif 'version' in self.data_src:
130 deprecated_items.append('version')
131 ret_dict['version'] = self.data_src['version']
132 if 'start_url' in self.data_src:
133 app_url = self.data_src['start_url']
134 elif 'launch_path' in self.data_src:
135 deprecated_items.append('launch_path')
136 app_url = self.data_src['launch_path']
137 elif ('app' in self.data_src and
138 'launch' in self.data_src['app'] and
139 'local_path' in self.data_src['app']['launch']):
140 deprecated_items.append('app.launch.local_path')
141 app_url = self.data_src['app']['launch']['local_path']
144 if app_url.lower().startswith(('http://', 'https://')):
147 app_local_path = app_url
149 file_path_prefix = os.path.split(self.input_path)[0]
150 if 'icons' in self.data_src:
151 icons = self.data_src['icons']
152 if type(icons) == dict:
153 deprecated_items.append('icons defined as index:value')
154 ret_dict['icons'] = icons
155 elif type(icons) == list:
158 if 'sizes' in icon and 'src' in icon:
159 icons_dict[icon['sizes'].split('x')[0]] = icon['src']
160 ret_dict['icons'] = icons_dict
162 ret_dict['icons'] = {}
164 ret_dict['icons'] = {}
165 app_root = file_path_prefix
166 ret_dict['description'] = ''
167 if 'description' in self.data_src and 'xwalk_description' in self.data_src:
168 print('WARNING: the value in "description" will be ignored and support '
169 'for it will be removed in the future.')
170 ret_dict['description'] = self.data_src['xwalk_description']
171 elif 'xwalk_description' in self.data_src:
172 ret_dict['description'] = self.data_src['xwalk_description']
173 elif 'description' in self.data_src:
174 deprecated_items.append('description')
175 ret_dict['description'] = self.data_src['description']
176 ret_dict['app_url'] = app_url
177 ret_dict['app_root'] = app_root
178 ret_dict['app_local_path'] = app_local_path
179 ret_dict['permissions'] = ''
180 if 'xwalk_permissions' in self.data_src:
182 permission_list = self.data_src['xwalk_permissions']
183 ret_dict['permissions'] = HandlePermissionList(permission_list)
184 except (TypeError, ValueError, IOError):
185 print('\'Permissions\' field error in manifest.json file.')
187 elif 'permissions' in self.data_src:
188 deprecated_items.append('permissions')
190 permission_list = self.data_src['permissions']
191 ret_dict['permissions'] = HandlePermissionList(permission_list)
192 except (TypeError, ValueError, IOError):
193 print('\'Permissions\' field error in manifest.json file.')
195 orientation = {'landscape':'landscape',
196 'landscape-primary':'landscape',
197 'landscape-secondary':'reverseLandscape',
198 'portrait':'portrait',
199 'portrait-primary':'portrait',
200 'portrait-secondary':'reversePortrait',
202 'natural':'unspecified'}
203 if 'orientation' in self.data_src:
204 if self.data_src['orientation'] in orientation:
205 ret_dict['orientation'] = orientation[self.data_src['orientation']]
207 ret_dict['orientation'] = 'unspecified'
209 ret_dict['orientation'] = 'unspecified'
210 if 'display' in self.data_src and 'fullscreen' in self.data_src['display']:
211 ret_dict['fullscreen'] = 'true'
213 ret_dict['fullscreen'] = ''
214 if 'xwalk_launch_screen' in self.data_src:
215 launch_screen_dict = self.data_src['xwalk_launch_screen']
216 ParseLaunchScreen(ret_dict, launch_screen_dict, 'default')
217 ParseLaunchScreen(ret_dict, launch_screen_dict, 'portrait')
218 ParseLaunchScreen(ret_dict, launch_screen_dict, 'landscape')
219 elif 'launch_screen' in self.data_src:
220 deprecated_items.append('launch_screen')
221 launch_screen_dict = self.data_src['launch_screen']
222 ParseLaunchScreen(ret_dict, launch_screen_dict, 'default')
223 ParseLaunchScreen(ret_dict, launch_screen_dict, 'portrait')
224 ParseLaunchScreen(ret_dict, launch_screen_dict, 'landscape')
226 PrintDeprecationWarning(deprecated_items)
230 """Show the processed results, it is used for command-line
231 internal debugging."""
232 print("app_name: %s" % self.GetAppName())
233 print("version: %s" % self.GetVersion())
234 print("description: %s" % self.GetDescription())
235 print("icons: %s" % self.GetIcons())
236 print("app_url: %s" % self.GetAppUrl())
237 print("app_root: %s" % self.GetAppRoot())
238 print("app_local_path: %s" % self.GetAppLocalPath())
239 print("permissions: %s" % self.GetPermissions())
240 print("orientation: %s" % self.GetOrientation())
241 print("fullscreen: %s" % self.GetFullScreenFlag())
242 print('launch_screen.default.background_color: %s' %
243 self.GetLaunchScreenBackgroundColor('default'))
244 print('launch_screen.default.background_image: %s' %
245 self.GetLaunchScreenBackgroundImage('default'))
246 print('launch_screen.default.image: %s' %
247 self.GetLaunchScreenImage('default'))
248 print('launch_screen.default.image_border: %s' %
249 self.GetLaunchScreenImageBorder('default'))
250 print('launch_screen.portrait.background_color: %s' %
251 self.GetLaunchScreenBackgroundColor('portrait'))
252 print('launch_screen.portrait.background_image: %s' %
253 self.GetLaunchScreenBackgroundImage('portrait'))
254 print('launch_screen.portrait.image: %s' %
255 self.GetLaunchScreenImage('portrait'))
256 print('launch_screen.portrait.image_border: %s' %
257 self.GetLaunchScreenImageBorder('portrait'))
258 print('launch_screen.landscape.background_color: %s' %
259 self.GetLaunchScreenBackgroundColor('landscape'))
260 print('launch_screen.landscape.background_image: %s' %
261 self.GetLaunchScreenBackgroundImage('landscape'))
262 print('launch_screen.landscape.image: %s' %
263 self.GetLaunchScreenImage('landscape'))
264 print('launch_screen.landscape.image_border: %s' %
265 self.GetLaunchScreenImageBorder('landscape'))
267 def GetAppName(self):
268 """Return the application name."""
269 return self.ret_dict['app_name']
271 def GetVersion(self):
272 """Return the version number."""
273 return self.ret_dict['version']
276 """Return the icons."""
277 return self.ret_dict['icons']
280 """Return the URL of the application."""
281 return self.ret_dict['app_url']
283 def GetDescription(self):
284 """Return the description of the application."""
285 return self.ret_dict['description']
287 def GetAppRoot(self):
288 """Return the root path of the local web application."""
289 return self.ret_dict['app_root']
291 def GetAppLocalPath(self):
292 """Return the local relative path of the local web application."""
293 return self.ret_dict['app_local_path']
295 def GetPermissions(self):
296 """Return the permissions."""
297 return self.ret_dict['permissions']
299 def GetOrientation(self):
300 """Return the default allowed orientations"""
301 return self.ret_dict['orientation']
303 def GetFullScreenFlag(self):
304 """Return the set fullscreen flag of the application."""
305 return self.ret_dict['fullscreen']
307 def GetLaunchScreenBackgroundColor(self, orientation):
308 """Return the background color for launch_screen."""
309 key = 'launch_screen_background_color_' + orientation
310 return self.ret_dict.get(key, '')
312 def GetLaunchScreenBackgroundImage(self, orientation):
313 """Return the background image for launch_screen."""
314 key = 'launch_screen_background_image_' + orientation
315 return self.ret_dict.get(key, '')
317 def GetLaunchScreenImage(self, orientation):
318 """Return the image for launch_screen."""
319 key = 'launch_screen_image_' + orientation
320 return self.ret_dict.get(key, '')
322 def GetLaunchScreenImageBorder(self, orientation):
323 """Return the image border for launch_screen."""
324 key = 'launch_screen_image_border_' + orientation
325 return self.ret_dict.get(key, '')
329 """Respond to command mode and show the processed field values."""
330 parser = optparse.OptionParser()
331 info = ('The input json-format file name. Such as: '
332 '--jsonfile=manifest.json')
333 parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
335 opts, _ = parser.parse_args()
339 json_parser = ManifestJsonParser(opts.jsonfile)
340 json_parser.ShowItems()
344 if __name__ == '__main__':
345 sys.exit(main(sys.argv))