Example #1
0
    def menu(self):
        """
        This function realizes possibility work with database from console
        """
        ch = ''
        phone_book = Database("base")

        while ch != '5':
            print()
            View.print_menu()
            ch = input()
            if ch == '1':
                self.run_show_all_database(phone_book)
            elif ch == '2':
                self.run_get_record_from_database(phone_book)
            elif ch == '3':
                self.run_add_record_to_database(phone_book)
            elif ch == '4':
                self.run_delete_record_from_database(phone_book)
            elif ch == '5':
                break
            else:
                continue

        phone_book.save("base")
        View.print_program_completed()
Example #2
0
class TestModule(unittest.TestCase):

    test_base = Database("test")

    def test_len(self):
        self.assertEqual(self.test_base.__len__(), len(self.test_base.base))

    def test_save(self):
        self.assertEqual(self.test_base.save("test"), None)

    def test_check_record(self):
        self.test_base.base = [{
            "first_name": "a",
            "last_name": "b",
            "phone_number": "23"
        }]
        self.assertEqual(self.test_base.check_record_in_database("a", "b"), 0)
        self.test_base.base = []
        self.assertEqual(self.test_base.check_record_in_database("a", "b"), -1)

    def test_print_record(self):
        self.test_base.base = [{
            "first_name": "a",
            "last_name": "b",
            "phone_number": "123"
        }]
        self.assertEqual(self.test_base.print_one_record("c", "d"), -1)
        self.assertEqual(self.test_base.print_one_record("a", "b"), 1)

    def test_add_record(self):
        self.test_base.base = [{
            "first_name": "a",
            "last_name": "b",
            "phone_number": "123"
        }]
        self.assertEqual(self.test_base.add_record("a", "b", "123"), -1)
        self.assertEqual(self.test_base.add_record("c", "d", "567"), 1)

    def test_delete_record(self):
        self.test_base.base = [{
            "first_name": "a",
            "last_name": "b",
            "phone_number": "123"
        }]
        self.assertEqual(self.test_base.delete_record(0), [])

    def test_print_all_base(self):
        self.test_base.base = [{
            "first_name": "a",
            "last_name": "b",
            "phone_number": "123"
        }]
        self.assertEqual(self.test_base.print_all_database(), 1)
        self.test_base.base = []
        self.assertEqual(self.test_base.print_all_database(), -1)
Example #3
0
def setupDB():
    config = Config()
    dbConn = Database(config)

    # read SQL skeleton
    with open(os.path.join(os.getcwd(), 'setup/db_create.sql'), 'r') as f:
        sql = f.read()
    
    # fill in placeholders
    sql = sql.replace('&user', config.getProperty('Database', 'user'))

    # run SQL
    dbConn.execute(sql, None, None)

    # add admin user
    sql = '''
        INSERT INTO aide_admin.user (name, email, hash, issuperuser)
        VALUES (%s, %s, %s, %s)
        ON CONFLICT (name) DO NOTHING;
    '''
    adminPass = config.getProperty('Project', 'adminPassword')
    uHandler = UserHandling.backend.middleware.UserMiddleware(config)
    adminPass = uHandler._create_hash(adminPass.encode('utf8'))

    values = (config.getProperty('Project', 'adminName'), config.getProperty('Project', 'adminEmail'), adminPass, True,)
    dbConn.execute(sql, values, None)
    def __Authenticated_User_Instructions(self, _User, _Image):
        #### Take Finger Print ####
        Sound.FingerPrint_Instructions()
        sleep(3)
        if fingerprint.Get_FingerPrint() == _User:

            #sound 'لم تتم المطابقة'
            Sound.FingerPrintAuthFaild()
            return
            #start to recognize face again (start from begining)
        else:

            #### Put User Image ####
            self.__User_Name = _User
            self.__User_Image = _Image

            #### Authenticated User confirmed ####
            Sound.AuthenticatedUser_Instructions()

            sleep(1.5)

            #### Take Temperature ####
            Sound.Temperature_Instructions()
            _Temp = Temperature.GetTemperature()

            #### Save Into Database ####
            Db = Database.GetDatabase("SqlServer")
            Db.Connect()
            Db.Insert(
                "INSERT INTO Attendence (User_Card_id, Temperature) VALUES(?, ?)",
                _User, _Temp)

            #### Handle Temperature ####
            if Temperature.Is_Normal(_Temp):
                Sound.NormalTemp_Instructions()
                Sound.WelcomeMessage_Instructions()
            else:
                Sound.NotNormalTemp_Instructions()

            #### Print User ####
            print(_User)
            sleep(5)

            #### Restart Configurations ####
            self.__User_Name = None
            self.__User_Image = None
            self.__ShowPicture_Sound = False
Example #5
0
def migrate_aide():
    from modules import Database, UserHandling
    from util.configDef import Config

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')

    warnings = []
    errors = []

    # bring all projects up-to-date (if registered within AIDE)
    projects = dbConn.execute('SELECT shortname FROM aide_admin.project;',
                              None, 'all')
    if projects is not None and len(projects):

        # get all schemata and check if project still exists
        schemata = dbConn.execute(
            'SELECT schema_name FROM information_schema.schemata', None, 'all')
        if schemata is not None and len(schemata):
            schemata = set([s['schema_name'].lower() for s in schemata])
            for p in projects:
                try:
                    pName = p['shortname']

                    # check if project still exists
                    if not pName.lower() in schemata:
                        warnings.append(
                            f'WARNING: project "{pName}" is registered but does not exist in database.'
                        )
                        #TODO: option to auto-remove?
                        continue

                    # make modifications one at a time
                    for mod in MODIFICATIONS_sql:
                        dbConn.execute(mod.format(schema=pName), None, None)
                except Exception as e:
                    errors.append(str(e))
        else:
            warnings.append(
                'WARNING: no project schemata found within database.')
    else:
        warnings.append('WARNING: no project registered within AIDE.')

    return warnings, errors
db_connection_name = 'myfitnessdiary:us-east1:myfitnessdiary'

# When deployed to App Engine, the `GAE_ENV` environment variable will be
# set to `standard`
if os.environ.get('GAE_ENV') == 'standard':
    # If deployed, use the local socket interface for accessing Cloud SQL
    unix_socket = '/cloudsql/{}'.format(db_connection_name)
    engine_url = 'mysql+pymysql://{}:{}@/{}?unix_socket={}'.format(
        db_user, db_password, db_name, unix_socket)
else:
    # If running locally, use the IP address to connect
    host = db_ip
    engine_url = 'mysql+pymysql://{}:{}@{}/{}'.format(db_user, db_password,
                                                      host, db_name)

db = Database.Database(engine_url)

# End Globals


#   Flask-Login User class
class User(UserMixin):
    # tracks how many times this class has been created
    # doubles as a unique login id
    instances = 0

    def __init__(self, user_id, email, password, role, name, last_login):
        self.id = user_id
        self.email = email
        self.pass_hash = generate_password_hash(password)
        self.role = role
    #                 help='Timestamp of earliest predictions to consider (default: None, i.e. all).')
    args = parser.parse_args()

    # setup
    print('Setup...\n')
    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    from tqdm import tqdm
    from util.configDef import Config
    from modules import Database

    config = Config()

    # setup DB connection
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')

    # check if correct type of annotations
    annotationType = dbConn.execute(
        'SELECT annotationtype FROM aide_admin.project WHERE shortname = %s;',
        (args.project, ), 1)
    if not len(annotationType):
        raise Exception(
            f'Project with name "{args.project}" could not be found in database.'
        )
    annotationType = annotationType[0]['annotationtype']
    exportAnnotations = args.export_annotations
    if exportAnnotations and not (annotationType == 'boundingBoxes'):
    from PIL import Image
    import base64
    from io import BytesIO
    from util.configDef import Config
    from modules import Database

    config = Config()

    # check if correct type of annotations
    exportAnnotations = args.export_annotations
    if exportAnnotations and not config.getProperty('Project', 'annotationType') == 'segmentationMasks':
        print('Warning: project annotations are not segmentation masks; skipping annotation export...')
        exportAnnotations = False

    # setup DB connection
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')


    # check if valid file format provided
    valid_file_formats = (
        'jpg',
        'jpeg',
        'png',
        'gif',
        'tif',
        'tiff',
        'bmp',
        'ico',
Example #9
0
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    import glob
    from tqdm import tqdm
    import datetime
    from PIL import Image
    from util.configDef import Config
    from modules import Database

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')

    valid_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.tif', '.tiff',
                        '.bmp', '.ico', '.jfif', '.pjpeg', '.pjp')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if not imgBaseDir.endswith('/'):
    'ALTER TABLE {schema}.labelclass ADD COLUMN IF NOT EXISTS keystroke SMALLINT UNIQUE;',
    'ALTER TABLE {schema}.image ADD COLUMN IF NOT EXISTS last_requested TIMESTAMPTZ;'
]



if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Update AIde database structure.')
    parser.add_argument('--settings_filepath', type=str, default='config/settings.ini', const=1, nargs='?',
                    help='Manual specification of the directory of the settings.ini file; only considered if environment variable unset (default: "config/settings.ini").')
    args = parser.parse_args()

    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    from util.configDef import Config
    from modules import Database

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')


    # make modifications one at a time
    for mod in MODIFICATIONS_sql:
        dbConn.execute(mod.format(schema=dbSchema), None, None)

    print('Project {} is now up-to-date for the latest changes in AIde.'.format(config.getProperty('Project', 'projectName')))
Example #11
0
def assemble_server(verbose_start=True,
                    check_v1_config=True,
                    migrate_database=True,
                    force_migrate=False,
                    passive_mode=False):

    # force verbosity if any of the pre-flight checks is enabled
    verbose_start = any((verbose_start, check_v1_config, migrate_database))

    instance_args = os.environ['AIDE_MODULES'].split(',')

    if verbose_start:
        configPath = os.environ['AIDE_CONFIG_PATH']
        aideModules = ', '.join(instance_args)

        print(f'''\033[96m
#################################       
                                        version {AIDE_VERSION}
   ###    #### ########  ########       
  ## ##    ##  ##     ## ##             {platform.platform()}
 ##   ##   ##  ##     ## ##             
##     ##  ##  ##     ## ######         [config]
#########  ##  ##     ## ##             .> {configPath}
##     ##  ##  ##     ## ##             
##     ## #### ########  ########       [modules]
                                        .> {aideModules}
#################################\033[0m
''')

    statusOffset = LogDecorator.get_ljust_offset()

    # load configuration
    config = Config(None, verbose_start)
    bottle.BaseRequest.MEMFILE_MAX = 1024**3  #TODO: make hyperparameter in config?

    # connect to database
    dbConnector = Database(config, verbose_start)

    if check_v1_config:
        # check if config file points to unmigrated v1 project
        print('Checking database...'.ljust(statusOffset), end='')
        hasAdminTable = dbConnector.execute(
            '''
                SELECT EXISTS (
                    SELECT FROM information_schema.tables 
                    WHERE table_schema = 'aide_admin'
                    AND table_name = 'project'
                );
            ''', None, 1)
        if not hasAdminTable[0]['exists']:
            # not (yet) migrated, raise Exception with instructions to ensure compatibility
            LogDecorator.print_status('fail')
            print(f'''
        The current installation of AIDE:
            database host: {config.getProperty('Database', 'host')}
            database name: {config.getProperty('Database', 'name')}
            schema:        {config.getProperty('Database', 'schema', str, '(not specified)')}

        points to an installation of the legacy AIDE v1.
        If you wish to continue using AIDE v2, you have to upgrade the project accordingly.
        For instructions to do so, see here:
            https://github.com/microsoft/aerial_wildlife_detection/blob/multiProject/doc/upgrade_from_v1.md
            ''')
            sys.exit(1)

        else:
            LogDecorator.print_status('ok')

        # check if projects have been migrated
        print('Checking projects...'.ljust(statusOffset), end='')
        dbSchema = config.getProperty('Database', 'schema', str, None)
        if dbSchema is not None:
            isMigrated = dbConnector.execute(
                '''
                    SELECT COUNT(*) AS cnt
                    FROM aide_admin.project
                    WHERE shortname = %s;
                ''', (dbSchema, ), 1)
            if isMigrated is not None and len(
                    isMigrated) and isMigrated[0]['cnt'] == 0:
                LogDecorator.print_status('warn')
                print(f'''
        WARNING: the selected configuration .ini file
        ("{os.environ['AIDE_CONFIG_PATH']}")
        points to a project that has not yet been migrated to AIDE v2.
        Details:
            database host: {config.getProperty('Database', 'host')}
            database name: {config.getProperty('Database', 'name')}
            schema:        {dbSchema}

        If you wish to continue using AIDE v2 for this project, you have to upgrade it
        to v2 accordingly.
        For instructions to do so, see here:
            https://github.com/microsoft/aerial_wildlife_detection/blob/multiProject/doc/upgrade_from_v1.md
            ''')
            else:
                LogDecorator.print_status('ok')
        else:
            LogDecorator.print_status('ok')

    if migrate_database:
        # bring AIDE up-to-date
        print('Updating database...'.ljust(statusOffset), end='')
        warnings, errors = migrate_aide(force_migrate)
        if len(warnings) or len(errors):
            if len(errors):
                LogDecorator.print_status('fail')
            else:
                LogDecorator.print_status('warn')

            print(
                f'Warnings and/or errors occurred while updating AIDE to the latest version ({AIDE_VERSION}):'
            )
            print('\nWarnings:')
            for w in warnings:
                print(f'\t"{w}"')

            print('\nErrors:')
            for e in errors:
                print(f'\t"{e}"')

            if len(errors):
                sys.exit(2)

        else:
            LogDecorator.print_status('ok')

    # prepare bottle
    app = Bottle()

    # parse requested instances
    instances = {}

    # "singletons"
    dbConnector = REGISTERED_MODULES['Database'](config, verbose_start)
    userHandler = REGISTERED_MODULES['UserHandler'](config, app, dbConnector)
    taskCoordinator = REGISTERED_MODULES['TaskCoordinator'](config, app,
                                                            dbConnector,
                                                            verbose_start)
    taskCoordinator.addLoginCheckFun(userHandler.checkAuthenticated)

    for i in instance_args:

        moduleName = i.strip()
        if moduleName == 'UserHandler':
            continue

        moduleClass = REGISTERED_MODULES[moduleName]

        # verify
        _verify_unique(instances, moduleClass)

        # create instance
        if moduleName == 'AIController':
            instance = moduleClass(config, app, dbConnector, taskCoordinator,
                                   verbose_start, passive_mode)
        else:
            instance = moduleClass(config, app, dbConnector, verbose_start)
        instances[moduleName] = instance

        # add authentication functionality
        if hasattr(instance, 'addLoginCheckFun'):
            instance.addLoginCheckFun(userHandler.checkAuthenticated)

        # launch project meta modules
        if moduleName == 'LabelUI':
            aideAdmin = REGISTERED_MODULES['AIDEAdmin'](config, app,
                                                        dbConnector,
                                                        verbose_start)
            aideAdmin.addLoginCheckFun(userHandler.checkAuthenticated)
            reception = REGISTERED_MODULES['Reception'](config, app,
                                                        dbConnector)
            reception.addLoginCheckFun(userHandler.checkAuthenticated)
            configurator = REGISTERED_MODULES['ProjectConfigurator'](
                config, app, dbConnector)
            configurator.addLoginCheckFun(userHandler.checkAuthenticated)
            statistics = REGISTERED_MODULES['ProjectStatistics'](config, app,
                                                                 dbConnector)
            statistics.addLoginCheckFun(userHandler.checkAuthenticated)

        elif moduleName == 'FileServer':
            from modules.DataAdministration.backend import celery_interface as daa_int

        elif moduleName == 'AIController':
            from modules.AIController.backend import celery_interface as aic_int

            # launch model marketplace with AIController
            modelMarketplace = REGISTERED_MODULES['ModelMarketplace'](
                config, app, dbConnector, taskCoordinator)
            modelMarketplace.addLoginCheckFun(userHandler.checkAuthenticated)

        elif moduleName == 'AIWorker':
            from modules.AIWorker.backend import celery_interface as aiw_int

        # launch globally required modules
        dataAdmin = REGISTERED_MODULES['DataAdministrator'](config, app,
                                                            dbConnector,
                                                            taskCoordinator)
        dataAdmin.addLoginCheckFun(userHandler.checkAuthenticated)

        staticFiles = REGISTERED_MODULES['StaticFileServer'](config, app,
                                                             dbConnector)
        staticFiles.addLoginCheckFun(userHandler.checkAuthenticated)

    if verbose_start:
        print('\n')

    return app
    def __UnAuthenticated_User_Instructions(self):
        Visitor_id = ''.join(random.choice(string.digits) for i in range(13))
        Visitor_Dir = self.__Current_Dir + '/images/' + str(Visitor_id)
        os.mkdir(Visitor_Dir)
        ImageProcessing.Save_Image(self.__Origin_Frame,
                                   Visitor_Dir + '/Image.jpg')

        #### Not Authenticated User ####
        Sound.UnAuthenticatedUser_Instructions()

        #### Take Front Image ####
        Sound.GetUserFrontPicture_Instructions()
        Sound.Alarm_Sound()
        sleep(3)

        self.__Capture_Image = 1
        while self.__Capture_Image != 0:
            pass
        ImageProcessing.Save_Image(self.__Front_Image,
                                   Visitor_Dir + '/Front_Image.jpg')
        self.__Front_Image = None

        Sound.CameraCapture_Sound()
        sleep(0.5)

        #### Take Right Image ####
        Sound.GetUserRightPicture_Instructions()
        #Sound.Alarm_Sound()
        sleep(2)

        self.__Capture_Image = 3
        while self.__Capture_Image != 0:
            pass
        ImageProcessing.Save_Image(self.__Right_Image,
                                   Visitor_Dir + '/Right_Image.jpg')
        self.__Right_Image = None

        Sound.CameraCapture_Sound()
        sleep(0.5)

        #### Take Left Image ####
        Sound.GetUserLeftPicture_Instructions()
        #Sound.Alarm_Sound()
        sleep(2)

        self.__Capture_Image = 2
        while self.__Capture_Image != 0:
            pass
        ImageProcessing.Save_Image(self.__Left_Image,
                                   Visitor_Dir + '/Left_Image.jpg')
        self.__Left_Image = None

        Sound.CameraCapture_Sound()
        sleep(0.5)

        #### Done Image Capture ####
        Sound.DoneImageCapture_Instructions()
        sleep(0.5)

        #### Take Finger Print ####
        Sound.RegisterFingerPrint_Instructions()
        sleep(2)
        Sound.PutYourFingerAgain()
        fingerprint.Enrol_New(id=Visitor_id)

        sleep(3.5)

        #### Done Saving Finger Print ####
        Sound.FingerPrintRegistered_Instructions()
        sleep(2)

        #### Take Card Picture ####
        Sound.CardIdImage_Instruction()

        self.__Capture_Id_Card_Image = True
        threading.Thread(target=self.__TakeIdCardImage_Break).start()
        while True:
            if self.__Captured_Id_Card_Image and Detect_Card(
                    self.__Id_Card_Image, Visitor_Dir + '/Id_Card.jpg'):
                break
            elif self.__Break_All_Instructions:
                return

        self.__Captured_Id_Card_Image = False
        self.__Capture_Id_Card_Image = False
        self.__Id_Card_Image = None
        Sound.DoneIdCardCapture_Instruction()

        #### Say Your Name ####
        Sound.SayYourName_Instructions()
        SoundProccessing.Save_Sound(SoundProccessing.GetSound(5),
                                    Visitor_Dir + '/UserNameSound.wav')
        Sound.YourNameRegistered_Instructions()

        #### Take Temperature ####
        Sound.Temperature_Instructions()
        sleep(2)
        _Temp = Temperature.GetTemperature()

        #### Handle Temerature ####
        if Temperature.Is_Normal(_Temp):
            Sound.NormalTemp_Instructions()
            Sound.WelcomeMessage_Instructions()
        else:
            Sound.NotNormalTemp_Instructions()

        #### Save Into Database ####
        Db = Database.GetDatabase('SqlServer')
        Db.Connect()
        Db.Insert(
            'INSERT INTO [User]([Is_Visitor], [User_Card_id]) VALUES(?, ?)', 1,
            Visitor_id)
        Db.Insert(
            'INSERT INTO [Attendence]([User_Card_id], [Temperature]) VALUES(?, ?)',
            Visitor_id, _Temp)
Example #13
0
        'Upgrade and register an AIDE v1 project to an existing v2 installation.'
    )
    parser.add_argument(
        '--settings_filepath',
        type=str,
        help=
        'Path of the configuration .ini file for the v1 project to be upgraded to v2.'
    )
    args = parser.parse_args()

    from util.configDef import Config
    from modules import Database

    # v2 config file
    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')

    # v1 config file
    v1Config = Config(args.settings_filepath)

    # db schema of v1 project
    dbSchema = v1Config.getProperty('Database', 'schema')
    projectName = v1Config.getProperty('Project', 'projectName')

    # verify we're running a database on v2 standards
    isV2 = dbConn.execute(
        '''
        SELECT 1
        FROM   information_schema.tables 
    import base64
    from io import BytesIO
    from util.configDef import Config
    from modules import Database

    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith('/'):
        args.label_folder += '/'

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month, currentDT.day, currentDT.hour, currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')
    
    valid_image_extensions = (
        '.jpg',
        '.jpeg',
        '.png',
        '.gif',
        '.tif',
        '.tiff',
        '.bmp',
        '.ico',
        '.jfif',
        '.pjpeg',
Example #15
0
    from util.configDef import Config
    from modules import Database

    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith('/'):
        args.label_folder += '/'

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')

    valid_image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.tif', '.tiff',
                              '.bmp', '.ico', '.jfif', '.pjpeg', '.pjp')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if args.label_folder is not None and not os.path.isdir(args.label_folder):
Example #16
0
def migrate_aide(forceMigrate=False):
    from modules import Database, UserHandling
    from util.configDef import Config
    
    config = Config()
    dbConn = Database(config)
    if not dbConn.canConnect():
        raise Exception('Error connecting to database.')
    
    warnings = []
    errors = []

    # skip if not forced and if database has same version
    doMigrate = True
    
    # check if DB has version already implemented
    dbVersion = None
    hasVersion = dbConn.execute('''
        SELECT EXISTS (
            SELECT FROM information_schema.tables 
            WHERE  table_schema = 'aide_admin'
            AND    table_name   = 'version'
        ) AS hasVersion;
    ''', None, 1)
    if hasVersion[0]['hasversion']:
        # check DB version
        dbVersion = dbConn.execute('SELECT version FROM aide_admin.version;', None, 1)
        if dbVersion is not None and len(dbVersion):
            dbVersion = dbVersion[0]['version']
            needsUpdate = version.compare_versions(version.AIDE_VERSION, dbVersion)
            if needsUpdate is not None:
                if needsUpdate < 0:
                    # running an older version of AIDE with a newer DB version
                    warnings.append(f'WARNING: local AIDE version ({version.AIDE_VERSION}) is older than the one in the database ({dbVersion}); please update your installation.')
                elif needsUpdate == 0:
                    doMigrate = False
                else:
                    doMigrate = True

    if not doMigrate and not forceMigrate:
        return warnings, errors

    # bring all projects up-to-date (if registered within AIDE)
    projects = dbConn.execute('SELECT shortname FROM aide_admin.project;', None, 'all')
    if projects is not None and len(projects):

        # get all schemata and check if project still exists
        schemata = dbConn.execute('SELECT schema_name FROM information_schema.schemata', None, 'all')
        if schemata is not None and len(schemata):
            schemata = set([s['schema_name'].lower() for s in schemata])
            for p in projects:
                try:
                    pName = p['shortname']

                    # check if project still exists
                    if not pName.lower() in schemata:
                        warnings.append(f'WARNING: project "{pName}" is registered but does not exist in database.')
                        #TODO: option to auto-remove?
                        continue

                    # special modification for CNN-to-labelclass map: drop only dep. on version (remove ancient tests)
                    if version.compare_versions(version.AIDE_VERSION, dbVersion) in (-1, None):
                        dbConn.execute(sql.SQL('DROP TABLE IF EXISTS {};').format(
                            sql.Identifier(pName, 'cnn_labelclass')
                        ), None)

                    # make modifications one at a time
                    for mod in MODIFICATIONS_sql:
                        dbConn.execute(mod.format(schema=pName), None, None)

                    # pre-official 2.0: mark existing CNN states as "labelclass_autoupdate" (as this was the default behavior)
                    if version.compare_versions(dbVersion, '2.0.210514') == -1:
                        dbConn.execute(sql.SQL('''
                            UPDATE {}
                            SET labelclass_autoupdate = TRUE;
                        ''').format(sql.Identifier(pName, 'cnnstate')), None)

                except Exception as e:
                    errors.append(str(e))
        else:
            warnings.append('WARNING: no project schemata found within database.')
    else:
        warnings.append('WARNING: no project registered within AIDE.')

    # update DB version accordingly
    dbConn.execute('''
        DELETE FROM aide_admin.version;
        INSERT INTO aide_admin.version (version)
        VALUES (%s);
    ''', (version.AIDE_VERSION, ))

    return warnings, errors
Example #17
0
class KeysController:
    book = Database("base")

    parser = argparse.ArgumentParser(description='Keys controller '
                                     'for Database.')

    parser.add_argument("-show", help="show all database", action='store_true')
    parser.add_argument("-find",
                        help="find record in database",
                        action='store_true')
    parser.add_argument("-add",
                        help="add record to database",
                        action='store_true')
    parser.add_argument("-delete",
                        help="delete record from database",
                        action='store_true')

    def run_get_record_from_database(self):
        print("-" * 80)
        View.print_input_first_name()
        f_name = input()
        View.print_input_last_name()
        l_name = input()
        flag = self.book.print_one_record(f_name, l_name)
        if flag == -1:
            View.print_if_record_absent()
        print("-" * 80)

    def run_show_all_database(self):
        print("-" * 80)
        flag = self.book.print_all_database()
        if flag == -1:
            View.print_if_database_empty()
        print("-" * 80)

    def run_add_record_to_database(self):
        View.print_input_first_name()
        f_name = input()
        View.print_input_last_name()
        l_name = input()
        View.print_input_phone_number()
        phone_number = input()
        flag = self.book.add_record(f_name, l_name, phone_number)
        if flag == 1:
            View.print_add_message()
        else:
            View.print_if_record_exist()

    def run_delete_record_from_database(self):
        View.print_input_first_name()
        f_name = input()
        View.print_input_last_name()
        l_name = input()
        index = self.book.check_record_in_database(f_name, l_name)
        if index != -1:
            View.print_confirm_question()
            confirm = input()
            if confirm.lower() == 'y':
                self.book.delete_record(index)
                View.print_delete_message()
        else:
            View.print_if_record_absent()

    def __init__(self):
        self.args = self.parser.parse_args()
        if self.args.show:
            self.run_show_all_database()
        elif self.args.find:
            self.run_get_record_from_database()
        elif self.args.add:
            self.run_add_record_to_database()
        elif self.args.delete:
            self.run_delete_record_from_database()
        self.book.save("base")
            already launched modules on this instance.
            Raises an Exception if another module from the same type has already been launched on this instance
        '''
    for key in instances.keys():
        instance = instances[key]
        if moduleClass.__class__.__name__ == instance.__class__.__name__:
            raise Exception(
                'Module {} already launched on this server.'.format(
                    moduleClass.__class__.__name__))


# load configuration
config = Config()

# check if config file points to unmigrated v1 project
dbConnector = Database(config)
hasAdminTable = dbConnector.execute(
    '''
        SELECT EXISTS (
            SELECT FROM information_schema.tables 
            WHERE table_schema = 'aide_admin'
            AND table_name = 'project'
        );
    ''', None, 1)
if not hasAdminTable[0]['exists']:
    # not (yet) migrated, raise Exception with instructions to ensure compatibility
    print(f'''
The current installation of AIDE:
    database host: {config.getProperty('Database', 'host')}
    database name: {config.getProperty('Database', 'name')}
    schema:        {config.getProperty('Database', 'schema', str, '(not specified)')}
Example #19
0
                        nargs='?',
                        help='Target filename for the model.')
    args = parser.parse_args()

    # setup
    print('Setup...')
    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    from util.configDef import Config
    from modules import Database

    config = Config()

    # setup DB connection
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')

    # get state dict
    print('Retrieving model state...')
    stateDict_raw = dbConn.execute(
        'SELECT statedict FROM {schema}.cnnstate WHERE partial IS FALSE ORDER BY timecreated DESC LIMIT 1;'
        .format(schema=config.getProperty('Database', 'schema')), None, 1)

    # convert from bytes and save to disk
    print('Saving model state...')
    stateDict_parsed = io.BytesIO(stateDict_raw[0]['statedict'])
    stateDict_parsed = torch.load(stateDict_parsed,
                                  map_location=lambda storage, loc: storage)
    args = parser.parse_args()

    # setup
    print('Setup...')
    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    import glob
    from tqdm import tqdm
    import datetime
    from PIL import Image
    from util.configDef import Config
    from modules import Database

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if not imgBaseDir.endswith('/'):
        imgBaseDir += '/'

    # get all image paths from the database
Example #21
0
    from util.configDef import Config
    from modules import Database
    from datetime import datetime
    import pytz

    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith('/'):
        args.label_folder += '/'

    currentDT = datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month, currentDT.day, currentDT.hour, currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')
    dbSchema = config.getProperty('Database', 'schema')
    
    valid_image_extensions = (
        '.jpg',
        '.jpeg',
        '.png',
        '.gif',
        '.tif',
        '.tiff',
        '.bmp',
        '.ico',
        '.jfif',
        '.pjpeg',
Example #22
0
    from modules import Database

    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith(
            os.sep):
        args.label_folder += os.sep

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if dbConn.connectionPool is None:
        raise Exception('Error connecting to database.')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if args.label_folder is not None and not os.path.isdir(args.label_folder):
        raise Exception(
            '"{}" is not a valid directory on this machine.'.format(
                args.label_folder))
from modules import Database, UserHandling


if __name__ == '__main__':

    # setup
    parser = argparse.ArgumentParser(description='Set up AIDE database schema.')
    parser.add_argument('--settings_filepath', type=str, default='config/settings.ini', const=1, nargs='?',
                    help='Manual specification of the directory of the settings.ini file; only considered if environment variable unset (default: "config/settings.ini").')
    args = parser.parse_args()

    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = args.settings_filepath

    config = Config()
    dbConn = Database(config)

    # read SQL skeleton
    with open(os.path.join(os.getcwd(), 'setup/db_create.sql'), 'r') as f:
        sql = f.read()
    
    # fill in placeholders
    sql = sql.replace('&user', config.getProperty('Database', 'user'))

    # run SQL
    dbConn.execute(sql, None, None)

    # add admin user
    sql = '''
        INSERT INTO aide_admin.user (name, email, hash, issuperuser)
        VALUES (%s, %s, %s, %s)
Example #24
0
    from modules import Database

    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith(
            os.sep):
        args.label_folder += os.sep

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if not dbConn.canConnect():
        raise Exception('Error connecting to database.')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if args.label_folder is not None and not os.path.isdir(args.label_folder):
        raise Exception(
            '"{}" is not a valid directory on this machine.'.format(
                args.label_folder))
Example #25
0
    print('Setup...')
    if not 'AIDE_CONFIG_PATH' in os.environ:
        os.environ['AIDE_CONFIG_PATH'] = str(args.settings_filepath)

    from tqdm import tqdm
    import datetime
    from util.configDef import Config
    from modules import Database

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if not dbConn.canConnect():
        raise Exception('Error connecting to database.')
    project = args.project

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            f'"{imgBaseDir}" is not a valid directory on this machine. Are you running the script from the file server?'
        )

    if not imgBaseDir.endswith(os.sep):
        imgBaseDir += os.sep

    # locate all images and their base names