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 class ManifestJsonParser(object):
45 """ The class is used to parse json-format manifest file, recompose the
46 fields and provide the field interfaces required by the packaging tool.
49 input_path: the full path of the json-format manifest file.
51 def __init__(self, input_path):
52 self.input_path = input_path
53 input_file = open(self.input_path)
55 input_src = input_file.read()
56 self.data_src = json.JSONDecoder().decode(input_src)
57 self.ret_dict = self._output_items()
58 except (TypeError, ValueError, IOError) as error:
59 print('There is a parser error in manifest.json file: %s' % error)
61 except KeyError as error:
62 print('There is a field error in manifest.json file: %s' % error)
67 def _output_items(self):
68 """ The manifest field items are reorganized and returned as a
69 dictionary to support single or multiple values of keys.
72 A dictionary to the corresponding items. the dictionary keys are
73 described as follows, the value is set to "" if the value of the
75 app_name: The application name.
76 version: The version number.
77 icons: An array of icons.
78 app_url: The url of application, e.g. hosted app.
79 description: The description of application.
80 app_root: The root path of the web, this flag allows to package
81 local web application as apk.
82 app_local_path: The relative path of entry file based on app_root,
83 this flag should work with "--app-root" together.
84 permissions: The permission list.
85 required_version: The required crosswalk runtime version.
86 plugin: The plug-in information.
87 fullscreen: The fullscreen flag of the application.
88 launch_screen: The launch screen configuration.
91 if 'name' not in self.data_src:
92 print('Error: no \'name\' field in manifest.json file.')
94 ret_dict['app_name'] = self.data_src['name']
95 if 'version' not in self.data_src:
96 print('Error: no \'version\' field in manifest.json file.')
98 ret_dict['version'] = self.data_src['version']
99 if 'launch_path' in self.data_src:
100 app_url = self.data_src['launch_path']
101 elif ('app' in self.data_src and
102 'launch' in self.data_src['app'] and
103 'local_path' in self.data_src['app']['launch']):
104 app_url = self.data_src['app']['launch']['local_path']
107 if app_url.lower().startswith(('http://', 'https://')):
110 app_local_path = app_url
112 file_path_prefix = os.path.split(self.input_path)[0]
113 if 'icons' in self.data_src:
114 ret_dict['icons'] = self.data_src['icons']
116 ret_dict['icons'] = {}
117 app_root = file_path_prefix
118 ret_dict['description'] = ''
119 if 'description' in self.data_src:
120 ret_dict['description'] = self.data_src['description']
121 ret_dict['app_url'] = app_url
122 ret_dict['app_root'] = app_root
123 ret_dict['app_local_path'] = app_local_path
124 ret_dict['permissions'] = ''
125 if 'permissions' in self.data_src:
127 permission_list = self.data_src['permissions']
128 ret_dict['permissions'] = HandlePermissionList(permission_list)
129 except (TypeError, ValueError, IOError):
130 print('\'Permissions\' field error in manifest.json file.')
132 ret_dict['required_version'] = ''
133 if 'required_version' in self.data_src:
134 ret_dict['required_version'] = self.data_src['required_version']
135 ret_dict['plugin'] = ''
136 if 'plugin' in self.data_src:
137 ret_dict['plugin'] = self.data_src['plugin']
138 if 'display' in self.data_src and 'fullscreen' in self.data_src['display']:
139 ret_dict['fullscreen'] = 'true'
141 ret_dict['fullscreen'] = ''
142 if 'launch_screen' in self.data_src:
143 self.ParseLaunchScreen(ret_dict, 'default')
144 self.ParseLaunchScreen(ret_dict, 'portrait')
145 self.ParseLaunchScreen(ret_dict, 'landscape')
148 def ParseLaunchScreen(self, ret_dict, orientation):
149 if orientation in self.data_src['launch_screen']:
150 sub_dict = self.data_src['launch_screen'][orientation]
151 if 'background_color' in sub_dict:
152 ret_dict['launch_screen_background_color_' + orientation] = (
153 sub_dict['background_color'])
154 if 'background_image' in sub_dict:
155 ret_dict['launch_screen_background_image_' + orientation] = (
156 sub_dict['background_image'])
157 if 'image' in sub_dict:
158 ret_dict['launch_screen_image_' + orientation] = (
160 if 'image_border' in sub_dict:
161 ret_dict['launch_screen_image_border_' + orientation] = (
162 sub_dict['image_border'])
165 """Show the processed results, it is used for command-line
166 internal debugging."""
167 print("app_name: %s" % self.GetAppName())
168 print("version: %s" % self.GetVersion())
169 print("description: %s" % self.GetDescription())
170 print("icons: %s" % self.GetIcons())
171 print("app_url: %s" % self.GetAppUrl())
172 print("app_root: %s" % self.GetAppRoot())
173 print("app_local_path: %s" % self.GetAppLocalPath())
174 print("permissions: %s" % self.GetPermissions())
175 print("required_version: %s" % self.GetRequiredVersion())
176 print("plugins: %s" % self.GetPlugins())
177 print("fullscreen: %s" % self.GetFullScreenFlag())
178 print('launch_screen.default.background_color: %s' %
179 self.GetLaunchScreenBackgroundColor('default'))
180 print('launch_screen.default.background_image: %s' %
181 self.GetLaunchScreenBackgroundImage('default'))
182 print('launch_screen.default.image: %s' %
183 self.GetLaunchScreenImage('default'))
184 print('launch_screen.default.image_border: %s' %
185 self.GetLaunchScreenImageBorder('default'))
186 print('launch_screen.portrait.background_color: %s' %
187 self.GetLaunchScreenBackgroundColor('portrait'))
188 print('launch_screen.portrait.background_image: %s' %
189 self.GetLaunchScreenBackgroundImage('portrait'))
190 print('launch_screen.portrait.image: %s' %
191 self.GetLaunchScreenImage('portrait'))
192 print('launch_screen.portrait.image_border: %s' %
193 self.GetLaunchScreenImageBorder('portrait'))
194 print('launch_screen.landscape.background_color: %s' %
195 self.GetLaunchScreenBackgroundColor('landscape'))
196 print('launch_screen.landscape.background_image: %s' %
197 self.GetLaunchScreenBackgroundImage('landscape'))
198 print('launch_screen.landscape.image: %s' %
199 self.GetLaunchScreenImage('landscape'))
200 print('launch_screen.landscape.image_border: %s' %
201 self.GetLaunchScreenImageBorder('landscape'))
203 def GetAppName(self):
204 """Return the application name."""
205 return self.ret_dict['app_name']
207 def GetVersion(self):
208 """Return the version number."""
209 return self.ret_dict['version']
212 """Return the icons."""
213 return self.ret_dict['icons']
216 """Return the URL of the application."""
217 return self.ret_dict['app_url']
219 def GetDescription(self):
220 """Return the description of the application."""
221 return self.ret_dict['description']
223 def GetAppRoot(self):
224 """Return the root path of the local web application."""
225 return self.ret_dict['app_root']
227 def GetAppLocalPath(self):
228 """Return the local relative path of the local web application."""
229 return self.ret_dict['app_local_path']
231 def GetPermissions(self):
232 """Return the permissions."""
233 return self.ret_dict['permissions']
235 def GetRequiredVersion(self):
236 """Return the required crosswalk runtime version."""
237 return self.ret_dict['required_version']
239 def GetPlugins(self):
240 """Return the plug-in path and file name."""
241 return self.ret_dict['plugin']
243 def GetFullScreenFlag(self):
244 """Return the set fullscreen flag of the application."""
245 return self.ret_dict['fullscreen']
247 def GetLaunchScreenBackgroundColor(self, orientation):
248 """Return the background color for launch_screen."""
249 key = 'launch_screen_background_color_' + orientation
250 return self.ret_dict.get(key, '')
252 def GetLaunchScreenBackgroundImage(self, orientation):
253 """Return the background image for launch_screen."""
254 key = 'launch_screen_background_image_' + orientation
255 return self.ret_dict.get(key, '')
257 def GetLaunchScreenImage(self, orientation):
258 """Return the image for launch_screen."""
259 key = 'launch_screen_image_' + orientation
260 return self.ret_dict.get(key, '')
262 def GetLaunchScreenImageBorder(self, orientation):
263 """Return the image border for launch_screen."""
264 key = 'launch_screen_image_border_' + orientation
265 return self.ret_dict.get(key, '')
269 """Respond to command mode and show the processed field values."""
270 parser = optparse.OptionParser()
271 info = ('The input json-format file name. Such as: '
272 '--jsonfile=manifest.json')
273 parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
275 opts, _ = parser.parse_args()
279 json_parser = ManifestJsonParser(opts.jsonfile)
280 json_parser.ShowItems()
284 if __name__ == '__main__':
285 sys.exit(main(sys.argv))