[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 | |
---|
[2181] | 16 | def searchAndReplace(string, what, withs): |
---|
[1593] | 17 | """Emulate sed command s/""" |
---|
[2181] | 18 | res = re.sub(what,withs,string) |
---|
[1593] | 19 | return res |
---|
| 20 | OPERATORS = {'s': searchAndReplace,} |
---|
| 21 | |
---|
| 22 | def applyOperations(objects, operations): |
---|
| 23 | """Parse Operations """ |
---|
| 24 | operations=[OPERATIONS_PARSE.match(op).groups() for op in operations] |
---|
| 25 | result = {} |
---|
| 26 | for ob in objects: |
---|
[1726] | 27 | url = _marker |
---|
[2169] | 28 | if ob.has_key('canonical_link'): |
---|
| 29 | url = ob.canonical_link |
---|
[1726] | 30 | if url in [Missing_Value, _marker]: |
---|
[2169] | 31 | url = ICanonicalLink(ob.getObject()).canonical_link |
---|
[2181] | 32 | for operator, what, withs in operations: |
---|
| 33 | url = OPERATORS[operator](url, what, withs.replace("\\", "")) |
---|
[1593] | 34 | #TODO: Remove or replace following condition |
---|
| 35 | #it is senseless in the case we need intelligent |
---|
| 36 | #result set. Better condition would be to place |
---|
| 37 | #freshest brain into result |
---|
| 38 | if url in result.keys(): |
---|
| 39 | continue |
---|
| 40 | #TODO: replace brain with only data necessary to |
---|
| 41 | #generate sitemap |
---|
| 42 | result[url]=ob |
---|
| 43 | return result |
---|
| 44 | |
---|
| 45 | def 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 |
---|