#!python

import os
from optparse import OptionParser

if __name__ == "__main__":

    parser = OptionParser()
    (options, args) = parser.parse_args()

    if len(args) > 0:

        for arg in args:

            try:

                page = ''.join(item for item in str(arg).replace('-', '_').title() if not item.isspace())

                # Create folder
                os.mkdir(os.path.join(os.getcwd(), page))

                # Create package file
                with open(os.path.join(os.getcwd(), page, '__init__.py'), 'w') as f:
                    f.write('from page import *\n')

                # Create fixtures file
                with open(os.path.join(os.getcwd(), page, 'fixtures.py'), 'w') as f:

                    lines = ['# from sda.fixtures import *\n', '# from sda.fixtures import *\n',
                             '# from sda.structures import *\n\n', '# Implement custom fixtures below\n']

                    f.writelines(lines)

                # Create locators file
                with open(os.path.join(os.getcwd(), page, 'locators.py'), 'w') as f:

                    lines = ['from sda.locators import Locators\n',
                             '# from selenium.webdriver.common.by import By\n\n\n',
                             'class {0}PageLocators(Locators):\n\n'.format(page),
                             '    # --- Add your locators below ---\n\n',
                             '    # An example would be:\n',
                             '    # USERNAME = (By.ID, "id_username")\n\n',
                             '    pass\n']

                    f.writelines(lines)

                # Create page file
                with open(os.path.join(os.getcwd(), page, 'page.py'), 'w') as f:

                    lines = ['# from sda.fixtures import *\n',
                             'from sda.page import *\n',
                             '# from sda.structures import *\n\n\n',
                             'class {0}Page(Page):\n\n'.format(page),
                             '    def __init__(self, driver):\n\n',
                             '        super({0}Page, self).__init__(web_driver=driver)\n'.format(page),
                             '        # --- Add your page elements below ---\n\n',
                             '        # An example might be:\n',
                             '        # self.username = InputText(driver, By.ID, "id_username")\n\n'
                             '        pass\n']

                    f.writelines(lines)

            except OSError:
                print "Unable to create '{0}'. Please make sure the " \
                      "path is valid and does not already exist".format(os.path.join(os.getcwd(), str(arg)))
