Ejemplo n.º 1
0
def setconfigfile(listdataconfiguration):
    """
        This function changes, adds or deletes config option in configuration file
        eg list data configuration
            ["add","agentconf","global","log_level","DEBUG"]
            or
            ["del","agentconf","global","log_level"]
        :returns: it return False or True
    """
    if len(listdataconfiguration) > 1 and directoryconffile() is not None:
        fileofconf = os.path.join(directoryconffile(),
                                  listdataconfiguration[1])
    else:
        return False
    if listdataconfiguration[0].lower() == "add":
        if len(listdataconfiguration) != 5:
            return False
        if listdataconfiguration[2] != "" and listdataconfiguration[
                3] != "" and listdataconfiguration[4] != "":
            fileconf = ConfigParser.ConfigParser()
            fileconf.read(fileofconf)
            # test si section existe.
            if not listdataconfiguration[2] in fileconf.sections():
                fileconf.add_section(listdataconfiguration[2])
            fileconf.set(listdataconfiguration[2], listdataconfiguration[3],
                         listdataconfiguration[4])
            with open(fileofconf, 'w') as configfile:
                fileconf.write(configfile)
            return True
        else:
            return False
    elif listdataconfiguration[0].lower() == "del":
        if len(listdataconfiguration) < 4:
            return False
        fileconf = ConfigParser.ConfigParser()
        fileconf.read(fileofconf)
        if listdataconfiguration[2] != "" and fileconf.has_section(
                listdataconfiguration[2]):
            if len(fileconf.options(listdataconfiguration[2])) == 0:
                fileconf.remove_section(listdataconfiguration[2])
                with open(fileofconf, 'w') as configfile:
                    fileconf.write(configfile)
                return True
            if listdataconfiguration[3] != "" and fileconf.has_option(
                    listdataconfiguration[2], listdataconfiguration[3]):
                fileconf.remove_option(listdataconfiguration[2],
                                       listdataconfiguration[3])
                if len(fileconf.options(listdataconfiguration[2])) == 0:
                    fileconf.remove_section(listdataconfiguration[2])
                with open(fileofconf, 'w') as configfile:
                    fileconf.write(configfile)
                return True
            else:
                return False
        else:
            return False
    else:
        return False
Ejemplo n.º 2
0
def setconfigfile(listdataconfiguration):
    """
        This function changes, adds or deletes config option in configuration file
        eg list data configuration
            ["add","agentconf","global","log_level","DEBUG"]
            or
            ["del","agentconf","global","log_level"]
        :returns: it return False or True
    """
    if len (listdataconfiguration) > 1 and directoryconffile() is not None:
        fileofconf = os.path.join(directoryconffile(), listdataconfiguration[1])
    else:
        return False
    if listdataconfiguration[0].lower() == "add":
        if len(listdataconfiguration) != 5:
            return False
        if listdataconfiguration[2] != "" and listdataconfiguration[3] != "" and listdataconfiguration[4] != "":
            fileconf = ConfigParser.ConfigParser()
            fileconf.read(fileofconf)
            # test si section existe.
            if not listdataconfiguration[2] in fileconf.sections():
                fileconf.add_section(listdataconfiguration[2])
            fileconf.set(listdataconfiguration[2], listdataconfiguration[3], listdataconfiguration[4])
            with open(fileofconf, 'w') as configfile:
                fileconf.write(configfile)
            return True
        else:
            return False
    elif listdataconfiguration[0].lower() == "del":
        if len(listdataconfiguration) < 4:
            return False
        fileconf = ConfigParser.ConfigParser()
        fileconf.read(fileofconf)
        if listdataconfiguration[2] != "" and fileconf.has_section(listdataconfiguration[2]):
            if len(fileconf.options(listdataconfiguration[2])) == 0:
                fileconf.remove_section(listdataconfiguration[2])
                with open(fileofconf, 'w') as configfile:
                    fileconf.write(configfile)
                return True
            if listdataconfiguration[3] != "" and fileconf.has_option(listdataconfiguration[2], listdataconfiguration[3]):
                fileconf.remove_option(listdataconfiguration[2], listdataconfiguration[3])
                if len(fileconf.options(listdataconfiguration[2])) == 0:
                    fileconf.remove_section(listdataconfiguration[2])
                with open(fileofconf, 'w') as configfile:
                    fileconf.write(configfile)
                return True
            else:
                return False
        else:
            return False
    else:
        return False
Ejemplo n.º 3
0
 def remotefileeditaction( xmppobject, data ):
     if 'data' in data and 'action' in data['data']:
         if data['data']['action'] == 'loadfile':
             if 'file' in data['data']:
                 filename = os.path.join(directoryconffile(), data['data']['file'])
                 if os.path.isfile(filename): 
                     filedata = file_get_contents(filename)
                     data['data'] = { "result" : filedata, "error" : False , 'numerror' : 0  }
                     return json.dumps(data)
                 else:
                     data['data'] = { "result" : "error file missing",  "error" : True , 'numerror' : 128}
             else:
                 data['data'] = { "result" : "error name file missing" }
         elif data['data']['action'] == 'create':
             if 'file' in data['data'] and data['data']['file'] != "" and 'content' in data['data']:
                 filename = os.path.join(directoryconffile(), data['data']['file'])
                 file_put_contents(filename,  data['data']['content'])
                 data['data'] = { "result" : "create file %s"%filename, "error" : False , 'numerror' : 0 }
                 return json.dumps(data)
             else:
                 data['data'] = { "result" : "error create file : name file missing", "error" : True , 'numerror' : 129 }
         elif data['data']['action'] == 'save':
             if 'file' in data['data'] and data['data']['file'] != "" \
                     and 'content' in data['data']:
                 filename = os.path.join(directoryconffile(), data['data']['file'])
                 if os.path.isfile(filename):
                     #datestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+".ini"
                     #newfilename = filename[:-4] + "_" + datestr
                     #copyfile(filename, newfilename)
                     file_put_contents(filename,  data['data']['content'])
                     data['data'] = { "result" : "save file %s"%filename, "error" : False , 'numerror' : 0 }
                     return json.dumps(data)
                 else:
                     data['data'] = { "result" : "error save config file %s missing"%filename, "error" : True , 'numerror' : 130 }
         elif data['data']['action'] == 'listconfigfile':
             listfileedit = [ x for x in os.listdir(directoryconffile()) if (x.endswith(".ini") or x.endswith(".ini.local"))]
             data['data'] = { "result" : listfileedit, "error" : False , 'numerror' : 0 }
             return json.dumps(data)
         else:
             data['data'] = { "result" : "error the action parameter is not correct ", "error" : True , 'numerror' : 131 }
     else:
         data['data'] = { "result" : "error action remotefileeditaction parameter incorrect", "error" : True , 'numerror' : 132 }
     return json.dumps(data)
Ejemplo n.º 4
0
 def listremotefileedit( xmppobject, data ):
     listfileedit = [ x for x in os.listdir(directoryconffile()) if x.endswith(".ini")]
     data['data']={"result" : listfileedit}
     return json.dumps(data)
Ejemplo n.º 5
0
 def remotefileeditaction(xmppobject, data):
     if 'data' in data and 'action' in data['data']:
         if data['data']['action'] == 'loadfile':
             if 'file' in data['data']:
                 filename = os.path.join(directoryconffile(),
                                         data['data']['file'])
                 if os.path.isfile(filename):
                     filedata = file_get_contents(filename)
                     data['data'] = {
                         "result": filedata,
                         "error": False,
                         'numerror': 0
                     }
                     return json.dumps(data)
                 else:
                     data['data'] = {
                         "result": "error file missing",
                         "error": True,
                         'numerror': 128
                     }
             else:
                 data['data'] = {"result": "error name file missing"}
         elif data['data']['action'] == 'create':
             if 'file' in data['data'] and data['data'][
                     'file'] != "" and 'content' in data['data']:
                 filename = os.path.join(directoryconffile(),
                                         data['data']['file'])
                 file_put_contents(filename, data['data']['content'])
                 data['data'] = {
                     "result": "create file %s" % filename,
                     "error": False,
                     'numerror': 0
                 }
                 return json.dumps(data)
             else:
                 data['data'] = {
                     "result": "error create file : name file missing",
                     "error": True,
                     'numerror': 129
                 }
         elif data['data']['action'] == 'save':
             if 'file' in data['data'] and data['data']['file'] != "" \
                     and 'content' in data['data']:
                 filename = os.path.join(directoryconffile(),
                                         data['data']['file'])
                 if os.path.isfile(filename):
                     #datestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+".ini"
                     #newfilename = filename[:-4] + "_" + datestr
                     #copyfile(filename, newfilename)
                     file_put_contents(filename, data['data']['content'])
                     data['data'] = {
                         "result": "save file %s" % filename,
                         "error": False,
                         'numerror': 0
                     }
                     return json.dumps(data)
                 else:
                     data['data'] = {
                         "result":
                         "error save config file %s missing" % filename,
                         "error": True,
                         'numerror': 130
                     }
         elif data['data']['action'] == 'listconfigfile':
             listfileedit = [
                 x for x in os.listdir(directoryconffile())
                 if (x.endswith(".ini") or x.endswith(".ini.local"))
             ]
             data['data'] = {
                 "result": listfileedit,
                 "error": False,
                 'numerror': 0
             }
             return json.dumps(data)
         else:
             data['data'] = {
                 "result": "error the action parameter is not correct ",
                 "error": True,
                 'numerror': 131
             }
     else:
         data['data'] = {
             "result":
             "error action remotefileeditaction parameter incorrect",
             "error": True,
             'numerror': 132
         }
     return json.dumps(data)
Ejemplo n.º 6
0
 def listremotefileedit(xmppobject, data):
     listfileedit = [
         x for x in os.listdir(directoryconffile()) if x.endswith(".ini")
     ]
     data['data'] = {"result": listfileedit}
     return json.dumps(data)