def test_os_path_to_uri(self): for osname in ('posix', 'nt'): for path, nt_uri, posix_uri in file_paths: if isinstance(path, unicode): testname = repr(path) else: testname = path if osname == 'nt': uri = nt_uri elif osname == 'posix': uri = posix_uri else: break if uri is None: self.assertRaises( iri.IriError, lambda path=path, osname=osname: iri.os_path_to_uri( path, attemptAbsolute=False, osname=osname), osname + ': ' + testname) else: self.assertEqual( uri, iri.os_path_to_uri(path, attemptAbsolute=False, osname=osname), osname + ': ' + testname + ': ' + uri)
def test_os_path_to_uri(self): for osname in ("posix", "nt"): for path, nt_uri, posix_uri in file_paths: if isinstance(path, unicode): testname = repr(path) else: testname = path if osname == "nt": uri = nt_uri elif osname == "posix": uri = posix_uri else: break if uri is None: self.assertRaises( iri.IriError, lambda path=path, osname=osname: iri.os_path_to_uri(path, attemptAbsolute=False, osname=osname), osname + ": " + testname, ) else: self.assertEqual( uri, iri.os_path_to_uri(path, attemptAbsolute=False, osname=osname), osname + ": " + testname + ": " + uri, )
def test_apply_import1(): _run_xml( source_xml="<example><spam/></example>", transform_uri=iri.os_path_to_uri(__file__), transform_xml="""\ <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:import href="cs_20070210-1.xslt"/> <xsl:template match="/"> <xsl:apply-templates mode="foo"/> </xsl:template> <xsl:template match="example" mode="foo"> <div> <span>b</span> <xsl:apply-imports/> </div> </xsl:template> </xsl:transform>""", expected="""<?xml version="1.0" encoding="UTF-8"?> <div><span>b</span><span>a</span></div>""", )
def test_apply_import1(): _run_xml( source_xml = "<example><spam/></example>", transform_uri = iri.os_path_to_uri(__file__), transform_xml = """\ <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:import href="cs_20070210-1.xslt"/> <xsl:template match="/"> <xsl:apply-templates mode="foo"/> </xsl:template> <xsl:template match="example" mode="foo"> <div> <span>b</span> <xsl:apply-imports/> </div> </xsl:template> </xsl:transform>""", expected = """<?xml version="1.0" encoding="UTF-8"?> <div><span>b</span><span>a</span></div>""", )
def __new__(cls, arg, uri=None, encoding=None, resolver=None, sourcetype=0): """ arg - a string, Unicode object (only if you really know what you're doing), file-like object (stream), file path or URI. You can also pass an InputSource object, in which case the return value is just the same object, possibly with the URI modified uri - optional override URI. The base URI for the IS will be set to this value Returns an input source which can be passed to Amara APIs. """ #do the imports within the function to avoid circular crap #from amara._xmlstring import IsXml as isxml #These importa are tucked in here because amara.lib.iri is an expensive import from amara.lib.iri import is_absolute, os_path_to_uri from amara.lib.irihelpers import DEFAULT_RESOLVER resolver = resolver or DEFAULT_RESOLVER if isinstance(arg, InputSource): return arg #if arg == (u'', ''): -> UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if arg == '': #FIXME L10N raise ValueError("Cannot parse an empty string as XML") if isinstance(arg, urllib2.Request): uri = arg.get_full_url() #One of the rightly labeled "lame" helper methods in urllib2 ;) stream = resolver.resolve(arg) elif hasattr(arg, 'read'): #Create dummy Uri to use as base uri = uri or uuid4().urn stream = arg #XXX: Should we at this point refuse to proceed unless it's a basestring? elif sourcetype == XMLSTRING or isxml(arg): #See this article about XML detection heuristics #http://www.xml.com/pub/a/2007/02/28/what-does-xml-smell-like.html uri = uri or uuid4().urn stream = StringIO(arg) elif is_absolute(arg) and not os.path.isfile(arg): uri = arg stream = resolver.resolve(uri) #If the arg is beyond a certain length, don't even try it as a URI elif len(arg) < MAX_URI_LENGTH_FOR_HEURISTIC: uri = os_path_to_uri(arg) stream = resolver.resolve(uri) else: #FIXME L10N raise ValueError("Does not appear to be well-formed XML") #We might add the ability to load zips, gzips & bzip2s #http://docs.python.org/lib/module-zlib.html #http://docs.python.org/lib/module-gzip.html #http://docs.python.org/lib/module-bz2.html #http://docs.python.org/lib/zipfile-objects.html #import inspect; print inspect.stack() #InputSource.__new__ is in C: expat/input_source.c:inputsource_new return InputSource.__new__(cls, stream, uri, encoding)
def __init__(self, source, uri=None, validate=False, xinclude=True, external=False): if not uri: # it is relative to the calling module module = sys._getframe(1).f_globals['__name__'] module = sys.modules[module] moduledir = os.path.dirname(os.path.abspath(module.__file__)) path = str(uuid.uuid4()) uri = iri.os_path_to_uri(os.path.join(moduledir, path)) testsource.__init__(self, source, uri, validate, xinclude)
def test_cdm1(): print >> sys.stderr, FILE('data/cdm1.html') toppage = iri.os_path_to_uri(FILE('data/cdm1.html')) results = read_contentdm(toppage + '?', collection='/jthom', limit=1) #read_contentdm(toppage, collection='/jthom', limit=None) header = results.next() EXPECTED1 = {} assert header['basequeryurl'].startswith(toppage), (header, ) item = results.next() EXPECTED2 = {} assert item == EXPECTED2, (item, EXPECTED2) #import pprint; pprint.pprint(result, stream=sys.stderr) return
def test_uri_jail(self): start_uri = iri.os_path_to_uri(FILE('test_irihelpers.py')) #raise start_uri #print >> sys.stderr, "GRIPPO", start_uri start_base = start_uri.rsplit('/', 1)[0] + '/' #Only allow access files in the same directory as sampleresource.txt via URL jails auths = [(lambda u: u.rsplit('/', 1)[0] + '/' == start_base, True)] resolver = irihelpers.resolver(authorizations=auths) start_isrc = inputsource(start_uri, resolver=resolver) new_isrc = start_isrc.resolve('sampleresource.txt', start_base) self.assertEqual('Spam', new_isrc.stream.read().strip()) self.assertRaises(iri.IriError, resolver.resolve, 'http://google.com', start_base)
def get_processor(self, transform_hash, ext_functions=None, ext_elements=None): try: # First make sure we avoid race conditions... self._main_lock.acquire() if transform_hash not in self._granular_locks: self._granular_locks[transform_hash] = thread.allocate_lock() finally: self._main_lock.release() proc = None try: self._granular_locks[transform_hash].acquire() # If we do not have that stylesheet yet in the pool, let's add it if not self._processors.has_key(transform_hash): processor = Processor() self._processors[transform_hash] = processor_info( thread.allocate_lock(), stat(transform_hash)[ST_MTIME], processor ) for (ns, local), func in ext_functions.items(): processor.registerExtensionFunction(ns, local, func) for (ns, local), elem in ext_elements.items(): processor.registerExtensionElement(ns, local, elem) self._processors[transform_hash].instance.append_transform(iri.os_path_to_uri(transform_hash)) # If the stylesheet has been modified, reload it elif stat(transform_hash)[ST_MTIME] != self._processors[transform_hash].last_modified: self._processors[transform_hash].instance.reset() self._processors[transform_hash].last_modified = stat(transform_hash)[ST_MTIME] self._processors[transform_hash].instance.append_transform(iri.os_path_to_uri(transform_hash)) # now we can lock the processor... self._processors[transform_hash]['lock'].acquire() proc = self._processors[transform_hash].instance finally: self._granular_locks[transform_hash].release() # ... and return it return proc
def __init__(self, location=None, graph=None, debug=False, nsBindings = None, owlEmbeddings = False): self.owlEmbeddings = owlEmbeddings self.nsBindings = nsBindings if nsBindings else {} self.location = location self.rules = {} self.debug = debug if graph: assert location is None,"Must supply one of graph or location" self.graph = graph if debug: print "RIF in RDF graph was provided" else: assert graph is None,"Must supply one of graph or location" if debug: print "RIF document URL provided ", location if self.location.find('http:')+1: req = urllib2.Request(self.location) ##From: http://www.diveintopython.org/http_web_services/redirects.html #points an 'opener' to the address to 'sniff' out final Location header opener = urllib2.build_opener(SmartRedirectHandler()) f = opener.open(req) self.content = f.read() else: try: self.content = urllib2.urlopen(self.location).read() except ValueError: self.content = urllib2.urlopen(iri.os_path_to_uri(self.location)).read() # self.content = open(self.location).read() try: rdfContent = transform(self.content,inputsource(TRANSFORM_URI)) self.graph = Graph().parse(StringIO(rdfContent)) if debug: print "Extracted rules from RIF XML format" except ValueError: try: self.graph = Graph().parse(StringIO(self.content),format='xml') except: self.graph = Graph().parse(StringIO(self.content),format='n3') if debug: print "Extracted rules from RIF in RDF document" self.nsBindings.update(dict(self.graph.namespaces()))
def test_xslt_nested_imports_with_names_that_clash_across_import_precedence_mb_20020527(): _run_xml( source_xml = "<foo/>", transform_uri = iri.os_path_to_uri(__file__), transform_xml = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="resources/mb_20020527-1.xsl"/> <xsl:template name="i"> <foo>import.xsl</foo> </xsl:template> </xsl:stylesheet> """, expected = """<?xml version="1.0" encoding="UTF-8"?>\n<out><foo>import.xsl</foo></out>""", )
def test_xslt_include_with_variables_dm_20010506(): try: _run_html(source_xml=commonsource, transform_uri=iri.os_path_to_uri(__file__), transform_xml="""<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:include href="dm_20010506-1.xslt"/> <xsl:variable name="section.autolabel" select="1" /> <xsl:variable name="html.stylesheet">book.xsl</xsl:variable> <xsl:variable name="html.stylesheet.type">text/css</xsl:variable> </xsl:stylesheet>""", expected=None) except XsltError, err: assert err.code == XsltError.DUPLICATE_TOP_LEVEL_VAR, err
def test_xslt_include_with_variables_dm_20010506(): try: _run_html( source_xml = commonsource, transform_uri = iri.os_path_to_uri(__file__), transform_xml = """<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:include href="dm_20010506-1.xslt"/> <xsl:variable name="section.autolabel" select="1" /> <xsl:variable name="html.stylesheet">book.xsl</xsl:variable> <xsl:variable name="html.stylesheet.type">text/css</xsl:variable> </xsl:stylesheet>""", expected = None) except XsltError, err: assert err.code == XsltError.DUPLICATE_TOP_LEVEL_VAR, err
def test_basic_uri_resolver(self): data = [('http://foo.com/root/', 'path', 'http://foo.com/root/path'), ('http://foo.com/root', 'path', 'http://foo.com/path'), ] #import sys; print >> sys.stderr, filesource('sampleresource.txt').uri start_isrc = inputsource(FILE('sampleresource.txt')) #start_isrc = inputsource(filesource('sampleresource.txt').uri) for base, uri, exp in data: res = start_isrc.absolutize(uri, base) self.assertEqual(exp, res, "absolutize: %s %s" % (base, uri)) base = 'foo:foo.com' uri = 'path' self.assertRaises(iri.IriError, start_isrc.absolutize, uri, base) base = os.getcwd() if base[-1] != os.sep: base += os.sep new_isrc = start_isrc.resolve(FILE('sampleresource.txt'), iri.os_path_to_uri(base)) self.assertEqual('Spam', new_isrc.stream.readline().rstrip(), 'resolve')
def test_xslt_nested_imports_with_names_that_clash_across_import_precedence_mb_20020527( ): _run_xml( source_xml="<foo/>", transform_uri=iri.os_path_to_uri(__file__), transform_xml="""<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="resources/mb_20020527-1.xsl"/> <xsl:template name="i"> <foo>import.xsl</foo> </xsl:template> </xsl:stylesheet> """, expected= """<?xml version="1.0" encoding="UTF-8"?>\n<out><foo>import.xsl</foo></out>""", )
def test_xslt_import_with_params_dm_20010506(): _run_html( source_xml=commonsource, transform_uri=iri.os_path_to_uri(__file__), transform_xml="""<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:import href="dm_20010506-1.xslt"/> <xsl:param name="section.autolabel" select="1" /> <xsl:param name="html.stylesheet">book.xsl</xsl:param> <xsl:param name="html.stylesheet.type">text/css</xsl:param> </xsl:stylesheet>""", expected="""\ <html> START <link type='text/css' rel='stylesheet' href='book.xsl'> END </html>""", )
def test_xslt_import_with_params_dm_20010506(): _run_html( source_xml = commonsource, transform_uri = iri.os_path_to_uri(__file__), transform_xml = """<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:import href="dm_20010506-1.xslt"/> <xsl:param name="section.autolabel" select="1" /> <xsl:param name="html.stylesheet">book.xsl</xsl:param> <xsl:param name="html.stylesheet.type">text/css</xsl:param> </xsl:stylesheet>""", expected = """\ <html> START <link type='text/css' rel='stylesheet' href='book.xsl'> END </html>""", )
######################################################################## # test/xslt/og_20010503.py # Oliver Graf <*****@*****.**> has indentation probs with # ft:write-file import tempfile from amara.lib import iri from amara.test.xslt.xslt_support import _run_xml BASENAME = tempfile.mktemp() BASENAME_URI = iri.os_path_to_uri(BASENAME) file_expected = """<?xml version="1.0" encoding="ISO-8859-1"?> <datatree> <name>%s</name> <what>test</what> </datatree>""" def test_xslt_ft_write_file_og_20010503(): _run_xml( source_xml="""\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data>11</data> <data>12</data> <data>13</data> <data>14</data> <data>15</data> </test>
result = urllib2.HTTPRedirectHandler.http_error_302( self, req, fp, code, msg, headers) result.status = code return result RIF_NS = Namespace('http://www.w3.org/2007/rif#') XSD_NS = Namespace('http://www.w3.org/2001/XMLSchema#') ENT = Namespace("http://www.w3.org/ns/entailment/") mimetypes = { 'application/rdf+xml' : 'xml', 'text/n3' : 'n3', 'text/turtle' : 'turtle', } TRANSFORM_URI = iri.absolutize('rif-core-rdf.xsl',iri.os_path_to_uri(__file__)) IMPORT_PARTS=\ """ SELECT DISTINCT ?location ?profile { [] a rif:Import; rif:location ?location; rif:profile ?profile . }""" IMPLIES_PARTS=\ """ SELECT DISTINCT ?impl ?body ?bodyType ?head ?headType { ?impl a rif:Implies; rif:if ?body; rif:then ?head .
######################################################################## # test/xslt/og_20010507.py # Oliver Graf <*****@*****.**>: # ft:output cdata-section-elements is not used import tempfile from amara.lib import iri from amara.test.xslt.xslt_support import _run_text FILESTEM = tempfile.mktemp() FILESTEM_URI = iri.os_path_to_uri(FILESTEM) file_expected_1 = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <datatree> <content> <num><![CDATA[1000]]></num> <str><![CDATA[test]]></str> </content> <what>test</what> </datatree>""" def test_xslt_cdata_section_elements_og_20010507(): _run_text(source_xml="""\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data> <num>1000</num> <str>test</str>
######################################################################## # test/xslt/jk_20050406.py # See http://bugs.4suite.org/1180509 import tempfile, os import cStringIO import unittest from amara.lib import treecompare from amara.test import test_main from amara.test.xslt import xslt_test, filesource, stringsource from amara.lib.iri import os_path_to_uri fname = tempfile.mktemp() furi = os_path_to_uri(fname) commonsource = "<dummy/>" commontransform = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" exclude-result-prefixes="exsl"> <xsl:output method="html" indent="no"/> <xsl:param name="URL" select="'%s'"/> <xsl:template match="/">
def test_xslt_count_children_via_incrementing_counter_dn_20010504(): _run_text( source_xml = """\ <numbers> <num>3</num> <num>2</num> <num>9</num> <num>4</num> <num>6</num> <num>5</num> <num>7</num> <num>8</num> <num>1</num> </numbers>""", transform_uri = iri.os_path_to_uri(__file__), transform_xml = """\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:num="num" xmlns:num2="num2" > <num:node>num</num:node> <num2:node>num2</num2:node> <xsl:output method="text" /> <xsl:variable name="document" select="document('')" /> <xsl:variable name="gtNum-node" select="$document//num:*"/> <xsl:variable name="gtNum2-node" select="$document//num2:*"/> <xsl:template match="/"> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$gtNum-node" /> <xsl:with-param name="nodes" select="/numbers/num" /> </xsl:call-template> <xsl:text>
</xsl:text> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$gtNum2-node" /> <xsl:with-param name="nodes" select="/numbers/num" /> </xsl:call-template> </xsl:template> <xsl:template name="get-max"> <xsl:param name="greaterSelector" select="/*"/> <xsl:param name="nodes" /> <xsl:choose> <xsl:when test="$nodes"> <xsl:variable name="max-of-rest"> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$greaterSelector" /> <xsl:with-param name="nodes" select="$nodes[position()!=1]" /> </xsl:call-template> </xsl:variable> <xsl:variable name="isGreater"> <xsl:apply-templates select="$greaterSelector" > <xsl:with-param name="n1" select="$nodes[1]"/> <xsl:with-param name="n2" select="$max-of-rest"/> </xsl:apply-templates> </xsl:variable> <xsl:choose> <xsl:when test="$isGreater = 'true'"> <xsl:value-of select="$nodes[1]" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="$max-of-rest" /> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select="-999999999" /> <!-- minus infinity --> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="isGreaterNum" match="node()[namespace-uri()='num']"> <xsl:param name="n1"/> <xsl:param name="n2"/> <xsl:value-of select="$n1 > $n2"/> </xsl:template> <xsl:template name="isGreaterNum2" match="node()[namespace-uri()='num2']"> <xsl:param name="n1"/> <xsl:param name="n2"/> <xsl:value-of select="1 div $n1 > 1 div $n2"/> </xsl:template> </xsl:stylesheet>""", expected = """\ 9 1""")
######################################################################## # test/xslt/og_20010503.py # Oliver Graf <*****@*****.**> has indentation probs with # ft:write-file import tempfile from amara.lib import iri from amara.test.xslt.xslt_support import _run_xml BASENAME = tempfile.mktemp() BASENAME_URI = iri.os_path_to_uri(BASENAME) file_expected = """<?xml version="1.0" encoding="ISO-8859-1"?> <datatree> <name>%s</name> <what>test</what> </datatree>""" def test_xslt_ft_write_file_og_20010503(): _run_xml( source_xml = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data>11</data> <data>12</data> <data>13</data> <data>14</data> <data>15</data> </test>
# -*- encoding: utf-8 -*- ######################################################################## # test/xslt/borrowed/uo_20010503.py # From original 4Suite cvs: #This source doc used to bomb cDomlette just on parse, as Uche found out import os from amara.lib import iri from amara.test.xslt.xslt_support import _run_xml BASE_URI = iri.os_path_to_uri(os.path.abspath(__file__), attemptAbsolute=True) common_transform = """ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> """ def test_xslt_uo_20010503_1(): _run_xml(
def test_wwwlog2json_maxrecords_large(): # Size is greater than the number of records results = _make_log2json_request("?maxrecords=1000") items = results["items"] assert len(items) == 200, len(items) def test_wwwlog2json_nobots(): results = _make_log2json_request("?nobots=1") items = results["items"] assert len(items) == 183, len(items) # xslt.py XML_DATA = open(os.path.join(RESOURCE_DIR, "xslt_spec_example.xml")).read() XSLT_URL = iri.os_path_to_uri( os.path.join(RESOURCE_DIR, "xslt_spec_example.xslt")) def test_xslt(): url = server() + "akara.xslt?" + urllib.urlencode({"@xslt": XSLT_URL}) req = urllib2.Request(url) req.add_header("Content-Type", "text/xml") response = urllib2.urlopen(req, XML_DATA) doc = bindery.parse(response) assert str(doc.html.head.title) == "Document Title", repr( str(doc.html.head.title)) if __name__ == "__main__": raise SystemExit("Use nosetests")
# -*- encoding: utf-8 -*- ######################################################################## # test/xslt/borrowed/uo_20010503.py # From original 4Suite cvs: #This source doc used to bomb cDomlette just on parse, as Uche found out import os from amara.lib import iri from amara.test.xslt.xslt_support import _run_xml BASE_URI = iri.os_path_to_uri(os.path.abspath(__file__), attemptAbsolute=True) common_transform = """ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> """ def test_xslt_uo_20010503_1():
def __new__(cls, arg, uri=None, encoding=None, resolver=None, sourcetype=0): """ arg - a string, Unicode object (only if you really know what you're doing), file-like object (stream), file path or URI. You can also pass an InputSource object, in which case the return value is just the same object, possibly with the URI modified uri - optional override URI. The base URI for the IS will be set to this value Returns an input source which can be passed to Amara APIs. """ #do the imports within the function to avoid circular crap #from amara._xmlstring import IsXml as isxml #These importa are tucked in here because amara.lib.iri is an expensive import from amara.lib.iri import is_absolute, os_path_to_uri from amara.lib.irihelpers import DEFAULT_RESOLVER resolver = resolver or DEFAULT_RESOLVER if isinstance(arg, InputSource): return arg #if arg == (u'', ''): -> UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if arg == '': #FIXME L10N raise ValueError("Cannot parse an empty string as XML") if isinstance(arg, urllib2.Request): uri = arg.get_full_url( ) #One of the rightly labeled "lame" helper methods in urllib2 ;) stream = resolver.resolve(arg) elif hasattr(arg, 'read'): #Create dummy Uri to use as base uri = uri or uuid4().urn stream = arg #XXX: Should we at this point refuse to proceed unless it's a basestring? elif sourcetype == XMLSTRING or isxml(arg): #See this article about XML detection heuristics #http://www.xml.com/pub/a/2007/02/28/what-does-xml-smell-like.html uri = uri or uuid4().urn stream = StringIO(arg) elif is_absolute(arg) and not os.path.isfile(arg): uri = arg stream = resolver.resolve(uri) #If the arg is beyond a certain length, don't even try it as a URI elif len(arg) < MAX_URI_LENGTH_FOR_HEURISTIC: uri = os_path_to_uri(arg) stream = resolver.resolve(uri) else: #FIXME L10N raise ValueError("Does not appear to be well-formed XML") #We might add the ability to load zips, gzips & bzip2s #http://docs.python.org/lib/module-zlib.html #http://docs.python.org/lib/module-gzip.html #http://docs.python.org/lib/module-bz2.html #http://docs.python.org/lib/zipfile-objects.html #import inspect; print inspect.stack() #InputSource.__new__ is in C: expat/input_source.c:inputsource_new return InputSource.__new__(cls, stream, uri, encoding)
######################################################################## # test/xslt/tg_20010628.py # Thomas Guettler <*****@*****.**> wants to use XInclude in # his stylesheets import os from amara.test.xslt.xslt_support import _run_html from amara.lib import iri INCLUDE_URI = iri.os_path_to_uri(os.path.join(os.path.dirname(__file__), "resources", "tg_20010628-include.xml")) def test_xslt_xinclude_tg_20010628(): _run_html( source_xml = """\ <?xml version="1.0" encoding="iso-8859-1"?> <html xmlns:xinclude="http://www.w3.org/2001/XInclude"> <head> <title>MyTitle</title> </head> <body> <xinclude:include href="%(uri)s"/> </body> </html>""" % {'uri' : INCLUDE_URI}, transform_xml = """\ <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xinclude="http://www.w3.org/2001/XInclude">
items = results["items"] assert len(items) == 10, len(items) assert items[0]["origin"] == "host-24-225-218-245.patmedia.net" assert items[-1]["timestamp"] == "2006-10-01T06:33:55-07:00", items[-1]["timestamp"] def test_wwwlog2json_maxrecords_large(): # Size is greater than the number of records results = _make_log2json_request("?maxrecords=1000") items = results["items"] assert len(items) == 200, len(items) def test_wwwlog2json_nobots(): results = _make_log2json_request("?nobots=1") items = results["items"] assert len(items) == 183, len(items) # xslt.py XML_DATA = open(os.path.join(RESOURCE_DIR, "xslt_spec_example.xml")).read() XSLT_URL = iri.os_path_to_uri(os.path.join(RESOURCE_DIR, "xslt_spec_example.xslt")) def test_xslt(): url = server() + "akara.xslt?" + urllib.urlencode({"@xslt": XSLT_URL}) req = urllib2.Request(url) req.add_header("Content-Type", "text/xml") response = urllib2.urlopen(req, XML_DATA) doc = bindery.parse(response) assert str(doc.html.head.title) == "Document Title", repr(str(doc.html.head.title)) if __name__ == "__main__": raise SystemExit("Use nosetests")
######################################################################## # test/xslt/og_20010507.py # Oliver Graf <*****@*****.**>: # ft:output cdata-section-elements is not used import tempfile from amara.lib import iri from amara.test.xslt.xslt_support import _run_text FILESTEM = tempfile.mktemp() FILESTEM_URI = iri.os_path_to_uri(FILESTEM) file_expected_1 = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <datatree> <content> <num><![CDATA[1000]]></num> <str><![CDATA[test]]></str> </content> <what>test</what> </datatree>""" def test_xslt_cdata_section_elements_og_20010507(): _run_text( source_xml = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data> <num>1000</num> <str>test</str>