source: products/quintagroup.plonegooglesitemaps/branches/1.1.2/quintagroup/plonegooglesitemaps/browser/utils.py @ 2202

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

#199: Fix bug, tested with plone-3.x

  • Property svn:eol-style set to native
File size: 1.9 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            url = ICanonicalPath(ob.getObject()).canonical_path
32        for operator, what, with in operations:
33            url = OPERATORS[operator](url, what, with.replace("\\", ""))
34        #TODO: Remove or replace following condition
35        #it is senseless in the case we need intelligent
36        #result set. Better condition would be to place
37        #freshest brain into result
38        if url in result.keys():
39            continue
40        #TODO: replace brain with only data necessary to
41        #generate sitemap
42        result[url]=ob
43    return result
44
45def additionalURLs(context):
46    """Add URLs to sitemap that arn't objects"""
47    res = []
48
49    plone_home = getToolByName(context, 'portal_url')()
50    root = context.getPhysicalRoot().absolute_url()
51    URLs =  context.getUrls()
52
53    for url in URLs:
54        if ADD_ZOPE.match(url):
55            res.append(root+url)
56        elif ADD_PLONE.match(url):
57            res.append(plone_home+'/'+url)
58        else:
59            res.append(url)
60    return res
Note: See TracBrowser for help on using the repository browser.