summaryrefslogtreecommitdiffstats
path: root/build_tools/larch8/liblarch/larcon_base.py
blob: 9d343f5de351fb374aefdfe65316346be8633a39 (plain)
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
#!/usr/bin/env python2
#
# larcon_base.py   --  Basic backend framework for larcon applications
#
# (c) Copyright 2010 Michael Towers (larch42 at googlemail dot com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------
# 2010.08.14

import os, threading
from liblarch_conf import liblarchdir
from rootrun import init_rootrun
from translation import i18n_liblarch, _

class Backend:
    def __init__(self, appname, env, basedir=None):
        """The env(ironment), i.e. the global dictionary, is needed for
        fetching uim files.
        If the basedir is not provided it can be derived from the appname.
        """
        if basedir:
            self.basedir = basedir
        else:
            self.basedir = os.path.dirname(liblarchdir) + '/' + appname
        self.appenv = env

    def fss_fetch_layout(self, uim):
        fp = os.path.join(self.basedir, 'uim', uim)
        fh = open(fp)
        r = fh.read()
        fh.close()
        return eval(r, self.appenv)

    def fss_uim_fetch(self, uim):
        fp = os.path.join(liblarchdir, 'uim', uim)
        fh = open(fp)
        r = fh.read()
        fh.close()
        return eval(r)

    def setuisig(self, fn):
        self.ui_signal = fn

    def rootcall(self, cmd, fn_done):
        self.rcall = init_rootrun(self._pwget)
        self.rcall.run(cmd, fn_done)

    def _pwget(self, cb, prompt):
        self.pw_event = threading.Event()
        self.ui_signal('get_password', prompt)
        self.pw_event.wait()
        self.rcall.cb_done(cb, *self.pw_returned)

    def fss_sendpassword(self, ok, pw):
        # The result needs to be passed to the waiting _pwget method
        # (which is running in a different thread).
        self.pw_returned = (ok, pw)
        self.pw_event.set()
        return None