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