1 | import re |
---|
2 | from zope.component import queryAdapter |
---|
3 | |
---|
4 | from DateTime import DateTime |
---|
5 | from Missing import MV as Missing_Value |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | |
---|
8 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
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"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/") |
---|
14 | _marker = [] |
---|
15 | |
---|
16 | def searchAndReplace(string, what, with): |
---|
17 | """Emulate sed command s/""" |
---|
18 | res = re.sub(what,with,string) |
---|
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: |
---|
27 | url = _marker |
---|
28 | if ob.has_key('canonical_path'): |
---|
29 | url = ob.canonical_path |
---|
30 | if url in [Missing_Value, _marker]: |
---|
31 | url = ICanonicalPath(ob.getObject()).canonical_path |
---|
32 | for operator, what, with in operations: |
---|
33 | url = OPERATORS[operator](url, what, with.replace("\\", "")) |
---|
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 |
---|