[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | # Use of this source code is governed by a BSD-style license that can be | ||||
3 | # found in the LICENSE file. | ||||
4 | |||||
5 | import os | ||||
6 | import sys | ||||
7 | |||||
8 | import idl_schema | ||||
9 | import json_schema | ||||
lfg | 8a1bee3 | 2014-08-29 03:56:28 | [diff] [blame] | 10 | |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 11 | class SchemaLoader(object): |
rdevlin.cronin | 227d70c | 2016-12-21 20:24:17 | [diff] [blame] | 12 | '''Loads a schema from a provided filename. |
13 | |root|: path to the root directory. | ||||
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 14 | ''' |
rdevlin.cronin | 227d70c | 2016-12-21 20:24:17 | [diff] [blame] | 15 | def __init__(self, root): |
lfg | 8a1bee3 | 2014-08-29 03:56:28 | [diff] [blame] | 16 | self._root = root |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 17 | |
18 | def LoadSchema(self, schema): | ||||
[email protected] | 4329445 | 2013-09-30 15:29:34 | [diff] [blame] | 19 | '''Load a schema definition. The schema parameter must be a file name |
lfg | 8a1bee3 | 2014-08-29 03:56:28 | [diff] [blame] | 20 | with the full path relative to the root.''' |
thestig | b17997513 | 2015-01-15 23:19:23 | [diff] [blame] | 21 | _, schema_extension = os.path.splitext(schema) |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 22 | |
lfg | 8a1bee3 | 2014-08-29 03:56:28 | [diff] [blame] | 23 | schema_path = os.path.join(self._root, schema) |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 24 | if schema_extension == '.json': |
[email protected] | 4329445 | 2013-09-30 15:29:34 | [diff] [blame] | 25 | api_defs = json_schema.Load(schema_path) |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 26 | elif schema_extension == '.idl': |
[email protected] | 4329445 | 2013-09-30 15:29:34 | [diff] [blame] | 27 | api_defs = idl_schema.Load(schema_path) |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 28 | else: |
29 | sys.exit('Did not recognize file extension %s for schema %s' % | ||||
30 | (schema_extension, schema)) | ||||
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 31 | |
rdevlin.cronin | 227d70c | 2016-12-21 20:24:17 | [diff] [blame] | 32 | # TODO(devlin): This returns a list. Does it need to? Is it ever > 1? |
[email protected] | db20a22fa | 2013-03-23 18:49:30 | [diff] [blame] | 33 | return api_defs |