WIP Try to use a personnal arg parsing system
This commit is contained in:
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