Hi,

The second version.

Now it looks for a bigger value in whole config file.

One bug with name fixed too.

Languages still the same.

-- 
Pozdrawiam,
Jędrzej Nowak
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''****************************************************************************
**
** Copyright (C) 2008 Jędrzej Nowak <[EMAIL PROTECTED]>
** 
** This file may be used under the terms of the GNU General Public License
** version 2 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************'''


import re, sys, os
from itertools import ifilter

#import language file
from cherokee_im_ex_en import lang as LANG

#possible choices
av_choices='eE1iI2'

#main path to config file
FILE='/etc/cherokee/cherokee.conf'

class base_im_ex(object):
    """base class for import/export"""
    def _start(self,file):
        if not file:
            file=FILE
        self.file=file
        self.content=open(self.file,'r').readlines()
    def _filter(self,id=None):
        if id:
            patt=re.compile('vserver!%s!'%id)
        else:
            patt=re.compile('vserver!\d+!nick')
        self.filtered=[now for now in ifilter(lambda x: patt.match(x),self.content)]

class importer(base_im_ex):
    """class for imports"""
    def __init__(self,file):
        self._start(file)
        self.new_data=''
        self._get_content()
    def _get_content(self):
        print LANG['GET_PROMPT']
        self.new_data=sys.stdin.readlines()
        if self.new_data[-1]=="":
            self.new_data=new_data[:-1]
        self._get_new_id()
    def _get_new_id(self):
        self._filter()
        try:
            #looking for a highest id of vserver
            old_id=sorted([int(re.findall('vserver!(?P<id>\d+)!nick',now)[0]) for now in self.filtered])[-1]
            self.new_id=old_id+10
        except Exception, e:
            print "Error: %s"%e
            sys.exit(0)
        self._prepare_content()
    def _prepare_content(self):
        self._find_duplicates()
        #poprawione id vservera
        self.new_data=re.sub('vserver!\d+!', 'vserver!%s!'%self.new_id, ''.join(self.new_data)).split('\n')
        #temp
        self.new_data=[now+'\n' for now in ifilter(lambda x: x, self.new_data)]
        self._join_content()
    def _find_duplicates(self):
        name=re.findall('vserver!\d+!nick.?=.?(?P<name>.*)',''.join(self.new_data))[0]
        tmp_t=''.join(self.content)
        tmp_t_n='\n'.join(self.new_data)
        while re.findall('vserver!\d+!nick.?=.?%s'%name,tmp_t):
            print LANG["CHANGE_NAME"],name+"1"
            name+='1'
            tmp_t_n=re.sub('vserver!(?P<id>\d+)!nick(?P<sp1>.?)=(?P<sp2>.?)(?P<name>.*)',"vserver!\g<id>!nick\g<sp1>=\g<sp2>%s"%name,tmp_t_n)
        self.new_data=tmp_t_n
        #print self.new_data
        #sys.exit(0)
    def _join_content(self):
        patt=re.compile('vserver!.+')
        i=0
        list_of_content=self.content
        while not patt.match(list_of_content[i]):
            i+=1
        start_pos=i
        i=0
        while patt.match(list_of_content[i+start_pos]):
            i+=1
        end_pos=i+start_pos
        list_of_content[end_pos:0]=self.new_data
        text_of_content=''.join(list_of_content)
        print "\n--------------------------------------------------\n"
        print text_of_content
        print "\n--------------------------------------------------\n"
        try:
            file_back=open(self.file+"_back",'w')
            file_back.writelines(self.content)
            file_write=open(self.file,'w')
            file_write.write(text_of_content+'\n')
            print LANG["WRITE_SUCCESS"],self.file
        except IndexError:
            print LANG["WRITE_FAIL"],self.file

class exporter(base_im_ex):
    """for exports"""
    def __init__(self,file):
        self._start(file)
        self._filter()
        self._get_num()
    def _get_num(self):
        print LANG["NUM_PROMPT"]
        print ''.join(self.filtered)
        choice=raw_input(LANG["MY_CHOICE"])
        if choice=='q':
            print LANG["THE_END"]
            sys.exit(1)
        try:
            int(choice)
        except ValueError:
            print LANG["NOT_A_NUMBER"]
            self.get_num()
        self.num=choice
        self._get_content()
    def _get_content(self):
        self._filter(self.num)
        print "\n--------------------------------------------------\n"
        print ''.join(self.filtered)
        print "\n--------------------------------------------------\n"

if __name__=="__main__":
    print LANG["MENU"]
    choice=raw_input(LANG["MY_CHOICE"])
    if choice=="q":
        print LANG["THE_END"]
        sys.exit(0)
    elif choice not in av_choices:
        print LANG["WRONG_CHOICE"]
        sys.exit(1)
    print LANG["FILE_PROMPT"]
    file=raw_input(LANG["PATH_PROMPT"])
    if (file and not os.path.exists(file)) or not os.path.exists(FILE):
        print LANG["FILE_NOT_FOUND"]
        sys.exit(1)
    if av_choices.index(choice)<3:
        #export
        ex=exporter(file)
        sys.exit(0)
    else:
        #import
        im=importer(file)
        sys.exit(0)
_______________________________________________
Cherokee mailing list
[email protected]
http://lists.octality.com/listinfo/cherokee

Reply via email to