示例#1
0
def createDatabase(name: str) -> bool:
    """
    Creates a database in the format of python. If spaces are in the name, it will be cut off through trim(). Returns true if the database is successfully created.
    >>> import coconut
    #The database format must always be python, it still supports if name has no .py
    >>> coconut.database.createDatabase("Example.py")
    True
    """
    if backend.trim(name) == '':
        raise DatabaseNameError(f'Name {name} is invalid and is just spaces')
    else:
        if not name.lower().endswith('.py'):
            if len(name.split('.')) > 1:
                raise InvalidTypeDatabaseError(
                    'Extension type {} is invalid'.format(name.split('.')[-1]))
            else:
                if name.endswith('.py'):
                    pass
                else:
                    name = name + '.py'
                os.chdir(os.path.join(backend.throwback(), 'databases'))
                f = open(f'{name}', 'w+')
                f.close()
                return True
        else:
            os.chdir(os.path.join(backend.throwback(), 'databases'))
            f = open(f'{name}', 'w+')
            f.close()
            return True
示例#2
0
 def createDatabaseWithin(self, name: str) -> bool:
     """
     Cretes a database within the category itself. The format of the database must always be .py
     """
     os.chdir(self.path)
     if backend.trim(name) == '':
         raise DatabaseNameError(
             f'Name {name} is invalid and is just spaces')
     else:
         if not name.lower().endswith('.py'):
             if len(name.split('.')) > 1:
                 raise InvalidTypeDatabaseError(
                     'Extension type {} is invalid'.format(
                         name.split('.')[-1]))
             else:
                 if name.endswith('.py'):
                     pass
                 else:
                     name = name + '.py'
                 f = open(f'{name}', 'w+')
                 f.close()
                 return True
         else:
             os.chdir(os.path.join(backend.throwback(), 'databases'))
             f = open(f'{name}', 'w+')
             f.close()
             return True
示例#3
0
 def __init__(self, nameofcategory: str):
     self.nameofcategory = str(nameofcategory)
     self.path = os.path.join(backend.throwback(), 'databases',
                              self.nameofcategory)
     if os.path.exists(self.path):
         self.exists = True
     else:
         self.exists = False
示例#4
0
def readDatabase(name: str) -> str:
    """
    Reads the database
    """
    if not name.endswith('.py'):
        raise InvalidTypeDatabaseError(f'Name of database is invalid.')
    else:
        pass
    os.chdir(os.path.join(backend.throwback(), 'databases'))
    if os.path.exists(name):
        with open(name, 'r') as text:
            return text.read()
    else:
        raise NoDatabaseError(f'Database {name} does not exist')
示例#5
0
def executeDatabase(filename: str) -> bool:
    """
    Executes a database
    """
    if not filename.endswith('.py'):
        raise InvalidTypeDatabaseError('Name of database is invalid.')
    else:
        pass
    os.chdir(os.path.join(backend.throwback(), 'databases'))
    if os.path.exists(filename):
        with open(filename, 'r') as code:
            exec(code.read())
            code.close()
            return True
    else:
        raise NoDatabaseError(f'Database {filename} does not exist')
示例#6
0
def appendDatabase(filename: str, code: str) -> bool:
    """
    Appends a code of line to the database, that is where the real part kicks in!
    """
    if not filename.endswith('.py'):
        raise InvalidTypeDatabaseError('Name of database is invalid.')
    else:
        pass
    os.chdir(os.path.join(backend.throwback(), 'databases'))
    if os.path.exists(filename):
        with open(filename, 'a+') as Code:
            Code.write(str(code))
            Code.close()
            return True
    else:
        raise NoDatabaseError(f'Database {filename} does not exist')
示例#7
0
def deleteDatabase(name: str) -> bool:
    """
    Deletes the database.
    Returns True if the database has been successfully deleted.
    Raises NoDatabaseError if the name of the database does not exist.
    Raises InvalidTypeDatabaseError if name of database does not exist or type of file given is invalid.
    >>> import coconut.database
    >>> coconut.database.deleteDatabase("Example")
    """
    if not name.endswith('.py'):
        raise InvalidTypeDatabaseError(f'Name of database is invalid.')
    else:
        pass
    os.chdir(os.path.join(backend.throwback(), 'databases'))
    if os.path.exists(name):
        os.remove(name)
        return True
    else:
        raise NoDatabaseError(f'Database {name} does not exist')