source: products/qPloneGoogleSitemaps/branches/contenttype/browser/utils.py

Last change on this file was 402, checked in by crchemist, 14 years ago

All tests passed.

  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
1import re
2
3from DateTime import DateTime
4
5from Products.CMFCore.utils import getToolByName
6import Products.qPloneGoogleSitemaps.config as config
7
8ADD_ZOPE = re.compile('^/')
9ADD_PLONE = re.compile('^[^http://|https://|\\\]')
10OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/")
11
12def searchAndReplace(string, what, with):
13    """Emulate sed command s/"""
14    res = re.sub(what,with,string)
15    return res
16OPERATORS = {'s': searchAndReplace,}
17
18def applyOperations(objects, operations):
19    """Parse Operations """
20    operations=[OPERATIONS_PARSE.match(op).groups() for op in operations]
21    result = {}
22    for ob in objects:
23        url = ob.getURL()
24        for operator, what, with in operations:
25            url = OPERATORS[operator](url, what, with.replace("\\", ""))
26        #TODO: Remove or replace following condition
27        #it is senseless in the case we need intelligent
28        #result set. Better condition would be to place
29        #freshest brain into result
30        if url in result.keys():
31            continue
32        #TODO: replace brain with only data necessary to
33        #generate sitemap
34        result[url]=ob
35    return result
36
37def additionalURLs(context):
38    """Add URLs to sitemap that arn't objects"""
39    res = []
40
41    plone_home = getToolByName(context, 'portal_url')()
42    root = context.getPhysicalRoot().absolute_url()
43    URLs =  context.getUrls()
44
45    for url in URLs:
46        if ADD_ZOPE.match(url):
47            res.append(root+url)
48        elif ADD_PLONE.match(url):
49            res.append(plone_home+'/'+url)
50        else:
51            res.append(url)
52    return res
Note: See TracBrowser for help on using the repository browser.