1 | import re |
---|
2 | |
---|
3 | from Missing import MV as Missing_Value |
---|
4 | from Products.CMFCore.utils import getToolByName |
---|
5 | |
---|
6 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
7 | |
---|
8 | ADD_ZOPE = re.compile('^/') |
---|
9 | ADD_PLONE = re.compile('^[^http://|https://|\\\]') |
---|
10 | OPERATIONS_PARSE = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/") |
---|
11 | _marker = [] |
---|
12 | |
---|
13 | |
---|
14 | def searchAndReplace(string, what, withs): |
---|
15 | """Emulate sed command s/""" |
---|
16 | #TODO: Better solution would be to compile |
---|
17 | #'what' and 'withs' |
---|
18 | res = re.sub(what, withs, string) |
---|
19 | return res |
---|
20 | OPERATORS = {'s': searchAndReplace, } |
---|
21 | |
---|
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 |
---|
27 | |
---|
28 | def getUrlsObjects(objects): |
---|
29 | """Method dedicated to get url for each object""" |
---|
30 | results = set() |
---|
31 | for ob in objects: |
---|
32 | url = _marker |
---|
33 | if hasattr(ob, 'canonical_link'): |
---|
34 | url = ob.canonical_link |
---|
35 | if url in [Missing_Value, _marker]: |
---|
36 | url = ICanonicalLink(ob.getObject()).canonical_link |
---|
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 |
---|
41 | if url in results: |
---|
42 | continue |
---|
43 | #TODO: replace brain with only data necessary to |
---|
44 | #generate sitemap |
---|
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) |
---|
54 | result[url] = ob |
---|
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() |
---|
63 | URLs = context.getUrls() |
---|
64 | |
---|
65 | for url in URLs: |
---|
66 | if ADD_ZOPE.match(url): |
---|
67 | res.append(root + url) |
---|
68 | elif ADD_PLONE.match(url): |
---|
69 | res.append(plone_home + '/' + url) |
---|
70 | else: |
---|
71 | res.append(url) |
---|
72 | return res |
---|