[1593] | 1 | import re |
---|
[1726] | 2 | from zope.component import queryAdapter |
---|
[1593] | 3 | |
---|
| 4 | from DateTime import DateTime |
---|
[1726] | 5 | from Missing import MV as Missing_Value |
---|
| 6 | from Products.CMFCore.utils import getToolByName |
---|
[1593] | 7 | |
---|
[2169] | 8 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
[1593] | 9 | import quintagroup.plonegooglesitemaps.config as config |
---|
| 10 | |
---|
| 11 | ADD_ZOPE = re.compile('^/') |
---|
| 12 | ADD_PLONE = re.compile('^[^http://|https://|\\\]') |
---|
| 13 | OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/") |
---|
[1726] | 14 | _marker = [] |
---|
[1593] | 15 | |
---|
[3152] | 16 | |
---|
[2181] | 17 | def searchAndReplace(string, what, withs): |
---|
[1593] | 18 | """Emulate sed command s/""" |
---|
[3152] | 19 | res = re.sub(what, withs, string) |
---|
[1593] | 20 | return res |
---|
[3152] | 21 | OPERATORS = {'s': searchAndReplace, } |
---|
[1593] | 22 | |
---|
[3152] | 23 | |
---|
[1593] | 24 | def 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] | 48 | def 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 |
---|