Ejemplo n.º 1
0
 def test_simple_roundtrip(self):
     """Parse with string"""
     def myindex(content):
         doc = amara.parse(content)
         yield u'root-element-name', doc.xml_select(u'name(*)')
         yield u'element-count', doc.xml_select(u'count(//*)')
     fname = tempfile.mktemp('.xml')
     driver.init_db(sqlite3.connect(fname))
     drv = driver(sqlite3.connect(fname))
     content = MONTY_XML
     id = drv.create_resource(content, metadata=dict(myindex(content)))
     content1, metadata = drv.get_resource(id)
     doc = amara.parse(content)
     self.assertEqual(content, content1)
     self.assertEqual(metadata[u'root-element-name'], doc.xml_select(u'name(*)'))
     self.assertEqual(metadata[u'element-count'], doc.xml_select(u'count(//*)'))
     return
Ejemplo n.º 2
0
    def test_simple_roundtrip(self):
        """Parse with string"""
        def myindex(content):
            doc = amara.parse(content)
            yield u'root-element-name', doc.xml_select(u'name(*)')
            yield u'element-count', doc.xml_select(u'count(//*)')

        fname = tempfile.mktemp('.xml')
        driver.init_db(sqlite3.connect(fname))
        drv = driver(sqlite3.connect(fname))
        content = MONTY_XML
        id = drv.create_resource(content, metadata=dict(myindex(content)))
        content1, metadata = drv.get_resource(id)
        doc = amara.parse(content)
        self.assertEqual(content, content1)
        self.assertEqual(metadata[u'root-element-name'],
                         doc.xml_select(u'name(*)'))
        self.assertEqual(metadata[u'element-count'],
                         doc.xml_select(u'count(//*)'))
        return
Ejemplo n.º 3
0
Archivo: web.py Proyecto: dpla/akara
def store(environ, start_response):
    dbfile = environ['akara.DBFILE']
    drv = driver(sqlite3.connect(dbfile))
    def head_resource():
        get_resource()
        return ''

    def get_resource():
        key = shift_path_info(environ)

        content1, metadata = drv.get_resource(key)
        if content1 is None:
            #404 error
            start_response('404 Not Found', [('content-type', 'text/html')])
            response = four_oh_four.substitute(url=request_uri(environ))
            return response

        start_response('200 OK', [('content-type', str(metadata[CONTENT_TYPE]))])
        return content1.encode('utf-8')

    def post_resource():
        ctype = environ.get('CONTENT_TYPE', 'application/unknown')
        clen = int(environ.get('CONTENT_LENGTH', None))
        if not clen:
            start_response("411 Length Required", [('Content-Type','text/plain')])
            return ["Length Required"]
        key = shift_path_info(environ)
        now = datetime.now().isoformat()
        md = {
            CREATED: now,
            UPDATED: now,
            CONTENT_LENGTH: clen,
            CONTENT_TYPE: ctype,
        }
        #md = self.standard_index
        content = environ['wsgi.input'].read(clen)
        id = drv.create_resource(content, metadata=md)
        msg = 'Adding %i' % id
        new_uri = str(id)

        headers = [('Content-Type', 'text/plain')]
        headers.append(('Location', new_uri))
        headers.append(('Content-Location', new_uri))

        #environ['akara.etag'] = compute_etag(content)
        headers.append(('Content-Length', str(len(msg))))
        start_response("201 Created", headers)
        
        return msg

    dispatch = {
        'GET': get_resource,
        'HEAD': head_resource,
        'POST': post_resource,
    }

    method = dispatch.get(environ['REQUEST_METHOD'])
    if not method:
        response_headers = [('Content-type','text/plain')]
        start_response(response(httplib.METHOD_NOT_ALLOWED), response_headers)
        return ['Method Not Allowed']
    else:
        return [method()]
Ejemplo n.º 4
0
Archivo: web.py Proyecto: mredar/akara
def store(environ, start_response):
    dbfile = environ['akara.DBFILE']
    drv = driver(sqlite3.connect(dbfile))

    def head_resource():
        get_resource()
        return ''

    def get_resource():
        key = shift_path_info(environ)

        content1, metadata = drv.get_resource(key)
        if content1 is None:
            #404 error
            start_response('404 Not Found', [('content-type', 'text/html')])
            response = four_oh_four.substitute(url=request_uri(environ))
            return response

        start_response('200 OK',
                       [('content-type', str(metadata[CONTENT_TYPE]))])
        return content1.encode('utf-8')

    def post_resource():
        ctype = environ.get('CONTENT_TYPE', 'application/unknown')
        clen = int(environ.get('CONTENT_LENGTH', None))
        if not clen:
            start_response("411 Length Required",
                           [('Content-Type', 'text/plain')])
            return ["Length Required"]
        key = shift_path_info(environ)
        now = datetime.now().isoformat()
        md = {
            CREATED: now,
            UPDATED: now,
            CONTENT_LENGTH: clen,
            CONTENT_TYPE: ctype,
        }
        #md = self.standard_index
        content = environ['wsgi.input'].read(clen)
        id = drv.create_resource(content, metadata=md)
        msg = 'Adding %i' % id
        new_uri = str(id)

        headers = [('Content-Type', 'text/plain')]
        headers.append(('Location', new_uri))
        headers.append(('Content-Location', new_uri))

        #environ['akara.etag'] = compute_etag(content)
        headers.append(('Content-Length', str(len(msg))))
        start_response("201 Created", headers)

        return msg

    dispatch = {
        'GET': get_resource,
        'HEAD': head_resource,
        'POST': post_resource,
    }

    method = dispatch.get(environ['REQUEST_METHOD'])
    if not method:
        response_headers = [('Content-type', 'text/plain')]
        start_response(response(httplib.METHOD_NOT_ALLOWED), response_headers)
        return ['Method Not Allowed']
    else:
        return [method()]