python utility script

script i use daily, and find extremely useful for organizing data while juggling chaotic projects

  • creates a folder / file with todays date, when you see "ticket" in this it's because this was used with a ticket system
    • if i'm dinking with a project and keeping notes, keeping screenshots & whatever it's for organizing
    • ever make a project / folder and come back later and have no idea wtf you were doing? this solves that you can see where you left off, what you were dinking with in a simple hisotry structure, this spawned from my vim bujo workflow
  • creates a folder on a configured network path
    • this function uses cookiecutter template variables because this script comes from one of my cookiecutter templates, you can do some simple editing to that function to make this a standalone, or leave them and add it to your cookiecutter
    • a cool thing on this second function is it searches for a match, if there is one it deploys, if there are multiple it walks you through the matches letting you choose which to deploy with y/n
    • it also copies that path to the clipboard
import click
import glob
import os
import pyperclip
import datetime

CURRENT_DIRECTORY = os.getcwd()
ISODATE = datetime.date.today().strftime('%Y-%m-%d')
ISOFILE = ISODATE + '.md'
# TODO change to env var 
SUPPORT_FOLDER = '//some/network/path' 

@click.group()
def cli():
    pass

@cli.command('support')
@click.option('--name',prompt='customer name',
                default='{{cookiecutter.project_name}}')
@click.option('--ticketno',prompt='ticket no',
                default='{{cookiecutter.ticket_no}}')
def find_support_folder(name,ticketno):
    """create ticket folder on shared network path"""
    search_string = name[0:3] + '*'
    matches = glob.glob((SUPPORT_FOLDER + search_string))
    if len(matches) > 1:
        click.echo('multiple matches found - prompting which to use')
        for m in matches:
            if click.confirm('%s' % m):
                ticket_folder= m + '/utility-files/' + ticketno
                ticket_folder = ticket_folder.replace('\\','/')
                click.echo(ticket_folder)
                if click.confirm('create folder?'):
                    os.mkdir(ticket_folder)
                    pyperclip.copy(ticket_folder)
    elif len(matches) == 1:
        ticket_folder= matches[0] + '/Data/' + ticketno
        ticket_folder = ticket_folder.replace('\\','/')
        click.echo('match - %s' % ticket_folder)
        if click.confirm('mkdir %s '% ticket_folder):
            os.mkdir(ticket_folder)
            ticket_folder = ticket_folder.replace('/','\\')
            click.echo(ticket_folder)
            pyperclip.copy(ticket_folder)
    else:
        click.echo('no matches found')


@cli.command('sub')
def sub_folder():
    """create subfolder & template in this directory with named todays date"""
    folderpath = os.path.join(CURRENT_DIRECTORY, ISODATE)
    filepath = os.path.join(CURRENT_DIRECTORY,ISODATE,ISOFILE)
    click.echo(folderpath)
    click.echo(filepath)
    if not os.path.exists(folderpath):
        os.mkdir(folderpath)
    if not os.path.exists(filepath):
        f = open(filepath, 'a')
        f.write('# ' + ISODATE)
        f.write('\n')
        f.write('\n')
        f.write('> ' + CURRENT_DIRECTORY)
        f.close()
    click.echo('folder for today created')


# rando stuff i might use later 
# try:
    # os.makedirs("path/to/directory")
# except FileExistsError:
    # directory already exists
    # pass

if __name__ == "__main__":
    cli()

Comments