Skip to content
This repository has been archived by the owner on Dec 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from tubone24/makezip
Browse files Browse the repository at this point in the history
Makezip
  • Loading branch information
tubone24 committed Jan 17, 2019
2 parents 18ba502 + 09bd528 commit ff37895
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 50 deletions.
6 changes: 6 additions & 0 deletions docs/_modules/ebook_homebrew/cli.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ <h1>Source code for ebook_homebrew.cli</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">argparse</span>

<span class="kn">from</span> <span class="nn">ebook_homebrew.helper</span> <span class="k">import</span> <span class="n">auto</span>
<span class="kn">from</span> <span class="nn">ebook_homebrew.helper</span> <span class="k">import</span> <span class="n">show_version</span>


<div class="viewcode-block" id="execute_auto"><a class="viewcode-back" href="../../ebook_homebrew.html#ebook_homebrew.cli.execute_auto">[docs]</a><span class="k">def</span> <span class="nf">execute_auto</span><span class="p">(</span><span class="n">args_auto</span><span class="p">):</span>
Expand All @@ -166,6 +167,9 @@ <h1>Source code for ebook_homebrew.cli</h1><div class="highlight"><pre>
<span class="n">epilog</span><span class="o">=</span><span class="s2">&quot;More information? Access here: https://github.com/tubone24/ebook_homebrew&quot;</span><span class="p">,</span>
<span class="n">add_help</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="s2">&quot;--version&quot;</span><span class="p">,</span>
<span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s2">&quot;Show version&quot;</span><span class="p">)</span>

<span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s2">&quot;Choose subcommands. Usually choose </span><span class="se">\&quot;</span><span class="s2">auto</span><span class="se">\&quot;</span><span class="s2">&quot;</span><span class="p">)</span>

Expand Down Expand Up @@ -242,6 +246,8 @@ <h1>Source code for ebook_homebrew.cli</h1><div class="highlight"><pre>

<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s2">&quot;handler&quot;</span><span class="p">):</span>
<span class="n">args</span><span class="o">.</span><span class="n">handler</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">args</span><span class="o">.</span><span class="n">version</span><span class="p">:</span>
<span class="n">show_version</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span></div>

Expand Down
5 changes: 5 additions & 0 deletions docs/_modules/ebook_homebrew/helper.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ <h1>Source code for ebook_homebrew.helper</h1><div class="highlight"><pre>

<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">from</span> <span class="nn">.__init__</span> <span class="k">import</span> <span class="n">__version__</span>
<span class="kn">from</span> <span class="nn">.convert</span> <span class="k">import</span> <span class="n">Image2PDF</span>
<span class="kn">from</span> <span class="nn">.exceptions</span> <span class="k">import</span> <span class="n">BaseError</span>
<span class="kn">from</span> <span class="nn">.rename</span> <span class="k">import</span> <span class="n">ChangeFilename</span>
Expand Down Expand Up @@ -186,6 +187,10 @@ <h1>Source code for ebook_homebrew.helper</h1><div class="highlight"><pre>
<span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span></div>


<div class="viewcode-block" id="show_version"><a class="viewcode-back" href="../../ebook_homebrew.html#ebook_homebrew.helper.show_version">[docs]</a><span class="k">def</span> <span class="nf">show_version</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;ebook_homebrew: </span><span class="si">{version}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">version</span><span class="o">=</span><span class="n">__version__</span><span class="p">))</span></div>
</pre></div>

</div>
Expand Down
5 changes: 5 additions & 0 deletions docs/ebook_homebrew.html
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this
<code class="descclassname">ebook_homebrew.helper.</code><code class="descname">auto</code><span class="sig-paren">(</span><em>args</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ebook_homebrew/helper.html#auto"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ebook_homebrew.helper.auto" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="ebook_homebrew.helper.show_version">
<code class="descclassname">ebook_homebrew.helper.</code><code class="descname">show_version</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ebook_homebrew/helper.html#show_version"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#ebook_homebrew.helper.show_version" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</div>
<div class="section" id="module-ebook_homebrew.rename">
<span id="ebook-homebrew-rename-module"></span><h2>ebook_homebrew.rename module<a class="headerlink" href="#module-ebook_homebrew.rename" title="Permalink to this headline"></a></h2>
Expand Down
9 changes: 9 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ <h1 id="index">Index</h1>
| <a href="#I"><strong>I</strong></a>
| <a href="#L"><strong>L</strong></a>
| <a href="#M"><strong>M</strong></a>
| <a href="#S"><strong>S</strong></a>
| <a href="#T"><strong>T</strong></a>
| <a href="#W"><strong>W</strong></a>
| <a href="#Z"><strong>Z</strong></a>
Expand Down Expand Up @@ -321,6 +322,14 @@ <h2 id="M">M</h2>
</ul></td>
</tr></table>

<h2 id="S">S</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="ebook_homebrew.html#ebook_homebrew.helper.show_version">show_version() (in module ebook_homebrew.helper)</a>
</li>
</ul></td>
</tr></table>

<h2 id="T">T</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ebook_homebrew/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0a1'
__version__ = '1.0.1a1'
58 changes: 58 additions & 0 deletions ebook_homebrew/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import argparse

from ebook_homebrew.helper import auto
from ebook_homebrew.helper import show_version


def execute_auto(args_auto):
Expand All @@ -16,6 +17,9 @@ def main():
epilog="More information? Access here: https://github.com/tubone24/ebook_homebrew",
add_help=True,
)
parser.add_argument("-v", "--version",
action="store_true",
help="Show version")

subparsers = parser.add_subparsers(description="Choose subcommands. Usually choose \"auto\"")

Expand Down Expand Up @@ -88,10 +92,64 @@ def main():
action="store_true",
help="no verify users.")

parser_make_zip = subparsers.add_parser("makezip",
description="Make zip file for files "
"which you choose extension.",
help="Make zip file for adding specify extension files.")

parser_make_zip.add_argument("-s", "--src_dir",
action="store",
nargs=1,
const=None,
default=None,
required=True,
type=str,
help="Source directory which put original image files.",
metavar="SRC_DIR")

parser_make_zip.add_argument("--dst_dir",
action="store",
nargs=1,
const=None,
default=None,
type=str,
help="Destination directory which put e-book file.",
metavar="DST_DIR")

parser_make_zip.add_argument("-e", "--extension",
action="store",
nargs=1,
const=None,
default=None,
required=True,
type=str,
help="Destination directory which put e-book file.",
metavar="EXT")

parser_make_zip.add_argument("-f", "--filename",
action="store",
nargs=1,
const=None,
default=None,
required=True,
type=str,
help="Destination directory which put e-book file.",
metavar="FILENAME")

parser_make_zip.add_argument("-r", "--remove",
action="store_true",
help="Remove original image file.")

parser_make_zip.add_argument("-y", "--assume_yes",
action="store_true",
help="no verify users.")

args = parser.parse_args()

if hasattr(args, "handler"):
args.handler(args)
elif args.version:
show_version()
else:
parser.print_help()

Expand Down
25 changes: 3 additions & 22 deletions ebook_homebrew/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ def make_pdf(self, filename, remove_flag=False):

for file in files:
num = self._check_serial_number(file, self.__digits)
if not num:
logger.debug("Skip(No number): {filename}".format(filename=file))
elif not self.__regex_ext.search(file):
logger.debug("Skip(No target extension): {filename}".format(filename=file))
if self._check_skip_file(file, self.__regex_ext, num):
pass
else:
logger.debug(file)
pdf_file = self._convert_image_to_pdf(file)
result_merge_pdf = self._merge_pdf_file(pdf_file, filename)

if result_merge_pdf:
if self._merge_pdf_file(pdf_file, filename):
logger.info("Success write pdf for {page} page.".format(page=page_count + 1))
page_count += 1
if remove_flag:
Expand Down Expand Up @@ -134,18 +130,3 @@ def _write_pdf(self, file_name):
with open(file_name, "wb") as f:
self.__file_writer.write(f)
return True

def move_file(self, file, dst, assume_yes):
"""Move file
Args:
file (str): Target file name
dst (str): Target destination path
assume_yes (bool): If true, no verify users
Returns:
bool: If success, return true. Nothing target, return false.
"""
destination = os.path.join(dst, file)
return self._move_file(file=file, dst=destination, assume_yes=assume_yes)
35 changes: 35 additions & 0 deletions ebook_homebrew/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ def _check_digit_format(digits):
except TypeError:
raise InvalidDigitsFormat()

@staticmethod
def _check_skip_file(filename, regex_ext, num):
"""Check skip or target file for target extension and num regex
Args:
filename (str): Filename which check
regex_ext (Match): Compile extension object
num (Match): Number match object
Returns:
bool: If True, filename is skip file.
"""
if not num:
logger.debug("Skip(No number): {filename}".format(filename=filename))
return True
elif not regex_ext.search(filename):
logger.debug("Skip(No target extension): {filename}".format(filename=filename))
return True
else:
return False

@staticmethod
def _rename_file(old_name, new_name):
"""Rename filename.
Expand Down Expand Up @@ -209,3 +229,18 @@ def _check_image_file(self, file_name):
draw_pic.show()
else:
raise InvalidImageParameterType()

def move_file(self, file, dst, assume_yes):
"""Move file
Args:
file (str): Target file name
dst (str): Target destination path
assume_yes (bool): If true, no verify users
Returns:
bool: If success, return true. Nothing target, return false.
"""
destination = os.path.join(dst, file)
return self._move_file(file=file, dst=destination, assume_yes=assume_yes)
24 changes: 22 additions & 2 deletions ebook_homebrew/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import sys

from .__init__ import __version__
from .convert import Image2PDF
from .exceptions import BaseError
from .rename import ChangeFilename
from .utils.logging import get_logger

# from archive import MakeArchive
from .archive import MakeArchive

logger = get_logger("helper")

Expand All @@ -36,3 +36,23 @@ def auto(args):
logger.exception(e)
logger.critical(e)
sys.exit(1)


def make_zip(args):
try:
make_zip = MakeArchive(extension=args.extension[0], directory_path=args.src_dir[0])
make_zip.make_zip(filename=args.filename[0], remove_flag=args.remove, overwrite=False)
if args.dst_dir is not None:
make_zip.move_file(file=args.filename[0], dst=args.dst_dir[0], assume_yes=args.assume_yes)
except BaseError as e:
logger.exception(e)
sys.exit(2)
except Exception as e:
logger.error("Unhandled Error occurred.")
logger.exception(e)
logger.critical(e)
sys.exit(1)


def show_version():
print("ebook_homebrew: {version}".format(version=__version__))
6 changes: 2 additions & 4 deletions ebook_homebrew/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ def filename_to_digit_number(self):

for file in files:
num = self._check_serial_number(file, self.__digits)
if not num:
logger.debug("Skip(No number): {filename}".format(filename=file))
elif not self.__regex_ext.search(file):
logger.debug("Skip(No target extension): {filename}".format(filename=file))
if self._check_skip_file(file, self.__regex_ext, num):
pass
else:
new_name = self._create_new_name(num, max_digit, self.__extension)
if not self.__check_exist_file(new_name, file, True):
Expand Down

0 comments on commit ff37895

Please sign in to comment.