Skip to content

Commit

Permalink
Formatting updates, and added clobber option
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunter Senft-Grupp committed Mar 21, 2016
1 parent 29fe2a0 commit 0b1bad8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 40 deletions.
27 changes: 19 additions & 8 deletions bragly/brag.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import, print_function
import arrow
import bragly.persist as persist
from bragly.config import BRAG_DIR, CONFIG_FILE_PATH
import os
import pkg_resources

Expand Down Expand Up @@ -101,8 +102,8 @@ def _get_end_date(start=None, end=None, period=None):

return end

def init(mechanism):
directory = os.path.expanduser('~/.brag')
def init(mechanism, clobber=True):
directory = BRAG_DIR
print('Checking if {} exists...'.format(directory), end='', flush=True)
if not os.path.exists(directory):
print('\nmaking directory...', end='', flush=True)
Expand All @@ -122,11 +123,21 @@ def init(mechanism):
config_data = f.read()
print('OK')

config_file_path = os.path.join(directory, 'config.ini')

print('Writing to {}...'.format(config_file_path), end='', flush=True)
with open(config_file_path, 'w') as f:
f.write(config_data)
print('OK')
config_file_path = CONFIG_FILE_PATH
file_exists = os.path.exists(config_file_path)

if (file_exists and clobber) or not file_exists:
if file_exists:
print('Clobbering file {}.'.format(config_file_path), flush=True)
print('Writing to {}...'.format(config_file_path), end='', flush=True)
with open(config_file_path, 'w') as f:
f.write(config_data)
print('OK')
else:
print(
'Not clobbering file at {}. See example config file at {}.'.format(
config_file_path, config_example
)
)

return ['Initialization finished']
92 changes: 60 additions & 32 deletions bragly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import argparse
from bragly.brag import write, read, search, init, PERSIST_CHOICES
from bragly.config import BRAG_DIR, CONFIG_FILE_PATH
import arrow


def parse_args(args):
"""Parses arguments and dispatches the commands and argumnets to the
appropriate sub parser using argparse.ArgumentParser.parse_args.
Expand All @@ -23,39 +25,46 @@ def parse_args(args):

parser = argparse.ArgumentParser(prog='brag')
subparsers = parser.add_subparsers(help='sub command help')

# Writ command sub parser
write_parser = subparsers.add_parser('w', help='Write a new brag entry')
write_parser.add_argument('message', metavar='message', nargs='+', type=str, help='The brag message')
write_parser.add_argument('-t','--tags', nargs='*', type=str, help='The tags associated with this brag message')
write_parser.add_argument('-d', '--timestamp', type=arrow.get, help='The time stamp to use for this entry, in ISO-8601 format')
write_parser.add_argument('message', metavar='message', nargs='+', type=str,
help='The brag message')
write_parser.add_argument('-t', '--tags', nargs='*', type=str,
help='The tags associated with this brag message')
write_parser.add_argument('-d', '--timestamp', type=arrow.get,
help='The time stamp to use for this entry, in ISO-8601 format')
write_parser.set_defaults(func=write)

# Read command sub parser
read_parser = subparsers.add_parser('r', help='Read a group of brag entries')
read_parser.add_argument('-s', '--start', type=arrow.get, help="The start time for getting entries")
read_parser = subparsers.add_parser('r',
help='Read a group of brag entries')
read_parser.add_argument('-s', '--start', type=arrow.get,
help="The start time for getting entries")
# End date spec
read_parser_enddate_group = read_parser.add_mutually_exclusive_group()
read_parser_enddate_group = read_parser.add_mutually_exclusive_group()
read_parser_enddate_group.add_argument(
'-p',
'--period',
type=str,
'-p',
'--period',
type=str,
help='The time period after the start datetime to get entires. One of hour, day, week, month, year'
)
read_parser_enddate_group.add_argument('-e', '--end', type=arrow.get, help='The end time for getting entries')
read_parser_enddate_group.add_argument('-e', '--end', type=arrow.get,
help='The end time for getting entries')
# Other read options
read_parser.add_argument(
'-f',
'--form',
type=str,
default='json',
help='The format to display the results in. One of json, json-pretty, log. Default: %(default)s'
'-f',
'--form',
type=str,
default='json',
help='The format to display the results in. One of json, json-pretty, log. Default: %(default)s'
)
# Set the operation that will be called based on the command
read_parser.set_defaults(func=read)

# Search Command Sub parser
search_parser = subparsers.add_parser('s', help='Search for a group of brag entries')
search_parser = subparsers.add_parser('s',
help='Search for a group of brag entries')
search_parser.add_argument(
'-s',
'--start',
Expand All @@ -65,20 +74,23 @@ def parse_args(args):
# End date spec
search_parser_enddate_group = search_parser.add_mutually_exclusive_group()
search_parser_enddate_group.add_argument(
'-p',
'--period',
type=str,
'-p',
'--period',
type=str,
help='The time period after the start datetime to get entires. One of hour, day, week, month, year'
)
search_parser_enddate_group.add_argument('-e', '--end', type=arrow.get, help='The end time for getting entries')
search_parser_enddate_group.add_argument('-e', '--end', type=arrow.get,
help='The end time for getting entries')
# Other search options
search_parser.add_argument('-t', '--tags', nargs='*', type=str, help='Tags you want to search for')
search_parser.add_argument('-x', '--text', nargs='*', type=str, help='Keywords you want to search for')
search_parser.add_argument('-t', '--tags', nargs='*', type=str,
help='Tags you want to search for')
search_parser.add_argument('-x', '--text', nargs='*', type=str,
help='Keywords you want to search for')
search_parser.add_argument(
'-f',
'--form',
type=str,
default='json',
'-f',
'--form',
type=str,
default='json',
help='The format to display the results in. One of json, json-pretty, log. Default: %(default)s'
)
any_all_group = search_parser.add_mutually_exclusive_group()
Expand All @@ -95,7 +107,7 @@ def parse_args(args):

# Set the operation that will be called based on the command
search_parser.set_defaults(func=search)

args = vars(parser.parse_args(args))
# Do some additonal argument parsing if this is a search command
if 'func' in args:
Expand All @@ -106,19 +118,35 @@ def parse_args(args):

return parser, args

def parse_utility_args(args):

def parse_utility_args(args):
parser = argparse.ArgumentParser(prog='brag-util')
subparsers = parser.add_subparsers(help='sub command help')
init_parser = subparsers.add_parser('init', help='Initialize brag')
init_parser = subparsers.add_parser(
'init',
help='Initialize brag. If you want a different location for brag than'
' {} than be sure to set BRAG_DIR environmental variable. If you '
'want a different location for the configuration file then be sure'
' to set BRAG_CONFIG_PATH to something other than {}'.format(
BRAG_DIR,
CONFIG_FILE_PATH,
),
)
init_parser.add_argument(
'-m',
'--mechanism',
type=str,
choices=PERSIST_CHOICES,
default='files',
help='Select the persistence mechanism. Default: %(default)s.'
)
),
init_parser.add_argument(
'-c',
'--clobber',
help='If set, overwrites existing configuration files.',
action='store_true',
),

init_parser.set_defaults(func=init)
args = vars(parser.parse_args(args))

Expand Down

0 comments on commit 0b1bad8

Please sign in to comment.