当前位置: 首页>>代码示例>>Python>>正文


Python StatementCursor.update方法代码示例

本文整理汇总了Python中useless.db.midlevel.StatementCursor.update方法的典型用法代码示例。如果您正苦于以下问题:Python StatementCursor.update方法的具体用法?Python StatementCursor.update怎么用?Python StatementCursor.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在useless.db.midlevel.StatementCursor的用法示例。


在下文中一共展示了StatementCursor.update方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TableEditor

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import update [as 别名]
class TableEditor(ListWin):
    def __init__(self, conn, table, pkey=None, fields=[],
                 command_data=dict(new='new entry', edit='edit entry')):
        ListWin.__init__(self)
        self.conn = conn
        self.cursor = StatementCursor(self.conn)
        self.cursor.set_table(table)
        self._cdata = command_data
        self.tbar.add_button('new', self._cdata['new'], self.toolbar_button_pressed)
        self.tbar.add_button('edit', self._cdata['edit'], self.toolbar_button_pressed)
        if pkey is None:
            print get_pkey_info(StatementCursor(conn), table)
        self._pkey = pkey
        self.reset_rows()
        self._fields = fields
        self.fields = [pkey] + self._fields
        self.dialogs = {}.fromkeys(self._cdata.keys())

    def reset_rows(self):
        self.set_rows(self.cursor.select(order=self._pkey))

    def toolbar_button_pressed(self, button, data):
        row = None
        if data == 'new':
            if self.dialogs['new'] is None:
                row = self.cursor.select()[0]
                self._make_dialog('new', row, self.add_new_record)
        elif data == 'edit':
            if self.dialogs['edit'] is None:
                row = get_single_row(self.listbox, self._pkey)
                if row is not None:
                    self._make_dialog('edit', row, self.edit_record, row[self._pkey])
            
                
                
    def _make_dialog(self, name, row, okfun, pkey=None):
        if name == 'edit':
            fields = self._fields
        else:
            fields = self.fields
        make_record_dialog(self, name, row, okfun, pkey, fields, self._cdata)
        
    def add_new_record(self, *args):
        dialog = self.dialogs['new']
        self.cursor.insert(data=dict(dialog.items()))
        self.destroy_dialog(dialog)
        self.reset_rows()

    def edit_record(self, *args):
        dialog = self.dialogs['edit']
        clause = Eq(self._pkey, dialog.pkey)
        data = dict(dialog.items())
        self.cursor.update(data=data, clause=clause)
        self.destroy_dialog(dialog)
        self.reset_rows()
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:57,代码来源:base.py

示例2: VariablesConfig

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import update [as 别名]
class VariablesConfig(Configuration):
    def __init__(self, conn, table, section,
                 mainfield=None, mainvalue=None,
                 option='name', value='value'):
        self.conn = conn
        self.cursor = StatementCursor(self.conn)
        self.cursor.set_table(table)
        self._secfield = section
        bothnone = mainfield is None and mainvalue is None
        bothset = mainfield and mainvalue
        if not bothnone and not bothset:
            raise Error, 'both mainfield and mainvalue need to be set/unset'
        self._mainclause = None
        if bothset:
            self._mainclause = Eq(mainfield, mainvalue)
        self._fields = [self._secfield, option, value]
        Configuration.__init__(self)
        for row in self.cursor.select(fields=self._fields, clause=self._mainclause):
            if row[0] not in self.sections():
                self.add_section(row[0])
            self.set(*row)

    def write(self, cfile):
        sections = self.sections()
        sections.sort()
        for section in sections:
            cfile.write('[%s]\n' % section)
            keys = self.options(section)
            keys.sort()
            for k in keys:
                if k != '__name__':
                    v = str(self.get(section, k, raw=True)).replace('\n', '\n\t')
                    cfile.write('%s:\t%s\n' % (k, v))
            cfile.write('\n')
            
    def edit(self):
        tmp, path = tempfile.mkstemp('variables', 'config')
        tmp = file(path, 'w')
        self.write(tmp)
        tmp.close()
        os.system('$EDITOR %s' % path)
        tmp = file(path, 'r')
        newconfig = Configuration()
        newconfig.readfp(tmp)
        tmp.close()
        os.remove(path)
        return newconfig

    def diff(self, other):
        ltmp, lpath = tempfile.mkstemp('variables', 'config')
        ltmp = file(lpath, 'w')
        self.write(ltmp)
        ltmp.close()
        rtmp, rpath = tempfile.mkstemp('variables', 'config')
        rtmp = file(rpath, 'w')
        other.write(rtmp)
        rtmp.close()
        os.system('xxdiff %s %s' % (lpath, rpath))
        ltmp, rtmp = file(lpath, 'r'), file(rpath, 'r')
        lcfg, rcfg = ConfigParser(), ConfigParser()
        lcfg.readfp(ltmp)
        rcfg.readfp(rtmp)
        ltmp.close()
        rtmp.close()
        self.update(lcfg)
        other.update(rcfg)
        
    
        
    def update(self, newconfig):
        removed = [x for x in self.sections() if x not in newconfig.sections()]
        for section in removed:
            print 'removing', section
            sclause = Eq(self._secfield, section)
            if self._mainclause:
                sclause &= self._mainclause
            self.cursor.delete(clause=sclause)
        for section in newconfig.sections():
            print section
            sclause = Eq(self._secfield, section)
            if self._mainclause:
                sclause = self._mainclause & Eq(self._secfield, section)
            if not self.has_section(section):
                for k,v in newconfig.items(section):
                    idata = dict(zip(self._fields, [section, k, v]))
                    if self._mainclause:
                        idata[self._mainclause.left] = self._mainclause.right
                    print idata
                    self.cursor.insert(data=idata)
            else:
                for name, value in newconfig.items(section):
                    nclause = sclause & Eq(self._fields[1], name)
                    #print 'nclause', nclause
                    #print 'oldvalue', self.get(section, name)
                    if self.has_option(section, name):
                        oldvalue = self.get(section, name)
                        if value != oldvalue:
                            print 'updating', name, oldvalue, 'to', value
                            self.cursor.update(data={self._fields[2] : value}, clause=nclause)
                            #print self.cursor.select(clause=nclause)
#.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:103,代码来源:objects.py

示例3: VariablesConfig

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import update [as 别名]
class VariablesConfig(RawConfigParser):
    def __init__(self, conn, table, section, mainfield=None, mainvalue=None, option="name", value="value"):
        self.conn = conn
        self.cursor = StatementCursor(self.conn)
        self.cursor.set_table(table)
        self._secfield = section
        bothnone = mainfield is None and mainvalue is None
        bothset = mainfield and mainvalue
        if not bothnone and not bothset:
            raise Error, "both mainfield and mainvalue need to be set/unset"
        self._mainclause = None
        if bothset:
            self._mainclause = Eq(mainfield, mainvalue)
        self._fields = [self._secfield, option, value]
        RawConfigParser.__init__(self)
        for row in self.cursor.select(fields=self._fields, clause=self._mainclause):
            if row[0] not in self.sections():
                self.add_section(row[0])
            self.set(*row)

    def write(self, cfile):
        sections = self.sections()
        sections.sort()
        for section in sections:
            cfile.write("[%s]\n" % section)
            keys = self.options(section)
            keys.sort()
            for k in keys:
                if k != "__name__":
                    v = str(self.get(section, k)).replace("\n", "\n\t")
                    cfile.write("%s:\t%s\n" % (k, v))
            cfile.write("\n")

    def edit(self):
        tmp, path = tempfile.mkstemp("variables", "config")
        tmp = file(path, "w")
        self.write(tmp)
        tmp.close()
        os.system("$EDITOR %s" % path)
        tmp = file(path, "r")
        newconfig = RawConfigParser()
        newconfig.readfp(tmp)
        tmp.close()
        os.remove(path)
        return newconfig

    def diff(self, other):
        ltmp, lpath = tempfile.mkstemp("variables", "config")
        ltmp = file(lpath, "w")
        self.write(ltmp)
        ltmp.close()
        rtmp, rpath = tempfile.mkstemp("variables", "config")
        rtmp = file(rpath, "w")
        other.write(rtmp)
        rtmp.close()
        os.system("xxdiff %s %s" % (lpath, rpath))
        ltmp, rtmp = file(lpath, "r"), file(rpath, "r")
        lcfg, rcfg = RawConfigParser(), RawConfigParser()
        lcfg.readfp(ltmp)
        rcfg.readfp(rtmp)
        ltmp.close()
        rtmp.close()
        self.update(lcfg)
        other.update(rcfg)

    def update(self, newconfig):
        removed = [x for x in self.sections() if x not in newconfig.sections()]
        for section in removed:
            print "removing", section
            sclause = Eq(self._secfield, section)
            if self._mainclause:
                sclause &= self._mainclause
            self.cursor.delete(clause=sclause)
        for section in newconfig.sections():
            print section
            sclause = Eq(self._secfield, section)
            if self._mainclause:
                sclause = self._mainclause & Eq(self._secfield, section)
            if not self.has_section(section):
                for k, v in newconfig.items(section):
                    idata = dict(zip(self._fields, [section, k, v]))
                    if self._mainclause:
                        idata[self._mainclause.left] = self._mainclause.right
                    print idata
                    self.cursor.insert(data=idata)
            else:
                for name, value in newconfig.items(section):
                    nclause = sclause & Eq(self._fields[1], name)
                    # print 'nclause', nclause
                    # print 'value', self.get(section, name)
                    if self.has_option(section, name):
                        if value != self.get(section, name):
                            # print 'updating'
                            self.cursor.update(data={self._fields[2]: value}, clause=nclause)
                    else:
                        idata = dict(zip(self._fields, [section, name, value]))
                        if self._mainclause:
                            idata[self._mainclause.left] = self._mainclause.right
                        self.cursor.insert(data=idata)
                    if self.has_section(section):
#.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:103,代码来源:oldbase.py


注:本文中的useless.db.midlevel.StatementCursor.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。