Init (override) + info + version

This commit is contained in:
2018-07-13 01:10:24 +02:00
parent 4dc654a68c
commit 405cedb032
3 changed files with 69 additions and 21 deletions

54
pits.py
View File

@@ -19,6 +19,9 @@ logging.basicConfig(level=logging.ERROR, format='%(asctime)s :: {%(filename)10s:
DB_FILENAME = 'pits.db'
SCHEMA_FILENAME = 'pits_schema.sql'
PIT_VERSION = '0.0.1'
SCHEMA_VERSION = 1
def get_file_path(file_name):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
@@ -30,33 +33,30 @@ def strstr(haystack, needle):
else:
return haystack[pos:]
def info():
logging.info('info function')
def handle_note(args):
logging.info('>> handle note')
def create_connection(db_filename):
db_path = get_file_path(db_filename)
logging.debug("Try to connect to {}".format(db_path))
try:
conn = sqlite3.connect(db_path)
return conn
except Error as e:
print(e)
except sqlite3.Error as sqlite3_error:
print(sqlite3_error)
return None
def init():
# TODO Use init command like original pit
db_path = get_file_path(DB_FILENAME)
logging.debug('Search database in {}'.format(db_path))
db_is_new = not os.path.exists(db_path)
if not db_is_new:
print('Database already exist')
# TODO Permit to override
return
if os.path.exists(db_path):
valid = {"yes": True, "y": True, "ye": True}
print('{} already exists, do you want to override it? [y/N]:'.format(db_path), end='')
choice = input().lower()
if choice in valid:
os.remove(db_path)
else:
sys.exit(0)
with sqlite3.connect(db_path) as conn:
logging.info('Creating schema')
@@ -77,11 +77,27 @@ 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 info(_1, _2, conn):
print('Pit version: {}'.format(PIT_VERSION))
print('Pit file name: {}'.format(get_file_path(DB_FILENAME)))
print('Created by: {} on {}'.format(*action.get_first_action(conn)))
print('Last updated by: {} on {}'.format(*action.get_last_action(conn)))
print('Schema version: {}'.format(SCHEMA_VERSION))
print('Projects: {}'.format(count_object(conn, 'project')))
print('Tasks: {}'.format(count_object(conn, 'task')))
print('Notes: {}'.format(count_object(conn, 'note')))
print('Log entries: {}'.format(count_object(conn, 'action')))
def help(args, last_action, conn):
def count_object(conn, table_name):
query = "SELECT count(*) FROM " + table_name
cursor = conn.cursor()
cursor.execute(query)
return cursor.fetchone()[0]
def version(*_):
print('pit version 0.1.0')
def pit_help(args, *_):
print('TODO help')
# TODO help
@@ -93,7 +109,7 @@ def main():
note.handle_note,
action.handle_action,
info,
help,
pit_help,
version]
candidate = -1