#!/usr/bin/env python

import os
import sys
import yaml
import argparse
from pyvert import convert_dict


def main():

    parser = argparse.ArgumentParser(description='populate a directory '
                                                 'structure from a given'
                                                 ' YAML file')

    parser.add_argument('file',
                        help='path to YAML file to use for generation')
    parser.add_argument('target',
                        nargs='?',
                        help='path to generated directory (leave empty to '
                             'use current directory')

    args = parser.parse_args()

    y = os.path.abspath(args.file)
    p = os.getcwd() if not args.target else os.path.abspath(args.target)

    try:
        with open(y, 'r') as f:
            try:
                d = yaml.load(f)
                dest = convert_dict.to_dir(data=d, path=p)
                print('Directory created at {}'.format(os.path.join(os.getcwd(), dest)))
                sys.exit(0)

            except Exception as e:
                print(e)
                sys.exit(1)

    except Exception as e:
        print(e)
        sys.exit(2)

if __name__ == '__main__':
    main()
