#!/usr/bin/env python

import os, subprocess, argparse, re
from datetime import datetime

class JarvisSettings(object):

    def __init__(self):
        # TODO: Make a test mode where it writes to a test directory.
        self._env_dir_jarvis_root = os.environ['JARVIS_DIR_ROOT']
        self._env_author = os.environ['JARVIS_AUTHOR']
        self._env_version = "0.1.0"

    @property
    def root_directory(self):
        return self._env_dir_jarvis_root

    @property
    def author(self):
        return self._env_author

    @property
    def document_version(self):
        return self._env_version


if "__main__" == __name__:
    parser = argparse.ArgumentParser(description='Jarvis is used for personal information management')

    subparsers = parser.add_subparsers(help='Actions for Jarvis',
            dest='action_name')

    parser_new = subparsers.add_parser('new', help='Create an information element')
    subparsers_new = parser_new.add_subparsers(help='Types of new information element',
            dest='element_type')
    parser_new_log = subparsers_new.add_parser('log', help='Create a new log entry')
    parser_new_tag = subparsers_new.add_parser('tag', help='Create a new tag element')
    parser_new_tag.add_argument('tag_name', help='Tag name')

    parser_show = subparsers.add_parser('show', help='Show information elements')
    subparsers_show = parser_show.add_subparsers(help='Types of show actions',
            dest='show_type')
    parser_show_tags = subparsers_show.add_parser('tags', help='Show tags')

    args = parser.parse_args()

    # NOTE: Argparse should filter and validate and ensure that only the known
    # choices get through to this point.
    # UPDATE: There is a case where the subsubparser allows for Nones to slip
    # through. The required flag doesn't seem to be reinforced so the following
    # is acceptable to Argparse which sucks for me:
    #   jarvis.py new

    js = JarvisSettings()

    if args.action_name == 'new':
        created = datetime.utcnow().replace(microsecond=0)

        def create_stub_file(subdir_name, filename):
            dir_target = "{0}/{1}".format(js.root_directory, subdir_name)

            filepath = "{0}/{1}.md".format(dir_target, filename)

            if os.path.isfile(filepath):
                raise IOError("File already exists! {0}".format(filepath))

            with open(filepath, 'w') as f:
                f.write("Author: {0}\n".format(js.author))
                f.write("Created: {0}\n".format(created.isoformat()))
                f.write("Version: {0}\n".format(js.document_version))
                f.write("Tags: \n")

            return filepath

        if args.element_type == 'log':
            # datetime.fromtimestamp(0) is not Unix epoch and returns
            # 1969-12-31 19:00 instead.
            epoch = datetime(1970, 1, 1)

            filepath = create_stub_file('LogEntries',
                int((created - epoch).total_seconds()))
        elif args.element_type == 'tag':
            filepath = create_stub_file('Tags', args.tag_name)

            # Add the title which should be the tag name
            with open(filepath, 'a') as f:
                # TODO: Would be very cool to reinforce camel case for the file
                # name and canonical for title.
                f.write("\n# {0}\n".format(args.tag_name))
        else:
            raise NotImplementedError("Unknown information type: {0}"
                    .format(args.element_type))

        if filepath:
            subprocess.call(["vim", filepath])
            print("Created: {0}, {1}".format(args.element_type, filepath))
        else:
            print("Failed to create new information element")
    elif args.action_name == 'show':

        if args.show_type == 'tags':
            tags_dir = "{0}/{1}".format(js.root_directory, 'Tags')
            tag_pattern = re.compile('(\w*)\.md')

            for tag_file in sorted(os.listdir(tags_dir)):
                tag = tag_pattern.search(tag_file).group(1)
                print(tag)
        else:
            raise NotImplementedError("Unknown show type: {0}"
                    .format(args.show_type))

@@ -52,16 +52,34 @@
 rse, re%0A
+import webbrowser%0A
 from dat
@@ -842,32 +842,50 @@
 'action_name')%0A%0A
+    # New actions%0A
     parser_new =
@@ -1303,24 +1303,43 @@
 Tag name')%0A%0A
+    # Show actions%0A
     parser_s
@@ -1585,17 +1585,173 @@
 lp='
-Show tags
+List all tags')%0A    parser_show_tag = subparsers_show.add_parser('tag', help='Open a tag in the browser')%0A    parser_show_tag.add_argument('tag_name', help='Tag name
 ')%0A%0A
@@ -3962,49 +3962,8 @@
 ':%0A%0A
-        if args.show_type == 'tags':%0A    
     
@@ -4016,24 +4016,62 @@
 ory, 'Tags')
+%0A%0A        if args.show_type == 'tags':
 %0A           
@@ -4255,16 +4255,524 @@
 nt(tag)%0A
+        elif args.show_type == 'tag':%0A            filepath = %22%7B0%7D/%7B1%7D.md%22.format(tags_dir, args.tag_name)%0A%0A            if not os.path.isfile(filepath):%0A                raise IOError(%22Tag does not exist! %7B0%7D%22.format(filepath))%0A%0A            # Previews the markdown. This will require you to change the%0A            # mimeapps.list setting file in order to chose your markdown preview%0A            # tool.%0A            filepath_browser = %22file://%7B0%7D%22.format(filepath)%0A            webbrowser.open(filepath_browser)%0A
         

Add the ability to preview a tag markdown file