~sylvain-pineau/checkbox/wrap_exporter

« back to all changes in this revision

Viewing changes to plainbox/plainbox/impl/service.py

  • Committer: Sylvain Pineau
  • Date: 2013-07-16 23:50:22 UTC
  • Revision ID: sylvain.pineau@canonical.com-20130716235022-4iuanu3lrxaa023f
impl:service: Added the Exporter Wrapper class

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
=========================================================
23
23
"""
24
24
 
 
25
from shutil import copyfileobj
25
26
import functools
 
27
import io
26
28
import itertools
27
29
import logging
28
30
 
46
48
SERVICE_IFACE = _BASE_IFACE + "PlainBox.Service1"
47
49
SESSION_IFACE = _BASE_IFACE + "PlainBox.Session1"
48
50
PROVIDER_IFACE = _BASE_IFACE + "PlainBox.Provider1"
 
51
EXPORTER_IFACE = _BASE_IFACE + "PlainBox.Exporter1"
49
52
JOB_IFACE = _BASE_IFACE + "PlainBox.JobDefinition1"
50
53
JOB_RESULT_IFACE = _BASE_IFACE + "PlainBox.Result1"
51
54
JOB_STATE_IFACE = _BASE_IFACE + "PlainBox.JobState1"
623
626
    # TODO: setter<metadata>
624
627
 
625
628
 
 
629
class SessionStateExporterWrapper(PlainBoxObjectWrapper):
 
630
    """
 
631
    Wrapper for exposing SessionStateExporter objects on DBus
 
632
    """
 
633
 
 
634
    HIDDEN_INTERFACES = frozenset()
 
635
 
 
636
    def __shared_initialize__(self, **kwargs):
 
637
        self._name = kwargs.get('name')
 
638
        self._cls = kwargs.get('cls')
 
639
        self._option_list = []
 
640
        self._output_file = None
 
641
        self._data_subset = None
 
642
        self._exporter = None
 
643
 
 
644
    def _get_preferred_object_path(self):
 
645
        return "/plainbox/exporter/{}".format(self.name)
 
646
 
 
647
    # Value added
 
648
 
 
649
    @dbus.service.property(dbus_interface=EXPORTER_IFACE, signature="s")
 
650
    def name(self):
 
651
        """
 
652
        name of this exporter
 
653
        """
 
654
        return self._name
 
655
 
 
656
    @dbus.service.method(
 
657
        dbus_interface=EXPORTER_IFACE, in_signature='', out_signature='as')
 
658
    def SupportedOptionList(self):
 
659
        """
 
660
        Return the list of supported options
 
661
        """
 
662
        return self._cls.supported_option_list
 
663
 
 
664
    @dbus.service.method(
 
665
        dbus_interface=EXPORTER_IFACE, in_signature='as', out_signature='')
 
666
    def SetOptions(self, option_list):
 
667
        """
 
668
        Set export options
 
669
        """
 
670
        self._option_list = option_list
 
671
 
 
672
    @dbus.service.method(
 
673
        dbus_interface=EXPORTER_IFACE, in_signature='s', out_signature='')
 
674
    def SetOutputFile(self, output_file):
 
675
        """
 
676
        Set export options
 
677
        """
 
678
        self._output_file = output_file
 
679
 
 
680
    @dbus.service.method(
 
681
        dbus_interface=EXPORTER_IFACE, in_signature='o', out_signature='')
 
682
    @PlainBoxObjectWrapper.translate
 
683
    def GetSessionDataSubset(self, session: 'o'):
 
684
        self._exporter = self._cls(self._option_list)
 
685
        self._data_subset = self._exporter.get_session_data_subset(session)
 
686
 
 
687
    @dbus.service.method(
 
688
        dbus_interface=EXPORTER_IFACE, in_signature='', out_signature='')
 
689
    def Dump(self):
 
690
        self._exported_stream = io.BytesIO()
 
691
        self._exporter.dump(self._data_subset, self._exported_stream)
 
692
        self._exported_stream.seek(0)  # Need to rewind the file, puagh
 
693
        if self._output_file:
 
694
            with open(self._output_file, 'wb') as output_file:
 
695
                copyfileobj(self._exported_stream, output_file)
 
696
 
 
697
 
626
698
class ProviderWrapper(PlainBoxObjectWrapper):
627
699
    """
628
700
    Wrapper for exposing Provider1 objects on DBus
687
759
        self._provider_wrapper_list = [
688
760
            ProviderWrapper(provider)
689
761
            for provider in self.native.provider_list]
 
762
        self._exporter_wrapper_list = [
 
763
            SessionStateExporterWrapper(None, name=name, cls=exporter)
 
764
            for name, exporter in self.native.exporter_list.items()]
690
765
 
691
766
    def _get_preferred_object_path(self):
692
767
        return "/plainbox/service1"
695
770
        super(ServiceWrapper, self).publish_objects(connection)
696
771
        for wrapper in self._provider_wrapper_list:
697
772
            wrapper.publish_objects(connection)
 
773
        for wrapper in self._exporter_wrapper_list:
 
774
            wrapper.publish_objects(connection)
698
775
 
699
776
    def publish_children(self):
700
777
        # First publish all of our providers
702
779
        # Then ask the providers to publish their own objects
703
780
        for wrapper in self._provider_wrapper_list:
704
781
            wrapper.publish_children()
 
782
        # Publish all of our session exporters
 
783
        self.add_managed_object_list(self._exporter_wrapper_list)
705
784
 
706
785
    # Value added
707
786