# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None
# 
# Path: utils/lang.py
# class Lang:
#     """
#     Internationalization tool.
#     Loads messages in a given language and keeps them as object attributes.
#     Usage :
#     - lang = Lang('en')
#     - lang.message_key
#     """
#     LANG_PATH = 'assets/lang/'
#     """ Defines lang files directory """
# 
#     def __init__(self, language='en'):
#         """
#         Loads a lang file's content as class attributes.
#         Tries to find given language, falls back to english if needed.
#         :param language: language to load (default : en)
#         :type language: str
#         :rtype: bool
#         """
#         self.language = language
#         self.language = self.language.replace('..', '')
#         self.language = self.language.replace('/', '')
# 
#         # Path to the "lang" directory
#         langfile = Lang.LANG_PATH + self.language + '.json'
# 
#         # Is the wanted language available ?
#         if not os.path.isfile(langfile):
#             langfile = Lang.LANG_PATH + 'en.json' # Load default
#             self.language = 'en'
# 
#         # Load file
#         data = open(langfile, encoding='utf-8').read()
#         data = json.loads(data)
# 
#         # Load keys as attributes
#         for key, value in data.items():
#             setattr(self, key, value)
# 
#     @classmethod
#     def available(cls):
#         """
#         Returns a list of available languages
#         :rtype: list
#         """
#         available = []
#         for langfile in os.listdir(Lang.LANG_PATH):
#             if os.path.isfile(Lang.LANG_PATH + langfile):
#                 available.append(langfile.replace('.json', ''))
# 
#         return available


import random
import string
import re
import hashlib
import getpass
import models
from utils import Database, Lang
        else:
            params.add_item('buzzer_on', '0')
            params.add_item('buzzer_port', '0')

        # Ask for : camera resolution
        params.delete_item('camera_res_x')
        params.delete_item('camera_res_y')

        for axis in ['x', 'y']:
            if axis == 'x':
                question = lang.config_camera_res_x
            else:
                question = lang.config_camera_res_y

            resolution = input(question)
            if not re.findall('([0-9]+)', resolution):
                resolution = 500

            params.add_item('camera_res_{}'.format(axis), resolution)

        # Ask for : user password
        params.delete_item('user_password')
        user_password = getpass.getpass(lang.config_password)
        if not user_password:
            user_password = 'admin'
        user_password = bytearray(user_password, encoding='utf-8')
        user_password = hashlib.sha1(user_password).hexdigest()
        params.add_item('user_password', user_password)

        # Close connection to the database

# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None
# 
# Path: utils/lang.py
# class Lang:
#     """
#     Internationalization tool.
#     Loads messages in a given language and keeps them as object attributes.
#     Usage :
#     - lang = Lang('en')
#     - lang.message_key
#     """
#     LANG_PATH = 'assets/lang/'
#     """ Defines lang files directory """
# 
#     def __init__(self, language='en'):
#         """
#         Loads a lang file's content as class attributes.
#         Tries to find given language, falls back to english if needed.
#         :param language: language to load (default : en)
#         :type language: str
#         :rtype: bool
#         """
#         self.language = language
#         self.language = self.language.replace('..', '')
#         self.language = self.language.replace('/', '')
# 
#         # Path to the "lang" directory
#         langfile = Lang.LANG_PATH + self.language + '.json'
# 
#         # Is the wanted language available ?
#         if not os.path.isfile(langfile):
#             langfile = Lang.LANG_PATH + 'en.json' # Load default
#             self.language = 'en'
# 
#         # Load file
#         data = open(langfile, encoding='utf-8').read()
#         data = json.loads(data)
# 
#         # Load keys as attributes
#         for key, value in data.items():
#             setattr(self, key, value)
# 
#     @classmethod
#     def available(cls):
#         """
#         Returns a list of available languages
#         :rtype: list
#         """
#         available = []
#         for langfile in os.listdir(Lang.LANG_PATH):
#             if os.path.isfile(Lang.LANG_PATH + langfile):
#                 available.append(langfile.replace('.json', ''))
# 
#         return available


# Path: controllers/config.py
import random
import string
import re
import hashlib
import getpass
import models
from utils import Database, Lang

        else:
            params.add_item('buzzer_on', '0')
            params.add_item('buzzer_port', '0')

        # Ask for : camera resolution
        params.delete_item('camera_res_x')
        params.delete_item('camera_res_y')

        for axis in ['x', 'y']:
            if axis == 'x':
                question = lang.config_camera_res_x
            else:
                question = lang.config_camera_res_y

            resolution = input(question)
            if not re.findall('([0-9]+)', resolution):
                resolution = 500

            params.add_item('camera_res_{}'.format(axis), resolution)

        # Ask for : user password
        params.delete_item('user_password')
        user_password = getpass.getpass(lang.config_password)
        if not user_password:
            user_password = 'admin'
        user_password = bytearray(user_password, encoding='utf-8')
        user_password = hashlib.sha1(user_password).hexdigest()
        params.add_item('user_password', user_password)

        # Close connection to the database

        Database.off()