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( ...@@ -13,7 +13,7 @@ setup(
'prompt-toolkit': ['prompt-toolkit'], 'prompt-toolkit': ['prompt-toolkit'],
}, },
entry_points= { entry_points= {
'console_scripts': ['topydo = topydo.cli.Main:main'], 'console_scripts': ['topydo = topydo.cli.CLI:main'],
}, },
classifiers = [ classifiers = [
"Development Status :: 4 - Beta", "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. # 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 # 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 # it under the terms of the GNU General Public License as published by
...@@ -19,10 +19,7 @@ ...@@ -19,10 +19,7 @@
import getopt import getopt
import sys import sys
def usage(): USAGE = """\
""" Prints the command-line usage of topydo. """
print """\
Synopsis: topydo [-c <config>] [-d <archive>] [-t <todo.txt>] subcommand [help|args] Synopsis: topydo [-c <config>] [-d <archive>] [-t <todo.txt>] subcommand [help|args]
topydo -h topydo -h
topydo -v topydo -v
...@@ -53,8 +50,6 @@ Available commands: ...@@ -53,8 +50,6 @@ Available commands:
Run `topydo help <subcommand>` for command-specific help. Run `topydo help <subcommand>` for command-specific help.
""" """
sys.exit(0)
def write(p_file, p_string): def write(p_file, p_string):
""" """
Write p_string to file p_file, trailed by a newline character. Write p_string to file p_file, trailed by a newline character.
...@@ -90,7 +85,6 @@ except ConfigError as config_error: ...@@ -90,7 +85,6 @@ except ConfigError as config_error:
error(str(config_error)) error(str(config_error))
sys.exit(1) sys.exit(1)
from topydo.lib.Commands import get_subcommand
from topydo.lib.ArchiveCommand import ArchiveCommand from topydo.lib.ArchiveCommand import ArchiveCommand
from topydo.lib.SortCommand import SortCommand from topydo.lib.SortCommand import SortCommand
from topydo.lib import TodoFile from topydo.lib import TodoFile
...@@ -98,7 +92,7 @@ from topydo.lib import TodoList ...@@ -98,7 +92,7 @@ from topydo.lib import TodoList
from topydo.lib import TodoListBase from topydo.lib import TodoListBase
from topydo.lib.Utils import escape_ansi from topydo.lib.Utils import escape_ansi
class CLIApplication(object): class CLIApplicationBase(object):
""" """
Class that represents the Command Line Interface of Topydo. Class that represents the Command Line Interface of Topydo.
...@@ -111,6 +105,12 @@ class CLIApplication(object): ...@@ -111,6 +105,12 @@ class CLIApplication(object):
self.path = self.config.todotxt() self.path = self.config.todotxt()
self.archive_path = self.config.archive() self.archive_path = self.config.archive()
self.todofile = None
def _usage(self):
print USAGE
sys.exit(0)
def _process_flags(self): def _process_flags(self):
try: try:
opts, args = getopt.getopt(sys.argv[1:], "c:d:ht:v") opts, args = getopt.getopt(sys.argv[1:], "c:d:ht:v")
...@@ -131,7 +131,7 @@ class CLIApplication(object): ...@@ -131,7 +131,7 @@ class CLIApplication(object):
elif opt == "-v": elif opt == "-v":
version() version()
else: else:
usage() self._usage()
self.path = alt_path if alt_path else self.config.todotxt() self.path = alt_path if alt_path else self.config.todotxt()
self.archive_path = alt_archive \ self.archive_path = alt_archive \
...@@ -162,6 +162,12 @@ class CLIApplication(object): ...@@ -162,6 +162,12 @@ class CLIApplication(object):
else: else:
pass # TODO pass # TODO
def _input(self):
"""
Returns a function that retrieves user input.
"""
return raw_input
def _execute(self, p_command, p_args): def _execute(self, p_command, p_args):
""" """
Execute a subcommand with arguments. p_command is a class (not an Execute a subcommand with arguments. p_command is a class (not an
...@@ -172,36 +178,23 @@ class CLIApplication(object): ...@@ -172,36 +178,23 @@ class CLIApplication(object):
self.todolist, self.todolist,
lambda o: write(sys.stdout, o), lambda o: write(sys.stdout, o),
error, error,
raw_input) self._input())
return False if command.execute() == False else True if command.execute() != False:
self._post_execute()
return True
def run(self): return False
""" Main entry function. """
args = self._process_flags()
todofile = TodoFile.TodoFile(self.path)
self.todolist = TodoList.TodoList(todofile.read())
(subcommand, args) = get_subcommand(args)
if subcommand == None:
usage()
if self._execute(subcommand, args) == False:
sys.exit(1)
def _post_execute(self):
if self.todolist.is_dirty(): if self.todolist.is_dirty():
self._archive() self._archive()
if config().keep_sorted(): if config().keep_sorted():
self._execute(SortCommand, []) self._execute(SortCommand, [])
todofile.write(str(self.todolist)) self.todofile.write(str(self.todolist))
def main(): def run(self):
""" Main entry point of the CLI. """ raise NotImplementedError
CLIApplication().run()
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