# Path: photobooth/camera/CameraInterface.py
# class CameraInterface:
# 
#     def __init__(self):
# 
#         self.hasPreview = False
#         self.hasIdle = False
#         self._initConfig()
# 
#     def __enter__(self):
# 
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
# 
#         self.cleanup()
# 
#     def cleanup(self):
# 
#         pass
# 
#     @property
#     def hasPreview(self):
# 
#         return self._has_preview
# 
#     @hasPreview.setter
#     def hasPreview(self, value):
# 
#         if not isinstance(value, bool):
#             raise ValueError('Expected bool')
# 
#         self._has_preview = value
# 
#     @property
#     def hasIdle(self):
# 
#         return self._has_idle
# 
#     @hasIdle.setter
#     def hasIdle(self, value):
# 
#         if not isinstance(value, bool):
#             raise ValueError('Expected bool')
# 
#         self._has_idle = value
# 
#     @property
#     def config(self):
#         return self._cfg
# 
#     def setActive(self):
# 
#         if not self.hasIdle:
#             pass
#         else:
#             raise NotImplementedError()
# 
#     def setIdle(self):
# 
#         if not self.hasIdle:
#             raise RuntimeError('Camera does not have idle functionality')
# 
#         raise NotImplementedError()
# 
#     def getPreview(self):
# 
#         if not self.hasPreview:
#             raise RuntimeError('Camera does not have preview functionality')
# 
#         raise NotImplementedError()
# 
#     def getPicture(self):
# 
#         raise NotImplementedError()
# 
#     def _initConfig(self):
# 
#         self._cfg = configparser.ConfigParser(interpolation=None)
#         filename = os.path.join(os.path.dirname(__file__), 'models',
#                                 'defaults.cfg')
#         self._cfg.read(filename)
# 
#     def loadConfig(self, model):
# 
#         name = ''.join(c for c in model.lower() if c.isalnum()) + '.cfg'
#         filename = os.path.join(os.path.dirname(__file__), 'models', name)
#         logging.info('Loading camera config "{}"'.format(name))
#         self._cfg.read(filename)


import io
import logging
from PIL import Image
from picamera import PiCamera
from .CameraInterface import CameraInterface

class CameraPicamera(CameraInterface):

    def __init__(self):

        super().__init__()

        self.hasPreview = True
        self.hasIdle = True

        logging.info('Using PiCamera')

        self._cap = None

        self.setActive()
        self._preview_resolution = (self._cap.resolution[0] // 2,
                                    self._cap.resolution[1] // 2)
        self.setIdle()

    def setActive(self):

        if self._cap is None or self._cap.closed:
            self._cap = PiCamera()

    def setIdle(self):

        if self._cap is not None and not self._cap.closed:
            self._cap.close()
            self._cap = None


# Path: photobooth/camera/CameraInterface.py
# class CameraInterface:
# 
#     def __init__(self):
# 
#         self.hasPreview = False
#         self.hasIdle = False
#         self._initConfig()
# 
#     def __enter__(self):
# 
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
# 
#         self.cleanup()
# 
#     def cleanup(self):
# 
#         pass
# 
#     @property
#     def hasPreview(self):
# 
#         return self._has_preview
# 
#     @hasPreview.setter
#     def hasPreview(self, value):
# 
#         if not isinstance(value, bool):
#             raise ValueError('Expected bool')
# 
#         self._has_preview = value
# 
#     @property
#     def hasIdle(self):
# 
#         return self._has_idle
# 
#     @hasIdle.setter
#     def hasIdle(self, value):
# 
#         if not isinstance(value, bool):
#             raise ValueError('Expected bool')
# 
#         self._has_idle = value
# 
#     @property
#     def config(self):
#         return self._cfg
# 
#     def setActive(self):
# 
#         if not self.hasIdle:
#             pass
#         else:
#             raise NotImplementedError()
# 
#     def setIdle(self):
# 
#         if not self.hasIdle:
#             raise RuntimeError('Camera does not have idle functionality')
# 
#         raise NotImplementedError()
# 
#     def getPreview(self):
# 
#         if not self.hasPreview:
#             raise RuntimeError('Camera does not have preview functionality')
# 
#         raise NotImplementedError()
# 
#     def getPicture(self):
# 
#         raise NotImplementedError()
# 
#     def _initConfig(self):
# 
#         self._cfg = configparser.ConfigParser(interpolation=None)
#         filename = os.path.join(os.path.dirname(__file__), 'models',
#                                 'defaults.cfg')
#         self._cfg.read(filename)
# 
#     def loadConfig(self, model):
# 
#         name = ''.join(c for c in model.lower() if c.isalnum()) + '.cfg'
#         filename = os.path.join(os.path.dirname(__file__), 'models', name)
#         logging.info('Loading camera config "{}"'.format(name))
#         self._cfg.read(filename)


# Path: photobooth/camera/CameraPicamera.py
import io
import logging
from PIL import Image
from picamera import PiCamera
from .CameraInterface import CameraInterface


class CameraPicamera(CameraInterface):

    def __init__(self):

        super().__init__()

        self.hasPreview = True
        self.hasIdle = True

        logging.info('Using PiCamera')

        self._cap = None

        self.setActive()
        self._preview_resolution = (self._cap.resolution[0] // 2,
                                    self._cap.resolution[1] // 2)
        self.setIdle()

    def setActive(self):

        if self._cap is None or self._cap.closed:
            self._cap = PiCamera()

    def setIdle(self):

        if self._cap is not None and not self._cap.closed:
            self._cap.close()
            self._cap = None


    def getPreview(self):