示例#1
0
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()
示例#2
0
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()
示例#3
0
class ColorThingyWindow(CommandBoxWindow):
    def __init__(self, rgb, conn, name='ColorThingyWindow'):
        CommandBoxWindow.__init__(self, name=name)
        self.browser = ColorThingy(rgb)
        self.rgb = rgb
        self.cmd = StatementCursor(conn, 'themer')
        self.theme = None
        self.vbox.add(self.browser)
        self.tbar.add_button('import', 'import', self.__ask_import__)
        self.tbar.add_button('insert', 'insert', self.__ask_insert__)
        self.tbar.add_button('update', 'update', self.__update_theme__)
        self.tbar.add_button('save', 'save', self.__save_files__)
        self._insert_box = None
        self._import_box = None

    def __ask_import__(self, *args):
        if not self._import_box:
            #print args
            #print '__import__'
            themes = self.cmd.getall(['theme'], 'themes')
            self._import_box = CList('hello')
            self._import_box.set_rows(themes)
            self._import_box.set_ok(self.__import_ok__)

    def __import_ok__(self, *args):
        theme = self._import_box.listbox.get_selected_data()[0]['theme']
        self._import_box.destroy()
        self._import_box = None
        rows = self.cmd.select(table=theme)
        b = self.browser
        b._style.quick_set(rows)
        for e, sb in b._elements.items():
            for s in sb.keys():
                color = b._style.elements[e][s]
                sb[s] = color.name
                _bg_widget(sb[s].button, color)
        self.theme = theme

    def __ask_insert__(self, *args):
        if not self._import_box:
            self._insert_box = EntryDialog('please insert name')
            self._insert_box.set_ok(self.__insert_ok__)

    def __insert_ok__(self, *args):
        style = self.browser._style
        tname = self._insert_box.get()
        self.theme = tname
        table = ThemeTable(tname)
        self.cmd.create_table(table)
        self.cmd.insert('themes', {'theme': tname})
        for row in style.to_irows():
            self.cmd.insert(tname, row)

    def __update_theme__(self, *args):
        if self.theme:
            for element, states in self.browser._style.to_urows():
                self.cmd.update(self.theme, states, "element = '%s'" % element)

    def __save_files__(self, *args):
        colordict = Rgbdb()
        tmpl = GtkrcTemplate(self.theme, theme_base, self.browser._style)
        tmpl.write_files()
示例#4
0
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):
                        for name, value in self.items(section):
                            if not newconfig.has_option(section, name):
                                clause = sclause & Eq(self._fields[1], name)
                                #print 'dclause', clause
                                self.cursor.delete(clause=clause)
示例#5
0
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):
                        for name, value in self.items(section):
                            if not newconfig.has_option(section, name):
                                clause = sclause & Eq(self._fields[1], name)
                                #print 'dclause', clause
                                self.cursor.delete(clause=clause)
示例#6
0
class RepositoryManager(object):
    def __init__(self, conn):
        self.conn = conn
        self.main = StatementCursor(self.conn, 'RepositoryMain')
        self.repos = StatementCursor(self.conn, 'Repository')
        self.repos.set_table('repository')
        self.sources = StatementCursor(self.conn, 'Sources')
        self.sources.set_table('sources')
        self.release = ReleaseCursor(self.conn)
        self.repsections = StatementCursor(self.conn, 'repos_section')
        self.repsections.set_table('repos_section')
        self.__init_db__()

    def __init_db__(self):
        if not len(self.main.tables()):
            map(self.main.create_table, primary_tables())

    def drop_source(self, name, type):
        nameclause = Eq('name', name)
        clause = nameclause & Eq('type', type)
        for section in self.get_sections(name):
            self.main.drop(list_tablename(name, type, section))
        self.sources.delete(clause=clause)
        sources = self.sources.select(clause=clause)
        if not len(sources):
            self.repsections.delete(clause=nameclause)
            self.repos.delete(clause=nameclause)

    def add_source(self, name, source):
        source = make_source(source)
        if name not in [x.name for x in self.repos.select()]:
            self.repos.insert(data=dict(name=name))
        clause = Eq('name', name) & Eq('type', source.type)
        count = int(
            self.sources.select(fields=['count(name)'], clause=clause)[0][0])
        if count == 0:
            if islocaluri(source.uri):
                data = dict(name=name,
                            type=source.type,
                            uri=source.uri,
                            suite=source.suite)
                self.sources.insert(data=data)
                current_sections = self.get_sections(name)
                sdata = dict(name=name, section=None)
                for section in source.sections:
                    if section not in current_sections:
                        sdata['section'] = section
                        self.repsections.insert(data=sdata)
                    fullparse = FullParseTable(name, source.type, section)
                    if fullparse.name not in self.main.tables():
                        self.main.create_table(fullparse)
                    listtable = ListTable(name, source.type, section)
                    if listtable.name not in self.main.tables():
                        self.main.create_table(listtable)
            else:
                raise Error, 'local uris first'
        else:
            if not islocaluri(source.uri):
                data = dict(remote=source.uri)
                self.sources.update(data=data, clause=clause)
            else:
                raise ExistsError, 'already there'

    def get_sections(self, name):
        clause = Eq('name', name)
        return [
            x.section for x in self.repsections.iselect(fields=['section'],
                                                        clause=clause)
        ]

    def make_source_line(self, name, type, remote=False):
        repsrc = self.make_source(name, type, remote)
        return str(repsrc)

    def make_source(self, name, type, remote=False):
        clause = Eq('name', name) & Eq('type', type)
        source = self.sources.select_row(clause=clause)
        repsrc = RepositorySource()
        repsrc.type = source.type
        if remote:
            repsrc.uri = source.remote
        else:
            repsrc.uri = source.uri
        repsrc.suite = source.suite
        repsrc.sections = self.get_sections(name)
        repsrc.set_path()
        return repsrc

    def listfile(self, name, type, section=None):
        source = self.make_source(name, type)
        release = Release(source)
        print 'Need to pull from database!!!'
        if source.has_release():
            return join(source.distpath, release.path(section))
        else:
            return join(source.distpath, source.listfile())

    def get_remote(self, name, type, remote=True):
        return self.make_source(name, type, remote=True)

    def parse_section(self, name, type, section=None):
        listfile = self.listfile(name, type, section)
        debug(listfile)
        if not isfile(listfile):
            raise NoFileError, 'file not there'
        if type == 'deb':
            return full_parse(listfile)
        elif type == 'deb-src':
            return parse_sources(listfile)
        else:
            raise Error, 'bad source type'

    def add_repository(self, name):
        self.repos.insert(data=dict(name=name))
class RepositoryManager(object):
    def __init__(self, conn):
        self.conn = conn
        self.main = StatementCursor(self.conn, 'RepositoryMain')
        self.repos = StatementCursor(self.conn, 'Repository')
        self.repos.set_table('repository')
        self.sources = StatementCursor(self.conn, 'Sources')
        self.sources.set_table('sources')
        self.release = ReleaseCursor(self.conn)
        self.repsections = StatementCursor(self.conn, 'repos_section')
        self.repsections.set_table('repos_section')
        self.__init_db__()
        
    def __init_db__(self):
        if not len(self.main.tables()):
            map(self.main.create_table, primary_tables())

    def drop_source(self, name, type):
        nameclause = Eq('name', name)
        clause = nameclause & Eq('type', type)
        for section in self.get_sections(name):
            self.main.drop(list_tablename(name, type, section))
        self.sources.delete(clause=clause)
        sources = self.sources.select(clause=clause)
        if not len(sources):
            self.repsections.delete(clause=nameclause)
            self.repos.delete(clause=nameclause)
            
    def add_source(self, name, source):
        source = make_source(source)
        if name not in [x.name for x in self.repos.select()]:
            self.repos.insert(data=dict(name=name))
        clause = Eq('name', name) & Eq('type', source.type)
        count = int(self.sources.select(fields=['count(name)'], clause=clause)[0][0])
        if count == 0:
            if islocaluri(source.uri):
                data = dict(name=name, type=source.type, uri=source.uri,
                            suite=source.suite)
                self.sources.insert(data=data)
                current_sections = self.get_sections(name)
                sdata = dict(name=name, section=None)
                for section in source.sections:
                    if section not in current_sections:
                        sdata['section'] = section
                        self.repsections.insert(data=sdata)
                    fullparse = FullParseTable(name, source.type, section)
                    if fullparse.name not in self.main.tables():
                        self.main.create_table(fullparse)
                    listtable = ListTable(name, source.type, section)
                    if listtable.name not in self.main.tables():
                        self.main.create_table(listtable)
            else:
                raise Error, 'local uris first'
        else:
            if not islocaluri(source.uri):
                data = dict(remote=source.uri)
                self.sources.update(data=data, clause=clause)
            else:
                raise ExistsError, 'already there'

    def get_sections(self, name):
        clause = Eq('name', name)
        return [x.section for x in self.repsections.iselect(fields=['section'], clause=clause)]

    def make_source_line(self, name, type, remote=False):
        repsrc = self.make_source(name, type, remote)
        return str(repsrc)

    def make_source(self, name, type, remote=False):
        clause = Eq('name', name) & Eq('type', type)
        source = self.sources.select_row(clause=clause)
        repsrc = RepositorySource()
        repsrc.type = source.type
        if remote:
            repsrc.uri = source.remote
        else:
            repsrc.uri = source.uri
        repsrc.suite = source.suite
        repsrc.sections = self.get_sections(name)
        repsrc.set_path()
        return repsrc

    def listfile(self, name, type, section=None):
        source = self.make_source(name, type)
        release = Release(source)
        print 'Need to pull from database!!!'
        if source.has_release():
            return join(source.distpath, release.path(section))
        else:
            return join(source.distpath, source.listfile())
    def get_remote(self, name, type, remote=True):
        return self.make_source(name, type, remote=True)

    def parse_section(self, name, type, section=None):
        listfile = self.listfile(name, type, section)
        debug(listfile)
        if not isfile(listfile):
            raise NoFileError, 'file not there'
        if type == 'deb':
            return full_parse(listfile)
        elif type == 'deb-src':
            return parse_sources(listfile)
        else:
            raise Error, 'bad source type'

    def add_repository(self, name):
        self.repos.insert(data=dict(name=name))
示例#8
0
class ColorThingyWindow(CommandBoxWindow):
    def __init__(self, rgb, conn, name='ColorThingyWindow'):
        CommandBoxWindow.__init__(self, name=name)
        self.browser = ColorThingy(rgb)
        self.rgb = rgb
        self.cmd = StatementCursor(conn, 'themer')
        self.theme = None
        self.vbox.add(self.browser)
        self.tbar.add_button('import', 'import', self.__ask_import__)
        self.tbar.add_button('insert', 'insert', self.__ask_insert__)
        self.tbar.add_button('update', 'update', self.__update_theme__)
        self.tbar.add_button('save', 'save', self.__save_files__)
        self._insert_box = None
        self._import_box = None

    def __ask_import__(self, *args):
        if not self._import_box:
            #print args
            #print '__import__'
            themes = self.cmd.getall(['theme'], 'themes')
            self._import_box = CList('hello')
            self._import_box.set_rows(themes)
            self._import_box.set_ok(self.__import_ok__)

    def __import_ok__(self, *args):
        theme = self._import_box.listbox.get_selected_data()[0]['theme']
        self._import_box.destroy()
        self._import_box = None
        rows = self.cmd.select(table=theme)
        b = self.browser
        b._style.quick_set(rows)
        for e, sb in b._elements.items():
            for s in sb.keys():
                color = b._style.elements[e][s]        
                sb[s] = color.name
                _bg_widget(sb[s].button, color)
        self.theme = theme
                                     
    def __ask_insert__(self, *args):
        if not self._import_box:
            self._insert_box = EntryDialog('please insert name')
            self._insert_box.set_ok(self.__insert_ok__)

    def __insert_ok__(self, *args):
        style = self.browser._style
        tname = self._insert_box.get()
        self.theme = tname
        table = ThemeTable(tname)
        self.cmd.create_table(table)
        self.cmd.insert('themes', {'theme':tname})
        for row in style.to_irows():
            self.cmd.insert(tname, row)

    def __update_theme__(self, *args):
        if self.theme:
            for element, states in self.browser._style.to_urows():
                self.cmd.update(self.theme, states, "element = '%s'" %element)
    def __save_files__(self, *args):
        colordict = Rgbdb()
        tmpl = GtkrcTemplate(self.theme, theme_base, self.browser._style)
        tmpl.write_files()