summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/schema_loader.py
blob: c434dc167462a26a6e913a28e50d8b9bb79a4126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys

import idl_schema
import json_schema
from model import Model

class SchemaLoader(object):
  '''Resolves a type name into the namespace the type belongs to.
  '''
  def __init__(self, api_path):
    self._api_path = api_path

  def ResolveType(self, full_name, default_namespace):
    name_parts = full_name.rsplit('.', 1)
    if len(name_parts) == 1:
      if full_name not in default_namespace.types:
        return None
      return default_namespace
    namespace_name, type_name = name_parts
    real_name = None
    for ext in ['json', 'idl']:
      filename = '%s.%s' % (namespace_name, ext)
      if os.path.exists(filename):
        real_name = filename
        break
    if real_name is None:
      return None
    namespace = Model().AddNamespace(self.LoadSchema(real_name)[0],
                                     os.path.join(self._api_path, real_name))
    if type_name not in namespace.types:
      return None
    return namespace

  def LoadSchema(self, schema):
    schema_filename, schema_extension = os.path.splitext(schema)

    if schema_extension == '.json':
      api_defs = json_schema.Load(schema)
    elif schema_extension == '.idl':
      api_defs = idl_schema.Load(schema)
    else:
      sys.exit('Did not recognize file extension %s for schema %s' %
               (schema_extension, schema))
    if len(api_defs) != 1:
      sys.exit('File %s has multiple schemas. Files are only allowed to contain'
               'a single schema.' % schema)

    return api_defs