Init (override) + info + version
This commit is contained in:
34
action.py
34
action.py
@@ -100,4 +100,36 @@ def read_current(conn):
|
|||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
last_action = Action(cursor.fetchone())
|
last_action = Action(cursor.fetchone())
|
||||||
return last_action
|
return last_action
|
||||||
|
|
||||||
|
def _get_border_action(conn, last = False):
|
||||||
|
# Used for info
|
||||||
|
logging.info('> get_border_action')
|
||||||
|
# query = 'SELECT * FROM action WHERE id = (SELECT MAX(id) FROM action'
|
||||||
|
# query = 'SELECT project_id, task_id, note_id FROM action'
|
||||||
|
query = 'SELECT username, created_at FROM action ORDER BY id {} LIMIT 1;'
|
||||||
|
query = query.format('DESC' if last else '')
|
||||||
|
|
||||||
|
logging.debug(query)
|
||||||
|
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
data = cursor.fetchone()
|
||||||
|
|
||||||
|
formated_date = datetime.strptime(data[1][:26], '%Y-%m-%d %H:%M:%S.%f').strftime('%b %d, %Y at %H:%M')
|
||||||
|
# TODO Extrat a format date method
|
||||||
|
|
||||||
|
return (data[0], formated_date)
|
||||||
|
|
||||||
|
|
||||||
|
def get_first_action(conn):
|
||||||
|
"""
|
||||||
|
Get first action entry and return a tuple with username and readable date
|
||||||
|
"""
|
||||||
|
return _get_border_action(conn)
|
||||||
|
|
||||||
|
def get_last_action(conn):
|
||||||
|
"""
|
||||||
|
Get last action entry and return a tuple with username and readable date
|
||||||
|
"""
|
||||||
|
return _get_border_action(conn, True)
|
||||||
54
pits.py
54
pits.py
@@ -19,6 +19,9 @@ logging.basicConfig(level=logging.ERROR, format='%(asctime)s :: {%(filename)10s:
|
|||||||
DB_FILENAME = 'pits.db'
|
DB_FILENAME = 'pits.db'
|
||||||
SCHEMA_FILENAME = 'pits_schema.sql'
|
SCHEMA_FILENAME = 'pits_schema.sql'
|
||||||
|
|
||||||
|
PIT_VERSION = '0.0.1'
|
||||||
|
SCHEMA_VERSION = 1
|
||||||
|
|
||||||
def get_file_path(file_name):
|
def get_file_path(file_name):
|
||||||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
|
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
|
||||||
|
|
||||||
@@ -30,33 +33,30 @@ def strstr(haystack, needle):
|
|||||||
else:
|
else:
|
||||||
return haystack[pos:]
|
return haystack[pos:]
|
||||||
|
|
||||||
def info():
|
|
||||||
logging.info('info function')
|
|
||||||
|
|
||||||
def handle_note(args):
|
|
||||||
logging.info('>> handle note')
|
|
||||||
|
|
||||||
def create_connection(db_filename):
|
def create_connection(db_filename):
|
||||||
db_path = get_file_path(db_filename)
|
db_path = get_file_path(db_filename)
|
||||||
logging.debug("Try to connect to {}".format(db_path))
|
logging.debug("Try to connect to {}".format(db_path))
|
||||||
try:
|
try:
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
return conn
|
return conn
|
||||||
except Error as e:
|
except sqlite3.Error as sqlite3_error:
|
||||||
print(e)
|
print(sqlite3_error)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
# TODO Use init command like original pit
|
|
||||||
db_path = get_file_path(DB_FILENAME)
|
db_path = get_file_path(DB_FILENAME)
|
||||||
|
|
||||||
logging.debug('Search database in {}'.format(db_path))
|
logging.debug('Search database in {}'.format(db_path))
|
||||||
db_is_new = not os.path.exists(db_path)
|
|
||||||
if not db_is_new:
|
if os.path.exists(db_path):
|
||||||
print('Database already exist')
|
valid = {"yes": True, "y": True, "ye": True}
|
||||||
# TODO Permit to override
|
print('{} already exists, do you want to override it? [y/N]:'.format(db_path), end='')
|
||||||
return
|
choice = input().lower()
|
||||||
|
if choice in valid:
|
||||||
|
os.remove(db_path)
|
||||||
|
else:
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
with sqlite3.connect(db_path) as conn:
|
with sqlite3.connect(db_path) as conn:
|
||||||
logging.info('Creating schema')
|
logging.info('Creating schema')
|
||||||
@@ -77,11 +77,27 @@ def init():
|
|||||||
# conn.executescript("""
|
# conn.executescript("""
|
||||||
# insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""")
|
# insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""")
|
||||||
|
|
||||||
def version(args, last_action, conn):
|
def info(_1, _2, conn):
|
||||||
print('TODO version')
|
print('Pit version: {}'.format(PIT_VERSION))
|
||||||
# TODO 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')
|
print('TODO help')
|
||||||
# TODO help
|
# TODO help
|
||||||
|
|
||||||
@@ -93,7 +109,7 @@ def main():
|
|||||||
note.handle_note,
|
note.handle_note,
|
||||||
action.handle_action,
|
action.handle_action,
|
||||||
info,
|
info,
|
||||||
help,
|
pit_help,
|
||||||
version]
|
version]
|
||||||
candidate = -1
|
candidate = -1
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
-- schema.sql
|
-- schema.sql
|
||||||
|
|
||||||
-- Schema for pit in python / SQLite mode
|
-- Schema for pit in python / SQLite mode
|
||||||
-- v0.1
|
-- v1
|
||||||
|
|
||||||
CREATE TABLE project (
|
CREATE TABLE project (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
|
|||||||
Reference in New Issue
Block a user