[2368] | 1 | from types import StringTypes |
---|
[2365] | 2 | from logging import NOTSET, DEBUG, INFO, ERROR |
---|
| 3 | from logging import Logger, StreamHandler, Formatter |
---|
| 4 | from StringIO import StringIO |
---|
| 5 | from zope.component import getAdapter |
---|
[2362] | 6 | from Acquisition import aq_base, aq_inner |
---|
| 7 | |
---|
[3599] | 8 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
| 9 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
[2368] | 10 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK |
---|
| 11 | from quintagroup.canonicalpath.adapters import PROPERTY_PATH |
---|
| 12 | from quintagroup.canonicalpath.adapters import DefaultPropertyAdapter |
---|
[2362] | 13 | |
---|
[3145] | 14 | |
---|
[2362] | 15 | class CanonicalConvertor(object): |
---|
| 16 | """Convert canonical link to canonical path and vice versa.""" |
---|
| 17 | |
---|
[2365] | 18 | def __init__(self, portal_url): |
---|
[2362] | 19 | """Create instanse of convertor. |
---|
| 20 | - *portal_url* (string), add in the front of canonical path property |
---|
| 21 | value for get canonical link. |
---|
| 22 | - *logger_name* - name of the logger |
---|
| 23 | """ |
---|
[2365] | 24 | self._initLogger() |
---|
[2362] | 25 | self.portal_url = portal_url |
---|
| 26 | |
---|
[2371] | 27 | # General APIs |
---|
[2365] | 28 | def getLogs(self): |
---|
| 29 | self._inout.flush() |
---|
| 30 | return self._inout.getvalue() |
---|
[2362] | 31 | |
---|
[2365] | 32 | def cleanupLogs(self): |
---|
| 33 | self._inout = StringIO() |
---|
| 34 | |
---|
[2368] | 35 | def convertILinkToPath(self, obj): |
---|
[2362] | 36 | """Convert canonical link to canonical path""" |
---|
| 37 | return self._convert(obj, ICanonicalLink, ICanonicalPath, |
---|
| 38 | self._convertL2P) |
---|
| 39 | |
---|
[2368] | 40 | def convertIPathToLink(self, obj): |
---|
[2362] | 41 | """Convert canonical path to canonical link""" |
---|
| 42 | return self._convert(obj, ICanonicalPath, ICanonicalLink, |
---|
| 43 | self._convertP2L) |
---|
| 44 | |
---|
[2368] | 45 | def convertPLinkToPath(self, obj, prop=PROPERTY_LINK): |
---|
| 46 | """Convert canonical link, got from the *prop* parameter |
---|
| 47 | to canonical path. |
---|
| 48 | """ |
---|
| 49 | return self._convert(obj, prop, ICanonicalPath, |
---|
| 50 | self._convertL2P) |
---|
| 51 | |
---|
| 52 | def convertPPathToLink(self, obj, prop=PROPERTY_PATH): |
---|
| 53 | """Convert canonical path, got from the *prop* parameter |
---|
| 54 | to canonical link. |
---|
| 55 | """ |
---|
| 56 | return self._convert(obj, prop, ICanonicalLink, |
---|
| 57 | self._convertP2L) |
---|
| 58 | |
---|
[2371] | 59 | # Service methods |
---|
[2362] | 60 | def _convert(self, obj, src_iface, dst_iface, converter): |
---|
| 61 | """Convert canonical from source canonical interface |
---|
| 62 | to destination canonical interface. |
---|
| 63 | |
---|
| 64 | Return True is successfull, False otherwise. |
---|
| 65 | Log results in logger. |
---|
| 66 | """ |
---|
[3145] | 67 | src_msg = type(src_iface) in StringTypes and src_iface \ |
---|
[3599] | 68 | or src_iface.__name__ |
---|
[2365] | 69 | msg = "Migrate %s into %s for %s object: " \ |
---|
[3599] | 70 | % (src_msg, dst_iface.__name__, obj.absolute_url()) |
---|
[2365] | 71 | try: |
---|
[2370] | 72 | src = self._getOrMakeAdapter(obj, src_iface) |
---|
[2365] | 73 | dst = getAdapter(obj, dst_iface) |
---|
| 74 | # XXX: Check is this correct work XXX |
---|
| 75 | obj = aq_base(aq_inner(obj)) |
---|
| 76 | # XXX |
---|
| 77 | converter(src, dst) |
---|
| 78 | except Exception: |
---|
| 79 | import sys |
---|
| 80 | et, em, etr = map(str, sys.exc_info()) |
---|
| 81 | lev = ERROR |
---|
| 82 | msg += "ERROR: %s: %s" % (et, em) |
---|
| 83 | else: |
---|
| 84 | lev = INFO |
---|
| 85 | msg += "SUCCESS" |
---|
| 86 | self._logger.log(lev, msg) |
---|
[3145] | 87 | |
---|
[2365] | 88 | return lev == INFO and True or False |
---|
[3145] | 89 | |
---|
[2368] | 90 | def _getOrMakeAdapter(self, obj, arg): |
---|
| 91 | """Function return adapter for process of the property. |
---|
[3145] | 92 | Adapter get by interface(if arg is not a string - interface assumed) |
---|
[2368] | 93 | OR if arg is string - adapter created from DefaultCanonicalAdapter. |
---|
| 94 | """ |
---|
| 95 | if type(arg) in StringTypes: |
---|
| 96 | adapter = DefaultPropertyAdapter(obj) |
---|
[2371] | 97 | adapter.prop = arg |
---|
[2368] | 98 | return adapter |
---|
| 99 | else: |
---|
| 100 | return getAdapter(obj, arg) |
---|
| 101 | |
---|
[2362] | 102 | def _convertP2L(self, src, dst): |
---|
| 103 | """Convert canonical path to canonical link.""" |
---|
[2368] | 104 | cpath = src.getProp() |
---|
[2362] | 105 | cpath = cpath.startswith('/') and cpath or '/' + cpath |
---|
| 106 | dst.canonical_link = self.portal_url + cpath |
---|
[2368] | 107 | src.delProp() |
---|
[2362] | 108 | |
---|
| 109 | def _convertL2P(self, src, dst): |
---|
| 110 | """Convert canonical link to canonical path.""" |
---|
| 111 | raise NotImplementedError( |
---|
| 112 | "Convertion from canonical link to canonical path not implemented") |
---|
| 113 | |
---|
[2365] | 114 | def _initLogger(self): |
---|
| 115 | self._inout = StringIO() |
---|
| 116 | handler = StreamHandler(self._inout) |
---|
| 117 | handler.setLevel(DEBUG) |
---|
| 118 | formatter = Formatter(fmt="[%(asctime)s]: %(message)s", |
---|
[3599] | 119 | datefmt="%H:%M:%S") |
---|
[2365] | 120 | handler.setFormatter(formatter) |
---|
| 121 | self._logger = Logger("quintagroup.canonicalpath", NOTSET) |
---|
| 122 | self._logger.addHandler(handler) |
---|