"""Do work"""

import argparse
import logging
import os
import sys
from cameracontroller.cameracontroller import CameraController
from storage.cloudstorage import CloudStorage

logger = logging.getLogger('pypic')
log_dir = os.path.expanduser('~/log')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

logging.basicConfig(
    filename=os.path.join(log_dir, 'pypiclog'),
    format='%(asctime)s :: %(levelname)s :: %(message)s',
    level=logging.DEBUG
)

def exception_handler(exception_type, exception, traceback):
    logger.error(str(exception))

sys.excepthook = exception_handler

def main():
    """Main script execution"""

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c', '--continuous',
        action='store_true',
        help='If set, run the video feed continuously'
    )
    parser.add_argument(
        '-d', '--duration',
        default=10,
        type=float,
        help='Duration (in seconds) to run the video loop'
    )
    args = parser.parse_args()

    camera_controller = CameraController(
        os.path.expanduser('~/pypic_output'),
        CloudStorage(
            os.environ.get('AZSTORAGE_ACCOUNT_NAME'),
            os.environ.get('AZSTORAGE_ACCOUNT_KEY')
        )
    )
    camera_controller.record_video(
        continuous=args.continuous,
        duration=args.duration
    )

if __name__ == '__main__':
    main()

"""Do work"""

import argparse
import logging
import os
import sys
from cameracontroller.cameracontroller import CameraController
from storage.cloudstorage import CloudStorage

logger = logging.getLogger('pypic')
log_dir = os.path.expanduser('~/log')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

logging.basicConfig(
    filename=os.path.join(log_dir, 'pypiclog'),
    format='%(asctime)s :: %(levelname)s :: %(message)s',
    level=logging.ERROR
)

def exception_handler(exception_type, exception, traceback):
    logger.error(str(exception))

sys.excepthook = exception_handler

def main():
    """Main script execution"""

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c', '--continuous',
        action='store_true',
        help='If set, run the video feed continuously'
    )
    parser.add_argument(
        '-d', '--duration',
        default=10,
        type=float,
        help='Duration (in seconds) to run the video loop'
    )
    parser.add_argument(
        '-o', '--outputdir',
        help='Local directory to store video files'
    )
    args = parser.parse_args()

    if not args.outputdir:
        error_msg = 'You must specify an output directory (-o, --outputdir)'
        logger.error(error_msg)
        print(error_msg)
        sys.exit(1)

    cloud_storage_account_name = os.environ.get('AZSTORAGE_ACCOUNT_NAME')
    cloud_storage_account_key = os.environ.get('AZSTORAGE_ACCOUNT_KEY')
    cloud_storage = None
    if cloud_storage_account_name and cloud_storage_account_key:
        cloud_storage = CloudStorage(
            cloud_storage_account_name,
            cloud_storage_account_key
        )

    camera_controller = CameraController(
        os.path.expanduser(args.outputdir),
        cloud_storage
    )
    camera_controller.record_video(
        continuous=args.continuous,
        duration=args.duration
    )

if __name__ == '__main__':
    main()

add configurable output dir and make cloud storage optional