source: products/quintagroup.canonicalpath/trunk/quintagroup/canonicalpath/upgrades.py @ 3599

Last change on this file since 3599 was 3599, checked in by vmaksymiv, 7 years ago

PPP fixes

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1from types import StringTypes
2from logging import NOTSET, DEBUG, INFO, ERROR
3from logging import Logger, StreamHandler, Formatter
4from StringIO import StringIO
5from zope.component import getAdapter
6from Acquisition import aq_base, aq_inner
7
8from quintagroup.canonicalpath.interfaces import ICanonicalPath
9from quintagroup.canonicalpath.interfaces import ICanonicalLink
10from quintagroup.canonicalpath.adapters import PROPERTY_LINK
11from quintagroup.canonicalpath.adapters import PROPERTY_PATH
12from quintagroup.canonicalpath.adapters import DefaultPropertyAdapter
13
14
15class CanonicalConvertor(object):
16    """Convert canonical link to canonical path and vice versa."""
17
18    def __init__(self, portal_url):
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        """
24        self._initLogger()
25        self.portal_url = portal_url
26
27    # General APIs
28    def getLogs(self):
29        self._inout.flush()
30        return self._inout.getvalue()
31
32    def cleanupLogs(self):
33        self._inout = StringIO()
34
35    def convertILinkToPath(self, obj):
36        """Convert canonical link to canonical path"""
37        return self._convert(obj, ICanonicalLink, ICanonicalPath,
38                             self._convertL2P)
39
40    def convertIPathToLink(self, obj):
41        """Convert canonical path to canonical link"""
42        return self._convert(obj, ICanonicalPath, ICanonicalLink,
43                             self._convertP2L)
44
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
59    # Service methods
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        """
67        src_msg = type(src_iface) in StringTypes and src_iface \
68            or src_iface.__name__
69        msg = "Migrate %s into %s for %s object: " \
70            % (src_msg, dst_iface.__name__, obj.absolute_url())
71        try:
72            src = self._getOrMakeAdapter(obj, src_iface)
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)
87
88        return lev == INFO and True or False
89
90    def _getOrMakeAdapter(self, obj, arg):
91        """Function return adapter for process of the property.
92           Adapter get by interface(if arg is not a string - interface assumed)
93           OR if arg is string - adapter created from DefaultCanonicalAdapter.
94        """
95        if type(arg) in StringTypes:
96            adapter = DefaultPropertyAdapter(obj)
97            adapter.prop = arg
98            return adapter
99        else:
100            return getAdapter(obj, arg)
101
102    def _convertP2L(self, src, dst):
103        """Convert canonical path to canonical link."""
104        cpath = src.getProp()
105        cpath = cpath.startswith('/') and cpath or '/' + cpath
106        dst.canonical_link = self.portal_url + cpath
107        src.delProp()
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
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",
119                              datefmt="%H:%M:%S")
120        handler.setFormatter(formatter)
121        self._logger = Logger("quintagroup.canonicalpath", NOTSET)
122        self._logger.addHandler(handler)
Note: See TracBrowser for help on using the repository browser.