source: products/quintagroup.plonegooglesitemaps/branches/1.7.1/quintagroup/plonegooglesitemaps/browser/utils.py @ 3399

Last change on this file since 3399 was 3399, checked in by potar, 12 years ago

Rewrote code according with pep8

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