简体   繁体   中英

Troublesome DAT editing with Python 2.7 and ConfObj/Parser

Edit - final open source code here if interested. https://github.com/qetennyson/ConfigFileBuilder

Hi there, first question here. I'm relatively new to Python (using 2.7 here) and have always been a pretty average programmer as it is.

I'm working on a short program that builds configuration files for these proprietary, internet connected power switches of which I have about 90. Each needs a unique configuration for the city it's going to.

I don't know a ton about filetypes, but these guys are DATs which I figured were similar enough to INI for me to bang my head against the keyboard for six to seven hours, years, eras.

Here's my existing code.

from configobj import ConfigObj
import csv
import os

config = ConfigObj('configtest3.dat', list_values=True, raise_errors=False)

with open('config.csv') as csvfile:
    csvreader = csv.reader(csvfile, dialect='excel')
    office = 'null'
    while (office != 'LASTOFFICEINLIST'):
        for row in csvreader:
            config.filename = row[0] + '-powerswitch'
            config['Hostname'] = row[0]
            config['Ipaddr'] = row[2]
            config['OutletName1'] = row[3]
            config['Gateway'] = row[4]
            config['DNS'] = 'DNSTEST'
            config['Account1'] = row[6]
            config['Password1'] = 'passwordtest'
            config['TimeServer1'] = 'x.x.x.x' <--Sanitized
            config['TimeZone'] = '800'
            office = row[0]
            config.write()

You're thinking "that should work fine" and it does, with one exception!

If I don't remove the "Default" from the beginning of the DAT file (shown here):

#The following line must not be removed.
Default
SSID1=MegaBoot_112D36
WebInit=1

...then ConfObj will refuse to read it (I'm assuming it's a key/value issue).

I haven't actually tested one of these devices without the "Default" there in the configuration, but I'm inclined to listen to the line telling me not to remove it, and I don't want to brick a device really.

In my beginner-ness, I did some more research and realized that what I could do would be remove default programmatically, and then add it back after I've done my ConfObj work, but I wanted to check in here first. Plus, I was able to get "Default" out pretty easily:

with open ('configtest3.dat', 'r+') as f:
    config = f.readlines()
    f.seek(0)
    for i in config:
        if i != 'Default\n':
            f.write(i)
        f.truncate()

...but am unsure of how to shoehorn it back in there.

I could be shoehorning this entire thing!

Please enlighten me.


Problem solved! Thank you for your feedback Sweater-Baron.

Here's my final code, which could probably do with some refactoring, but I'll get to that. I finally have a fully functioning config generator!

from configobj import ConfigObj
import csv
import os

config = ConfigObj('configtest.dat', list_values=True, raise_errors=False)

with open('config.csv') as csvfile:
    configcsv = csv.reader(csvfile, dialect='excel')
    office = 'null'
    while (office != 'Wooster'):
        for row in configcsv:
            config.filename = row[0] + '-powerswitch.dat'
            config['Hostname'] = row[0]
            config['Ipaddr'] = row[2]
            config['OutletName1'] = row[3]
            config['Gateway'] = row[4]
            config['DNS'] = 'DNSTEST'
            config['Account1'] = row[6]
            config['Password1'] = 'passwordtest'
            config['TimeServer1'] = '10.116.65.17'
            config['TimeZone'] = '800'
            office = row[0]
            config.write()

with open('config.csv') as csvfile:
    configcsv = csv.reader(csvfile, dialect='excel')
    csvfile.seek(0)
    office = 'null'
    while (office != 'Wooster'):
        for row in configcsv:
            filename = row[0] + '-powerswitch.dat'
            with open(filename, 'r') as cfgFile:
                almostCorrect = cfgFile.read()
            correct = "#The following line must not be removed.\nDefault\n" + almostCorrect
            with open(filename, 'w') as cfgFile:
                cfgFile.write(correct)
            print row[0]
            office = row[0]

I realized one thing I was getting hung up on was the initial deletion of "Default" at all. Instead, I just deleted that from the base file from which I'm configuring the 90 files. I'm not sure why I thought I needed it in there in the first place, if I just want to add it back in the end!

Thanks again.

This is sort of an intellectually lazy solution, but it seems like it would require minimal changes to your existing code:

Use your existing code to create a config file without the "Default" line. Then read that file back into Python as a string. Then erase everything from the file, add the "Default" line to it, and then write back all the other contents of the file that you just read out of it.

The following code will add "Default" to the beginning of a text file (also not great code, just demonstrating what I mean):

with open("ConfigFilePath", "r") as cfgFile:
    almostCorrect = cfgFile.read()
correct = "Default\n" + almostCorrect
# Overwrite the original file with our new string which has "Default" at the beginning:
with open("ConfigFilePath", "w") as cfgFile:
    cfgFile.write(correct)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM