def moincms(wikibase, outputdir, pattern): if pattern: pattern = re.compile(pattern) #print (wikibase, outputdir, rewrite) req = urllib2.Request(wikibase, headers={'Accept': RDF_IMT}) resp = urllib2.urlopen(req) original_wiki_base = dict(resp.info())[ORIG_BASE_HEADER] feed = bindery.parse(resp) process_list = [] for item in feed.RDF.channel.items.Seq.li: uri = split_fragment(item.resource)[0] #print >> sys.stderr, (uri, str(item.resource), split_fragment(item.resource)) #Deal with the wrapped URI if original_wiki_base: #print >> sys.stderr, (uri, original_wiki_base.rstrip('/')+'/') relative = relativize(uri, original_wiki_base.rstrip('/')+'/').lstrip('/') uri = absolutize(relative, wikibase) #print >> sys.stderr, (uri, relative) if pattern and not pattern.match(relative): continue n = node.factory(uri, relative, outputdir) if n.up_to_date(): pass #print >> sys.stderr, 'Up to date. Skipped...' else: process_list.append(n) #Process nodes needing update according to priority for n in sorted(process_list, key=attrgetter('PRIORITY'), reverse=True): #print >> sys.stderr, "processing ", n.rest_uri n.render() return
def test_relativize(self): for targetUri, againstUri, relativeUri, subPathUri in relativize_test_cases: res = iri.relativize(targetUri, againstUri) self.assertEqual(relativeUri, res, "target=%r against=%r (subPathOnly=False)" % (targetUri, againstUri)) if res is not None: res = iri.absolutize(res, againstUri) self.assertEqual( res, targetUri, "target=%r against=%r (subPathOnly=False, Absolutize)" % (targetUri, againstUri) ) res = iri.relativize(targetUri, againstUri, True) self.assertEqual(subPathUri, res, "target=%r against=%r (subPathOnly=True)" % (targetUri, againstUri)) if res is not None: res = iri.absolutize(res, againstUri) self.assertEqual( res, targetUri, "target=%r against=%r (subPathOnly=True, Absolutize)" % (targetUri, againstUri) )
def moincms(wikibase, outputdir, pattern): if pattern: pattern = re.compile(pattern) #print (wikibase, outputdir, rewrite) req = urllib2.Request(wikibase, headers={'Accept': RDF_IMT}) resp = urllib2.urlopen(req) original_wiki_base = dict(resp.info())[ORIG_BASE_HEADER] feed = bindery.parse(resp) process_list = [] for item in feed.RDF.channel.items.Seq.li: uri = split_fragment(item.resource)[0] #print >> sys.stderr, (uri, str(item.resource), split_fragment(item.resource)) #Deal with the wrapped URI if original_wiki_base: #print >> sys.stderr, (uri, original_wiki_base.rstrip('/')+'/') relative = relativize(uri, original_wiki_base.rstrip('/') + '/').lstrip('/') uri = absolutize(relative, wikibase) #print >> sys.stderr, (uri, relative) if pattern and not pattern.match(relative): continue n = node.factory(uri, relative, outputdir) if n.up_to_date(): pass #print >> sys.stderr, 'Up to date. Skipped...' else: process_list.append(n) #Process nodes needing update according to priority for n in sorted(process_list, key=attrgetter('PRIORITY'), reverse=True): #print >> sys.stderr, "processing ", n.rest_uri n.render() return
def wrapped_uri(original_wiki_base, link): abs_link = absolutize(link, original_wiki_base) #print >> sys.stderr, 'abs_link: ', abs_link rel_link = relativize(abs_link, original_wiki_base) #print >> sys.stderr, 'rel_link: ', rel_link rest_uri = absolutize(rel_link, REST_WIKI_BASE) #print >> sys.stderr, 'rest_uri: ', rest_uri return rest_uri
def test_relativize(self): for targetUri, againstUri, relativeUri, subPathUri in relativize_test_cases: res = iri.relativize(targetUri, againstUri) self.assertEqual( relativeUri, res, 'target=%r against=%r (subPathOnly=False)' % (targetUri, againstUri)) if res is not None: res = iri.absolutize(res, againstUri) self.assertEqual( res, targetUri, 'target=%r against=%r (subPathOnly=False, Absolutize)' % (targetUri, againstUri)) res = iri.relativize(targetUri, againstUri, True) self.assertEqual( subPathUri, res, 'target=%r against=%r (subPathOnly=True)' % (targetUri, againstUri)) if res is not None: res = iri.absolutize(res, againstUri) self.assertEqual( res, targetUri, 'target=%r against=%r (subPathOnly=True, Absolutize)' % (targetUri, againstUri))
def unwrap_uri(original_base, wrapped_base, rest_uri): ''' Constructs an absolute URL to the original Moin page original_base - The base URI of the actual Moin instance wrapped_base - The base URI of the REST-wrapped proxy of the Moin instance rest_uri - moinrest-wrapped URI Returns a tuple unwrapped_link >>> from akara.util.moin import unwrap_uri >>> unwrap_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://localhost:8880/moin/w/spam') 'http://example.com/mywiki/spam' >>> unwrap_uri('http://example.com/', 'http://localhost:8880/moin/w/', 'http://localhost:8880/moin/w/spam') 'http://example.com/spam' ''' rel = relativize(rest_uri, wrapped_base.rstrip('/') + '/') return absolutize(rel, original_base.rstrip('/') + '/')
def unwrap_uri(original_base, wrapped_base, rest_uri): ''' Constructs an absolute URL to the original Moin page original_base - The base URI of the actual Moin instance wrapped_base - The base URI of the REST-wrapped proxy of the Moin instance rest_uri - moinrest-wrapped URI Returns a tuple unwrapped_link >>> from akara.util.moin import unwrap_uri >>> unwrap_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://localhost:8880/moin/w/spam') 'http://example.com/mywiki/spam' >>> unwrap_uri('http://example.com/', 'http://localhost:8880/moin/w/', 'http://localhost:8880/moin/w/spam') 'http://example.com/spam' ''' rel = relativize(rest_uri, wrapped_base.rstrip('/')+'/') return absolutize(rel, original_base.rstrip('/')+'/')
def wiki_uri(original_base, wrapped_base, link, relative_to=None, raw=False): ''' Constructs absolute URLs to the original and REST-wrapper for a page, given a link from another page original_base - The base URI of the actual Moin instance wrapped_base - The base URI of the REST-wrapped proxy of the Moin instance link - the relative link, generally from one wiki page to another relative_to - the REST-wrapped version of the page from which the relative link came, defaults to same as wrapped_base raw - the link is a full hierarchical path, rather than relative to the wiki base Returns a tuple (wrapped_uri, abs_link) wrapped_uri - the URI wrapped for REST ops abs_link - the full, original wiki URL >>> from akara.util.moin import wiki_uri >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/spam') ('http://localhost:8880/moin/w/spam', 'http://example.com/mywiki/spam') >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://google.com/spam') (None, None) >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://google.com/spam', raw=True) (None, None) >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/mywiki/spam', raw=True) ('http://localhost:8880/moin/w/spam', 'http://example.com/mywiki/spam') >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/mywiki/spam') ('http://localhost:8880/moin/w/mywiki/spam', 'http://example.com/mywiki/mywiki/spam') ''' #rel_link = relativize(abs_link, original_wiki_base) #e.g. original wiki base is http://myhost:8080/mywiki/ and link is /a/b #abs_link is http://myhost:8080/mywiki/a/b note the need to strip the leading / to get that #from akara import logger; logger.debug('wiki_uri' + repr((original_base, wrapped_base, link, relative_to, absolutize(link, original_base.rstrip('/')+'/')))) if raw and not is_absolute(link): (scheme, authority, path, query, fragment) = split_uri_ref(original_base) link = link[len(path):] link = link.lstrip('/') abs_link = absolutize(link, original_base.rstrip('/') + '/') rel_to_wikibase = relativize(abs_link, original_base.rstrip('/') + '/') if not rel_to_wikibase: #It's not a relative wiki link return None, None rest_uri = absolutize(rel_to_wikibase, wrapped_base.rstrip('/') + '/') return rest_uri, abs_link
def update_resource(self, path=None): ''' Update a resource based on WSGI environment or a uri path ''' if path: docid = path if is_absolute(path): docid = relativize(path, self.remotedb) else: docid = self.environ['PATH_INFO'].lstrip('/').rsplit(self.space_tag, 1)[1].lstrip('/') #e.g. '/mydb/MyDoc' -> 'MyDoc' if logger: logger.debug('query ' + repr((self.remotedb, docid, join(self.remotedb, docid)))) body = self.environ['wsgi.input'].read() # If the document already exists, we need to determine its current rev and add it to the # input body, skipping the process if rev is provided in the PUT request body body_js = json.loads(body) rev = json.loads(body).get('_rev',None) if not rev: # Need to GET the rev resp, content = self.h.request(join(self.remotedb, docid), "GET") if str(resp.status).startswith('2'): rev = json.loads(content).get('_rev',None) logger.debug('update_resource: found existing rev = '+repr(rev)) if rev: body_js['_rev'] = rev body = json.dumps(body_js) headers = {'content-type':self.environ['CONTENT_TYPE']} resp, content = self.h.request(join(self.remotedb, docid), "PUT", body=body, headers=headers) if logger: logger.debug('resp ' + repr((content[:100], resp))) self.prep_slave_response(resp) if not (self.resp_status.startswith('2') or self.resp_status.startswith('304')): if logger: logger.debug("Error looking up resource: %s: %s\n" % (content, self.resp_status)) return '' #No resource could be retrieved return content
def wiki_uri(original_base, wrapped_base, link, relative_to=None, raw=False): ''' Constructs absolute URLs to the original and REST-wrapper for a page, given a link from another page original_base - The base URI of the actual Moin instance wrapped_base - The base URI of the REST-wrapped proxy of the Moin instance link - the relative link, generally from one wiki page to another relative_to - the REST-wrapped version of the page from which the relative link came, defaults to same as wrapped_base raw - the link is a full hierarchical path, rather than relative to the wiki base Returns a tuple (wrapped_uri, abs_link) wrapped_uri - the URI wrapped for REST ops abs_link - the full, original wiki URL >>> from akara.util.moin import wiki_uri >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/spam') ('http://localhost:8880/moin/w/spam', 'http://example.com/mywiki/spam') >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://google.com/spam') (None, None) >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', 'http://google.com/spam', raw=True) (None, None) >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/mywiki/spam', raw=True) ('http://localhost:8880/moin/w/spam', 'http://example.com/mywiki/spam') >>> wiki_uri('http://example.com/mywiki/', 'http://localhost:8880/moin/w/', '/mywiki/spam') ('http://localhost:8880/moin/w/mywiki/spam', 'http://example.com/mywiki/mywiki/spam') ''' #rel_link = relativize(abs_link, original_wiki_base) #e.g. original wiki base is http://myhost:8080/mywiki/ and link is /a/b #abs_link is http://myhost:8080/mywiki/a/b note the need to strip the leading / to get that #from akara import logger; logger.debug('wiki_uri' + repr((original_base, wrapped_base, link, relative_to, absolutize(link, original_base.rstrip('/')+'/')))) if raw and not is_absolute(link): (scheme, authority, path, query, fragment) = split_uri_ref(original_base) link = link[len(path):] link = link.lstrip('/') abs_link = absolutize(link, original_base.rstrip('/')+'/') rel_to_wikibase = relativize(abs_link, original_base.rstrip('/')+'/') if not rel_to_wikibase: #It's not a relative wiki link return None, None rest_uri = absolutize(rel_to_wikibase, wrapped_base.rstrip('/')+'/') return rest_uri, abs_link
def delete_resource(self, path=None): ''' Delete a resource based on WSGI environment or a uri path ''' if path: docid = path if is_absolute(path): docid = relativize(path, self.remotedb) else: docid = self.environ['PATH_INFO'].lstrip('/').rsplit(self.space_tag, 1)[1].lstrip('/') #e.g. '/mydb/MyDoc' -> 'MyDoc' if logger: logger.debug('query ' + repr((self.remotedb, docid, join(self.remotedb, docid)))) resp, content = self.h.request(join(self.remotedb, docid), "DELETE")#, headers=headers) if logger: logger.debug('resp ' + repr((content[:100], resp))) self.prep_slave_response(resp) if not (self.resp_status.startswith('2') or self.resp_status.startswith('304')): if logger: logger.debug("Error looking up resource: %s: %s\n" % (content, self.resp_status)) return '' #No resource could be retrieved return content
def resource_factory(self, path=None): ''' Look up and retrieve a new resource based on WSGI environment or a uri path ''' if path: docid = path if is_absolute(path): docid = relativize(path, self.remotedb) else: docid = self.environ['PATH_INFO'].lstrip('/').rsplit(self.space_tag, 1)[1].lstrip('/') #e.g. '/mydb/MyDoc' -> 'MyDoc' #resp, content = self.h.request(slave_uri + ';history', "GET", headers=auth_headers) if logger: logger.debug('query ' + repr((self.remotedb, docid, join(self.remotedb, docid)))) resp, content = self.h.request(join(self.remotedb, urllib.quote_plus(docid))) if logger: logger.debug('resp ' + repr((content[:100], resp))) self.prep_slave_response(resp) if not (self.resp_status.startswith('2') or self.resp_status.startswith('304')): if logger: logger.debug("Error looking up resource: %s: %s\n" % (content, self.resp_status)) return '' #No resource could be retrieved data = json.loads(content) return resource.factory(self, docid, data)
logger.debug('Return from urllib2.opener') doc = htmlparse(resp) raise_embedded_error(doc) logger.debug('HTML parse complete post urllib2.opener') except urllib2.URLError,e: raise UnexpectedResponseError(url=url,code=e.code,error=str(e)) wrapped_url = join(wrapped_wiki_base, page) msg = 'Page updated OK: %s (%s)'%(url, wrapped_url) #response.add_header("Content-Length", str(len(msg))) moin_base_info = base + ' ' + wrapped_wiki_base + ' ' + original_page headers = [ ("Content-Type", "text/plain"), ("Content-Location", wrapped_url), (moin.ORIG_BASE_HEADER, moin_base_info), (moin.WIKI_RELATIVE_HEADER, relativize(wrapped_url, wrapped_wiki_base)), ] start_response(status_response(httplib.CREATED), headers) return [msg] # POST handler @dispatcher.method("POST") def post_page(environ, start_response): ''' Attachments use URI path params (for a bit of discussion see http://groups.google.com/group/comp.lang.python/browse_thread/thread/4662d41aca276d99) ''' #ctype = environ.get('CONTENT_TYPE', 'application/unknown') req_headers = copy_headers_to_dict(environ,exclude=['HTTP_ACCEPT_ENCODING'])
logger.debug('Return from urllib2.opener') doc = htmlparse(resp) raise_embedded_error(doc) logger.debug('HTML parse complete post urllib2.opener') except urllib2.URLError, e: raise UnexpectedResponseError(url=url, code=e.code, error=str(e)) wrapped_url = join(wrapped_wiki_base, page) msg = 'Page updated OK: %s (%s)' % (url, wrapped_url) #response.add_header("Content-Length", str(len(msg))) moin_base_info = base + ' ' + wrapped_wiki_base + ' ' + original_page headers = [ ("Content-Type", "text/plain"), ("Content-Location", wrapped_url), (moin.ORIG_BASE_HEADER, moin_base_info), (moin.WIKI_RELATIVE_HEADER, relativize(wrapped_url, wrapped_wiki_base)), ] start_response(status_response(httplib.CREATED), headers) return [msg] # POST handler @dispatcher.method("POST") def post_page(environ, start_response): ''' Attachments use URI path params (for a bit of discussion see http://groups.google.com/group/comp.lang.python/browse_thread/thread/4662d41aca276d99) ''' #ctype = environ.get('CONTENT_TYPE', 'application/unknown') req_headers = copy_headers_to_dict(environ,