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, 8 years ago

Rewrote code according with pep8

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1import re
2
3from Missing import MV as Missing_Value
4from Products.CMFCore.utils import getToolByName
5
6from quintagroup.canonicalpath.interfaces import ICanonicalLink
7
8ADD_ZOPE = re.compile('^/')
9ADD_PLONE = re.compile('^[^http://|https://|\\\]')
10OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/")
11_marker = []
12
13
14def searchAndReplace(string, what, withs):
15    """Emulate sed command s/"""
16    res = re.sub(what, withs, string)
17    return res
18OPERATORS = {'s': searchAndReplace, }
19
20
21def applyOperations(objects, operations):
22    """Parse Operations """
23    operations = [OPERATIONS_PARSE.match(op).groups() for op in operations]
24    result = {}
25    for ob in objects:
26        url = _marker
27        if hasattr(ob, 'canonical_link'):
28            url = ob.canonical_link
29        if url in [Missing_Value, _marker]:
30            url = ICanonicalLink(ob.getObject()).canonical_link
31        for operator, what, withs in operations:
32            url = OPERATORS[operator](url, what, withs.replace("\\", ""))
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
39        #TODO: replace brain with only data necessary to
40        #generate sitemap
41        result[url] = ob
42    return result
43
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.