Esempio n. 1
0
    def test_run_something(self):
        vm = VirtualModule('example', _CODE)
        self.assertEqual(vm.dir(), ['run_me', 'run_me2'])

        res = vm.call('run_me', 'test')
        self.assertEqual(res, 'Test')

        res = vm.doc('run_me')
        self.assertEqual(res, 'Will capitalize.')
    def _main(self, request):
        if request.path == '/__main__':
            tmpl = os.path.join(_HERE, 'templates', 'main.mako')
            with open(tmpl) as f:
                tmpl = Template(f.read())

            return tmpl.render(appviews=self.appviews,
                               libraries=self.libraries)

        if request.path == '/__main__/add' and request.method == 'POST':

            def _name2path(name):
                # MORE CHARS
                path = name.lower()
                path = path.replace(' ', '')
                return '/' + path

            data = {}
            name = data['name'] = request.POST['name']
            data['root'] = _name2path(data['name'])
            data['description'] = request.POST['description']
            rbr = _DEFAULT_APP % data
            code = _DEFAULT_CODE
            location = os.path.join(_APPS, name)
            os.mkdir(location)
            appview = AppView(wsgiapp=self,
                              location=location,
                              rbr=rbr,
                              code=code,
                              name=name)
            appview.generate()
            self.appviews.append((appview.get_root(), appview))
            self.appviews.sort(by_len)
            # XXX
            url = 'http://%s%s/__editor__' % (request.environ['HTTP_HOST'],
                                              appview.get_root())
            return HTTPFound(location=url)

        if request.path == '/__main__/addlib' and request.method == 'POST':
            # loading a new lib
            name = request.POST['name']
            file_ = request.POST['file']
            if hasattr(file_, 'file'):
                code = file_.file.read()
            else:
                code = ''
            self.libraries.append(VirtualModule(name, code))
            # XXX
            url = 'http://%s/__lib__/%s' % (request.environ['HTTP_HOST'], name)
            return HTTPFound(location=url)

        return self._404()
    def _load_libs(self):
        libs = []
        for element in os.listdir(self.libdir):
            path = os.path.join(self.libdir, element)
            name = os.path.split(path)[-1]
            name, ext = os.path.splitext(name)
            type_ = ext[1:]

            with open(path) as f:
                content = f.read()

            libs.append(VirtualModule(name, content, type_))
        return libs
    def __init__(self, location=None, rbr_content=_RBR,
                 app_content=_APP, context=None,
                 name='undefined'):
        """
        location contains the application, composed of:
           + an app.rbr file
           + an app.py file

        If location is not provided, will look into the
        current dir
        """

        if location is not None and os.path.exists(location):
            self.root = location
        else:
            self.root = os.getcwd()

        self.name = name

        # rbr file
        self.rbr_file = os.path.join(self.root, '%s.rbr' % self.name)
        if os.path.exists(self.rbr_file):
            with open(self.rbr_file) as f:
                self.rbr_content = f.read()
        else:
            self.rbr_content = rbr_content

        # rbr file
        self.app_file = os.path.join(self.root, '%s.py' % self.name)
        if os.path.exists(self.app_file):
            with open(self.app_file) as f:
                self.app_content = f.read()
        else:
            self.app_content = app_content

        # XXX see how to do this in a more elegant way
        old = sys.path[:]
        sys.path.insert(0, self.root)
        try:
            self.module = VirtualModule(self.name, self.app_content)
        finally:
            sys.path[:] = old

        self.root_path = ''
        self.context = context
        self._load_rbr()
        self._commitmapper()