예제 #1
0
 def define_func(self, workbook, name, args, expr):
     try:
         expression.define_func(workbook, name, args, expr)
         c = self.db.cursor()
         c.execute('INSERT INTO Funcs VALUES (?, ?, ?, ?, ?)', \
             (workbook['id'], name, args, expr, ''))
         self.db.commit()
     except expression.DeclarationError as exc:
         msg.reply('Error: ' + exc.message)
     except expression.ExpressionError as exc:
         self.exc_handler('', [], exc, workbook, expr)
예제 #2
0
    def load_workbook(self, target, name):
        if ':' not in name:
            name = target + ':' + name

        if self.workbooks.has_key(name):
            self.target_to_workbook[target] = self.workbooks[name]
            return self.target_to_workbook[target]

        self.target_to_workbook[target] = self.workbooks[name] = workbook = {}
        workbook['name'] = name
        workbook['exc_handler'] = self.exc_handler
        workbook['globals'] = expression.BUILTIN_VARS.copy()
        workbook['funcs'] = expression.BUILTIN_FUNCS.copy()

        c = self.db.cursor()
        c.execute('SELECT WorkbookId FROM Workbook WHERE Name=:name', workbook)
        row = c.fetchone()
        if not row:
            c.execute('INSERT INTO Workbook (Name) ' \
                'VALUES (:name)', workbook)
            self.db.commit()
            workbook['id'] = c.lastrowid
            return workbook

        workbook['id'] = row[0]

        c.execute('SELECT Name, Type, Value FROM Vars WHERE WorkbookId=:id', workbook)
        for row in c.fetchall():
            name, type_, value = row
            if type_ == VAR_TYPE_INT:
                value = int(value)
            elif type_ == VAR_TYPE_FLOAT:
                value = float(value)
            elif type_ == VAR_TYPE_COMPLEX:
                value = complex(value)
            else:
                value = str(value)
            workbook['globals'][str(name)] = value

        c.execute('SELECT Name, Args, Expr, Desc FROM Funcs WHERE WorkbookId=:id', workbook)
        for row in c.fetchall():
            name, args, expr, desc = str(row[0]), str(row[1]), str(row[2]), str(row[3])
            expression.define_func(workbook, name, args, expr, desc)

        return workbook