import sqlite3 import os import argparse import json import getpass import datetime import logging import sys import project import action import task import note # 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.ERROR, format='%(asctime)s :: {%(filename)10s:%(lineno)3d} :: %(funcName)s :: %(levelname)s :: %(message)s') 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) def strstr(haystack, needle): pos = haystack.find(needle) if pos < 0: # not found return None else: return haystack[pos:] 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 sqlite3.Error as sqlite3_error: print(sqlite3_error) return None def init(): db_path = get_file_path(DB_FILENAME) logging.debug('Search database in {}'.format(db_path)) 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') schema_path = get_file_path(SCHEMA_FILENAME) logging.debug('Schema file path: {}'.format(schema_path)) with open(schema_path, 'rt') as f: schema = f.read() conn.executescript(schema) action_query = ''' INSERT INTO action (username, action, message) VALUES (?, ?, ?); ''' conn.execute(action_query, (getpass.getuser(), 'init', 'Initialized pit',)) # TODO Add schema version # logging.info('Inserting initial data') # conn.executescript(""" # insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""") 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 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 def main(): command = [ 'project', 'task', 'note', 'log', 'info', 'help', 'version', 'init' ] handler = [ project.handle_project, task.handle_task, note.handle_note, action.handle_action, info, pit_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_current(conn) logging.debug('Last action: {}'.format(last_action)) logging.debug(argv) logging.debug(argv[2:]) handler[candidate](argv[2:], last_action, conn) conn.commit() conn.close() if __name__ == '__main__': main()