1 | import re |
---|
2 | |
---|
3 | from DateTime import DateTime |
---|
4 | |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | import Products.qPloneGoogleSitemaps.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 | url = ob.getURL() |
---|
24 | for operator, what, with in operations: |
---|
25 | url = OPERATORS[operator](url, what, with.replace("\\", "")) |
---|
26 | #TODO: Remove or replace following condition |
---|
27 | #it is senseless in the case we need intelligent |
---|
28 | #result set. Better condition would be to place |
---|
29 | #freshest brain into result |
---|
30 | if url in result.keys(): |
---|
31 | continue |
---|
32 | #TODO: replace brain with only data necessary to |
---|
33 | #generate sitemap |
---|
34 | result[url]=ob |
---|
35 | return result |
---|
36 | |
---|
37 | def additionalURLs(context): |
---|
38 | """Add URLs to sitemap that arn't objects""" |
---|
39 | res = [] |
---|
40 | |
---|
41 | plone_home = getToolByName(context, 'portal_url')() |
---|
42 | root = context.getPhysicalRoot().absolute_url() |
---|
43 | URLs = context.getUrls() |
---|
44 | |
---|
45 | for url in URLs: |
---|
46 | if ADD_ZOPE.match(url): |
---|
47 | res.append(root+url) |
---|
48 | elif ADD_PLONE.match(url): |
---|
49 | res.append(plone_home+'/'+url) |
---|
50 | else: |
---|
51 | res.append(url) |
---|
52 | return res |
---|