#!/usr/bin/env python
# $Id: report_client.py 174393 2018-03-13 16:40:03Z kbu $
#
# Copyright (C) 1990 - 2009 CONTACT Software GmbH
# All rights reserved.
# http://www.contact.de/
"""
report client implementation
"""

import io
import os
import pickle
import shutil
import traceback

from cdb import CADDOK
from cdb import cdbuuid
from cdb import misc
from cdb import mq
from cdb import ue

from cdb.objects import ByID
from cdb.objects.pdd import Sandbox
from cs.tools.powerreports import XMLReportTemplate
from cs.tools.powerreports.reportserver.report_proxy import ReportProxy
from cs.tools.powerreports.reportserver.reportlib import getConfValue, log

__all__ = ['ReportClient',
           'ReportClientRemote',
           'ReportClientMQ']


class ReportClient(object):

    def __init__(self, tmpl_cdb_object_id, cdb_file_cdb_object_id, **args):
        self.tmpl_cdb_object_id = tmpl_cdb_object_id
        self.cdb_file_cdb_object_id = cdb_file_cdb_object_id
        self.args = args
        self.sys_args = args.get("__sys_args__", None)
        if not self.sys_args:
            log("__sys_args__ missing in args. Cannot create report", 0, misc.kLogErr)

        self.sandbox_name = "report_client__%s" % (cdbuuid.create_uuid())

        # create an explicit sandbox to control the removal
        self.sandbox = Sandbox(self.sandbox_name)
        self.workdir = self.sandbox._location
        log("ReportClient.__init__(): sandbox is '%s'" % self.workdir)
        self.xml_fname = None

    def __del__(self):
        log("ReportClient.__del__ (base)")
        self._cleanup()

    def _get_xml_source(self, args):
        source = None
        if "source" in args:
            source = ByID(args["source"])
        else:
            log("ReportClient: XML Source not found in args.", 0, misc.kLogErr)
        return source

    def _cleanup(self):
        if self.sandbox:
            self.sandbox.clear()
            self.sandbox = None

    def _get_context_objects(self, args):
        objects = []
        if "objects" in args and args["objects"]:
            objects = [ByID(o) for o in args["objects"]]
        return objects

    def _export_xml_data(self):
        # Export data from xml source
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log("source missing in sys_args. Cannot create report.", 0, misc.kLogErr)
            return
        log("ReportClient: START EXPORT")
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
                                             self.tmpl_cdb_object_id,
                                             self.cdb_file_cdb_object_id, "0",
                                             **self.args)
        log("ReportClient: END EXPORT")
        return xml_fname

    def _checkout_template(self):
        tmpl = XMLReportTemplate.KeywordQuery(cdb_object_id=self.tmpl_cdb_object_id)
        if not tmpl:
            raise ue.Exception("powerreports_tmpl_not_found", self.tmpl_cdb_object_id)
        tmpl = tmpl[0]
        cdbfile = tmpl.Files[0]
        if not cdbfile:
            raise ue.Exception("powerreports_tmpl_file_not_found", self.cdb_file_cdb_object_id)

        self.sandbox.add(cdbfile)
        if self.sandbox.status(cdbfile) == Sandbox.NEEDS_UPDATE:
            self.sandbox.checkout(cdbfile)

        template_fname = self.sandbox.pathname(cdbfile)
        log("ReportClient: Checked out report template: %s" % template_fname)

        return template_fname

    def _pickle_args(self, args_basename):
        args_fname = misc.jail_filename(self.workdir, args_basename)
        assert isinstance(args_fname, unicode)
        with io.open(args_fname, "wb") as f:
            p = pickle.Pickler(f)
            p.dump(self.args)
        return args_fname

    # extraction of report template and report data
    def _prepare_report(self):
        self.template_fname = self._checkout_template()
        self.xml_fname = self._export_xml_data()

    def _copy_results(self, dst_path, gen_ret):
        ret = gen_ret

        def _copy(src_fname, dst_path):
            dst_fname = os.path.join(dst_path, os.path.basename(src_fname))
            log("Copying '%s' to '%s'" % (src_fname, dst_fname))
            shutil.copyfile(src_fname, dst_fname)
            return dst_fname

        if dst_path:
            if not os.path.exists(dst_path):
                log("Destination path '%s' does not exist" % dst_path, 0, misc.kLogErr)
            else:
                report_format = self.sys_args["report_format"]
                if any(f in report_format for f in ["Excel", "E-Link"]):
                    ret["xls"] = _copy(gen_ret["xls"], dst_path)
                if any(f in report_format for f in ["PDF", "E-Link"]):
                    ret["pdf"] = _copy(gen_ret["pdf"], dst_path)
        return ret

    def create_report(self, target_path=None):
        # IMPORTANT: import of report_generator requires win32!
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {"status": "",
               "xls": None,
               "pdf": None}
        try:
            log("ReportRemote.create_report(): workdir: '%s', target_path '%s'" %
                (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(
                self.workdir, self.template_fname,
                self.xml_fname,
                self.sys_args.get("target",
                                  "%s Report" % Message.GetMessage("branding_product_name")),
                ("PDF" in self.sys_args["report_format"]))
            if ret["status"] == "OK":
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret["status"] in ["", "OK"]:
                ret["status"] = "%s" % ex
            log("%s" % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % (ret))
        return ret


class ReportClientRemote(ReportClient):
    def create_report(self, target_path=None):
        ret = {"status": "",
               "xls": None,
               "pdf": None}
        try:
            log("ReportClientRemote.create_report(): workdir: '%s', target_path '%s'" %
                (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.xml_fname, self.args)
            if ret["status"] == "OK":
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret["status"] in ["", "OK"]:
                ret["status"] = "%s" % ex
            log("%s" % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % (ret))
        return ret


class ReportClientMQ(ReportClient):
    def create_report(self):
        # extend args (filenames template and xml data)
        args_basename = "report_args"
        # set up mq job
        temp_queue_folder = getConfValue("REPORT_QUEUE_TEMPFOLDER", "report_queue_payload")
        queue = mq.Queue("xsd_reports",
                         payloaddir=os.path.join(CADDOK.TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        # prepare args
        args_fname = self._pickle_args(args_basename)
        # attach pickled args
        job.add_attachment(args_basename, args_fname)
        job.start()

bb08a0b6c131393c91d97d0ce843813df1030afb
<mask token>


class ReportClient(object):
    <mask token>

    def __del__(self):
        log('ReportClient.__del__ (base)')
        self._cleanup()
    <mask token>
    <mask token>
    <mask token>

    def _export_xml_data(self):
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log('source missing in sys_args. Cannot create report.', 0,
                misc.kLogErr)
            return
        log('ReportClient: START EXPORT')
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
            self.tmpl_cdb_object_id, self.cdb_file_cdb_object_id, '0', **
            self.args)
        log('ReportClient: END EXPORT')
        return xml_fname
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def create_report(self, target_path=None):
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(self.workdir, self.
                template_fname, self.xml_fname, self.sys_args.get('target',
                '%s Report' % Message.GetMessage('branding_product_name')),
                'PDF' in self.sys_args['report_format'])
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientRemote(ReportClient):

    def create_report(self, target_path=None):
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportClientRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.
                xml_fname, self.args)
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientMQ(ReportClient):

    def create_report(self):
        args_basename = 'report_args'
        temp_queue_folder = getConfValue('REPORT_QUEUE_TEMPFOLDER',
            'report_queue_payload')
        queue = mq.Queue('xsd_reports', payloaddir=os.path.join(CADDOK.
            TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        args_fname = self._pickle_args(args_basename)
        job.add_attachment(args_basename, args_fname)
        job.start()

<mask token>


class ReportClient(object):
    <mask token>

    def __del__(self):
        log('ReportClient.__del__ (base)')
        self._cleanup()
    <mask token>

    def _cleanup(self):
        if self.sandbox:
            self.sandbox.clear()
            self.sandbox = None

    def _get_context_objects(self, args):
        objects = []
        if 'objects' in args and args['objects']:
            objects = [ByID(o) for o in args['objects']]
        return objects

    def _export_xml_data(self):
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log('source missing in sys_args. Cannot create report.', 0,
                misc.kLogErr)
            return
        log('ReportClient: START EXPORT')
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
            self.tmpl_cdb_object_id, self.cdb_file_cdb_object_id, '0', **
            self.args)
        log('ReportClient: END EXPORT')
        return xml_fname
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def create_report(self, target_path=None):
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(self.workdir, self.
                template_fname, self.xml_fname, self.sys_args.get('target',
                '%s Report' % Message.GetMessage('branding_product_name')),
                'PDF' in self.sys_args['report_format'])
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientRemote(ReportClient):

    def create_report(self, target_path=None):
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportClientRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.
                xml_fname, self.args)
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientMQ(ReportClient):

    def create_report(self):
        args_basename = 'report_args'
        temp_queue_folder = getConfValue('REPORT_QUEUE_TEMPFOLDER',
            'report_queue_payload')
        queue = mq.Queue('xsd_reports', payloaddir=os.path.join(CADDOK.
            TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        args_fname = self._pickle_args(args_basename)
        job.add_attachment(args_basename, args_fname)
        job.start()

<mask token>


class ReportClient(object):
    <mask token>

    def __del__(self):
        log('ReportClient.__del__ (base)')
        self._cleanup()

    def _get_xml_source(self, args):
        source = None
        if 'source' in args:
            source = ByID(args['source'])
        else:
            log('ReportClient: XML Source not found in args.', 0, misc.kLogErr)
        return source

    def _cleanup(self):
        if self.sandbox:
            self.sandbox.clear()
            self.sandbox = None

    def _get_context_objects(self, args):
        objects = []
        if 'objects' in args and args['objects']:
            objects = [ByID(o) for o in args['objects']]
        return objects

    def _export_xml_data(self):
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log('source missing in sys_args. Cannot create report.', 0,
                misc.kLogErr)
            return
        log('ReportClient: START EXPORT')
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
            self.tmpl_cdb_object_id, self.cdb_file_cdb_object_id, '0', **
            self.args)
        log('ReportClient: END EXPORT')
        return xml_fname
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def create_report(self, target_path=None):
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(self.workdir, self.
                template_fname, self.xml_fname, self.sys_args.get('target',
                '%s Report' % Message.GetMessage('branding_product_name')),
                'PDF' in self.sys_args['report_format'])
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientRemote(ReportClient):

    def create_report(self, target_path=None):
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportClientRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.
                xml_fname, self.args)
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientMQ(ReportClient):

    def create_report(self):
        args_basename = 'report_args'
        temp_queue_folder = getConfValue('REPORT_QUEUE_TEMPFOLDER',
            'report_queue_payload')
        queue = mq.Queue('xsd_reports', payloaddir=os.path.join(CADDOK.
            TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        args_fname = self._pickle_args(args_basename)
        job.add_attachment(args_basename, args_fname)
        job.start()

<mask token>


class ReportClient(object):
    <mask token>

    def __del__(self):
        log('ReportClient.__del__ (base)')
        self._cleanup()

    def _get_xml_source(self, args):
        source = None
        if 'source' in args:
            source = ByID(args['source'])
        else:
            log('ReportClient: XML Source not found in args.', 0, misc.kLogErr)
        return source

    def _cleanup(self):
        if self.sandbox:
            self.sandbox.clear()
            self.sandbox = None

    def _get_context_objects(self, args):
        objects = []
        if 'objects' in args and args['objects']:
            objects = [ByID(o) for o in args['objects']]
        return objects

    def _export_xml_data(self):
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log('source missing in sys_args. Cannot create report.', 0,
                misc.kLogErr)
            return
        log('ReportClient: START EXPORT')
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
            self.tmpl_cdb_object_id, self.cdb_file_cdb_object_id, '0', **
            self.args)
        log('ReportClient: END EXPORT')
        return xml_fname
    <mask token>

    def _pickle_args(self, args_basename):
        args_fname = misc.jail_filename(self.workdir, args_basename)
        assert isinstance(args_fname, unicode)
        with io.open(args_fname, 'wb') as f:
            p = pickle.Pickler(f)
            p.dump(self.args)
        return args_fname
    <mask token>
    <mask token>

    def create_report(self, target_path=None):
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(self.workdir, self.
                template_fname, self.xml_fname, self.sys_args.get('target',
                '%s Report' % Message.GetMessage('branding_product_name')),
                'PDF' in self.sys_args['report_format'])
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientRemote(ReportClient):

    def create_report(self, target_path=None):
        ret = {'status': '', 'xls': None, 'pdf': None}
        try:
            log(
                "ReportClientRemote.create_report(): workdir: '%s', target_path '%s'"
                 % (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.
                xml_fname, self.args)
            if ret['status'] == 'OK':
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret['status'] in ['', 'OK']:
                ret['status'] = '%s' % ex
            log('%s' % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % ret)
        return ret


class ReportClientMQ(ReportClient):

    def create_report(self):
        args_basename = 'report_args'
        temp_queue_folder = getConfValue('REPORT_QUEUE_TEMPFOLDER',
            'report_queue_payload')
        queue = mq.Queue('xsd_reports', payloaddir=os.path.join(CADDOK.
            TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        args_fname = self._pickle_args(args_basename)
        job.add_attachment(args_basename, args_fname)
        job.start()

#!/usr/bin/env python
# $Id: report_client.py 174393 2018-03-13 16:40:03Z kbu $
#
# Copyright (C) 1990 - 2009 CONTACT Software GmbH
# All rights reserved.
# http://www.contact.de/
"""
report client implementation
"""

import io
import os
import pickle
import shutil
import traceback

from cdb import CADDOK
from cdb import cdbuuid
from cdb import misc
from cdb import mq
from cdb import ue

from cdb.objects import ByID
from cdb.objects.pdd import Sandbox
from cs.tools.powerreports import XMLReportTemplate
from cs.tools.powerreports.reportserver.report_proxy import ReportProxy
from cs.tools.powerreports.reportserver.reportlib import getConfValue, log

__all__ = ['ReportClient',
           'ReportClientRemote',
           'ReportClientMQ']


class ReportClient(object):

    def __init__(self, tmpl_cdb_object_id, cdb_file_cdb_object_id, **args):
        self.tmpl_cdb_object_id = tmpl_cdb_object_id
        self.cdb_file_cdb_object_id = cdb_file_cdb_object_id
        self.args = args
        self.sys_args = args.get("__sys_args__", None)
        if not self.sys_args:
            log("__sys_args__ missing in args. Cannot create report", 0, misc.kLogErr)

        self.sandbox_name = "report_client__%s" % (cdbuuid.create_uuid())

        # create an explicit sandbox to control the removal
        self.sandbox = Sandbox(self.sandbox_name)
        self.workdir = self.sandbox._location
        log("ReportClient.__init__(): sandbox is '%s'" % self.workdir)
        self.xml_fname = None

    def __del__(self):
        log("ReportClient.__del__ (base)")
        self._cleanup()

    def _get_xml_source(self, args):
        source = None
        if "source" in args:
            source = ByID(args["source"])
        else:
            log("ReportClient: XML Source not found in args.", 0, misc.kLogErr)
        return source

    def _cleanup(self):
        if self.sandbox:
            self.sandbox.clear()
            self.sandbox = None

    def _get_context_objects(self, args):
        objects = []
        if "objects" in args and args["objects"]:
            objects = [ByID(o) for o in args["objects"]]
        return objects

    def _export_xml_data(self):
        # Export data from xml source
        objects = self._get_context_objects(self.sys_args)
        xml_source_obj = self._get_xml_source(self.sys_args)
        if not xml_source_obj:
            log("source missing in sys_args. Cannot create report.", 0, misc.kLogErr)
            return
        log("ReportClient: START EXPORT")
        xml_fname = xml_source_obj.export_ex(objects, self.template_fname,
                                             self.tmpl_cdb_object_id,
                                             self.cdb_file_cdb_object_id, "0",
                                             **self.args)
        log("ReportClient: END EXPORT")
        return xml_fname

    def _checkout_template(self):
        tmpl = XMLReportTemplate.KeywordQuery(cdb_object_id=self.tmpl_cdb_object_id)
        if not tmpl:
            raise ue.Exception("powerreports_tmpl_not_found", self.tmpl_cdb_object_id)
        tmpl = tmpl[0]
        cdbfile = tmpl.Files[0]
        if not cdbfile:
            raise ue.Exception("powerreports_tmpl_file_not_found", self.cdb_file_cdb_object_id)

        self.sandbox.add(cdbfile)
        if self.sandbox.status(cdbfile) == Sandbox.NEEDS_UPDATE:
            self.sandbox.checkout(cdbfile)

        template_fname = self.sandbox.pathname(cdbfile)
        log("ReportClient: Checked out report template: %s" % template_fname)

        return template_fname

    def _pickle_args(self, args_basename):
        args_fname = misc.jail_filename(self.workdir, args_basename)
        assert isinstance(args_fname, unicode)
        with io.open(args_fname, "wb") as f:
            p = pickle.Pickler(f)
            p.dump(self.args)
        return args_fname

    # extraction of report template and report data
    def _prepare_report(self):
        self.template_fname = self._checkout_template()
        self.xml_fname = self._export_xml_data()

    def _copy_results(self, dst_path, gen_ret):
        ret = gen_ret

        def _copy(src_fname, dst_path):
            dst_fname = os.path.join(dst_path, os.path.basename(src_fname))
            log("Copying '%s' to '%s'" % (src_fname, dst_fname))
            shutil.copyfile(src_fname, dst_fname)
            return dst_fname

        if dst_path:
            if not os.path.exists(dst_path):
                log("Destination path '%s' does not exist" % dst_path, 0, misc.kLogErr)
            else:
                report_format = self.sys_args["report_format"]
                if any(f in report_format for f in ["Excel", "E-Link"]):
                    ret["xls"] = _copy(gen_ret["xls"], dst_path)
                if any(f in report_format for f in ["PDF", "E-Link"]):
                    ret["pdf"] = _copy(gen_ret["pdf"], dst_path)
        return ret

    def create_report(self, target_path=None):
        # IMPORTANT: import of report_generator requires win32!
        from cs.tools.powerreports.reportserver.report_generator import ReportGenerator
        ret = {"status": "",
               "xls": None,
               "pdf": None}
        try:
            log("ReportRemote.create_report(): workdir: '%s', target_path '%s'" %
                (self.workdir, target_path))
            self._prepare_report()
            ret = ReportGenerator().create_report(
                self.workdir, self.template_fname,
                self.xml_fname,
                self.sys_args.get("target",
                                  "%s Report" % Message.GetMessage("branding_product_name")),
                ("PDF" in self.sys_args["report_format"]))
            if ret["status"] == "OK":
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret["status"] in ["", "OK"]:
                ret["status"] = "%s" % ex
            log("%s" % traceback.format_exc())
        finally:
            log("ReportRemote.create_report(): result is '%s'" % (ret))
        return ret


class ReportClientRemote(ReportClient):
    def create_report(self, target_path=None):
        ret = {"status": "",
               "xls": None,
               "pdf": None}
        try:
            log("ReportClientRemote.create_report(): workdir: '%s', target_path '%s'" %
                (self.workdir, target_path))
            p = ReportProxy()
            self._prepare_report()
            ret = p.create(self.workdir, self.template_fname, self.xml_fname, self.args)
            if ret["status"] == "OK":
                ret = self._copy_results(target_path, ret)
        except Exception as ex:
            if ret["status"] in ["", "OK"]:
                ret["status"] = "%s" % ex
            log("%s" % traceback.format_exc())
        finally:
            log("ReportClientRemote.create_report(): result is '%s'" % (ret))
        return ret


class ReportClientMQ(ReportClient):
    def create_report(self):
        # extend args (filenames template and xml data)
        args_basename = "report_args"
        # set up mq job
        temp_queue_folder = getConfValue("REPORT_QUEUE_TEMPFOLDER", "report_queue_payload")
        queue = mq.Queue("xsd_reports",
                         payloaddir=os.path.join(CADDOK.TMPDIR, temp_queue_folder))
        job = queue.new(cdbf_object_id=self.tmpl_cdb_object_id)
        # prepare args
        args_fname = self._pickle_args(args_basename)
        # attach pickled args
        job.add_attachment(args_basename, args_fname)
        job.start()
