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


Python StatementCursor.delete方法代码示例

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


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

示例1: remove_client

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
 def remove_client(self, client):
     profiles, families, traits = self._client_schema(client)
     disks, mtypes, machines = self._client_mdata(client)
     cursor = StatementCursor(self.conn)
     if machines:
         cursor.delete(table='machines', clause=In('machine', machines))
     for mtype in mtypes:
         cursor.execute("select * from delete_mtype('%s')" % mtype)
     for disk in disks:
         cursor.execute("select * from delete_disk('%s')" % disk)
     for profile in profiles:
         cursor.execute("select * from delete_profile('%s')" % profile)
     for family in families:
         cursor.execute("select * from delete_family('%s')" % family)
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:16,代码来源:database.py

示例2: DiskManager

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
class DiskManager(object):
    def __init__(self, conn):
        self.conn = conn
        self.parser = PartitionParser()
        self.cursor = StatementCursor(self.conn)
        
    def _quick_partition(self, device, data):
        i, o = os.popen2('sfdisk %s' % device)
        i.write(data)
        i.close()
        

    def get_partition_info(self, device, parser=None):
        if parser is None:
            parser = self.parser
        command = 'bash -c "sfdisk -d %s | grep %s"' % (device, device)
        part_info = commands.getoutput(command)
        return self._parse_diskconfig(device, part_info)

    def _parse_diskconfig(self, device, astring):
        parsed = self.parser.parseString(astring)
        partitions = []
        for p in parsed:
            pnum = p[0].split(device)[1]
            pdict = dict(partition=pnum, start=p[1], size=p[2], Id=p[3])
            partitions.append(pdict)
        return partitions

    def _submit_diskconfig(self, diskname, device, astring):
        workspace = 'partition_workspace'
        self.cursor.delete(table=workspace, clause=(Eq('diskname', diskname)))
        row = dict(diskname=diskname)
        for partition in self._parse_diskconfig(device, astring):
            print 'submitting', partition
            row.update(partition)
            self.cursor.insert(table=workspace, data=row)

    def submit_partitions(self, diskname, device):
        self.cursor.set_table('partition_workspace')
        self.cursor.delete(clause=(Eq('diskname', diskname)))
        row = dict(diskname=diskname)
        print 'submitting', device
        for partition in self.get_partition_info(device):
            print 'submitting', partition
            row.update(partition)
            self.cursor.insert(data=row)

    def approve_disk(self, diskname):
        clause = Eq('diskname', diskname)
        workspace = 'partition_workspace'
        sql = Statement('select')
        sql.table = workspace
        sql.clause = clause
        new_rows = sql.select(order='partition')
        if diskname not in [r.diskname for r in self.cursor.select(table='disks')]:
            self.cursor.insert(table='disks', data=dict(diskname=diskname))
        else:
            self.cursor.delete(table='partitions', clause=clause)
        self.cursor.execute('insert into partitions %s' % new_rows)
    
    def get_partitions_by_name(self, diskname):
        return self.cursor.select(table='partitions',
                             clause=Eq('diskname', diskname),
                             order='partition')


    def make_partition_dump(self, device, partitions):
        output = '# partition table of %s\n'
        output += 'unit: sectors\n'
        for p in partitions:
            line = '%s%s : start= %8d, size= %8d, Id=%2d' % \
                   (device, p.partition, p.start, p.size, p.id)
            output += line + '\n'
        return output

    def partition_disk(self, diskname, device):
        partitions = self.get_partitions_by_name(diskname)
        data = self.make_partition_dump(device, partitions)
        self._quick_partition(device, data)

    def clear_partition_table(self, device):
        command = 'dd if=/dev/zero of=%s count=1 bs=512' % device
        os.system(command)
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:85,代码来源:cfdisk.py

示例3: quick_wipe

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
 def quick_wipe(conn):
     cursor = StatementCursor(conn)
     cursor.delete(table='machines')
     cursor.delete(table='partition_workspace')
     cursor.delete(table='partitions')
     cursor.delete(table='filesystem_mounts')
     cursor.delete(table='filesystem_disks')
     cursor.delete(table='partition_mounts')
     cursor.delete(table='machine_disks')
     cursor.delete(table='machine_types')
     cursor.delete(table='mounts')
     cursor.delete(table='disks')
     cursor.delete(table='filesystems')
     cursor.delete(table='kernels')
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:16,代码来源:main.py

示例4: VariablesConfig

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [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

示例5: Family

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
class Family(object):
    def __init__(self, conn):
        object.__init__(self)
        self.conn = conn
        self.cursor = StatementCursor(self.conn)
        self.current = None
        self.parent = SimpleRelation(self.conn, 'family_parent', 'family')
        self.env = FamilyEnvironment(self.conn)
        
    def set_family(self, family):
        self.current = family
        self.parent.set_current(family)
        self.env.set_family(family)

    def _check_family(self, family):
        if family is not None:
            return family
        else:
            family = self.current
        if family is None:
            raise Error, 'either pass a family argument or call set_family on this object'
        return family
    
    def get_related_families(self, families=[]):
        rows = self.cursor.select(table='family_parent')
        graph = kjGraph([(r.family, r.parent) for r in rows])
        dfamilies = Set()
        for fam in families:
            dfamilies |= Set([fam]) | Set(graph.reachable(fam).items())
        return dfamilies

    def parent_rows(self, family=None):
        family = self._check_family(family)
        self.parent.set_clause(family)
        rows = self.parent.cmd.select(fields=['parent'], order='parent')
        self.parent.reset_clause()
        return rows

    def parents(self, family=None):
        family = self._check_family(family)
        rows = self.parent_rows(family)
        return [x.parent for x in rows]
    
    def environment_rows(self, family=None):
        family = self._check_family(family)
        clause = Eq('family', family)
        args = dict(fields=['trait', 'name', 'value'], clause=clause, order=['trait', 'name'])
        return self.env.cursor.select(**args)

    def family_rows(self):
        return self.cursor.select(fields=['family'], table='families', order='family')

    def all_families(self):
        return [r.family for r in self.family_rows()]

    def get_all_defaults(self):
        suite_cursor = SuiteCursor(self.conn)
        suites = suite_cursor.get_suites()
        stmt = select_multisuite_union(suites, 'variables')
        print stmt
        self.cursor.execute(stmt)
        return self.cursor.fetchall()

    def create_family(self, family):
        if family not in self.all_families():
            self.cursor.insert(table='families', data=dict(family=family))
        else:
            raise ExistsError, '%s already exists' % family

    def delete_family(self, family=None):
        family = self._check_family(family)
        self.cursor.execute("select * from delete_family('%s')" % family)
        
    def insert_parents(self, parents, family=None):
        family = self._check_family(family)
        self.parent.insert('parent', parents)

    def delete_parents(self, parents=[], family=None):
        family = self._check_family(family)
        if not parents:
            parents = self.parents()
        if not parents:
            return
        table= 'family_parent'
        clause = Eq('family', family) & In('parent', parents)
        self.cursor.delete(table=table, clause=clause)
        
    def FamilyData(self, families=[]):
        # change sep here
        sep = PAELLA_TRAIT_NAME_SEP
        # we don't know what to do here
        # it is possible to have an empty
        # list of families, and if that's the
        # case we don't need to make the
        # list of [self.current] .  I need to look
        # for everywhere this method is called.
        # In the installer, the family is never set,
        # and we use the list of families provided
        # by the profile and the machine.  In this
        # case, the families argument will always
#.........这里部分代码省略.........
开发者ID:umeboshi2,项目名称:paella,代码行数:103,代码来源:main.py

示例6: Profile

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
class Profile(StatementCursor):
    def __init__(self, conn):
        StatementCursor.__init__(self, conn)
        self.conn = conn
        self.set_table('profiles')
        self._traits = ProfileTrait(conn)
        self._env = ProfileEnvironment(conn)
        self._pfam = StatementCursor(conn)
        self._pfam.set_table('profile_family')
        self._fam = Family(conn)
        self.current = None
        
    def drop_profile(self, profile):
        self.delete(clause=Eq('profile', profile))

    def make_new_profile(self, profile, suite):
        data = dict(profile=profile, suite=suite)
        self.insert(data=data)

    def make_environment_object(self):
        return ProfileEnvironment(self.conn)
    
    def set_profile(self, profile):
        self.clause = Eq('profile', profile)
        self.current = self.select_row(clause=self.clause)
        self._traits.set_profile(profile)
        self._env.set_profile(profile)
        
    def get_profile_data(self):
        return self._env.ProfileData()

    def get_family_data(self):
        families = self.get_families()
        return self._fam.FamilyData(families)

    def _make_traitlist(self, traits, log=None):
        tp = TraitParent(self.conn, self.current.suite)
        listed = traits
        all = list(tp.get_traitset(listed))
        setfun = tp.set_trait
        parfun = tp.parents
        return make_deplist(listed, all, setfun, parfun, log)

    def make_traitlist_with_traits(self, traits, log=None):
        return self._make_traitlist(traits, log=log)
    
    def make_traitlist(self, log=None):
        listed = [x.trait for x in self._traits.trait_rows()]
        return self._make_traitlist(listed, log=log)

    def get_traitlist_for_traits(self, traits):
        return self._make_traitlist(traits)
    
    def family_rows(self, profile=None):
        if profile is None:
            profile = self.current.profile
        return self._pfam.select(clause=Eq('profile', profile), order='family')
    

    def get_families(self, profile=None):
        return [r.family for r in self.family_rows(profile)]

    def get_trait_rows(self):
        return self._traits.trait_rows()
    
    def append_family(self, family):
        if family not in self.get_families():
            self._pfam.insert(data=dict(profile=self.current.profile, family=family))

    def append_trait(self, trait, ord):
        self._traits.insert_trait(trait, ord)
        
    def set_suite(self, suite):
        clause = Eq('profile', self.current.profile)
        self.update(data=dict(suite=suite), clause=clause)
        self.set_profile(self.current.profile)

    def copy_profile(self, src, dest):
        current = self.current
        self.set_profile(src)
        pfield = "'%s' as profile" % dest
        pclause = Eq('profile', src)
        pcursor = self
        tcursor = self._traits.cmd
        fcusor = self._pfam
        vcursor = self._env.env.cursor
        cursors = [ pcursor, tcursor, fcusor, vcursor]
        for cursor in cursors:
            sel = str(cursor.stmt.select(fields = [pfield] + cursor.fields()[1:], clause=pclause))
            cursor.execute('insert into %s (%s)' % (cursor.stmt.table, sel))
        if current is not None:
            self.set_profile(current.profile)
            

    def get_profile_list(self, suite=None):
        if suite is None:
            plist = [r.profile for r in self.select()]
        elif suite == 'all':
            plist = [(r.profile, r.suite) for r in self.select()]
        else:
#.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:103,代码来源:main.py

示例7: ProfileBrowser

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
class ProfileBrowser(ListNoteBook, HasDialogs):
    def __init__(self, conn, suites, name='ProfileBrowser'):
        self.menu = self.__make_mainmenu_(suites)
        ListNoteBook.__init__(self, name=name)
        self.conn = conn
        self.profiles = Profile(self.conn)
        self.profiletrait = ProfileTrait(self.conn)
        self.family = Family(self.conn)
        self.pfamily = StatementCursor(self.conn)
        self.pfamily.set_table('profile_family')
        self.trait_menu = make_menu(['drop', 'order'], self.trait_command)
        self.pdata_menu = make_menu(['edit', 'drop'], self.variable_command)
        self.family_menu = make_menu(['drop'], self.family_command)
        self.reset_rows()
        self.append_page(ScrollCList(rcmenu=self.trait_menu), 'traits')
        self.append_page(ScrollCList(rcmenu=self.pdata_menu), 'variables')
        self.append_page(ScrollCList(rcmenu=self.family_menu), 'families')
        self.dialogs = {}.fromkeys(['order'])
        
    def __make_mainmenu_(self, suites):
        suite_commands = ['change to %s' %suite for suite in suites]
        profile_commands = ['drop', 'set defaults', 'append defaults']
        commands = suite_commands + profile_commands
        return make_menu(commands, self.profile_command)

    def reset_rows(self):
        self.set_rows(self.profiles.select(fields=['profile', 'suite'], order='profile'))
        self.set_row_select(self.profile_selected)

    def profile_selected(self, listbox, row, column, event):
        row = listbox.get_selected_data()[0]
        self.current = row
        self.select_profile(self.current.profile)

    def select_profile(self, profile):
        self.variables = ProfileEnvironment(self.conn, profile)
        self.profiletrait.set_profile(profile)
        self.__set_pages(profile)

    def __set_pages(self, profile):
        pages = dict(self.pages)
        #set traits for profile
        pages['traits'].set_rows(self.profiletrait.trait_rows())
        pages['traits'].set_select_mode('multi')
        clause = Eq('profile', self.current.profile)
        cursor = self.variables.env.cursor
        pages['variables'].set_rows(cursor.select(clause=clause), ['trait'])
        pages['variables'].set_select_mode('multi')
        pfrows = self.pfamily.select(fields=['family'], clause=clause)
        pages['families'].set_rows(pfrows)
        pages['families'].set_select_mode('multi')
        self.__set_droptargets__(pages)
        
        
    def __set_droptargets__(self, pages):
        set_receive_targets(pages['traits'].listbox,
                            self.drop_trait, TARGETS.get('trait', self.current.suite))
        set_receive_targets(pages['families'].listbox,
                            self.drop_families, FTargets.get('family', 'flavor'))
        

    def trait_command(self, menuitem, action):
        traits = self._get_listbox('traits', 'trait')
        if action == 'drop':
            clause = In('trait', traits) & Eq('profile', self.current.profile)
            self.profiletrait.cmd.delete(clause=clause)
            self.__set_pages(self.current.profile)
        elif action == 'order':
            if not self.dialogs['order']:
                self.dialogs['order'] = dialogs.Entry('enter order', name='order')
                self.dialogs['order'].set_ok(self.set_order)
                self.dialogs['order'].set_cancel(self.destroy_dialog)


    def variable_command(self, menuitem, action):
        rows = self.pages['variables'].get_selected_data()
        cursor = self.variables.env.cursor
        if action == 'drop':
            for row in rows:
                clause = Eq('profile', self.current.profile) & Eq('trait', row.trait)
                clause &= Eq('name', row.name)
                cursor.delete(clause=clause)
        elif action == 'edit':
            self.edit_profilevars()

    def family_command(self, menuitem, action):
        print action
        families = [x[0] for x in self.pages['families'].get_selected_data()]
        if action == 'drop':
            clause = Eq('profile', self.current.profile) & In('family', families)
            self.pfamily.delete(clause=clause)

        
    def edit_profilevars(self):
        config = ProfileVariablesConfig(self.conn, self.current.profile)
        newconfig = config.edit()
        config.update(newconfig)
        self.select_profile(self.current.profile)
                            
    def set_order(self, button):
#.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:103,代码来源:profilegen.py

示例8: Profile

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [as 别名]
class Profile(StatementCursor):
    def __init__(self, conn):
        StatementCursor.__init__(self, conn)
        self.conn = conn
        self.set_table('profiles')
        self._traits = ProfileTrait(conn)
        self._env = ProfileEnvironment(conn)
        self._pfam = StatementCursor(conn)
        self._pfam.set_table('profile_family')
        self._fam = Family(conn)
        self.current = None
        
    def drop_profile(self, profile):
        self.delete(clause=Eq('profile', profile))

    def set_profile(self, profile):
        self.clause = Eq('profile', profile)
        self.current = self.select_row(clause=self.clause)
        self._traits.set_profile(profile)
        self._env.set_profile(profile)
        
    def get_profile_data(self):
        return self._env.ProfileData()

    def get_family_data(self):
        families = self.get_families()
        return self._fam.FamilyData(families)

    def _make_traitlist(self, traits, log=None):
        tp = TraitParent(self.conn, self.current.suite)
        listed = traits
        all = list(tp.get_traitset(listed))
        setfun = tp.set_trait
        parfun = tp.parents
        return make_deplist(listed, all, setfun, parfun, log)
        
    def make_traitlist(self, log=None):
        listed = [x.trait for x in self._traits.trait_rows()]
        return self._make_traitlist(listed, log=log)

    def get_traitlist_for_traits(self, traits):
        return self._make_traitlist(traits)
    
    def family_rows(self, profile=None):
        if profile is None:
            profile = self.current.profile
        return self._pfam.select(clause=Eq('profile', profile), order='family')
    

    def get_families(self, profile=None):
        return [r.family for r in self.family_rows(profile)]

    def get_trait_rows(self):
        return self._traits.trait_rows()
    
    def append_family(self, family):
        if family not in self.get_families():
            self._pfam.insert(data=dict(profile=self.current.profile, family=family))

    def append_trait(self, trait, ord):
        self._traits.insert_trait(trait, ord)
        
    def set_suite(self, suite):
        clause = Eq('profile', self.current.profile)
        self.update(data=dict(suite=suite), clause=clause)
        self.set_profile(self.current.profile)

    def copy_profile(self, src, dest):
        current = self.current
        self.set_profile(src)
        pfield = "'%s' as profile" % dest
        pclause = Eq('profile', src)
        pcursor = self
        tcursor = self._traits.cmd
        fcusor = self._pfam
        vcursor = self._env.env.cursor
        cursors = [ pcursor, tcursor, fcusor, vcursor]
        for cursor in cursors:
            sel = str(cursor.stmt.select(fields = [pfield] + cursor.fields()[1:], clause=pclause))
            cursor.execute('insert into %s (%s)' % (cursor.stmt.table, sel))
        if current is not None:
            self.set_profile(current.profile)
            

    def get_profile_list(self, suite=None):
        if suite is None:
            plist = [r.profile for r in self.select()]
        elif suite == 'all':
            plist = [(r.profile, r.suite) for r in self.select()]
        else:
            clause = Eq('suite', suite)
            plist = [r.profile for r in self.select(clause=clause)]
        return plist

    def edit_variables(self):
        config = ProfileVariablesConfig(self.conn, self.current.profile)
        newconfig = config.edit()
        config.update(newconfig)

    def delete_trait(self, trait):
#.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:paella-svn,代码行数:103,代码来源:main.py

示例9: VariablesConfig

# 需要导入模块: from useless.db.midlevel import StatementCursor [as 别名]
# 或者: from useless.db.midlevel.StatementCursor import delete [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.delete方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。