File: RemoteData.py

package info (click to toggle)
ginga 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,276 kB
  • sloc: python: 94,998; javascript: 410; makefile: 146
file content (122 lines) | stat: -rw-r--r-- 3,219 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#
# RemoteData.py -- Remote Data plugin for Ginga image viewer
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
"""
"""
import sys

from ginga import GingaPlugin
from ginga.util.grc import RemoteClient
from ginga.gw import Widgets

from RemoteImage import RemoteImage

help_msg = sys.modules[__name__].__doc__


class RemoteData(GingaPlugin.LocalPlugin):

    def __init__(self, fv, fitsimage):
        # superclass defines some variables for us, like logger
        super(RemoteData, self).__init__(fv, fitsimage)

        # What port to connect to for requests
        self.port = 9909
        # What host to connect to
        self.host = 'localhost'

        self.ev_quit = fv.ev_quit

    def build_gui(self, container):
        vbox = Widgets.VBox()

        fr = Widgets.Frame("Remote Control")

        captions = [
            ("Addr:", 'label', "Addr", 'llabel'),
            ("Set Addr:", 'label', "Set Addr", 'entryset'),
            ("Remote Path:", 'label', "Remote Path", 'entry'),
            ]
        w, b = Widgets.build_info(captions)
        self.w.update(b)

        addr = self.host + ':' + str(self.port)
        b.addr.set_text(addr)

        b.set_addr.set_length(100)
        b.set_addr.set_text(addr)
        b.set_addr.set_tooltip("Set address to connect to remote server")
        b.set_addr.add_callback('activated', self.set_addr_cb)
        b.remote_path.add_callback('activated', self.load_cb)

        fr.set_widget(w)
        vbox.add_widget(fr, stretch=0)

        # stretch
        vbox.add_widget(Widgets.Label(''), stretch=1)

        btns = Widgets.HBox()
        btns.set_spacing(4)
        btns.set_border_width(4)

        btn = Widgets.Button("Close")
        btn.add_callback('activated', lambda w: self.close())
        btns.add_widget(btn)
        btns.add_widget(Widgets.Label(''), stretch=1)
        vbox.add_widget(btns)

        container.add_widget(vbox, stretch=1)


    def start(self):
        pass

    def stop(self):
        pass

    def restart_cb(self, w):
        # restart server
        self.server.stop()
        self.start()

    def set_addr_cb(self, w):
        # get and parse address
        addr = w.get_text()
        host, port = addr.split(':')
        self.host = host
        self.port = int(port)
        self.w.addr.set_text(addr)

        self.proxy = RemoteClient(self.host, self.port)

    def load_cb(self, w):
        path = w.get_text().strip()

        try:
            image = RemoteImage(self.proxy, logger=self.logger)
            image.load_file(path)

            chname = self.fv.get_channelName(self.fitsimage)
            imname = image.get('name', None)
            if imname is None:
                imname = self.fv.name_image_from_path(path)
                image.set(name=imname)

            self.logger.debug("Adding image '%s'" % (imname))
            self.fv.add_image(imname, image, chname=chname)

        except Exception as e:
            self.fv.show_error("Error loading remote image: %s" % (str(e)))

    def close(self):
        self.fv.stop_local_plugin(str(self))
        return True

    def __str__(self):
        return 'remotedata'


#END