WIP Try to use a personnal arg parsing system
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
import pits #TODO Cyclic dependency
|
||||
import project
|
||||
import task
|
||||
import note
|
||||
import action
|
||||
|
||||
def help(args, last_action, conn):
|
||||
logging.info('help function')
|
||||
|
||||
def create_parser():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.set_defaults(func=help)
|
||||
|
||||
subparsers = parser.add_subparsers()
|
||||
|
||||
# init Create empty Pit database or reinitialize an existing one
|
||||
parser_init = subparsers.add_parser('init')#, usage='toto')
|
||||
parser_init.add_argument('-f', '--force')
|
||||
# parser_init.set_defaults(func=pits.init_database)
|
||||
|
||||
create_project_parser(subparsers)
|
||||
create_task_parser(subparsers)
|
||||
create_note_parser(subparsers)
|
||||
|
||||
# log Show chronological Pit activity log (== action)
|
||||
parser_action = subparsers.add_parser('log')
|
||||
parser_action.set_defaults(func=action.handle_action)
|
||||
|
||||
# info Show summary information about your Pit database
|
||||
# Pit version: 0.1.0
|
||||
# Pit file name: /home/budd/.pit
|
||||
# Created by: budd on Jul 04, 2018 at 02:00
|
||||
# Last updated by: budd on Jul 04, 2018 at 19:53
|
||||
# Schema version: 1
|
||||
# Projects: 1
|
||||
# Tasks: 3
|
||||
# Notes: 3
|
||||
# Log entries: 9
|
||||
# TODO
|
||||
|
||||
# help Show help information about Pit
|
||||
|
||||
# version Show Pit version number
|
||||
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
|
||||
|
||||
return parser
|
||||
|
||||
def create_project_parser(subparsers):
|
||||
# PROJECT ARGS PARSER
|
||||
# project Create, search, and manage Pit projects
|
||||
parser_project = subparsers.add_parser('project')#, usage='toto')
|
||||
group_project = parser_project.add_mutually_exclusive_group()
|
||||
group_project.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of project')
|
||||
group_project.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a project')
|
||||
group_project.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a project')
|
||||
parser_project.add_argument('-n', type=str, dest='edit_name', metavar='name', help='Edit: new name of project')
|
||||
parser_project.add_argument('-s', type=str, dest='status', metavar='status')
|
||||
parser_project.add_argument('-q', type=int)
|
||||
parser_project.set_defaults(func=project.handle_project)
|
||||
|
||||
def create_task_parser(subparsers):
|
||||
# TASK ARGS PARSER
|
||||
# task Create, search, and manage Pit tasks
|
||||
parser_task = subparsers.add_parser('task')
|
||||
group_task = parser_task.add_mutually_exclusive_group()
|
||||
group_task.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of task')
|
||||
group_task.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a task')
|
||||
group_task.add_argument('-m', type=int, dest='moving_id', nargs='?', const=-1, metavar='number', help='Moving a task')
|
||||
group_task.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a task')
|
||||
parser_task.add_argument('-n', type=str, dest='edit_name', metavar='name', help='Edit: new name of project')
|
||||
parser_task.add_argument('-s', type=str, dest='status', metavar='status')
|
||||
parser_task.add_argument('-q', type=int)
|
||||
parser_task.add_argument('-i', type=int, dest='project_id', metavar='project id')
|
||||
parser_task.set_defaults(func=task.handle_task)
|
||||
|
||||
def create_note_parser(subparsers):
|
||||
# NOTE ARGS PARSER
|
||||
# note Create, search, and manage Pit notes
|
||||
parser_note = subparsers.add_parser('note')
|
||||
group_note = parser_note.add_mutually_exclusive_group()
|
||||
group_note.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of task')
|
||||
group_note.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a task')
|
||||
group_note.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a task')
|
||||
parser_note.set_defaults(func=note.handle_note)
|
||||
68
pits.py
68
pits.py
@@ -5,16 +5,16 @@ import json
|
||||
import getpass
|
||||
import datetime
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import project
|
||||
import action
|
||||
import task
|
||||
import note
|
||||
import pit_argparser
|
||||
|
||||
# logging.basicConfig(level=logging.ERROR, format='%(levelname)7s :: %(message)s')
|
||||
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)s :: %(message)s')
|
||||
# logging.basicConfig(level=logging.DEBUG, format='%(levelname)7s :: %(message)s')
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: {%(filename)10s:%(lineno)d} :: %(funcName)s :: %(levelname)s :: %(message)s')
|
||||
logging.basicConfig(level=logging.ERROR, format='%(asctime)s :: {%(filename)10s:%(lineno)3d} :: %(funcName)s :: %(levelname)s :: %(message)s')
|
||||
|
||||
DB_FILENAME = 'pits.db'
|
||||
SCHEMA_FILENAME = 'pits_schema.sql'
|
||||
@@ -22,6 +22,14 @@ SCHEMA_FILENAME = 'pits_schema.sql'
|
||||
def get_file_path(file_name):
|
||||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
|
||||
|
||||
|
||||
def strstr(haystack, needle):
|
||||
pos = haystack.find(needle)
|
||||
if pos < 0: # not found
|
||||
return None
|
||||
else:
|
||||
return haystack[pos:]
|
||||
|
||||
def info():
|
||||
logging.info('info function')
|
||||
|
||||
@@ -46,7 +54,7 @@ def init():
|
||||
logging.debug('Search database in {}'.format(db_path))
|
||||
db_is_new = not os.path.exists(db_path)
|
||||
if not db_is_new:
|
||||
logging.debug('Database already exist')
|
||||
print('Database already exist')
|
||||
return
|
||||
|
||||
with sqlite3.connect(db_path) as conn:
|
||||
@@ -68,18 +76,54 @@ def init():
|
||||
# conn.executescript("""
|
||||
# insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""")
|
||||
|
||||
def version(args, last_action, conn):
|
||||
print('TODO version')
|
||||
# TODO version
|
||||
|
||||
def help(args, last_action, conn):
|
||||
print('TODO help')
|
||||
# TODO help
|
||||
|
||||
if __name__ == '__main__':
|
||||
init()
|
||||
parser = pit_argparser.create_parser()
|
||||
args = parser.parse_args()
|
||||
command = [ 'project', 'task', 'note', 'log', 'info', 'help', 'version', 'init' ]
|
||||
handler = [
|
||||
project.handle_project,
|
||||
task.handle_task,
|
||||
note.handle_note,
|
||||
action.handle_action,
|
||||
info,
|
||||
help,
|
||||
version]
|
||||
candidate = -1
|
||||
|
||||
argv = sys.argv
|
||||
|
||||
if len(argv) == 1:
|
||||
argv.append('help')
|
||||
argv.append(None)
|
||||
|
||||
for i in range(len(command)):
|
||||
if strstr(command[i], argv[1]) == command[i]:
|
||||
if candidate < 0:
|
||||
candidate = i
|
||||
else:
|
||||
print('Ambiguous command ({})'.format(argv[1]))
|
||||
sys.exit(1)
|
||||
|
||||
if candidate < 0:
|
||||
print("Invalid command ({}), run '{} help' for help".format(argv[1], os.path.basename(__file__)))
|
||||
sys.exit(1)
|
||||
|
||||
if candidate == (len(command) -1):
|
||||
init()
|
||||
sys.exit(0)
|
||||
|
||||
conn = create_connection(DB_FILENAME)
|
||||
last_action = action.read_last_action(conn)
|
||||
logging.debug('Last action: {}'.format(last_action))
|
||||
|
||||
args.func(args, last_action, conn)
|
||||
logging.debug(argv)
|
||||
logging.debug(argv[2:])
|
||||
handler[candidate](argv[2:], last_action, conn)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print('END')
|
||||
print(os.path.dirname(os.path.abspath(__file__)))
|
||||
conn.close()
|
||||
77
project.py
77
project.py
@@ -5,31 +5,59 @@ import json
|
||||
import getpass
|
||||
import datetime
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import action
|
||||
|
||||
class Project:
|
||||
name = ''
|
||||
status = 'active'
|
||||
|
||||
|
||||
def handle_project(args, last_action, conn):
|
||||
logging.info('> handle project')
|
||||
logging.debug('args: ' + str(args))
|
||||
|
||||
do_something = False
|
||||
|
||||
if args.create_name:
|
||||
do_something = True
|
||||
create_project(args, conn)
|
||||
|
||||
if args.delete_id:
|
||||
do_something = True
|
||||
delete_project(args.delete_id, conn)
|
||||
|
||||
if args.edit_id:
|
||||
do_something = True
|
||||
edit_project(args, args.edit_id, conn)
|
||||
|
||||
if not do_something:
|
||||
if not args:
|
||||
list_project(last_action.project_id, conn)
|
||||
sys.exit(0)
|
||||
|
||||
def create_project(args, conn):
|
||||
if args[0].isdigit():
|
||||
view_project()
|
||||
sys.exit(0)
|
||||
|
||||
# Use sys.exit when no add/update in Database
|
||||
|
||||
if args[0] == '-c':
|
||||
if len(args) == 1:
|
||||
print('required project name')
|
||||
sys.exit(1)
|
||||
|
||||
project = Project()
|
||||
project.name = arg_string(args[1], 'project name')
|
||||
create_project(project, conn)
|
||||
elif args[0] == '-e':
|
||||
print('Update project')
|
||||
elif args[0] == '-d':
|
||||
if len(args) > 1:
|
||||
delete_project(arg_number(args[1]), conn)
|
||||
else:
|
||||
delete_project(last_action.project_id, conn)
|
||||
|
||||
def arg_string(arg, required = ''):
|
||||
if required and arg.startswith('-'):
|
||||
print('required ', required)
|
||||
sys.exit('1')
|
||||
else:
|
||||
return arg.strip()
|
||||
def arg_number(arg, required = ''):
|
||||
if not arg.isdigit():
|
||||
print('Invalid value') # TODO
|
||||
sys.exit('1')
|
||||
|
||||
return int(arg)
|
||||
|
||||
def create_project(project, conn):
|
||||
# TODO Project is same name is forbidden
|
||||
logging.info('>> Create project')
|
||||
|
||||
@@ -38,17 +66,14 @@ def create_project(args, conn):
|
||||
VALUES (?, ?, ?, ?);
|
||||
"""
|
||||
|
||||
status = 'active'
|
||||
if args.status: status = args.status
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (getpass.getuser(), args.create_name, status, datetime.datetime.now(),))
|
||||
|
||||
cursor.execute(query, (getpass.getuser(), project.name, project.status, datetime.datetime.now(),))
|
||||
project_id = cursor.lastrowid
|
||||
action_message = '{} (status: {})'.format(args.create_name, status)
|
||||
action_message = '{} (status: {})'.format(project.name, project.status)
|
||||
action.create_action(cursor, project_id=project_id, message=action_message)
|
||||
logging.debug(action_message)
|
||||
|
||||
print('created project {}: {} (status: {})'.format(project_id, args.create_name, status))
|
||||
print('created project {}: {} (status: {})'.format(project_id, project.name, project.status))
|
||||
|
||||
def edit_project(args, project_id, conn):
|
||||
logging.info('>> Edit project')
|
||||
@@ -103,4 +128,8 @@ def list_project(active_project_id, conn):
|
||||
logging.debug('Project row: {}'.format(row))
|
||||
project_id, username, name, status, date, nb_task = row
|
||||
print('{:1} {:2d}: ({:8}) | {} | {} ({} tasks)'.format('*' if active_project_id == project_id else '',
|
||||
project_id, username, status, name, nb_task))
|
||||
project_id, username, status, name, nb_task))
|
||||
|
||||
def view_project():
|
||||
print("TODO View Project")
|
||||
#TODO
|
||||
Reference in New Issue
Block a user