Example #1
0
class Database:
    workspace_name = "mydatabase/"
    config_file_name = "users.config"
    users_table = "users_table.dat"
    users = []
    validador = ""
    last_id = 0  # id de los bots

    def __init__(self):
        if (os.path.isdir(self.workspace_name) is False):
            self.create_default()
        elif (os.path.isfile(self.workspace_name + self.config_file_name) is
              False):
            self.create_default("onlyconfig")
        else:
            config_file = open(self.workspace_name + self.config_file_name,
                               "r")
            if (config_file.mode == "r"):
                self.last_id = config_file.readline()  # last bot id
                user = config_file.readline()  # reads admin user
                while user:
                    self.users.append(user)
                    user = config_file.readline()  # reads next user
                config_file.close()
        self.validador = Validator(self.workspace_name)

    def getWorkspace(self):
        return self.workspace_name

    def getConfig_file(self):
        return self.workspace_name + self.config_file_name

    def getValidador(self):
        return self.validador

    def _getLastId(self):
        lid = int(self.last_id)
        lid = lid + 1
        self.last_id = lid
        return self.last_id

    def add_user(self, user_name,
                 user_pword):  # falta implementarlo en la web, mas adelante
        user_id = self.validador.createUser(user_name, user_pword)
        if (user_id == -1):
            return "Existing user"
        config_file = open(self.workspace_name + self.config_file_name, "a")
        config_file.write(user_id + "\n")
        config_file.close()
        os.mkdir(os.path.join(self.workspace_name + user_id))
        return "User added"

    def add_bot(self, user_id, toFollow=None, user_email="<YOUR BOT EMAIL>"):
        if toFollow is None:
            toFollow = []
        user_path = self.workspace_name + str(user_id) + "/"
        nbot = pyBot().create(user_email, self._getLastId(), toFollow,
                              user_path)
        f = open(user_path + str(user_id) + ".config", "a+")
        f.write(nbot.getId() + "\n")

    def create_default(self, conf=""):  # genera la sesion root, id 0
        if (conf == ""):
            os.mkdir(os.path.join("./" + self.workspace_name))
        config_file = open(self.workspace_name + self.config_file_name, "w+")
        config_file.write("0\n")
        config_file.write("0\n")
        config_file.close()
        if (os.path.isdir(self.workspace_name + "0") is False):
            adminpath = os.path.join("./" + self.workspace_name + "0")
            os.mkdir(adminpath)
            testbot = open(adminpath + "/" + "0.config", "w+")
            testbot.write("0")
            testbot.close()
            botpath = adminpath + "/" + "0" + ".bot"
            f = open(botpath, "w+")
            f.write("0" + "\n")
            f.write("<ADMINs EMAIL>" + "\n")
            f.write("<ADMIN ACCOUNT>" + "\n")
            f.write("<ADMIN PASSWORD>" + "\n")
            f.write("@wazime.es")