Hi! Developing my school project app, I have this:

I have a dir called _configdir with some .ini files, these files have
this structure:

[ROOMNAME]
owner=somename
data1=somedata
data2=somedata
data3=sometada

Those inis are sent to my sever from many other pcs trough ftp. I made
this script for parsing and saving the values into a db:

import os
import ConfigParser
import traceback
import pymysql
import time

_configDir = "_configuploads"


def save(val):


    connection = pymysql.connect(host='127.0.0.1', port=3306,
user='script', passwd="123", db='hotel')
    cursor = connection.cursor()

    doop = val['roomname']

    cursor.execute("SELECT * FROM rooms WHERE roomname = '%s'"%doop)
    row = cursor.fetchone()

    if row == None:
        print doop,'Not in db... Inserting'

        keys = val.keys()

        values = []

        for each in keys:
            values.append(val[each])

        s1 = ','.join(keys)

        count = len(val)

        per = ("%s,"*count)[:-1]

        command = "insert into rooms (%s)"%s1+" values (%s)"%per
        args =  tuple(values)
        cursor.execute(command,args)


    else:
        print doop,'Already in DB... Updating'
        keys = val.keys()

        values = []

        for each in keys:
            values.append(val[each])

        listy = []
        for key,value in val.items():
            listy.append("%s='%s'"%(key,value))

        sready = ", ".join(listy)


        command = "update rooms set %s"%sready+"where roomname =
'%s'"%doop

        cursor.execute(command)



    cursor.close()
    connection.close()

def parse(filename):
    try:
        config = ConfigParser.ConfigParser()

        config.read(filename)
        rooms = config.sections()

        dictio = {}
        for room in rooms:
            dictio[room] = {}
            options = config.options(room)

            for option in options:
                dictio[room][option] = config.get(room,option)

        rrom = dictio.keys()[0]

        valdict = dictio.values()[0]
        valdict['room'] = '%s'%rrom


        save(valdict)

        return 0
    except:
        print "Something failed!!!!!"
        print traceback.format_exc()
        return 1



def main():
    objects = os.listdir(_configDir)
    for item in objects:
        fitem = os.path.join(os.getcwd(),_configDir,item)
        parse(fitem)


while True:
    time.sleep(5)
    main()



And it works fantastic!!

But I need to have the same functionality of that script into my
web2py app...

How could I do it???

Reply via email to