def mojom_path(filename): with open(filename) as f: source = f.read() tree = Parse(source, filename) _, name = os.path.split(filename) mojom = Translate(tree, name) elements = mojom['namespace'].split('.') elements.append("%s" % mojom['name']) return os.path.join(*elements)
def _load_mojom(path_to_mojom): filename = os.path.abspath(path_to_mojom) name = os.path.basename(filename) # Read in mojom file. with open(filename) as f: source = f.read() # Parse tree = Parse(source, name) mojom = Translate(tree, name) return mojom
def main(argv): if len(argv) < 2: print "usage: %s filename" % sys.argv[0] return 1 for filename in argv[1:]: with open(filename) as f: print "%s:" % filename print Translate(Parse(f.read(), filename), os.path.splitext(os.path.basename(filename))[0]) return 0
def process_mojom(path_to_mojom): filename = os.path.abspath(path_to_mojom) name = os.path.basename(filename) # Read in mojom file. try: with open(filename) as f: source = f.read() except IOError: print("Error reading %s" % filename) sys.exit(2) # Parse try: tree = Parse(source, name) except Error: print("Error parsing %s" % filename) sys.exit(2) mojom = Translate(tree, name) # Output path attributes = mojom.get('attributes') print(mojom_path(mojom['name'], mojom['namespace'], attributes))
def _GenerateModule(self, args, remaining_args, generator_modules, rel_filename): # Return the already-generated module. if rel_filename.path in self._processed_files: return self._processed_files[rel_filename.path] tree = self._parsed_files[rel_filename.path] dirname, name = os.path.split(rel_filename.path) mojom = Translate(tree, name) if args.debug_print_intermediate: pprint.PrettyPrinter().pprint(mojom) # Process all our imports first and collect the module object for each. # We use these to generate proper type info. for import_data in mojom['imports']: rel_import_file = FindImportFile( RelativePath(dirname, rel_filename.source_root), import_data['filename'], args.import_directories) import_data['module'] = self._GenerateModule( args, remaining_args, generator_modules, rel_import_file) module = OrderedModuleFromData(mojom) # Set the path as relative to the source root. module.path = rel_filename.relative_path() # Normalize to unix-style path here to keep the generators simpler. module.path = module.path.replace('\\', '/') if self._should_generate(rel_filename.path): for language, generator_module in generator_modules.iteritems(): generator = generator_module.Generator( module, args.output_dir, typemap=self._typemap.get(language, {}), variant=args.variant, bytecode_path=args.bytecode_path, for_blink=args.for_blink, use_new_wrapper_types=args.use_new_wrapper_types, export_attribute=args.export_attribute, export_header=args.export_header, generate_non_variant_code=args.generate_non_variant_code) filtered_args = [] if hasattr(generator_module, 'GENERATOR_PREFIX'): prefix = '--' + generator_module.GENERATOR_PREFIX + '_' filtered_args = [arg for arg in remaining_args if arg.startswith(prefix)] generator.GenerateFiles(filtered_args) # Save result. self._processed_files[rel_filename.path] = module return module
def _GenerateModule(self, args, remaining_args, generator_modules, filename): # Return the already-generated module. if filename in self._processed_files: return self._processed_files[filename] tree = self._parsed_files[filename] dirname, name = os.path.split(filename) mojom = Translate(tree, name) if args.debug_print_intermediate: pprint.PrettyPrinter().pprint(mojom) # Process all our imports first and collect the module object for each. # We use these to generate proper type info. for import_data in mojom['imports']: import_filename = FindImportFile(dirname, import_data['filename'], args.import_directories) import_data['module'] = self._GenerateModule( args, remaining_args, generator_modules, import_filename) module = OrderedModuleFromData(mojom) # Set the path as relative to the source root. module.path = os.path.relpath(os.path.abspath(filename), os.path.abspath(args.depth)) # Normalize to unix-style path here to keep the generators simpler. module.path = module.path.replace('\\', '/') if self._should_generate(filename): for generator_module in generator_modules: generator = generator_module.Generator(module, args.output_dir) filtered_args = [] if hasattr(generator_module, 'GENERATOR_PREFIX'): prefix = '--' + generator_module.GENERATOR_PREFIX + '_' filtered_args = [arg for arg in remaining_args if arg.startswith(prefix)] if args.generate_type_info: filtered_args.append("--generate_type_info") generator.GenerateFiles(filtered_args) # Save result. self._processed_files[filename] = module return module
def ProcessFile(args, generator_modules, filename, _processed_files={}, _imported_filename_stack=[]): # Memoized results. if filename in _processed_files: return _processed_files[filename] # Ensure we only visit each file once. if filename in _imported_filename_stack: print "%s: Error: Circular dependency" % filename + \ MakeImportStackMessage(_imported_filename_stack + [filename]) sys.exit(1) try: with open(filename) as f: source = f.read() except IOError as e: print "%s: Error: %s" % (e.filename, e.strerror) + \ MakeImportStackMessage(_imported_filename_stack + [filename]) sys.exit(1) try: tree = Parse(source, filename) except Error as e: print str(e) + MakeImportStackMessage(_imported_filename_stack + [filename]) sys.exit(1) dirname, name = os.path.split(filename) mojom = Translate(tree, name) if args.debug_print_intermediate: pprint.PrettyPrinter().pprint(mojom) # Process all our imports first and collect the module object for each. # We use these to generate proper type info. for import_data in mojom['imports']: import_filename = os.path.join(dirname, import_data['filename']) import_data['module'] = ProcessFile( args, generator_modules, import_filename, _processed_files=_processed_files, _imported_filename_stack=_imported_filename_stack + [filename]) module = OrderedModuleFromData(mojom) # Set the path as relative to the source root. module.path = os.path.relpath(os.path.abspath(filename), os.path.abspath(args.depth)) # Normalize to unix-style path here to keep the generators simpler. module.path = module.path.replace('\\', '/') for generator_module in generator_modules: generator = generator_module.Generator(module, args.output_dir) generator.GenerateFiles() # Save result. _processed_files[filename] = module return module
def ProcessFile(self, args, remaining_args, generator_modules, filename, _imported_filename_stack=None): # Memoized results. if filename in self._processed_files: return self._processed_files[filename] if _imported_filename_stack is None: _imported_filename_stack = [] # Ensure we only visit each file once. if filename in _imported_filename_stack: print "%s: Error: Circular dependency" % filename + \ MakeImportStackMessage(_imported_filename_stack + [filename]) sys.exit(1) try: with open(filename) as f: source = f.read() except IOError as e: print "%s: Error: %s" % (e.filename, e.strerror) + \ MakeImportStackMessage(_imported_filename_stack + [filename]) sys.exit(1) try: tree = Parse(source, filename) except Error as e: full_stack = _imported_filename_stack + [filename] print str(e) + MakeImportStackMessage(full_stack) sys.exit(1) dirname, name = os.path.split(filename) mojom = Translate(tree, name) if args.debug_print_intermediate: pprint.PrettyPrinter().pprint(mojom) # Process all our imports first and collect the module object for each. # We use these to generate proper type info. for import_data in mojom['imports']: import_filename = FindImportFile(dirname, import_data['filename'], args.import_directories) import_data['module'] = self.ProcessFile( args, remaining_args, generator_modules, import_filename, _imported_filename_stack=_imported_filename_stack + [filename]) module = OrderedModuleFromData(mojom) # Set the path as relative to the source root. module.path = os.path.relpath(os.path.abspath(filename), os.path.abspath(args.depth)) # Normalize to unix-style path here to keep the generators simpler. module.path = module.path.replace('\\', '/') if self._should_generate(filename): for generator_module in generator_modules: generator = generator_module.Generator(module, args.output_dir) filtered_args = [] if hasattr(generator_module, 'GENERATOR_PREFIX'): prefix = '--' + generator_module.GENERATOR_PREFIX + '_' filtered_args = [ arg for arg in remaining_args if arg.startswith(prefix) ] generator.GenerateFiles(filtered_args) # Save result. self._processed_files[filename] = module return module