def load_namespace_from_source_string(namespace, source): ss = create_scanner_from_source_string(source) transformer = Transformer(namespace) transformer.parse(ss.get_symbols()) cbp = GtkDocCommentBlockParser() blocks = cbp.parse_comment_blocks(ss.get_comments()) main = MainTransformer(transformer, blocks) main.transform()
def test_split_csymbol(self): cmd = [ sys.executable, '-c', 'import sys; sys.stdout.write("test_" + sys.stdin.read())'] namespace = ast.Namespace('Test', '1.0') xformer = Transformer(namespace, symbol_filter_cmd=cmd) self.assertEqual( xformer.split_csymbol('foo_bar_quux')[1], "foo_bar_quux")
def check(args): filename = args[0] output = ChunkedIO() namespace = Namespace('Test', '1.0') logger = MessageLogger.get(namespace=namespace, output=output) logger.enable_warnings(True) transformer = Transformer(namespace) transformer.set_include_paths([ os.path.join(top_srcdir, 'gir'), top_builddir, os.path.join(top_builddir, 'gir'), ]) transformer.register_include(Include.from_string('GObject-2.0')) ss = SourceScanner() options = Options() exit_code = process_packages(options, ['gobject-2.0']) if exit_code: sys.exit(exit_code) ss.set_cpp_options(options.cpp_includes, options.cpp_defines, options.cpp_undefines) ss.parse_files([filename]) ss.parse_macros([filename]) transformer.parse(ss.get_symbols()) cbp = GtkDocCommentBlockParser() blocks = cbp.parse_comment_blocks(ss.get_comments()) main = MainTransformer(transformer, blocks) main.transform() final = IntrospectablePass(transformer, blocks) final.validate() emitted_warnings = [w[w.find(':') + 1:] for w in output.getvalue()] expected_warnings = _extract_expected(filename) sortkey = lambda x: int(x.split(':')[0]) expected_warnings.sort(key=sortkey) emitted_warnings.sort(key=sortkey) if len(expected_warnings) != len(emitted_warnings): raise SystemExit("ERROR in '%s': %d warnings were emitted, " "expected %d:\n%s" % (os.path.basename(filename), len(emitted_warnings), len(expected_warnings), _diff(expected_warnings, emitted_warnings))) for emitted_warning, expected_warning in zip(emitted_warnings, expected_warnings): if expected_warning != emitted_warning: raise SystemExit( "ERROR in '%s': expected warning does not match emitted " "warning:\n%s" % (filename, _diff([expected_warning], [emitted_warning])))
def test_underscore_t_sed_filter(self): cmd = r"sed " \ r"-e 's/^test_t$/TestContext/' " \ r"-e 's/\(.*\)_t$/\1/' " \ r"-e 's/^test_/Test_/' " \ r"-e 's/_\([a-z]\)/" + '\\u' + r"\1/g'" namespace = ast.Namespace('Test', '1.0') xformer = Transformer(namespace, identifier_filter_cmd=cmd) self.assertEqual(xformer.strip_identifier('test_t'), 'Context') self.assertEqual(xformer.strip_identifier('test_foo_t'), 'Foo') self.assertEqual(xformer.strip_identifier('test_foot'), 'Foot') self.assertEqual(xformer.strip_identifier('test_foo_bart'), 'FooBart') self.assertEqual(xformer.strip_identifier('test_foo_tart'), 'FooTart')
def test_underscore_t_sed_filter(self): cmd = ( r"sed " r"-e 's/^test_t$/TestContext/' " r"-e 's/\(.*\)_t$/\1/' " r"-e 's/^test_/Test_/' " r"-e 's/_\([a-z]\)/\u\1/g'" ) namespace = ast.Namespace("Test", "1.0") xformer = Transformer(namespace, identifier_filter_cmd=cmd) self.assertEqual(xformer.strip_identifier("test_t"), "Context") self.assertEqual(xformer.strip_identifier("test_foo_t"), "Foo") self.assertEqual(xformer.strip_identifier("test_foot"), "Foot") self.assertEqual(xformer.strip_identifier("test_foo_bart"), "FooBart") self.assertEqual(xformer.strip_identifier("test_foo_tart"), "FooTart")
def check(args): filename = args[0] output = StringIO() namespace = Namespace("Test", "1.0") logger = MessageLogger.get(namespace=namespace, output=output) logger.enable_warnings(True) transformer = Transformer(namespace) transformer.set_include_paths( [os.path.join(top_srcdir, 'gir'), top_builddir]) transformer.register_include(Include.from_string("GObject-2.0")) ss = SourceScanner() options = Options() exit_code = process_packages(options, ['gobject-2.0']) if exit_code: sys.exit(exit_code) ss.set_cpp_options(options.cpp_includes, options.cpp_defines, options.cpp_undefines) ss.parse_files([filename]) ss.parse_macros([filename]) transformer.parse(ss.get_symbols()) ap = AnnotationParser() blocks = ap.parse(ss.get_comments()) main = MainTransformer(transformer, blocks) main.transform() final = IntrospectablePass(transformer, blocks) final.validate() raw = output.getvalue() if raw.endswith('\n'): raw = raw[:-1] warnings = raw.split('\n') failed_tests = 0 expected_warnings = _extract_expected(filename) if '' in warnings: warnings.remove('') if len(expected_warnings) != len(warnings): raise SystemExit( "ERROR in %r: expected %d warnings, but got %d:\n" "----\nexpected:\n%s\n----\ngot:\n%s\n----" % (os.path.basename(filename), len(expected_warnings), len(warnings), '\n'.join([w[1] for w in expected_warnings]), '\n'.join( [w.split(':', 2)[2][1:] for w in warnings]))) for warning, (sort_key, expected) in zip(warnings, expected_warnings): actual = warning.split(":", 1)[1] if _diff(expected, actual, filename): raise SystemExit("ERROR: tests %r failed" % (filename, ))
def check(args): filename = args[0] output = ChunkedIO() namespace = Namespace('Test', '1.0') logger = MessageLogger.get(namespace=namespace, output=output) logger.enable_warnings((WARNING, ERROR, FATAL)) transformer = Transformer(namespace) transformer.set_include_paths([os.path.join(top_srcdir, 'gir'), top_builddir]) transformer.register_include(Include.from_string('GObject-2.0')) ss = SourceScanner() options = Options() exit_code = process_packages(options, ['gobject-2.0']) if exit_code: sys.exit(exit_code) ss.set_cpp_options(options.cpp_includes, options.cpp_defines, options.cpp_undefines) ss.parse_files([filename]) ss.parse_macros([filename]) transformer.parse(ss.get_symbols()) cbp = GtkDocCommentBlockParser() blocks = cbp.parse_comment_blocks(ss.get_comments()) main = MainTransformer(transformer, blocks) main.transform() final = IntrospectablePass(transformer, blocks) final.validate() emitted_warnings = [w[w.find(':') + 1:] for w in output.getvalue()] expected_warnings = _extract_expected(filename) sortkey = lambda x: int(x.split(':')[0]) expected_warnings.sort(key=sortkey) emitted_warnings.sort(key=sortkey) if len(expected_warnings) != len(emitted_warnings): raise SystemExit("ERROR in '%s': %d warnings were emitted, " "expected %d:\n%s" % (os.path.basename(filename), len(emitted_warnings), len(expected_warnings), _diff(expected_warnings, emitted_warnings))) for emitted_warning, expected_warning in zip(emitted_warnings, expected_warnings): if expected_warning != emitted_warning: raise SystemExit("ERROR in '%s': expected warning does not match emitted " "warning:\n%s" % (filename, _diff([expected_warning], [emitted_warning])))
def test_underscore_t_identifier_filter(self): cmd = [sys.executable, '-c', textwrap.dedent(""" import sys, re for line in sys.stdin: line = re.sub(r"^test_t$", "TestContext", line) line = re.sub(r"_t$", "", line) line = re.sub(r"^test_", "Test_", line) line = re.sub(r"_([a-z])", lambda m: m.group(1).title(), line) sys.stdout.write(line)""")] namespace = ast.Namespace('Test', '1.0') xformer = Transformer(namespace, identifier_filter_cmd=cmd) self.assertEqual(xformer.strip_identifier('test_t'), 'Context') self.assertEqual(xformer.strip_identifier('test_foo_t'), 'Foo') self.assertEqual(xformer.strip_identifier('test_foot'), 'Foot') self.assertEqual(xformer.strip_identifier('test_foo_bart'), 'FooBart') self.assertEqual(xformer.strip_identifier('test_foo_tart'), 'FooTart')
def create_transformer(namespace, options): transformer = Transformer(namespace, accept_unprefixed=options.accept_unprefixed, identifier_filter_cmd=options.identifier_filter_cmd, symbol_filter_cmd=options.symbol_filter_cmd) transformer.set_include_paths(options.include_paths) if options.passthrough_gir: transformer.disable_cache() transformer.set_passthrough_mode() for include in options.includes: if os.sep in include: _error("Invalid include path %r" % (include, )) try: include_obj = Include.from_string(include) except: _error("Malformed include %r\n" % (include, )) transformer.register_include(include_obj) for include_path in options.includes_uninstalled: transformer.register_include_uninstalled(include_path) return transformer
def scanner_main(args): parser = _get_option_parser() (options, args) = parser.parse_args(args) if len(args) <= 1: _error('Need at least one filename') if options.typelib_xml: return typelib_xml_strip(args[1]) if options.inject: if len(args) != 4: _error('Need three filenames; e.g. g-ir-scanner ' '--inject Source.gir Additions.xml SourceOut.gir') return inject(*args[1:4]) if options.xpath_assertions: return validate(options.xpath_assertions, args[1]) if not options.namespace_name: _error('Namespace name missing') if options.format == 'gir': from giscanner.girwriter import GIRWriter as Writer else: _error("Unknown format: %s" % (options.format, )) if not (options.libraries or options.program): _error("Must specify --program or --library") libraries = options.libraries filenames = [] for arg in args: if (arg.endswith('.c') or arg.endswith('.h')): if not os.path.exists(arg): _error('%s: no such a file or directory' % (arg, )) # Make absolute, because we do comparisons inside scannerparser.c # against the absolute path that cpp will give us filenames.append(os.path.abspath(arg)) cachestore = CacheStore() transformer = Transformer(cachestore, options.namespace_name, options.namespace_version) if options.strip_prefix: transformer.set_strip_prefix(options.strip_prefix) else: transformer.set_strip_prefix(options.namespace_name) transformer.set_include_paths(options.include_paths) shown_include_warning = False for include in options.includes: if os.sep in include: raise ValueError("Invalid include path %r" % (include, )) try: include_obj = Include.from_string(include) except: sys.stderr.write("Malformed include %r\n" % (include, )) sys.exit(1) transformer.register_include(include_obj) packages = set(options.packages) packages.update(transformer.get_pkgconfig_packages()) process_packages(parser, options, packages) # Run the preprocessor, tokenize and construct simple # objects representing the raw C symbols ss = SourceScanner() ss.set_cpp_options(options.cpp_includes, options.cpp_defines, options.cpp_undefines) ss.parse_files(filenames) ss.parse_macros(filenames) # Transform the C symbols into AST nodes transformer.set_source_ast(ss) # Transform the C AST nodes into higher level # GLib/GObject nodes glibtransformer = GLibTransformer(transformer, noclosure=options.noclosure) # Do enough parsing that we have the get_type() functions to reference # when creating the introspection binary glibtransformer.init_parse() if options.program: args=[options.program] args.extend(options.program_args) binary = IntrospectionBinary(args) else: binary = compile_introspection_binary(options, glibtransformer.get_get_type_functions()) shlibs = resolve_shlibs(options, binary, libraries) glibtransformer.set_introspection_binary(binary) namespace = glibtransformer.parse() ap = AnnotationParser(namespace, ss, transformer) try: ap.parse() except InvalidAnnotationError, e: raise SystemExit("ERROR in annotation: %s" % (str(e), ))
def create_transformer(namespace, options): transformer = Transformer(namespace, accept_unprefixed=options.accept_unprefixed) transformer.set_include_paths(options.include_paths) if options.passthrough_gir: transformer.disable_cache() transformer.set_passthrough_mode() shown_include_warning = False for include in options.includes: if os.sep in include: _error("Invalid include path %r" % (include, )) try: include_obj = Include.from_string(include) except: _error("Malformed include %r\n" % (include, )) transformer.register_include(include_obj) for include_path in options.includes_uninstalled: transformer.register_include_uninstalled(include_path) return transformer
def create_transformer(namespace, options): identifier_filter_cmd = shlex.split(options.identifier_filter_cmd) symbol_filter_cmd = shlex.split(options.symbol_filter_cmd) transformer = Transformer(namespace, accept_unprefixed=options.accept_unprefixed, identifier_filter_cmd=identifier_filter_cmd, symbol_filter_cmd=symbol_filter_cmd) transformer.set_include_paths(options.include_paths) if options.passthrough_gir or options.reparse_validate_gir: transformer.disable_cache() transformer.set_passthrough_mode() for include in options.includes: if os.sep in include: _error("Invalid include path '%s'" % (include, )) try: include_obj = Include.from_string(include) except Exception: _error("Malformed include '%s'\n" % (include, )) transformer.register_include(include_obj) for include_path in options.includes_uninstalled: transformer.register_include_uninstalled(include_path) return transformer
def load_namespace_from_source_string(namespace, source): ss = create_scanner_from_source_string(source) xformer = Transformer(namespace) xformer.parse(ss.get_symbols())
def test_invalid_argument(self): cmd = r'sed --not-a-valid-argument' namespace = ast.Namespace('Test', '1.0') xformer = Transformer(namespace, identifier_filter_cmd=cmd) self.assertRaises(ValueError, xformer.strip_identifier, 'test_t')
def test_invalid_command(self): cmd = r'this-is-not-a-real-command' namespace = ast.Namespace('Test', '1.0') xformer = Transformer(namespace, identifier_filter_cmd=cmd) self.assertRaises(ValueError, xformer.strip_identifier, 'test_t')
def test_main(self): transformer = Transformer(None) DocWriter(transformer, "gjs", "devdocs")