Skip to content
Snippets Groups Projects
contribute.py 2.49 KiB
Newer Older
import click
import datetime
from distutils.dir_util import copy_tree

@click.command()
@click.option('--date', default=datetime.datetime.today().strftime('%Y-%m-%d'), help='Date of the presentation - format: YYYY-MM-DD')
@click.option('--name', default='myPresentation', help='Short name of the presentation.')

def main(date, name):
    """Copies the template folder"""

    # validate the date
    validateDate(date)

    # get the directory
    mainDir = date[:4]

    # get the root directory
    rootDir = os.path.dirname(os.path.realpath(__file__))

    # generate the full name of the presentation
    fullName = date + "_" + name

    # generate the full path
    fullPath = os.path.join(os.getcwd(), mainDir, fullName)
    slidesPath = os.path.join(fullPath, 'slides')
    # print out a summary
    click.echo(' > Date: {0}' . format(date))
    click.echo(' > Name: {0}' . format(name))
    click.echo(' > Directory: {0}' . format(fullPath))

    # create the directory
    if not os.path.exists(fullPath):
        os.mkdir(fullPath)
        # create an empty slides folder inside
        if not os.path.exists(slidesPath):
            os.mkdir(slidesPath)

        click.echo(' > Directory for slides {0} created.' . format(slidesPath))
        click.echo(' > Directory for slides{0} already exists.' . format(slidesPath))

    # change to the root directory of the presentation
    os.chdir(fullPath)

    # generate the symlink to the theme
    if not os.path.islink('theme'):
        os.symlink('../../theme', 'theme')
        click.echo(' > Symlink to theme created.')
    else:
        click.echo(' > Symlink to theme already exists.')

    # generate the symlink to the package.json file
    if not os.path.islink('package.json'):
        os.symlink('../../theme/package.json', 'package.json')
        click.echo(' > Symlink to package.json created.')
    else:
        click.echo(' > Symlink to package.json already exists.')

    # copy the contents of the template folder
    if not os.path.isfile(os.path.join(fullPath, 'slides', 'index.md')):
        copy_tree(os.path.join(rootDir, 'template', 'slides'), slidesPath)
        click.echo(' > Template slides copied.')
    else:
        click.echo(' > Slide deck already exists.')


def validateDate(input):
    """Validate a date string to fit the ISO format"""
    try:
        datetime.datetime.strptime(input, '%Y-%m-%d')
    except ValueError:
        print('The date {} is invalid'.format(input))
        raise

if __name__ == '__main__':