source: products/quintagroup.plonegooglesitemaps/branches/two_request/utils.py @ 3478

Last change on this file since 3478 was 3478, checked in by potar, 7 years ago

added urlFilter; rewrote code

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1import re
2
3from Missing import MV as Missing_Value
4from Products.CMFCore.utils import getToolByName
5
6from quintagroup.canonicalpath.interfaces import ICanonicalLink
7
8ADD_ZOPE = re.compile('^/')
9ADD_PLONE = re.compile('^[^http://|https://|\\\]')
10OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/")
11_marker = []
12
13   
14def searchAndReplace(string, what, withs):
15    """Emulate sed command s/"""
16    #TODO: Better solution would be to compile
17    #'what' and 'withs'
18    res = re.sub(what, withs, string)
19    return res
20OPERATORS = {'s': searchAndReplace, }
21
22def urlFilter(url, operations):
23    """ Method dedicated to filter url according with operations """
24    for operator, what, withs in operations:
25        url = OPERATORS[operator](url, what, withs.replace("\\", ""))
26    return url
27
28def getUrlsObjects(objects):
29    """Method dedicated to get url for each object"""
30    results = set()
31    for ob in objects:
32        url = _marker
33        if hasattr(ob, 'canonical_link'):
34            url = ob.canonical_link
35        if url in [Missing_Value, _marker]:
36            url = ICanonicalLink(ob.getObject()).canonical_link
37        #TODO: Remove or replace following condition
38        #it is senseless in the case we need intelligent
39        #result set. Better condition would be to place
40        #freshest brain into result
41        if url in results:
42            continue
43        #TODO: replace brain with only data necessary to
44        #generate sitemap
45        results.add(url)
46        yield url, ob
47
48def applyOperations(objects, operations):
49    """Parse Operations"""
50    operations = [OPERATIONS_PARSE.match(op).groups() for op in operations]
51    result = {}
52    for url, ob in getUrlsObjects(objects):
53        url = urlFilter(url, operations)
54        result[url] = ob
55    return result
56
57def additionalURLs(context):
58    """Add URLs to sitemap that arn't objects"""
59    res = []
60
61    plone_home = getToolByName(context, 'portal_url')()
62    root = context.getPhysicalRoot().absolute_url()
63    URLs = context.getUrls()
64
65    for url in URLs:
66        if ADD_ZOPE.match(url):
67            res.append(root + url)
68        elif ADD_PLONE.match(url):
69            res.append(plone_home + '/' + url)
70        else:
71            res.append(url)
72    return res
Note: See TracBrowser for help on using the repository browser.