diff --git a/fimfarchive/commands/__init__.py b/fimfarchive/commands/__init__.py index 00ccc3c..47e813d 100644 --- a/fimfarchive/commands/__init__.py +++ b/fimfarchive/commands/__init__.py @@ -5,7 +5,7 @@ Command module. # # Fimfarchive, preserves stories from Fimfiction. -# Copyright (C) 2015 Joakim Soderlund +# Copyright (C) 2020 Joakim Soderlund # # 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 @@ -23,6 +23,7 @@ Command module. from .base import Command +from .build import BuildCommand from .root import RootCommand from .update import UpdateCommand @@ -30,5 +31,6 @@ from .update import UpdateCommand __all__ = ( 'Command', 'RootCommand', + 'BuildCommand', 'UpdateCommand', ) diff --git a/fimfarchive/commands/build.py b/fimfarchive/commands/build.py new file mode 100644 index 0000000..c913355 --- /dev/null +++ b/fimfarchive/commands/build.py @@ -0,0 +1,119 @@ +""" +Build command. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2020 Joakim Soderlund +# +# 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 . +# + + +from argparse import ArgumentParser, Namespace, FileType + +from fimfarchive.fetchers import DirectoryFetcher, FimfarchiveFetcher +from fimfarchive.flavors import DataFormat +from fimfarchive.tasks import BuildTask +from fimfarchive.utils import tqdm + +from .base import Command + + +__all__ = ( + 'BuildCommand', +) + + +class BuildCommand(Command): + """ + Builds a new Fimfarchive release. + """ + + @property + def parser(self) -> ArgumentParser: + """ + Returns a command line arguments parser. + """ + parser = ArgumentParser( + prog='', + description=self.__doc__, + ) + + parser.add_argument( + '--archive', + help="previous version of the archive", + type=FileType('rb'), + required=True, + metavar='PATH', + ) + + parser.add_argument( + '--output', + help="directory for the output archive", + default='worktree/build', + ) + + parser.add_argument( + '--meta', + help="directory to fetch meta from", + default='worktree/update/meta', + ) + + parser.add_argument( + '--data', + help="directory to fetch data from", + default='worktree/render/epub', + ) + + parser.add_argument( + '--extras', + help="directory to fetch extras from", + default='worktree/extras', + ) + + return parser + + def configure(self, opts: Namespace) -> BuildTask: + """ + Returns a configured task instance. + + Args: + opts: Parsed command line arguments. + """ + fimfarchive = FimfarchiveFetcher( + source=opts.archive, + ) + + directory = DirectoryFetcher( + meta_path=opts.meta, + data_path=opts.data, + flavors=[DataFormat.EPUB], + ) + + return BuildTask( + output=opts.output, + upcoming=tqdm(directory), + previous=fimfarchive, + extras=opts.extras, + ) + + def __call__(self, *args): + opts = self.parser.parse_args(args) + task = self.configure(opts) + + task.run() + + return 0 diff --git a/fimfarchive/commands/root.py b/fimfarchive/commands/root.py index b5adcc9..cf92524 100644 --- a/fimfarchive/commands/root.py +++ b/fimfarchive/commands/root.py @@ -5,7 +5,7 @@ Root command. # # Fimfarchive, preserves stories from Fimfiction. -# Copyright (C) 2015 Joakim Soderlund +# Copyright (C) 2020 Joakim Soderlund # # 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 @@ -25,6 +25,7 @@ Root command. from typing import Dict, Type from .base import Command +from .build import BuildCommand from .update import UpdateCommand @@ -38,6 +39,7 @@ class RootCommand(Command): The main application command. """ commands: Dict[str, Type[Command]] = { + 'build': BuildCommand, 'update': UpdateCommand, }