Skip to content

Commit

Permalink
Merge pull request #32 from sezanzeb/logs_and_codestyle
Browse files Browse the repository at this point in the history
Logs and codestyle
  • Loading branch information
kassoulet committed Jun 3, 2020
2 parents bc2f10e + ea37914 commit c36f8b2
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 425 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ Testing
To start unittests, use::

make test

173 changes: 108 additions & 65 deletions bin/soundconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

"""
SoundConverter Launcher.
"""

"""SoundConverter Launcher."""

# imports and package setup

Expand Down Expand Up @@ -58,20 +55,21 @@
gettext.bindtextdomain(PACKAGE, '@datadir@/locale')
gettext.textdomain(PACKAGE)
gettext.install(PACKAGE, localedir='@datadir@/locale')
#from gettext import gettext as _
# rom gettext import gettext as _
except locale.Error:
print(' cannot use system locale.')
locale.setlocale(locale.LC_ALL, 'C')
gettext.textdomain(PACKAGE)
gettext.install(PACKAGE, localedir='@datadir@/locale')


def _add_soundconverter_path():
""" Makes the soundconverter package importable, which
has been installed to LIBDIR during make install """
"""Make the soundconverter package importable, which has been installed to LIBDIR during make install."""
root = os.path.join(LIBDIR, 'soundconverter', 'python')
if not root in sys.path:
if root not in sys.path:
sys.path.insert(0, root)


_add_soundconverter_path()
import soundconverter
soundconverter.NAME = NAME
Expand All @@ -85,9 +83,12 @@ def _add_soundconverter_path():

# command line argument parsing, launch-mode


def check_mime_type(mime):
types = {'vorbis': 'audio/x-vorbis', 'flac': 'audio/x-flac', 'wav' : 'audio/x-wav',
'mp3': 'audio/mpeg', 'aac': 'audio/x-m4a'}
types = {
'vorbis': 'audio/x-vorbis', 'flac': 'audio/x-flac', 'wav': 'audio/x-wav',
'mp3': 'audio/mpeg', 'aac': 'audio/x-m4a'
}
mime = types.get(mime, mime)
if mime not in list(types.values()):
print(('Cannot use "%s" mime type.' % mime))
Expand All @@ -104,73 +105,119 @@ def mode_callback(option, opt, value, parser, **kwargs):


class ModifiedOptionParser(OptionParser):
"""
A OptionParser class that doesn't remove newlines on the epilog in order
to show usage examples https://stackoverflow.com/questions/1857346/
"""An OptionParser class that doesn't remove newlines on the epilog in order to show usage examples.
https://stackoverflow.com/questions/1857346/
See optparse.OptionParser for the original docstring
"""

def format_epilog(self, formatter):
if self.epilog == None:
if self.epilog is None:
return ""
return self.epilog


def parse_command_line():
""" Creates and returns the OptionParser, which parse the
command line arguments and displays help with --help. """

parser = ModifiedOptionParser(epilog='\nExample:\n'
' soundconverter -b [file] [dir] -r -m audio/x-vorbis -s .ogg -o [output dir] -Q 4\n')

parser.add_option('-c', '--check', dest='mode', action='callback',
callback=mode_callback, callback_kwargs={'mode':'check'},
help=_('Print which files cannot be read by gstreamer. '
"""Create and return the OptionParser, which parse the command line arguments and displays help with --help."""
parser = ModifiedOptionParser(
epilog='\nExample:\n'
' soundconverter -b [file] [dir] -r -m audio/x-vorbis -s .ogg -o [output dir] -Q 4\n'
)

parser.add_option(
'-c', '--check', dest='mode', action='callback',
callback=mode_callback, callback_kwargs={'mode': 'check'},
help=_(
'Print which files cannot be read by gstreamer. '
'Useful before converting. This will disable the GUI and '
'run in batch mode, from the command line.'))
parser.add_option('-b', '--batch', dest='mode', action='callback',
callback=mode_callback, callback_kwargs={'mode':'batch'},
help=_('Convert in batch mode, from the command line, '
'run in batch mode, from the command line.'
)
)
parser.add_option(
'-b', '--batch', dest='mode', action='callback',
callback=mode_callback, callback_kwargs={'mode': 'batch'},
help=_(
'Convert in batch mode, from the command line, '
'without a graphical user interface. You '
'can use this from, say, shell scripts.'))
parser.add_option('-t', '--tags', dest="mode", action='callback',
callback=mode_callback, callback_kwargs={'mode':'tags'},
help=_('Show tags for input files instead of converting '
'can use this from, say, shell scripts.'
)
)
parser.add_option(
'-t', '--tags', dest="mode", action='callback',
callback=mode_callback, callback_kwargs={'mode': 'tags'},
help=_(
'Show tags for input files instead of converting '
'them. This indicates command line batch mode '
'and disables the graphical user interface.'))
parser.add_option('-q', '--quiet', action="store_true", dest="quiet",
help=_("Be quiet. Don't write normal output, only errors."))
parser.add_option('-d', '--debug', action="store_true", dest="debug",
help=_('Displays additional debug information'))
parser.add_option('-j', '--jobs', action='store', type='int', dest='forced-jobs',
metavar='NUM', help=_('Force number of concurrent conversions.'))
'and disables the graphical user interface.'
)
)
parser.add_option(
'-q', '--quiet', action="store_true", dest="quiet",
help=_("Be quiet. Don't write normal output, only errors.")
)
parser.add_option(
'-d', '--debug', action="store_true", dest="debug",
help=_('Displays additional debug information')
)
parser.add_option(
'-j', '--jobs', action='store', type='int', dest='forced-jobs',
metavar='NUM', help=_('Force number of concurrent conversions.')
)

# batch mode settings
batch_option_group = OptionGroup(parser, 'Batch Mode Options',
'Those options will only have effect when the -b, -c or -t '
'option is provided')
batch_option_group.add_option('-m', '--mime-type', dest="cli-output-type",
help=_('Set the output MIME type. The default '
batch_option_group = OptionGroup(
parser, 'Batch Mode Options',
'Those options will only have effect when the -b, -c or -t '
'option is provided'
)
batch_option_group.add_option(
'-m', '--mime-type', dest="cli-output-type",
help=_(
'Set the output MIME type. The default '
'is %s. Note that you will probably want to set the output '
'suffix as well. Supported MIME types: %s') % (settings['cli-output-type'],
'suffix as well. Supported MIME types: %s'
) % (
settings['cli-output-type'],
'audio/x-m4a (AAC) audio/x-flac (FLAC) audio/mpeg (MP3) audio/x-vorbis (Vorbis)'
'audio/x-wav (WAV)'))
batch_option_group.add_option('-s', '--suffix', dest="cli-output-suffix",
help=_('Set the output filename suffix. '
'audio/x-wav (WAV)'
)
)
batch_option_group.add_option(
'-s', '--suffix', dest="cli-output-suffix",
help=_(
'Set the output filename suffix. '
'The default is %s. Note that the suffix does not '
'affect\n the output MIME type.') % settings['cli-output-suffix'])
batch_option_group.add_option('-r', '--recursive', action="store_true", dest="recursive",
help=_('Go recursively into subdirectories'))
batch_option_group.add_option('-i', '--ignore', action="store_true", dest="ignore-existing",
help=_('Ignore files for which the target already exists instead '
'of converting them again'))
batch_option_group.add_option('-o', '--output', action="store", dest="output-path",
help=_('Put converted files into a different directory while rebuilding '
'affect\n the output MIME type.'
) % settings['cli-output-suffix']
)
batch_option_group.add_option(
'-r', '--recursive', action="store_true", dest="recursive",
help=_('Go recursively into subdirectories')
)
batch_option_group.add_option(
'-i', '--ignore', action="store_true", dest="ignore-existing",
help=_(
'Ignore files for which the target already exists instead '
'of converting them again'
)
)
batch_option_group.add_option(
'-o', '--output', action="store", dest="output-path",
help=_(
'Put converted files into a different directory while rebuilding '
'the original directory structure. This includes the name of the original '
'directory.'))
batch_option_group.add_option('-Q', '--quality', action="store", type='int', dest="quality",
metavar='NUM', help=_('Quality of the converted output file. Between 0 '
'(lowest) and 5 (highest). Default is 3.'), default=3)
'directory.'
)
)
batch_option_group.add_option(
'-Q', '--quality', action="store", type='int', dest="quality",
metavar='NUM', help=_(
'Quality of the converted output file. Between 0 '
'(lowest) and 5 (highest). Default is 3.'
),
default=3
)

parser.add_option_group(batch_option_group)

Expand All @@ -180,6 +227,7 @@ def parse_command_line():

return parser


parser = parse_command_line()

options, files = parser.parse_args(args)
Expand All @@ -194,7 +242,7 @@ def parse_command_line():
settings['cli-output-type'] = check_mime_type(settings['cli-output-type'])

if not settings.get('quiet'):
print(( '%s %s' % (NAME, VERSION) ))
print(('%s %s' % (NAME, VERSION)))
if settings['forced-jobs']:
print(('Using %d thread(s)' % settings['forced-jobs']))

Expand All @@ -209,8 +257,3 @@ def parse_command_line():
CLI_Convert(files)
elif settings['mode'] == 'check':
CLI_Check(files)





3 changes: 0 additions & 3 deletions soundconverter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA



Loading

0 comments on commit c36f8b2

Please sign in to comment.