[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 | |
---|
[3478] | 13 | |
---|
[2181] | 14 | def searchAndReplace(string, what, withs): |
---|
[1593] | 15 | """Emulate sed command s/""" |
---|
[3478] | 16 | #TODO: Better solution would be to compile |
---|
| 17 | #'what' and 'withs' |
---|
[3152] | 18 | res = re.sub(what, withs, string) |
---|
[1593] | 19 | return res |
---|
[3152] | 20 | OPERATORS = {'s': searchAndReplace, } |
---|
[1593] | 21 | |
---|
[3478] | 22 | def urlFilter(url, operations): |
---|
| 23 | """ Method dedicated to filter url according with operations """ |
---|
| 24 | for operator, what, withs in operations: |
---|
| 25 | url = OPERATORS[operator](url, what, withs.replace("\\", "")) |
---|
| 26 | return url |
---|
[3152] | 27 | |
---|
[3478] | 28 | def getUrlsObjects(objects): |
---|
| 29 | """Method dedicated to get url for each object""" |
---|
| 30 | results = set() |
---|
[1593] | 31 | for ob in objects: |
---|
[1726] | 32 | url = _marker |
---|
[3399] | 33 | if hasattr(ob, 'canonical_link'): |
---|
[2169] | 34 | url = ob.canonical_link |
---|
[1726] | 35 | if url in [Missing_Value, _marker]: |
---|
[2169] | 36 | url = ICanonicalLink(ob.getObject()).canonical_link |
---|
[1593] | 37 | #TODO: Remove or replace following condition |
---|
| 38 | #it is senseless in the case we need intelligent |
---|
| 39 | #result set. Better condition would be to place |
---|
| 40 | #freshest brain into result |
---|
[3478] | 41 | if url in results: |
---|
[1593] | 42 | continue |
---|
[3152] | 43 | #TODO: replace brain with only data necessary to |
---|
[1593] | 44 | #generate sitemap |
---|
[3478] | 45 | results.add(url) |
---|
| 46 | yield url, ob |
---|
| 47 | |
---|
| 48 | def applyOperations(objects, operations): |
---|
| 49 | """Parse Operations""" |
---|
| 50 | operations = [OPERATIONS_PARSE.match(op).groups() for op in operations] |
---|
| 51 | result = {} |
---|
| 52 | for url, ob in getUrlsObjects(objects): |
---|
| 53 | url = urlFilter(url, operations) |
---|
[3152] | 54 | result[url] = ob |
---|
[1593] | 55 | return result |
---|
| 56 | |
---|
| 57 | def additionalURLs(context): |
---|
| 58 | """Add URLs to sitemap that arn't objects""" |
---|
| 59 | res = [] |
---|
| 60 | |
---|
| 61 | plone_home = getToolByName(context, 'portal_url')() |
---|
| 62 | root = context.getPhysicalRoot().absolute_url() |
---|
[3152] | 63 | URLs = context.getUrls() |
---|
[1593] | 64 | |
---|
| 65 | for url in URLs: |
---|
| 66 | if ADD_ZOPE.match(url): |
---|
[3152] | 67 | res.append(root + url) |
---|
[1593] | 68 | elif ADD_PLONE.match(url): |
---|
[3152] | 69 | res.append(plone_home + '/' + url) |
---|
[1593] | 70 | else: |
---|
| 71 | res.append(url) |
---|
| 72 | return res |
---|