[1593] | 1 | import re |
---|
| 2 | |
---|
| 3 | from DateTime import DateTime |
---|
| 4 | |
---|
| 5 | from Products.CMFCore.utils import getToolByName |
---|
| 6 | import quintagroup.plonegooglesitemaps.config as config |
---|
| 7 | |
---|
| 8 | ADD_ZOPE = re.compile('^/') |
---|
| 9 | ADD_PLONE = re.compile('^[^http://|https://|\\\]') |
---|
| 10 | OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/") |
---|
| 11 | |
---|
| 12 | def searchAndReplace(string, what, with): |
---|
| 13 | """Emulate sed command s/""" |
---|
| 14 | res = re.sub(what,with,string) |
---|
| 15 | return res |
---|
| 16 | OPERATORS = {'s': searchAndReplace,} |
---|
| 17 | |
---|
| 18 | def applyOperations(objects, operations): |
---|
| 19 | """Parse Operations """ |
---|
| 20 | operations=[OPERATIONS_PARSE.match(op).groups() for op in operations] |
---|
| 21 | result = {} |
---|
| 22 | for ob in objects: |
---|
| 23 | if ob.has_key('canonical_path'): |
---|
| 24 | url = ob.canonical_path |
---|
| 25 | else: |
---|
| 26 | url = '/'+'/'.join(ob.getPath().split('/')[2:]) |
---|
| 27 | for operator, what, with in operations: |
---|
| 28 | url = OPERATORS[operator](url, what, with.replace("\\", "")) |
---|
| 29 | #TODO: Remove or replace following condition |
---|
| 30 | #it is senseless in the case we need intelligent |
---|
| 31 | #result set. Better condition would be to place |
---|
| 32 | #freshest brain into result |
---|
| 33 | if url in result.keys(): |
---|
| 34 | continue |
---|
| 35 | #TODO: replace brain with only data necessary to |
---|
| 36 | #generate sitemap |
---|
| 37 | result[url]=ob |
---|
| 38 | return result |
---|
| 39 | |
---|
| 40 | def additionalURLs(context): |
---|
| 41 | """Add URLs to sitemap that arn't objects""" |
---|
| 42 | res = [] |
---|
| 43 | |
---|
| 44 | plone_home = getToolByName(context, 'portal_url')() |
---|
| 45 | root = context.getPhysicalRoot().absolute_url() |
---|
| 46 | URLs = context.getUrls() |
---|
| 47 | |
---|
| 48 | for url in URLs: |
---|
| 49 | if ADD_ZOPE.match(url): |
---|
| 50 | res.append(root+url) |
---|
| 51 | elif ADD_PLONE.match(url): |
---|
| 52 | res.append(plone_home+'/'+url) |
---|
| 53 | else: |
---|
| 54 | res.append(url) |
---|
| 55 | return res |
---|