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
RevLine 
[1593]1import re
[1726]2from zope.component import queryAdapter
[1593]3
4from DateTime import DateTime
[1726]5from Missing import MV as Missing_Value
6from Products.CMFCore.utils import getToolByName
[1593]7
[2169]8from quintagroup.canonicalpath.interfaces import ICanonicalLink
[1593]9import quintagroup.plonegooglesitemaps.config as config
10
11ADD_ZOPE = re.compile('^/')
12ADD_PLONE = re.compile('^[^http://|https://|\\\]')
13OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/")
[1726]14_marker = []
[1593]15
[3152]16
[2181]17def searchAndReplace(string, what, withs):
[1593]18    """Emulate sed command s/"""
[3152]19    res = re.sub(what, withs, string)
[1593]20    return res
[3152]21OPERATORS = {'s': searchAndReplace, }
[1593]22
[3152]23
[1593]24def applyOperations(objects, operations):
25    """Parse Operations """
[3152]26    operations = [OPERATIONS_PARSE.match(op).groups() for op in operations]
[1593]27    result = {}
28    for ob in objects:
[1726]29        url = _marker
[2169]30        if ob.has_key('canonical_link'):
31            url = ob.canonical_link
[1726]32        if url in [Missing_Value, _marker]:
[2169]33            url = ICanonicalLink(ob.getObject()).canonical_link
[2181]34        for operator, what, withs in operations:
35            url = OPERATORS[operator](url, what, withs.replace("\\", ""))
[1593]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
[3152]42        #TODO: replace brain with only data necessary to
[1593]43        #generate sitemap
[3152]44        result[url] = ob
[1593]45    return result
46
[3152]47
[1593]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()
[3152]54    URLs = context.getUrls()
[1593]55
56    for url in URLs:
57        if ADD_ZOPE.match(url):
[3152]58            res.append(root + url)
[1593]59        elif ADD_PLONE.match(url):
[3152]60            res.append(plone_home + '/' + url)
[1593]61        else:
62            res.append(url)
63    return res
Note: See TracBrowser for help on using the repository browser.