blob: 648d094fa03ac9b7df1277c2138750b860fe95a4 [file] [log] [blame]
[email protected]db20a22fa2013-03-23 18:49:301# 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
5import os
6import sys
7
8import idl_schema
9import json_schema
lfg8a1bee32014-08-29 03:56:2810
[email protected]db20a22fa2013-03-23 18:49:3011class SchemaLoader(object):
rdevlin.cronin227d70c2016-12-21 20:24:1712 '''Loads a schema from a provided filename.
13 |root|: path to the root directory.
[email protected]db20a22fa2013-03-23 18:49:3014 '''
rdevlin.cronin227d70c2016-12-21 20:24:1715 def __init__(self, root):
lfg8a1bee32014-08-29 03:56:2816 self._root = root
[email protected]db20a22fa2013-03-23 18:49:3017
18 def LoadSchema(self, schema):
[email protected]43294452013-09-30 15:29:3419 '''Load a schema definition. The schema parameter must be a file name
lfg8a1bee32014-08-29 03:56:2820 with the full path relative to the root.'''
thestigb179975132015-01-15 23:19:2321 _, schema_extension = os.path.splitext(schema)
[email protected]db20a22fa2013-03-23 18:49:3022
lfg8a1bee32014-08-29 03:56:2823 schema_path = os.path.join(self._root, schema)
[email protected]db20a22fa2013-03-23 18:49:3024 if schema_extension == '.json':
[email protected]43294452013-09-30 15:29:3425 api_defs = json_schema.Load(schema_path)
[email protected]db20a22fa2013-03-23 18:49:3026 elif schema_extension == '.idl':
[email protected]43294452013-09-30 15:29:3427 api_defs = idl_schema.Load(schema_path)
[email protected]db20a22fa2013-03-23 18:49:3028 else:
29 sys.exit('Did not recognize file extension %s for schema %s' %
30 (schema_extension, schema))
[email protected]db20a22fa2013-03-23 18:49:3031
rdevlin.cronin227d70c2016-12-21 20:24:1732 # TODO(devlin): This returns a list. Does it need to? Is it ever > 1?
[email protected]db20a22fa2013-03-23 18:49:3033 return api_defs