Upstream version 7.35.136.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / manifest_json_parser.py
1 #!/usr/bin/env python
2
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.
6
7 """
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.
11
12 Sample usage from shell script:
13 python manifest_json_parser.py --jsonfile=/path/to/manifest.json
14 """
15
16 import json
17 import optparse
18 import os
19 import re
20 import sys
21
22 def HandlePermissionList(permission_list):
23   """This function is used to handle the permission list and return the string
24   of permissions.
25
26   Args:
27     permission_list: the permission list, e.g.["permission1", "permission2"].
28
29   Returns:
30     The string of permissions with ':' as separator.
31     e.g. "permission1:permission2".
32   """
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 '
38             '\'.\' are allowed.')
39       sys.exit(1)
40   return ':'.join(permissions)
41
42
43 class ManifestJsonParser(object):
44   """ The class is used to parse json-format manifest file, recompose the fields
45   and provide the field interfaces required by the packaging tool.
46
47   Args:
48     input_path: the full path of the json-format manifest file.
49   """
50   def __init__(self, input_path):
51     self.input_path = input_path
52     input_file = open(self.input_path)
53     try:
54       input_src = input_file.read()
55       self.data_src = json.JSONDecoder().decode(input_src)
56       self.ret_dict = self._output_items()
57     except (TypeError, ValueError, IOError) as error:
58       print('There is a parser error in manifest.json file: %s' % error)
59       sys.exit(1)
60     except KeyError as error:
61       print('There is a field error in manifest.json file: %s' % error)
62       sys.exit(1)
63     finally:
64       input_file.close()
65
66   def _output_items(self):
67     """ The manifest field items are reorganized and returned as a
68     dictionary to support single or multiple values of keys.
69
70     Returns:
71       A dictionary to the corresponding items. the dictionary keys are
72       described as follows, the value is set to "" if the value of the
73       key is not set.
74     app_name:         The application name.
75     version:          The version number.
76     icons:            An array of icons.
77     app_url:          The url of application, e.g. hosted app.
78     description:      The description of application.
79     app_root:         The root path of the web, this flag allows to package
80                       local web application as apk.
81     app_local_path:   The relative path of entry file based on app_root,
82                       this flag should work with "--app-root" together.
83     permissions:      The permission list.
84     required_version: The required crosswalk runtime version.
85     plugin:           The plug-in information.
86     fullscreen:       The fullscreen flag of the application.
87     launch_screen:    The launch screen configuration.
88     """
89     ret_dict = {}
90     if 'name' not in self.data_src:
91       print('Error: no \'name\' field in manifest.json file.')
92       sys.exit(1)
93     ret_dict['app_name'] = self.data_src['name']
94     if 'version' not in self.data_src:
95       print('Error: no \'version\' field in manifest.json file.')
96       sys.exit(1)
97     ret_dict['version'] = self.data_src['version']
98     if 'launch_path' in self.data_src:
99       app_url = self.data_src['launch_path']
100     elif ('app' in self.data_src and
101         'launch' in self.data_src['app'] and
102             'local_path' in self.data_src['app']['launch']):
103       app_url = self.data_src['app']['launch']['local_path']
104     else:
105       app_url = ''
106     if app_url.lower().startswith(('http://', 'https://')):
107       app_local_path = ''
108     else:
109       app_local_path = app_url
110       app_url = ''
111     file_path_prefix = os.path.split(self.input_path)[0]
112     if 'icons' in self.data_src:
113       ret_dict['icons'] = self.data_src['icons']
114     else:
115       ret_dict['icons'] = {}
116     app_root = file_path_prefix
117     ret_dict['description'] = ''
118     if 'description' in self.data_src:
119       ret_dict['description'] = self.data_src['description']
120     ret_dict['app_url'] = app_url
121     ret_dict['app_root'] = app_root
122     ret_dict['app_local_path'] = app_local_path
123     ret_dict['permissions'] = ''
124     if 'permissions' in self.data_src:
125       try:
126         permission_list = self.data_src['permissions']
127         ret_dict['permissions'] = HandlePermissionList(permission_list)
128       except (TypeError, ValueError, IOError):
129         print('\'Permissions\' field error in manifest.json file.')
130         sys.exit(1)
131     ret_dict['required_version'] = ''
132     if 'required_version' in self.data_src:
133       ret_dict['required_version'] = self.data_src['required_version']
134     ret_dict['plugin'] = ''
135     if 'plugin' in self.data_src:
136       ret_dict['plugin'] = self.data_src['plugin']
137     if 'display' in self.data_src and 'fullscreen' in self.data_src['display']:
138       ret_dict['fullscreen'] = 'true'
139     else:
140       ret_dict['fullscreen'] = ''
141     if 'launch_screen' in self.data_src:
142       self.ParseLaunchScreen(ret_dict, 'default')
143       self.ParseLaunchScreen(ret_dict, 'portrait')
144       self.ParseLaunchScreen(ret_dict, 'landscape')
145     return ret_dict
146
147   def ParseLaunchScreen(self, ret_dict, orientation):
148     if orientation in self.data_src['launch_screen']:
149       sub_dict = self.data_src['launch_screen'][orientation]
150       if 'background_color' in sub_dict:
151         ret_dict['launch_screen_background_color_' + orientation] = (
152             sub_dict['background_color'])
153       if 'background_image' in sub_dict:
154         ret_dict['launch_screen_background_image_' + orientation] = (
155             sub_dict['background_image'])
156       if 'image' in sub_dict:
157         ret_dict['launch_screen_image_' + orientation] = (
158             sub_dict['image'])
159       if 'image_border' in sub_dict:
160         ret_dict['launch_screen_image_border_' + orientation] = (
161             sub_dict['image_border'])
162
163   def ShowItems(self):
164     """Show the processed results, it is used for command-line
165     internal debugging."""
166     print("app_name: %s" % self.GetAppName())
167     print("version: %s" % self.GetVersion())
168     print("description: %s" % self.GetDescription())
169     print("icons: %s" % self.GetIcons())
170     print("app_url: %s" % self.GetAppUrl())
171     print("app_root: %s" % self.GetAppRoot())
172     print("app_local_path: %s" % self.GetAppLocalPath())
173     print("permissions: %s" % self.GetPermissions())
174     print("required_version: %s" % self.GetRequiredVersion())
175     print("plugins: %s" % self.GetPlugins())
176     print("fullscreen: %s" % self.GetFullScreenFlag())
177     print('launch_screen.default.background_color: %s' %
178         self.GetLaunchScreenBackgroundColor('default'))
179     print('launch_screen.default.background_image: %s' %
180         self.GetLaunchScreenBackgroundImage('default'))
181     print('launch_screen.default.image: %s' %
182         self.GetLaunchScreenImage('default'))
183     print('launch_screen.default.image_border: %s' %
184         self.GetLaunchScreenImageBorder('default'))
185     print('launch_screen.portrait.background_color: %s' %
186         self.GetLaunchScreenBackgroundColor('portrait'))
187     print('launch_screen.portrait.background_image: %s' %
188         self.GetLaunchScreenBackgroundImage('portrait'))
189     print('launch_screen.portrait.image: %s' %
190         self.GetLaunchScreenImage('portrait'))
191     print('launch_screen.portrait.image_border: %s' %
192         self.GetLaunchScreenImageBorder('portrait'))
193     print('launch_screen.landscape.background_color: %s' %
194         self.GetLaunchScreenBackgroundColor('landscape'))
195     print('launch_screen.landscape.background_image: %s' %
196         self.GetLaunchScreenBackgroundImage('landscape'))
197     print('launch_screen.landscape.image: %s' %
198         self.GetLaunchScreenImage('landscape'))
199     print('launch_screen.landscape.image_border: %s' %
200         self.GetLaunchScreenImageBorder('landscape'))
201
202
203   def GetAppName(self):
204     """Return the application name."""
205     return self.ret_dict['app_name']
206
207   def GetVersion(self):
208     """Return the version number."""
209     return self.ret_dict['version']
210
211   def GetIcons(self):
212     """Return the icons."""
213     return self.ret_dict['icons']
214
215   def GetAppUrl(self):
216     """Return the URL of the application."""
217     return self.ret_dict['app_url']
218
219   def GetDescription(self):
220     """Return the description of the application."""
221     return self.ret_dict['description']
222
223   def GetAppRoot(self):
224     """Return the root path of the local web application."""
225     return self.ret_dict['app_root']
226
227   def GetAppLocalPath(self):
228     """Return the local relative path of the local web application."""
229     return self.ret_dict['app_local_path']
230
231   def GetPermissions(self):
232     """Return the permissions."""
233     return self.ret_dict['permissions']
234
235   def GetRequiredVersion(self):
236     """Return the required crosswalk runtime version."""
237     return self.ret_dict['required_version']
238
239   def GetPlugins(self):
240     """Return the plug-in path and file name."""
241     return self.ret_dict['plugin']
242
243   def GetFullScreenFlag(self):
244     """Return the set fullscreen flag of the application."""
245     return self.ret_dict['fullscreen']
246
247   def GetLaunchScreenBackgroundColor(self, orientation):
248     """Return the background color for launch_screen."""
249     if 'launch_screen_background_color_' + orientation in self.ret_dict:
250       return self.ret_dict['launch_screen_background_color_' + orientation]
251     else:
252       return ''
253
254   def GetLaunchScreenBackgroundImage(self, orientation):
255     """Return the background image for launch_screen."""
256     if 'launch_screen_background_image_' + orientation in self.ret_dict:
257       return self.ret_dict['launch_screen_background_image_' + orientation]
258     else:
259       return ''
260
261   def GetLaunchScreenImage(self, orientation):
262     """Return the image for launch_screen."""
263     if 'launch_screen_image_' + orientation in self.ret_dict:
264       return self.ret_dict['launch_screen_image_' + orientation]
265     else:
266       return ''
267
268   def GetLaunchScreenImageBorder(self, orientation):
269     """Return the image border for launch_screen."""
270     if 'launch_screen_image_border_' + orientation in self.ret_dict:
271       return self.ret_dict['launch_screen_image_border_' + orientation]
272     else:
273       return ''
274
275
276 def main(argv):
277   """Respond to command mode and show the processed field values."""
278   parser = optparse.OptionParser()
279   info = ('The input json-format file name. Such as: '
280           '--jsonfile=manifest.json')
281   parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
282                     help=info)
283   opts, _ = parser.parse_args()
284   if len(argv) == 1:
285     parser.print_help()
286     return 0
287   json_parser = ManifestJsonParser(opts.jsonfile)
288   json_parser.ShowItems()
289   return 0
290
291
292 if __name__ == '__main__':
293   sys.exit(main(sys.argv))