# 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


from io import BytesIO
    from pyzbar.pyzbar import decode as pyzbar_decode
    from PIL import Image as pil_image
from utils import Database
import os
    import picamera
import utils
import models
#-----------------------------------------------------------------------------
# Scanner controller class
#-----------------------------------------------------------------------------
class Scanner:
    """
    This controller class handles:
    - Barcode scanning from camera's stream
    - Products fetching through FindProduct
    Usage :
    - controllers.Scanner.execute()
    """
    @classmethod
    def execute(cls):
        """
        This method constantly tries to catch barcodes from the camera :
        if it does, search for info and add it to the user's grocery list.
        """
        # Intro
        print("-"*80)
        print("[:{ Jean-Pierre's Barcode Scanner : Running")
        print("-"*80)

        # Database connection
        Database.on()

        # Load parameters
        params = models.Params()

        # Database : end connection
        # (open it only when needed because this program is meant to be shutdown harshly)

# 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: controllers/scanner.py
from io import BytesIO
    from pyzbar.pyzbar import decode as pyzbar_decode
    from PIL import Image as pil_image
from utils import Database
import os
    import picamera
import utils
import models

#-----------------------------------------------------------------------------
# Scanner controller class
#-----------------------------------------------------------------------------
class Scanner:
    """
    This controller class handles:
    - Barcode scanning from camera's stream
    - Products fetching through FindProduct
    Usage :
    - controllers.Scanner.execute()
    """
    @classmethod
    def execute(cls):
        """
        This method constantly tries to catch barcodes from the camera :
        if it does, search for info and add it to the user's grocery list.
        """
        # Intro
        print("-"*80)
        print("[:{ Jean-Pierre's Barcode Scanner : Running")
        print("-"*80)

        # Database connection
        Database.on()

        # Load parameters
        params = models.Params()

        # Database : end connection
        # (open it only when needed because this program is meant to be shutdown harshly)

        Database.off()