Use an utils module
This commit is contained in:
28
phelp.py
28
phelp.py
@@ -1,31 +1,5 @@
|
|||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
|
from utils import strstr
|
||||||
|
|
||||||
def strstr(haystack, needle):
|
|
||||||
"""
|
|
||||||
Return a sub-string of haystack, going from first occurence of needle until the end.
|
|
||||||
Return None if not found.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
haystack {string} -- the entry string
|
|
||||||
needle {string} -- the part of string searched
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
string -- sub-string of haystack, starting from needle. None if not exist.
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
email = 'name@example.com'
|
|
||||||
domain = strstr(email, '@');
|
|
||||||
print(domain) // Display : @example.com
|
|
||||||
"""
|
|
||||||
pos = haystack.find(needle)
|
|
||||||
if pos < 0: # not found
|
|
||||||
return None
|
|
||||||
|
|
||||||
return haystack[pos:]
|
|
||||||
|
|
||||||
# TODO Review help message / output / usage
|
|
||||||
|
|
||||||
def handle_help(args, *_):
|
def handle_help(args, *_):
|
||||||
if not args:
|
if not args:
|
||||||
|
|||||||
82
pits.py
82
pits.py
@@ -13,6 +13,8 @@ import task
|
|||||||
import note
|
import note
|
||||||
import phelp
|
import phelp
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
# logging.basicConfig(level=logging.ERROR, format='%(levelname)7s :: %(message)s')
|
# 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.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')
|
logging.basicConfig(level=logging.ERROR, format='%(asctime)s :: {%(filename)10s:%(lineno)3d} :: %(funcName)s :: %(levelname)s :: %(message)s')
|
||||||
@@ -23,46 +25,8 @@ SCHEMA_FILENAME = 'pits_schema.sql'
|
|||||||
PIT_VERSION = '0.0.1'
|
PIT_VERSION = '0.0.1'
|
||||||
SCHEMA_VERSION = 1
|
SCHEMA_VERSION = 1
|
||||||
|
|
||||||
def get_file_path(file_name):
|
def pits_init():
|
||||||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
|
db_path = utils.get_file_path(DB_FILENAME)
|
||||||
|
|
||||||
|
|
||||||
def strstr(haystack, needle):
|
|
||||||
"""
|
|
||||||
Return a sub-string of haystack, going from first occurence of needle until the end.
|
|
||||||
Return None if not found.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
haystack {string} -- the entry string
|
|
||||||
needle {string} -- the part of string searched
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
string -- sub-string of haystack, starting from needle. None if not exist.
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
email = 'name@example.com'
|
|
||||||
domain = strstr(email, '@');
|
|
||||||
print(domain) // Display : @example.com
|
|
||||||
"""
|
|
||||||
pos = haystack.find(needle)
|
|
||||||
if pos < 0: # not found
|
|
||||||
return None
|
|
||||||
|
|
||||||
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))
|
logging.debug('Search database in {}'.format(db_path))
|
||||||
|
|
||||||
@@ -77,7 +41,7 @@ def init():
|
|||||||
|
|
||||||
with sqlite3.connect(db_path) as conn:
|
with sqlite3.connect(db_path) as conn:
|
||||||
logging.info('Creating schema')
|
logging.info('Creating schema')
|
||||||
schema_path = get_file_path(SCHEMA_FILENAME)
|
schema_path = utils.get_file_path(SCHEMA_FILENAME)
|
||||||
logging.debug('Schema file path: {}'.format(schema_path))
|
logging.debug('Schema file path: {}'.format(schema_path))
|
||||||
|
|
||||||
with open(schema_path, 'rt') as f:
|
with open(schema_path, 'rt') as f:
|
||||||
@@ -97,25 +61,19 @@ 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 info(_1, _2, conn):
|
def pits_info(_1, _2, conn):
|
||||||
print('Pit version: {}'.format(PIT_VERSION))
|
print('Pit version: {}'.format(PIT_VERSION))
|
||||||
print('Pit file name: {}'.format(get_file_path(DB_FILENAME)))
|
print('Pit file name: {}'.format(utils.get_file_path(DB_FILENAME)))
|
||||||
print('Created by: {} on {}'.format(*action.get_first_action(conn)))
|
print('Created by: {} on {}'.format(*action.get_first_action(conn)))
|
||||||
print('Last updated by: {} on {}'.format(*action.get_last_action(conn)))
|
print('Last updated by: {} on {}'.format(*action.get_last_action(conn)))
|
||||||
print('Schema version: {}'.format(SCHEMA_VERSION))
|
print('Schema version: {}'.format(SCHEMA_VERSION))
|
||||||
print('Projects: {}'.format(count_object(conn, 'project')))
|
print('Projects: {}'.format(utils.count_object(conn, 'project')))
|
||||||
print('Tasks: {}'.format(count_object(conn, 'task')))
|
print('Tasks: {}'.format(utils.count_object(conn, 'task')))
|
||||||
print('Notes: {}'.format(count_object(conn, 'note')))
|
print('Notes: {}'.format(utils.count_object(conn, 'note')))
|
||||||
print('Log entries: {}'.format(count_object(conn, 'action')))
|
print('Log entries: {}'.format(utils.count_object(conn, 'action')))
|
||||||
|
|
||||||
def count_object(conn, table_name):
|
def pits_version(*_):
|
||||||
query = "SELECT count(*) FROM " + table_name
|
print('Pits version 0.1.0')
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute(query)
|
|
||||||
return cursor.fetchone()[0]
|
|
||||||
|
|
||||||
def version(*_):
|
|
||||||
print('pit version 0.1.0')
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
cmd_handler = [
|
cmd_handler = [
|
||||||
@@ -123,10 +81,10 @@ def main():
|
|||||||
('task', task.handle_task),
|
('task', task.handle_task),
|
||||||
('note', note.handle_note),
|
('note', note.handle_note),
|
||||||
('log', action.handle_action),
|
('log', action.handle_action),
|
||||||
('info', info),
|
('info', pits_info),
|
||||||
('help', phelp.handle_help),
|
('help', phelp.handle_help),
|
||||||
('version', version),
|
('version', pits_version),
|
||||||
('init', init)
|
('init', pits_init)
|
||||||
]
|
]
|
||||||
candidate = -1
|
candidate = -1
|
||||||
|
|
||||||
@@ -137,7 +95,7 @@ def main():
|
|||||||
argv.append(None)
|
argv.append(None)
|
||||||
|
|
||||||
for i in range(len(cmd_handler)):
|
for i in range(len(cmd_handler)):
|
||||||
if strstr(cmd_handler[i][0], argv[1]) == cmd_handler[i][0]:
|
if utils.strstr(cmd_handler[i][0], argv[1]) == cmd_handler[i][0]:
|
||||||
if candidate < 0:
|
if candidate < 0:
|
||||||
candidate = i
|
candidate = i
|
||||||
else:
|
else:
|
||||||
@@ -145,14 +103,14 @@ def main():
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if candidate < 0:
|
if candidate < 0:
|
||||||
print("Invalid command ({}), run '{} help' for help".format(argv[1], os.path.basename(__file__)))
|
print("Invalid command ({}), run '{} help' for help".format(argv[1], utils.get_pits_path()))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if candidate == (len(cmd_handler) -1):
|
if candidate == (len(cmd_handler) -1):
|
||||||
init()
|
pits_init()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
conn = create_connection(DB_FILENAME)
|
conn = utils.create_connection(DB_FILENAME)
|
||||||
last_action = action.read_current(conn)
|
last_action = action.read_current(conn)
|
||||||
logging.debug('Last action: {}'.format(last_action))
|
logging.debug('Last action: {}'.format(last_action))
|
||||||
logging.debug(argv)
|
logging.debug(argv)
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ from action import TypeAction
|
|||||||
from args import get_string_arg
|
from args import get_string_arg
|
||||||
from args import arg_number
|
from args import arg_number
|
||||||
|
|
||||||
|
from utils import get_pits_path
|
||||||
|
|
||||||
class Project:
|
class Project:
|
||||||
def __init__(self, status = None):
|
def __init__(self, status = None):
|
||||||
self.name = None
|
self.name = None
|
||||||
@@ -112,8 +114,8 @@ def edit_project(project, project_id, conn):
|
|||||||
|
|
||||||
if not update_args:
|
if not update_args:
|
||||||
print("nothing to update")
|
print("nothing to update")
|
||||||
print("Tips: if you want to set an active project, just do '{} project <project_id>'"
|
print("Tips: if you want to active a project, just do '{} project <project_id>'"
|
||||||
.format(os.path.basename(sys.argv[0])))
|
.format(get_pits_path()))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
logging.debug('Project update args: {}'.format(update_args))
|
logging.debug('Project update args: {}'.format(update_args))
|
||||||
|
|||||||
6
task.py
6
task.py
@@ -13,6 +13,8 @@ from action import set_active
|
|||||||
from args import get_string_arg
|
from args import get_string_arg
|
||||||
from args import arg_number
|
from args import arg_number
|
||||||
|
|
||||||
|
from utils import get_pits_path
|
||||||
|
|
||||||
class Task:
|
class Task:
|
||||||
def __init__(self, status = None, priority = None):
|
def __init__(self, status = None, priority = None):
|
||||||
self.name = None
|
self.name = None
|
||||||
@@ -160,8 +162,8 @@ def edit_task(task, task_id, project_id, conn):
|
|||||||
|
|
||||||
if not update_args:
|
if not update_args:
|
||||||
print("nothing to update")
|
print("nothing to update")
|
||||||
print("Tips: if you want to set an active task, just do '{} task <task_id>'"
|
print("Tips: if you want to activate a task, just do '{} task <task_id>'"
|
||||||
.format(os.path.basename(sys.argv[0])))
|
.format(get_pits_path()))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
logging.debug('Task update args: {}'.format(update_args))
|
logging.debug('Task update args: {}'.format(update_args))
|
||||||
|
|||||||
52
utils.py
Normal file
52
utils.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import sqlite3
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def get_file_path(file_name):
|
||||||
|
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
|
||||||
|
|
||||||
|
|
||||||
|
def get_pits_path():
|
||||||
|
return os.path.basename(sys.argv[0])
|
||||||
|
|
||||||
|
|
||||||
|
def strstr(haystack, needle):
|
||||||
|
"""
|
||||||
|
Return a sub-string of haystack, going from first occurence of needle until the end.
|
||||||
|
Return None if not found.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
haystack {string} -- the entry string
|
||||||
|
needle {string} -- the part of string searched
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
string -- sub-string of haystack, starting from needle. None if not exist.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
email = 'name@example.com'
|
||||||
|
domain = strstr(email, '@');
|
||||||
|
print(domain) // Display : @example.com
|
||||||
|
"""
|
||||||
|
pos = haystack.find(needle)
|
||||||
|
if pos < 0: # not found
|
||||||
|
return None
|
||||||
|
|
||||||
|
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 count_object(conn, table_name):
|
||||||
|
query = "SELECT count(*) FROM " + table_name
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
return cursor.fetchone()[0]
|
||||||
Reference in New Issue
Block a user