#!/usr/bin/env python

import logging
import os

from portia2code.porter import load_project_data, port_project
from portia2code.utils import _validate_identifier

if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        level=logging.INFO)
    import json
    import sys

    from os.path import join

    log = logging.getLogger(__name__)
    project_dir = sys.argv[1]
    try:
        out_dir = os.path.abspath(sys.argv[2])
        dir_name = os.path.split(out_dir)[-1]
    except IndexError:
        out_dir = os.path.abspath('./')
        dir_name = os.path.split(os.path.abspath(project_dir))[-1]
    if dir_name.endswith('.zip'):
        out_dir = os.path.abspath(os.path.dirname(dir_name))
        dir_name = dir_name[:-len('.zip')]
    if not _validate_identifier(dir_name):
        raise ValueError('Output project name "%s" is not a valid name. Valid '
                         'names should only contain number letters and '
                         'underscores and may not start with a number' %
                         dir_name)
    if os.path.exists(out_dir) and os.path.isdir(out_dir):
        out_path = os.path.join(out_dir, '%s.zip' % dir_name)
    else:
        raise ValueError('Output path "%s" does not exist' % out_dir)

    def open_file_func(*path):
        """Open files from filesystem."""
        full_path = ''
        for path_item in path:
            full_path = join(full_path, path_item)
        full_path = '{}.json'.format(full_path)
        with open(full_path) as f:
            return json.load(f)

    def list_spiders_func(project):
        return [
            s[:-len('.json')] for s in os.listdir(join(project_dir, 'spiders'))
            if s.endswith('.json')
        ]

    # Port project from portia definitions to scrapy code
    schemas, extractors, spiders = load_project_data(open_file_func,
                                                     list_spiders_func,
                                                     project_dir)
    project_zip = port_project(dir_name, schemas, spiders, extractors).read()
    # Write contents to file
    log.info('Writing project to "%s"', out_path)
    with open(out_path, 'wb') as f:
        f.write(project_zip)
    log.info('Finished.')
    sys.exit(0)
