def _GetPublicFiles(): '''Gets all public file paths mapped to their contents. ''' def walk(path, prefix=''): path = ChromiumPath(path) public_files = {} for root, dirs, files in os.walk(path, topdown=True): relative_root = root[len(path):].lstrip(os.path.sep) dirs[:] = _FilterHidden(dirs) for filename in _FilterHidden(files): with open(os.path.join(root, filename), 'r') as f: request_path = posixpath.join(prefix, relative_root, filename) public_files[request_path] = f.read() return public_files # Public file locations are defined in content_providers.json, sort of. Epic # hack to pull them out; list all the files from the directories that # Chromium content providers ask for. public_files = {} content_providers = json_parse.Parse(ReadFile(CONTENT_PROVIDERS)) for content_provider in content_providers.itervalues(): if 'chromium' in content_provider: public_files.update( walk(content_provider['chromium']['dir'], prefix=content_provider['serveFrom'])) return public_files
def __init__(self, compiled_fs_factory, base_path, availability_finder_factory): def create_compiled_fs(fn, category): return compiled_fs_factory.Create(fn, APIDataSource, category=category) self._json_cache = create_compiled_fs( lambda api_name, api: self._LoadJsonAPI(api, False), 'json') self._idl_cache = create_compiled_fs( lambda api_name, api: self._LoadIdlAPI(api, False), 'idl') # These caches are used if an APIDataSource does not want to resolve the # $refs in an API. This is needed to prevent infinite recursion in # ReferenceResolver. self._json_cache_no_refs = create_compiled_fs( lambda api_name, api: self._LoadJsonAPI(api, True), 'json-no-refs') self._idl_cache_no_refs = create_compiled_fs( lambda api_name, api: self._LoadIdlAPI(api, True), 'idl-no-refs') self._idl_names_cache = create_compiled_fs(self._GetIDLNames, 'idl-names') self._names_cache = create_compiled_fs(self._GetAllNames, 'names') self._base_path = base_path self._availability_finder = availability_finder_factory.Create() self._parse_cache = create_compiled_fs( lambda _, json: json_parse.Parse(json), 'intro-cache') # These must be set later via the SetFooDataSourceFactory methods. self._ref_resolver_factory = None self._samples_data_source_factory = None
def ProcessSchema(path, file_data): '''Parses |file_data| using a method determined by checking the extension of the file at the given |path|. Then, trims 'nodoc' and handles inlineable types from the parsed schema data. ''' def trim_and_inline(schema, is_idl=False): '''Modifies an API schema in place by removing nodes that shouldn't be documented and inlining schema types that are only referenced once. ''' if RemoveNoDocs(schema): # A return of True signifies that the entire schema should not be # documented. Otherwise, only nodes that request 'nodoc' are removed. return None if is_idl: DetectInlineableTypes(schema) InlineDocs(schema) return schema if path.endswith('.idl'): idl = idl_schema.IDLSchema(idl_parser.IDLParser().ParseData(file_data)) return trim_and_inline(idl.process()[0], is_idl=True) schemas = json_parse.Parse(file_data) for schema in schemas: # Schemas could consist of one API schema (data for a specific API file) # or multiple (data from extension_api.json). trim_and_inline(schema) return schemas
def _LoadJsonAPI(self, api, disable_refs): return _JSCModel( json_parse.Parse(api)[0], self._ref_resolver_factory.Create() if not disable_refs else None, disable_refs, self._availability_finder, self._branch_utility, self._parse_cache, self._template_cache, self._LoadEventByName).ToDict()
def Process(self, path, file_data): '''Parses |file_data| using a method determined by checking the extension of the file at the given |path|. Then, trims 'nodoc' and if |self.retain_inlined_types| is given and False, removes inlineable types from the parsed schema data. ''' def trim_and_inline(schema, is_idl=False): '''Modifies an API schema in place by removing nodes that shouldn't be documented and inlining schema types that are only referenced once. ''' if self._RemoveNoDocs(schema): # A return of True signifies that the entire schema should not be # documented. Otherwise, only nodes that request 'nodoc' are removed. return None if is_idl: self._DetectInlineableTypes(schema) self._InlineDocs(schema) return schema if path.endswith('.idl'): idl = idl_schema.IDLSchema( idl_parser.IDLParser().ParseData(file_data)) # Wrap the result in a list so that it behaves like JSON API data. return [trim_and_inline(idl.process()[0], is_idl=True)] try: schemas = json_parse.Parse(file_data) except: raise ValueError('Cannot parse "%s" as JSON:\n%s' % (path, traceback.format_exc())) for schema in schemas: # Schemas could consist of one API schema (data for a specific API file) # or multiple (data from extension_api.json). trim_and_inline(schema) return schemas
def Process(self, path, file_data): if path.endswith('.idl'): idl = idl_schema.IDLSchema( idl_parser.IDLParser().ParseData(file_data)) # Wrap the result in a list so that it behaves like JSON API data. return [idl.process()[0]] return json_parse.Parse(file_data)
def ForJson(self, file_system): '''A CompiledFileSystem specifically for parsing JSON configuration data. These are memoized over file systems tied to different branches. ''' return self.Create( file_system, SingleFile(lambda _, data: json_parse.Parse(ToUnicode(data))), CompiledFileSystem, category='json')
def setUp(self): self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') self._compiled_fs_factory = CompiledFileSystem.Factory( TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA), ObjectStoreCreator.ForTest()) self._json_cache = self._compiled_fs_factory.Create( lambda _, json: json_parse.Parse(json), APIDataSourceTest, 'test')
def __init__(self, object_store_creator, compiled_host_fs_factory, branch_utility, create_file_system_at_version): self._object_store_creator = object_store_creator self._json_cache = compiled_host_fs_factory.Create( lambda _, json: json_parse.Parse(json), AvailabilityFinder, 'json-cache') self._branch_utility = branch_utility self._create_file_system_at_version = create_file_system_at_version self._object_store = object_store_creator.Create(AvailabilityFinder)
def _CreateFeaturesAndNamesFileSystems(self, version): '''The 'features' compiled file system's populate function parses and returns the contents of a _features.json file. The 'names' compiled file system's populate function creates a list of file names with .json or .idl extensions. ''' fs_factory = CompiledFileSystem.Factory( self._create_file_system_at_version(version), self._object_store_creator) features_fs = fs_factory.Create(lambda _, json: json_parse.Parse(json), AvailabilityFinder, category='features') names_fs = fs_factory.Create(self._GetExtNames, AvailabilityFinder, category='names') return (features_fs, names_fs)
def _LoadJsonAPI(self, api, disable_refs): return _JSCModel( json_parse.Parse(api)[0], self._ref_resolver_factory.Create() if not disable_refs else None, disable_refs).ToDict()
def _LoadPermissions(self, file_name, json_str): return json_parse.Parse(json_str)