source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/browser/utils.py @ 3152

Last change on this file since 3152 was 3152, checked in by zidane, 13 years ago

fixes pep8

  • 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 ICanonicalLink
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
16
17def searchAndReplace(string, what, withs):
18    """Emulate sed command s/"""
19    res = re.sub(what, withs, string)
20    return res
21OPERATORS = {'s': searchAndReplace, }
22
23
24def applyOperations(objects, operations):
25    """Parse Operations """
26    operations = [OPERATIONS_PARSE.match(op).groups() for op in operations]
27    result = {}
28    for ob in objects:
29        url = _marker
30        if ob.has_key('canonical_link'):
31            url = ob.canonical_link
32        if url in [Missing_Value, _marker]:
33            url = ICanonicalLink(ob.getObject()).canonical_link
34        for operator, what, withs in operations:
35            url = OPERATORS[operator](url, what, withs.replace("\\", ""))
36        #TODO: Remove or replace following condition
37        #it is senseless in the case we need intelligent
38        #result set. Better condition would be to place
39        #freshest brain into result
40        if url in result.keys():
41            continue
42        #TODO: replace brain with only data necessary to
43        #generate sitemap
44        result[url] = ob
45    return result
46
47
48def additionalURLs(context):
49    """Add URLs to sitemap that arn't objects"""
50    res = []
51
52    plone_home = getToolByName(context, 'portal_url')()
53    root = context.getPhysicalRoot().absolute_url()
54    URLs = context.getUrls()
55
56    for url in URLs:
57        if ADD_ZOPE.match(url):
58            res.append(root + url)
59        elif ADD_PLONE.match(url):
60            res.append(plone_home + '/' + url)
61        else:
62            res.append(url)
63    return res
Note: See TracBrowser for help on using the repository browser.