Commit 791fbe2a authored by Bram Schoenmakers's avatar Bram Schoenmakers

Prepare for other CLI interfaces.

Split the CLI class into a CLI base class.
parent 817a7585
......@@ -13,7 +13,7 @@ setup(
'prompt-toolkit': ['prompt-toolkit'],
},
entry_points= {
'console_scripts': ['topydo = topydo.cli.Main:main'],
'console_scripts': ['topydo = topydo.cli.CLI:main'],
},
classifiers = [
"Development Status :: 4 - Beta",
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" Entry file for the Python todo.txt CLI. """
import sys
from topydo.cli.CLIApplicationBase import CLIApplicationBase, write, error
from topydo.lib import TodoFile
from topydo.lib.Config import config, ConfigError
# First thing is to poke the configuration and check whether it's sane
# The modules below may already read in configuration upon import, so
# make sure to bail out if the configuration is invalid.
try:
config()
except ConfigError as config_error:
error(str(config_error))
sys.exit(1)
from topydo.lib.Commands import get_subcommand
from topydo.lib import TodoList
class CLIApplication(CLIApplicationBase):
"""
Class that represents the Command Line Interface of Topydo.
Handles input/output of the various subcommand.
"""
def __init__(self):
super(CLIApplication, self).__init__()
def run(self):
""" Main entry function. """
args = self._process_flags()
self.todofile = TodoFile.TodoFile(self.path)
self.todolist = TodoList.TodoList(self.todofile.read())
(subcommand, args) = get_subcommand(args)
if subcommand == None:
self._usage()
if self._execute(subcommand, args) == False:
sys.exit(1)
def main():
""" Main entry point of the CLI. """
CLIApplication().run()
if __name__ == '__main__':
main()
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -19,10 +19,7 @@
import getopt
import sys
def usage():
""" Prints the command-line usage of topydo. """
print """\
USAGE = """\
Synopsis: topydo [-c <config>] [-d <archive>] [-t <todo.txt>] subcommand [help|args]
topydo -h
topydo -v
......@@ -53,8 +50,6 @@ Available commands:
Run `topydo help <subcommand>` for command-specific help.
"""
sys.exit(0)
def write(p_file, p_string):
"""
Write p_string to file p_file, trailed by a newline character.
......@@ -90,7 +85,6 @@ except ConfigError as config_error:
error(str(config_error))
sys.exit(1)
from topydo.lib.Commands import get_subcommand
from topydo.lib.ArchiveCommand import ArchiveCommand
from topydo.lib.SortCommand import SortCommand
from topydo.lib import TodoFile
......@@ -98,7 +92,7 @@ from topydo.lib import TodoList
from topydo.lib import TodoListBase
from topydo.lib.Utils import escape_ansi
class CLIApplication(object):
class CLIApplicationBase(object):
"""
Class that represents the Command Line Interface of Topydo.
......@@ -111,6 +105,12 @@ class CLIApplication(object):
self.path = self.config.todotxt()
self.archive_path = self.config.archive()
self.todofile = None
def _usage(self):
print USAGE
sys.exit(0)
def _process_flags(self):
try:
opts, args = getopt.getopt(sys.argv[1:], "c:d:ht:v")
......@@ -131,7 +131,7 @@ class CLIApplication(object):
elif opt == "-v":
version()
else:
usage()
self._usage()
self.path = alt_path if alt_path else self.config.todotxt()
self.archive_path = alt_archive \
......@@ -162,6 +162,12 @@ class CLIApplication(object):
else:
pass # TODO
def _input(self):
"""
Returns a function that retrieves user input.
"""
return raw_input
def _execute(self, p_command, p_args):
"""
Execute a subcommand with arguments. p_command is a class (not an
......@@ -172,36 +178,23 @@ class CLIApplication(object):
self.todolist,
lambda o: write(sys.stdout, o),
error,
raw_input)
return False if command.execute() == False else True
def run(self):
""" Main entry function. """
args = self._process_flags()
self._input())
todofile = TodoFile.TodoFile(self.path)
self.todolist = TodoList.TodoList(todofile.read())
if command.execute() != False:
self._post_execute()
return True
(subcommand, args) = get_subcommand(args)
if subcommand == None:
usage()
if self._execute(subcommand, args) == False:
sys.exit(1)
return False
def _post_execute(self):
if self.todolist.is_dirty():
self._archive()
if config().keep_sorted():
self._execute(SortCommand, [])
todofile.write(str(self.todolist))
self.todofile.write(str(self.todolist))
def main():
""" Main entry point of the CLI. """
CLIApplication().run()
def run(self):
raise NotImplementedError
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment