Exemple #1
0
 def test_common_references(self):
     # Test that we correctly resolve the case where common entries resolve
     # one another.
     references = References()
     a_ref = references.get_common('b', 'a')
     a = Entry('a', None, [])
     references.add_common(a_ref)
     references.add_common(a)
     items = references.resolve()
     self.assertEqual(2, len(items))
     # What should this be??
     self.assertEqual(a, items[0])
     self.assertEqual(a, items[1])
Exemple #2
0
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")
Exemple #3
0
def load_specs(specs, main_name=None, should_remove_unused=False):
    """Load a specification from disk.

    When more than one specification is passed in, the entries from each
    specification are loaded. Entries from one specification can be used as
    references in the other specifications.

    Raises LoadError on error.

    specs -- A list of specifications to load. Each item in the listen is a
      tuple containing;
        * A tuple containing (filename, contents, format)
      Where 'contents' is a string or file object containing the contents
      of the specification (if None, the file is opened from disk), and
      'format' is the type of the specification, such as 'xml' or 'asn1'
      (if None, it is taken from the filename extension).
    main_name -- The name of the entry to use as the main decoder. If None,
      there must a single main decoder from the specs loaded (otherwise
      a UnspecifiedMainDecoderError will be thrown).
    should_remove_unused -- Should the loader remove any entries that are
      unused by the main decoder.
    return -- (decoder, common, lookup)
    """
    from bdec.spec.references import References

    references = References()
    decoders = []
    lookup = {}
    for filename, contents, format in specs:
        if contents is None:
            contents = open(filename, "r")
        elif isinstance(contents, basestring):
            contents = StringIO(contents)

        if format is None:
            format = os.path.splitext(filename)[1][1:]
        d, l = _load_spec(filename, contents, format, references)
        if d:
            decoders.append(d)
        lookup.update(l)

    decoder = None
    if main_name:
        # The user has a specific entry they would like to use as the main
        # decoder.
        for decoder in decoders:
            if decoder.name == main_name:
                # The requested decoder is one of the top level main entries
                break
        else:
            # The requested decoder is one of the common entries. If not, it
            # will simply fail to resolve.
            decoder = references.get_common(main_name)
    else:
        if len(decoders) != 1:
            # There isn't a single main decoders available from the spec; the
            # user must choose what entry will be the 'main' decoder.
            raise UnspecifiedMainEntry([d.name for d in decoders], references.get_names())
        decoder = decoders[0]

    decoder, common = _resolve(decoder, references, lookup, should_remove_unused)
    return decoder, common, lookup