Exemple #1
0
    def __init__(self, interp=None):
        self.__dict__ = self.__shared_state

        if not self._interp:
            from pure.cpure import PureEnv
            self._interp = PureEnv()
            self._loaded = True
        elif interp:
            self._interp = interp
            self._loaded = True
Exemple #2
0
class PureInterface(Borg):

    __shared_state = dict(
        _loaded = False,
        _interp = None,
        symbols = {},
        libs = (),
    )

    def __init__(self, interp=None):
        self.__dict__ = self.__shared_state

        if not self._interp:
            from pure.cpure import PureEnv
            self._interp = PureEnv()
            self._loaded = True
        elif interp:
            self._interp = interp
            self._loaded = True

    def eval(self, s):
        return self._interp.eval(s)

    def active(self):
        return self._loaded

    def libraries(self):
        if self.active():
            return self.libs
        else:
            raise Exception('No active interface.')

    def use(self, package, library):
        if self.active():
            if (package,library) not in self.libs:
                # Paths are relative to the /wise/ directory
                print 'Importing Pure library ' + '/'.join([package, library])
                result = self.eval('using ' + '::'.join([package, library]))
                self.libs += ((package,library),)

                if not result:
                    raise Exception('Could not import (%s,%s)' %
                            (package, library))
                else:
                    return True
        else:
            raise Exception('No active interface.')

    # Really no reason to call this, and quite a few reasons
    # *not* to since directly interacting with the interpreter
    # can cause segfaults.
    def get_interp(self):
        if self.active():
            return self._interp
        return self._interp or Exception('No active interface')

    # Modeled after rpy2's interface
    def __getitem__(self, key):
        if self.active():
            try:
                return self.symbols[key]
            except KeyError:
                raise Exception('No Pure symbol: %s' % key)
        else:
            raise Exception('No active interface')

    def jit_compile(self):
        if self.active():
            self._interp.compile_interp()
        else:
            raise Exception('No active interface')