def test_no_main_decoder(self): a = '<protocol ><common><sequence name="a" /></common></protocol>' try: load_specs([('<string a>', a, 'xml')]) self.fail("Should have failed to load the spec, but didn't!") except UnspecifiedMainEntry, ex: self.assertEqual('No top level protocol present! Choose one of ' 'the common entries to be the main:\n a', str(ex))
def __init__(self): spec_dir = os.path.join(os.path.split(__file__)[0], "spec") self._fake_6_bit_text_field = Field("6 bit ascii", length=0, format=Field.TEXT) try: self._aivdm = load_specs([os.path.join(spec_dir, "aivdm.xml")]) self._ais = load_specs(glob(os.path.join(spec_dir, "ais*.xml"))) except LoadError, ex: sys.exit(str(ex))
def test_unknown_main_decoder(self): a = ''' <protocol> <sequence name="a" /> </protocol>''' try: # The user specifies a decoder that doesn't exist.... load_specs([('<string a>', a, 'xml')], 'missing') self.fail('Oh dang. Should have failed.') except ReferenceError, ex: self.assertEqual("unknown[0]: Reference to unknown entry 'missing'!", str(ex))
def main(): parser = OptionParser(usage=__doc__) parser.add_option("-f", dest="filename", help="Read the xml from FILE " "instead of stdin.", metavar="FILENAME") parser.add_option( "--main", dest="main", help="Specify the entry to " "be encoded instead of the toplevel protocol object.", metavar="ENTRY", ) parser.add_option( "--remove-unused", dest="remove_unused", help="Remove " "unused entries from the specification after loading. This allows " "specs to include references that don't resolve.", action="store_true", ) options, args = parser.parse_args() if not args: sys.exit("No specifications listed! See '%s -h' for more info." % sys.argv[0]) try: protocol, common, lookup = load_specs( [(spec, None, None) for spec in args], options.main, options.remove_unused ) except bdec.spec.LoadError, ex: sys.exit(str(ex))
def _test_spec(self, test_path, spec_filename, successes, failures, config): assert successes or failures skip = self._get(config, self.language, test_path.lower()) or \ self._get(config, 'default', test_path.lower()) if skip == 'decode': print 'Skipping test.' return elif skip == 'encode': should_encode = False require_exact_encoding = True elif skip == 'encoding-equivalent': should_encode = True require_exact_encoding = False else: assert not skip, "Unknown test fixme status '%s'" % skip should_encode = True require_exact_encoding = True spec, common, lookup = load_specs([(spec_filename, None, None)]) for data_filename in successes: expected_xml = None expected_filename = '%s.expected.xml' % os.path.splitext(data_filename)[0] if os.path.exists(expected_filename): xml_file = file(expected_filename, 'r') expected_xml = xml_file.read() xml_file.close() self._test_success(spec, common, spec_filename, data_filename, expected_xml, should_encode, require_exact_encoding) for data_filename in failures: self._test_failure(spec, common, spec_filename, data_filename, should_encode)
def load_msword_spec(): """ Load the msword specifcation. return -- (decoder, common, lookup)""" spec_dir = os.path.join(os.path.split(__file__)[0], '..', '..') filename = os.path.join(spec_dir, 'msword.xml') return load_specs([filename])
def test_error_with_multiple_decoders(self): # We should get an error when multiple specifications have the top # level decoder, but we don't specify it. a = ''' <protocol> <sequence name="a" /> </protocol>''' b = ''' <protocol> <sequence name="b" /> </protocol>''' try: load_specs([('<string a>', a, 'xml'), ('<string b>', b, 'xml')]) self.fail("Should have failed to load the spec, but didn't!") except UnspecifiedMainEntry, ex: self.assertEqual('Multiple top level protocols available! Choose one of:\n a\n b', str(ex))
def test_png_sample(self): test_dir = os.path.dirname(__file__) spec_filename = os.path.join(test_dir, '..', 'png.xml') main_filename = os.path.join(test_dir, '..', '..', 'docs', 'files', 'main.c') png_filename = os.path.join(test_dir, 'png', 'white.png') (spec, common, lookup) = load_specs([(spec_filename, None, None)]) generate(spec, common, _CDecoder, False) shutil.copy(main_filename, _CDecoder.TEST_DIR) output = compile_and_run(open(png_filename, 'rb'), _CDecoder) self.assertEqual("Image width = 5\nImage height = 5\nComment = I'm an image comment!\n", output)
def test_png_sample(self): test_dir = os.path.dirname(__file__) spec_filename = os.path.join(test_dir, "..", "png.xml") main_filename = os.path.join(test_dir, "..", "..", "docs", "files", "main.c") png_filename = os.path.join(test_dir, "png", "white.png") (spec, common, lookup) = load_specs([(spec_filename, None, None)]) generate(spec, common, _CDecoder, False) shutil.copy(main_filename, _CDecoder.TEST_DIR) output = compile_and_run(open(png_filename, "rb"), _CDecoder) self.assertEqual("Image width = 5\nImage height = 5\nComment = I'm an image comment!\n", output)
def test_remove_unused(self): text = ''' <protocol> <sequence name="a" /> <common> <field name="b" length="${unknown length}" /> </common> </protocol> ''' self.assertRaises(ReferenceError, load_specs, [('<string>', text, 'xml')], should_remove_unused=False) specs, common, lookup = load_specs([('<string>', text, 'xml')], should_remove_unused=True) self.assertEqual(0, len(common))
def test_choosing_main_decoder(self): # Test that we can choose the main decoder when multiple options # exist. a = ''' <protocol> <sequence name="a" /> </protocol>''' b = ''' <protocol> <sequence name="b" /> </protocol>''' spec, common, lookup = load_specs([('<string a>', a, 'xml'), ('<string b>', b, 'xml')], 'b') self.assertEqual('b', spec.name)
def test_cross_specification_references(self): # Test that we can reference entries between specifications a = ''' <protocol> <sequence name="a"> <reference name="b" /> </sequence> </protocol>''' b = ''' <protocol> <common> <field name="b" length="8" type="integer" /> </common> </protocol>''' spec, common, lookup = load_specs([('<string a>', a, 'xml'), ('<string b>', b, 'xml')]) data = dt.Data('\x0a') items = list(spec.decode(data)) self.assertEquals(4, len(items)) self.assertEquals(10, items[-2][-1])
def main(): parser = OptionParser(usage=__doc__) parser.add_option('-f', dest='filename', help='Read the xml from FILE ' 'instead of stdin.', metavar='FILENAME') parser.add_option('--main', dest='main', help='Specify the entry to ' 'be encoded instead of the toplevel protocol object.', metavar='ENTRY') parser.add_option('--remove-unused', dest='remove_unused', help='Remove ' 'unused entries from the specification after loading. This allows ' "specs to include references that don't resolve.", action='store_true') options, args = parser.parse_args() if not args: sys.exit("No specifications listed! See '%s -h' for more info." % sys.argv[0]) try: protocol, common, lookup = load_specs([(spec, None, None) for spec in args], options.main, options.remove_unused) except bdec.spec.LoadError, ex: sys.exit(str(ex))
def _test_spec(self, test_path, spec_filename, entry_name, successes, failures, config): skip = self._get(config, self.decoder.NAME, test_path.lower()) or self._get( config, "default", test_path.lower() ) if skip == "decoding-broken": print "Skipping test." return elif skip == "encoding-broken": should_encode = False require_exact_encoding = True elif skip == "encoding-equivalent": should_encode = True require_exact_encoding = False else: assert not skip, "Unknown test fixme status '%s'" % skip should_encode = True require_exact_encoding = True spec, common, lookup = load_specs([(spec_filename, None, None)]) for data_filename in successes: expected_xml = None expected_filename = "%s.expected.xml" % os.path.splitext(data_filename)[0] if os.path.exists(expected_filename): xml_file = file(expected_filename, "r") expected_xml = xml_file.read() xml_file.close() if entry_name is not None: for entry in common: if entry.name == entry_name: spec = entry break else: raise Exception("Unknown common entry", entry_name) self._test_success( spec, common, spec_filename, data_filename, expected_xml, should_encode, require_exact_encoding ) for data_filename in failures: self._test_failure(spec, common, spec_filename, data_filename, should_encode)
from bdec.data import Data from bdec import DecodeError from bdec.spec.xmlspec import load from bdec.spec import load_specs from bdec.output.instance import decode from bdec.spec.references import References references = References() import sys with open(sys.argv[1], 'r') as pb: data = pb.read() spec = load_specs(["playback_faster.xml"])[0] try: values = decode(spec, Data(data)) except DecodeError, err: print 'Oh oh...', err raise for value in values: print(value) break print("done")
def loads(text): return load_specs([('<string>', text, 'xml')])
print bdec.__version__ sys.exit(0) elif opt == '--remove-unused': should_remove_unused = True elif opt == '--template': if os.path.exists(arg): template_dir = bdec.compiler.FilesystemTemplate(arg) else: template_dir = bdec.compiler.BuiltinTemplate(arg) elif opt == '--main': main_spec = arg else: assert False, 'Unhandled option %s!' % opt try: spec, common, lookup = load_specs([(s, None, None) for s in args], main_spec, should_remove_unused) except bdec.spec.LoadError, ex: sys.exit(str(ex)) if template_dir is None: template_dir = bdec.compiler.BuiltinTemplate('c') try: templates = bdec.compiler.load_templates(template_dir) bdec.compiler.generate_code(spec, templates, outputdir, common, options) except: sys.exit(mako.exceptions.text_error_template().render()) if __name__ == '__main__': main()
from bdec.data import Data from bdec import DecodeError from bdec.spec.xmlspec import load from bdec.spec import load_specs from bdec.output.instance import decode from bdec.spec.references import References references = References() def get_pb_line(fpath): with open(fpath) as f: for line in f: yield line count = 0 with open('playback.xml', 'r') as xml_f: spec = load_specs(["playback.xml"])[0] for line in get_pb_line(sys.argv[1]): try: values = decode(spec, Data(line)) except DecodeError, err: print 'Oh oh...', err raise count += 0 print(values) #import pdb #pdb.set_trace() #print(2) print("done", count)
def main(): main_spec, specs, binary, verbose, should_remove_unused = _parse_args() try: decoder, common, lookup = load_specs([(s, None, None) for s in specs], main_spec, should_remove_unused) except bdec.spec.LoadError, ex: sys.exit(str(ex))