Logging + DB connection + add init action
I hope script could be used anywhere in filesystem
This commit is contained in:
@@ -7,6 +7,7 @@ import datetime
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
import sys
|
||||
|
||||
class TypeAction(Enum):
|
||||
CREATE = 'created'
|
||||
@@ -34,9 +35,15 @@ def handle_action(args, last_action, conn):
|
||||
for row in cursor.fetchall():
|
||||
log_id, project_id, task_id, note_id, username, taction, message, created_at = row
|
||||
logging.debug(row)
|
||||
|
||||
formated_date = datetime.strptime(created_at[:26], '%Y-%m-%d %H:%M:%S.%f').strftime('%b %d, %Y %H:%M')
|
||||
|
||||
if taction == 'init':
|
||||
print('{} ({}): {}'.format(formated_date, username, message))
|
||||
sys.exit(0)
|
||||
|
||||
object_type = ''
|
||||
id_object = ''
|
||||
formated_date = datetime.strptime(created_at[:26], '%Y-%m-%d %H:%M:%S.%f').strftime('%b %d, %Y %H:%M')
|
||||
|
||||
object_type = '' # TODO Enum ?
|
||||
if (project_id):
|
||||
|
||||
54
pits.py
54
pits.py
@@ -13,7 +13,14 @@ import note
|
||||
import pit_argparser
|
||||
|
||||
# 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='%(levelname)7s :: %(message)s')
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: {%(filename)10s:%(lineno)d} :: %(funcName)s :: %(levelname)s :: %(message)s')
|
||||
|
||||
DB_FILENAME = 'pits.db'
|
||||
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 info():
|
||||
logging.info('info function')
|
||||
@@ -22,40 +29,57 @@ 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_filename)
|
||||
conn = sqlite3.connect(db_path)
|
||||
return conn
|
||||
except Error as e:
|
||||
print(e)
|
||||
|
||||
return None
|
||||
|
||||
db_filename = 'pits.db'
|
||||
schema_filename = 'pits_schema.sql'
|
||||
def init():
|
||||
# TODO Use init command like original pit
|
||||
db_path = get_file_path(DB_FILENAME)
|
||||
|
||||
# TODO Use init command like original pit
|
||||
# TODO Need to add an action - log init
|
||||
db_is_new = not os.path.exists(db_filename)
|
||||
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')
|
||||
return
|
||||
|
||||
with sqlite3.connect(db_filename) as conn:
|
||||
if db_is_new:
|
||||
with sqlite3.connect(db_path) as conn:
|
||||
logging.info('Creating schema')
|
||||
with open(schema_filename, 'rt') as f:
|
||||
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')""")
|
||||
|
||||
if __name__ == '__main__':
|
||||
init()
|
||||
parser = pit_argparser.create_parser()
|
||||
args = parser.parse_args()
|
||||
|
||||
with conn:
|
||||
last_action = action.read_last_action(conn)
|
||||
logging.debug('Last action: {}'.format(last_action))
|
||||
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)
|
||||
args.func(args, last_action, conn)
|
||||
|
||||
print('END')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print('END')
|
||||
print(os.path.dirname(os.path.abspath(__file__)))
|
||||
Reference in New Issue
Block a user