convertconf.py :  » Network » A2K » a2k-2.1 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » A2K 
A2K » a2k 2.1 » convertconf.py
#!/usr/bin/env python
#
# Copyright (C) 2000, 2001 Mark Cornick
# <mcornick@users.sourceforge.net> and contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# $Id: convertconf.py,v 1.6 2001/01/14 15:38:47 mcornick Exp $

# WARNING: The Surgeon General has determined that taking IRC too
# seriously is dangerous to your health.

# This script converts old Accutron 2000 configuration files to the
# new format used as of version 2.0beta5.

# This script is full of extremely ugly hacks. Do not emulate this.

import re,os,sys

def transform(line):
    "Convert 1.0 format to 2.0 format."
    line=re.sub('^HOST','host',line)
    line=re.sub('^PORT','port',line)
    line=re.sub('^NICK','nick',line)
    line=re.sub('^NAME','name',line)
    line=re.sub('^CHANNEL','channel',line)
    line=re.sub('^CHANMODES','chanmodes',line)
    line=re.sub('^BOTSNACKS','botsnacks',line)
    line=re.sub('^REMOTEPW','password',line)
    line=re.sub('^BOOKMARKS','urllist',line)
    line=re.sub('^DEBUG.*$','',line)
    line=re.sub('^OPDELAY.*$','',line)
    line=re.sub('^OP','op',line)
    return line

# Check for a new-style file
try:
    import a2kconf
    os.remove("a2kconf.pyc")
    if a2kconf.server:
        oldconf=open("a2kconf.py","r")
        newconf=open("newconf.py","w")
        for x in oldconf.readlines():
            newconf.write(x)
        newconf.close()
        oldconf.close()
        os.rename("a2kconf.py","a2kconf.py.orig")
        
except (ImportError,AttributeError):

    # first try the 2.0beta1 style single file
    try:
        oldconf=open("a2kconf.py","r")
        newconf=open("newconf.py","w")
        conf=oldconf.readlines()
        oldconf.close()
        for x in conf:
            newconf.write(transform(x))
        newconf.close()
        os.rename("a2kconf.py","a2kconf.py.orig")

    # failing that, try the 1.0 style twin files
    except:
        try:
            oldconf=open("accutronconf.py","r")
            newconf=open("newconf.py","w")
            conf=oldconf.readlines()
            oldconf.close()
            for x in conf:
                newconf.write(transform(x))
            newconf.close()
            oldconf=open("accutronops.py","r")
            newconf=open("newconf.py","a")
            conf=oldconf.readlines()
            oldconf.close()
            for x in conf:
                newconf.write(transform(x))
            newconf.close()
        except:
            print "I dunno what to do."
            sys.exit(1)

# And now, the sneaky part
import newconf

# Add the new opdelay option if needed
try:
    newconf.opdelay
except AttributeError:
    newconf.opdelay = 5

writeconf = open("a2kconf.py","w")
writeconf.write("""# Accutron 2000 2.x configuration module
# This file created by convertconf.py

# WARNING: The Surgeon General has determined that taking IRC too
# seriously is dangerous to your health.

# The IRC server(s) (host and port) to connect to. This is a list,
# and can contain one or more sets of hosts or ports, e.g.
# server = [('irc.foo.com', 6667),('irc.bar.com', 6667)]
""")

try:
    writeconf.write("server = %s\n" % newconf.server)
except AttributeError:
    writeconf.write("server = [('%s', %s)]\n" % (newconf.host, newconf.port))

writeconf.write("""# The nickname to use:
nick = '%s'
# The name to use:
name = '%s'
# The channel to join:
channel = '%s'
# What modes to set there:
chanmodes = '%s'
# Want to eat botsnacks? 1 = yes, 0 = no:
botsnacks = %s
# A password for remote commands via /msg:
password = '%s'
# The name of a file to save posted URLs in:
urllist = '%s'
# Number of seconds to wait after someone joins before giving them ops:
opdelay = %s
# Op list. Do not remove the following two lines:
import re
op = {}
# Add new entries below this line.
""" % (newconf.nick,newconf.name,newconf.channel,newconf.chanmodes,newconf.botsnacks,newconf.password,newconf.urllist,newconf.opdelay))
for ii in newconf.op.keys():
    writeconf.write("op['"+ii+"'] = re.compile('"+newconf.op[ii].pattern+"')\n")
writeconf.close()
os.remove('newconf.py')
os.remove('newconf.pyc')

print "Your configuration has been converted."
sys.exit(0)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.