1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#Module amara
#all = ["parse", "parse_path", "pushbind", "pushdom", "__version__"]
RESERVED_URI = 'http://uche.ogbuji.net/tech/4suite/amara/reserved/'
PI_BINDING = RESERVED_URI + 'pi-binding'
import os
import binderytools
import domtools
create_document = binderytools.create_document
pushbind = binderytools.pushbind
pushdom = domtools.pushdom
# Convenience functions for parsing
def parse(source, uri=None, rules=None, binderobj=None, prefixes=None, validate=False, binding_classes=None):
"""
Convenience function for parsing XML. Use this function with a single
argument, which is a string (not Unicode object), file-like object
(stream), file path or URI.
Returns a document binding object.
Only use this function to parse self-contained XML (i.e. not requiring
access to any other resource). For example, do not use it for XML with
external entities. If you get URI resolution errors, pass in a URI
parameter.
uri - establish a base URI for the XML document entity being parsed,
required if source is a string or stream containing XML that
uses any external resources. If source is a path or URI, then
this parameter, if given, is ignored
rules - a list of bindery rule objects to fine-tune the binding
binderobj - optional binder object to control binding details,
the default is None, in which case a binder object
will be created
prefixes - dictionary mapping prefixes to namespace URIs
the default is None
"""
from Ft.Xml import InputSource
from Ft.Lib import Uri, Uuid
from Ft.Xml.Lib.XmlString import IsXml
#if isinstance(source, InputSource.InputSource):
# pass
if hasattr(source, 'read'):
return binderytools.bind_stream(
source, uri=uri, rules=rules, binderobj=binderobj,
prefixes=prefixes, validate=validate, binding_classes=binding_classes)
elif IsXml(source):
return binderytools.bind_string(
source, uri=uri, rules=rules, binderobj=binderobj,
prefixes=prefixes, validate=validate, binding_classes=binding_classes)
elif Uri.IsAbsolute(source): #or not os.path.isfile(source):
return binderytools.bind_uri(
source, rules=rules, binderobj=binderobj,
prefixes=prefixes, validate=validate, binding_classes=binding_classes)
else:
return binderytools.bind_file(
source, rules=rules, binderobj=binderobj,
prefixes=prefixes, validate=validate, binding_classes=binding_classes)
#Need double layer of exception checking to support freeze utils
#(In that case there would be setuptools at freeze time, but not run time)
try:
import pkg_resources
try:
__version__ = pkg_resources.get_distribution('Amara').version
except pkg_resources.DistributionNotFound:
from __config__ import VERSION as __version__
except ImportError:
from __config__ import VERSION as __version__
|