Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: tools/json_schema_compiler/schema_loader.py

Issue 23534063: Make SchemaLoader independent of current working directory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use two separate paths Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import os 5 import os
6 import sys 6 import sys
7 7
8 import idl_schema 8 import idl_schema
9 import json_schema 9 import json_schema
10 from model import Model 10 from model import Model
11 11
12 class SchemaLoader(object): 12 class SchemaLoader(object):
13 '''Resolves a type name into the namespace the type belongs to. 13 '''Resolves a type name into the namespace the type belongs to.
14 ''' 14 '''
15 def __init__(self, api_path): 15 def __init__(self, api_path_relative_to_root, api_path):
not at google - send to devlin 2013/09/27 16:10:20 How about: display_path and real_path. Also, add a
msimonides 2013/09/30 07:18:48 Done.
16 self._api_path_relative_to_root = api_path_relative_to_root
16 self._api_path = api_path 17 self._api_path = api_path
17 18
18 def ResolveType(self, full_name, default_namespace): 19 def ResolveType(self, full_name, default_namespace):
19 name_parts = full_name.rsplit('.', 1) 20 name_parts = full_name.rsplit('.', 1)
20 if len(name_parts) == 1: 21 if len(name_parts) == 1:
21 if full_name not in default_namespace.types: 22 if full_name not in default_namespace.types:
22 return None 23 return None
23 return default_namespace 24 return default_namespace
24 namespace_name, type_name = name_parts 25 namespace_name, type_name = name_parts
25 real_name = None 26 real_name = None
26 for ext in ['json', 'idl']: 27 for ext in ['json', 'idl']:
27 filename = '%s.%s' % (namespace_name, ext) 28 filename = '%s.%s' % (namespace_name, ext)
28 if os.path.exists(filename): 29 filepath = os.path.join(self._api_path, filename);
30 if os.path.exists(filepath):
29 real_name = filename 31 real_name = filename
30 break 32 break
31 if real_name is None: 33 if real_name is None:
32 return None 34 return None
33 namespace = Model().AddNamespace(self.LoadSchema(real_name)[0], 35 namespace = Model().AddNamespace(
34 os.path.join(self._api_path, real_name)) 36 self.LoadSchema(real_name)[0],
37 os.path.join(self._api_path_relative_to_root, real_name))
35 if type_name not in namespace.types: 38 if type_name not in namespace.types:
36 return None 39 return None
37 return namespace 40 return namespace
38 41
39 def LoadSchema(self, schema): 42 def LoadSchema(self, schema):
43 '''Load a schema definition. The schema parameter must be a file name
44 without any path component - the file is loaded from the path defined by
45 the api_path argument passed to the constructor.'''
not at google - send to devlin 2013/09/27 16:10:20 Docstring style is '''Foo Bar. ''' not '''Foo
msimonides 2013/09/30 07:18:48 Done.
40 schema_filename, schema_extension = os.path.splitext(schema) 46 schema_filename, schema_extension = os.path.splitext(schema)
41 47
48 schema_path = os.path.join(self._api_path, schema);
42 if schema_extension == '.json': 49 if schema_extension == '.json':
43 api_defs = json_schema.Load(schema) 50 api_defs = json_schema.Load(schema_path)
44 elif schema_extension == '.idl': 51 elif schema_extension == '.idl':
45 api_defs = idl_schema.Load(schema) 52 api_defs = idl_schema.Load(schema_path)
46 else: 53 else:
47 sys.exit('Did not recognize file extension %s for schema %s' % 54 sys.exit('Did not recognize file extension %s for schema %s' %
48 (schema_extension, schema)) 55 (schema_extension, schema))
49 if len(api_defs) != 1: 56 if len(api_defs) != 1:
50 sys.exit('File %s has multiple schemas. Files are only allowed to contain' 57 sys.exit('File %s has multiple schemas. Files are only allowed to contain'
51 'a single schema.' % schema) 58 'a single schema.' % schema)
52 59
53 return api_defs 60 return api_defs
OLDNEW
« tools/json_schema_compiler/compiler.py ('K') | « tools/json_schema_compiler/compiler.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698