#!/usr/bin/env python
"""
book-queue.py

@author Elliot Penson
"""

import argh
from jinja2 import Environment, PackageLoader, select_autoescape

from good_reads_user import GoodReadsUser


def main(user_id, api_key):
    user = GoodReadsUser(user_id, api_key)
    shelves = [
        {
            'title': 'Read',
            'books': reversed(list(user.get_shelf('read', 'date_read')))
        },
        {
            'title': 'Currently Reading',
            'books': user.get_shelf('currently-reading', 'date_added')
        },
        {
            'title': 'To-Read',
            'books': user.get_shelf('to-read', 'position')
        }
    ]
    render_template('generated/index.html', shelves=shelves)


def render_template(output_file, **data):
    env = Environment(
        loader=PackageLoader('book_queue', 'templates'),
        autoescape=select_autoescape(['html', 'xml'])
    )
    template = env.get_template('index.html')
    output = template.render(**data)
    with open(output_file, 'w') as f:
        f.write(output)


if __name__ == '__main__':
    argh.dispatch_command(main)

@@ -61,16 +61,96 @@
 on%0A%22%22%22%0A%0A
+from os import mkdir, listdir, path%0Afrom shutil import copy2, copytree, rmtree%0A%0A
 import a
@@ -733,62 +733,594 @@
    r
-ender_template('generated/index.html', shelves=shelves
+mtree('generated/', ignore_errors=True)%0A    mkdir('generated/')%0A    render_template('generated/index.html', shelves=shelves)%0A    generate_assets()%0A%0A%0Adef generate_assets(source='assets/', destination='generated/'):%0A    %22%22%22Copy the contents of the source directory to destination directory.%22%22%22%0A    for asset in listdir(source):%0A        asset_source = path.join(source, asset)%0A        asset_destination = path.join(destination, asset)%0A        if path.isdir(asset_source):%0A            copytree(asset_source, asset_destination)%0A        else:%0A            copy2(asset_source, asset_destination
 )%0A%0A%0A

Copy assets to 'generated' directory