source: products/quintagroup.plonegooglesitemaps/tags/1.1/quintagroup/plonegooglesitemaps/browser/utils.py @ 1731

Last change on this file since 1731 was 1726, checked in by mylan, 14 years ago

Updated getting canonical_path data for sitemap record (use ICacnonicalPath adapter as fallback)

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