Skip to content

Commit

Permalink
Merge pull request #1 from adamjakab/devel
Browse files Browse the repository at this point in the history
devel
  • Loading branch information
adamjakab committed Mar 28, 2020
2 parents 0389eae + 7079654 commit 84c1a6f
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://travis-ci.org/adamjakab/BeetsPluginAutofix.svg?branch=master)](https://travis-ci.org/adamjakab/BeetsPluginAutofix)
[![Coverage Status](https://coveralls.io/repos/github/adamjakab/BeetsPluginAutofix/badge.svg?branch=master)](https://coveralls.io/github/adamjakab/BeetsPluginAutofix?branch=master)
[![PyPi](https://img.shields.io/pypi/v/beets-autofix.svg)](https://pypi.org/project/beets-autofix/)

[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.txt)

# Autofix (Beets Plugin)

Expand Down
20 changes: 20 additions & 0 deletions beetsplug/autofix/about.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright: Copyright (c) 2020., Adam Jakab
#
# Author: Adam Jakab <adam at jakab dot pro>
# Created: 3/27/20, 3:52 PM
# License: See LICENSE.txt

__author__ = u'Adam Jakab'
__email__ = u'adam@jakab.pro'
__copyright__ = u'Copyright (c) 2020, {} <{}>'.format(__author__, __email__)
__license__ = u'License :: OSI Approved :: MIT License'
__version__ = u'0.0.2'
__status__ = u'Kickstarted'

__PACKAGE_TITLE__ = u'Autofix'
__PACKAGE_NAME__ = u'beets-autofix'
__PACKAGE_DESCRIPTION__ = u'A beets plugin to execute repetitive tasks in one go.',
__PACKAGE_URL__ = u'https://github.com/adamjakab/BeetsPluginAutofix'
__PLUGIN_NAME__ = u'autofix'
__PLUGIN_ALIAS__ = None
__PLUGIN_SHORT_DESCRIPTION__ = u'execute repetitive tasks in one go'
26 changes: 14 additions & 12 deletions beetsplug/autofix/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
from beetsplug.autofix import common
from beetsplug.autofix.task import Task

# The plugin
__PLUGIN_NAME__ = u'autofix'
__PLUGIN_SHORT_DESCRIPTION__ = u'fix your library with little effort'


class AutofixCommand(Subcommand):
config: Subview = None
Expand All @@ -42,13 +38,15 @@ def __init__(self, config):
cfg = self.config.flatten()
self.cfg_max_exec_time = cfg.get("max_exec_time")

self.parser = OptionParser(usage='beet autofix [options] [QUERY...]')
self.parser = OptionParser(usage='beet {plg} [options] [QUERY...]'.format(
plg=common.plg_ns['__PLUGIN_NAME__']
))

self.parser.add_option(
'-m', '--max_exec_time',
action='store', dest='max_exec_time', type='int',
default=self.cfg_max_exec_time,
help=u'[default: {}] the number of seconds the execution can run'.format(
help=u'[default: {}] interrupt execution after this the number of seconds'.format(
self.cfg_max_exec_time)
)

Expand All @@ -60,8 +58,9 @@ def __init__(self, config):

super(AutofixCommand, self).__init__(
parser=self.parser,
name=__PLUGIN_NAME__,
help=__PLUGIN_SHORT_DESCRIPTION__
name=common.plg_ns['__PLUGIN_NAME__'],
aliases=[common.plg_ns['__PLUGIN_ALIAS__']] if common.plg_ns['__PLUGIN_ALIAS__'] else [],
help=common.plg_ns['__PLUGIN_SHORT_DESCRIPTION__']
)

def func(self, lib: Library, options, arguments):
Expand Down Expand Up @@ -208,9 +207,12 @@ def check_timer(self):
raise TimeoutError("Time up!")

def show_version_information(self):
from beetsplug.autofix.version import __version__
self._say("Plot(beets-{}) plugin for Beets: v{}".format(__PLUGIN_NAME__, __version__))
self._say("{pt}({pn}) plugin for Beets: v{ver}".format(
pt=common.plg_ns['__PACKAGE_TITLE__'],
pn=common.plg_ns['__PACKAGE_NAME__'],
ver=common.plg_ns['__version__']
), log_only=False)

@staticmethod
def _say(msg, log_only=False):
common.say(msg, log_only)
def _say(msg, log_only=True, is_error=False):
common.say(msg, log_only, is_error)
19 changes: 13 additions & 6 deletions beetsplug/autofix/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
# License: See LICENSE.txt

import logging
import os

from beets import config

__logger__ = logging.getLogger('beets.autofix')
# Get values as: plg_ns['__PLUGIN_NAME__']
plg_ns = {}
about_path = os.path.join(os.path.dirname(__file__), u'about.py')
with open(about_path) as about_file:
exec(about_file.read(), plg_ns)

__logger__ = logging.getLogger('beets.{plg}'.format(plg=plg_ns['__PLUGIN_NAME__']))


def is_plugin_enabled(plugin_name):
Expand All @@ -23,8 +30,8 @@ def get_plugin_config(plugin_name):
return cfg


def say(msg, log_only=False):
if log_only:
__logger__.debug(msg)
else:
__logger__.info(msg)
def say(msg, log_only=True, is_error=False):
_level = logging.DEBUG
_level = _level if log_only else logging.INFO
_level = _level if not is_error else logging.ERROR
__logger__.log(level=_level, msg=msg)
4 changes: 3 additions & 1 deletion beetsplug/autofix/config_default.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
max_exec_time: 300
max_exec_time: 0
tasks:
missing_file_checker:
enabled: no
year_fixer:
enabled: no
audio_conversion:
enabled: no
tag_cleaner:
Expand Down
2 changes: 0 additions & 2 deletions beetsplug/autofix/task/genre_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Created: 24/03/2020, 15.57
# License: See LICENSE.txt

from beets.library import Item

from beetsplug.autofix.task import Task

from beetsplug.autofix import common
Expand Down
54 changes: 54 additions & 0 deletions beetsplug/autofix/task/year_fixer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright: Copyright (c) 2020., Adam Jakab
#
# Author: Adam Jakab <adam at jakab dot pro>
# Created: 3/26/20, 8:14 PM
# License: See LICENSE.txt

from beetsplug.autofix.task import Task

from beetsplug.autofix import common
from beetsplug.yearfixer import YearFixerPlugin, YearFixerCommand


class YearFixerTask(Task):
plugin = None
plugin_config = None
year_fixed = False

def __init__(self):
super(YearFixerTask, self).__init__()
assert common.is_plugin_enabled("yearfixer"), "The 'yearfixer' plugin is not enabled!"
self.plugin = YearFixerPlugin()
self.plugin_config = common.get_plugin_config("yearfixer")

def setup(self, config, item):
self.config = config
self.item = item
self.year_fixed = False

def run(self):
if not self._item_needs_processing():
return

cmd: YearFixerCommand = self.plugin.commands()[0]

orig_year = self.item.get("year")
orig_original_year = self.item.get("original_year")
self._say("Calling yearfixer plugin for item: {}".format(self.item), log_only=True)
cmd.process_item(self.item)
if orig_year != self.item.get("year") or orig_original_year != self.item.get("original_year"):
self.year_fixed = True

def _item_needs_processing(self):
answer = False

if not self.item.get("year") or not self.item.get("original_year"):
answer = True

return answer

def needs_item_store(self):
return self.year_fixed

def needs_item_write(self):
return self.year_fixed
7 changes: 0 additions & 7 deletions beetsplug/autofix/version.py

This file was deleted.

20 changes: 10 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
# The text of the README file
README = (HERE / "README.md").read_text()

main_ns = {}
ver_path = convert_path('beetsplug/autofix/version.py')
with open(ver_path) as ver_file:
exec(ver_file.read(), main_ns)
plg_ns = {}
about_path = convert_path('beetsplug/autofix/about.py')
with open(about_path) as about_file:
exec(about_file.read(), plg_ns)

# Setup
setup(
name='beets-autofix',
version=main_ns['__version__'],
description='A beets plugin autofix',
author='Adam Jakab',
author_email='adam@jakab.pro',
url='https://github.com/adamjakab/BeetsPluginAutofix',
name=plg_ns['__PACKAGE_NAME__'],
version=plg_ns['__version__'],
description=plg_ns['__PACKAGE_DESCRIPTION__'],
author=plg_ns['__author__'],
author_email=plg_ns['__email__'],
url=plg_ns['__PACKAGE_URL__'],
license='MIT',
long_description=README,
long_description_content_type='text/markdown',
Expand Down
32 changes: 0 additions & 32 deletions test/00_completion_test.py

This file was deleted.

58 changes: 58 additions & 0 deletions test/00_sanity_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright: Copyright (c) 2020., Adam Jakab
#
# Author: Adam Jakab <adam at jakab dot pro>
# Created: 3/26/20, 4:50 PM
# License: See LICENSE.txt


from beetsplug.autofix import about
from test.helper import TestHelper, Assertions, \
PLUGIN_NAME, PLUGIN_SHORT_DESCRIPTION, PACKAGE_NAME, PACKAGE_TITLE, PLUGIN_VERSION, \
capture_log

plg_log_ns = 'beets.{}'.format(PLUGIN_NAME)


class CompletionTest(TestHelper, Assertions):
"""Test invocation of the plugin and basic package health.
"""

def test_about_descriptor_file(self):
self.assertTrue(hasattr(about, "__author__"))
self.assertTrue(hasattr(about, "__email__"))
self.assertTrue(hasattr(about, "__copyright__"))
self.assertTrue(hasattr(about, "__license__"))
self.assertTrue(hasattr(about, "__version__"))
self.assertTrue(hasattr(about, "__status__"))
self.assertTrue(hasattr(about, "__PACKAGE_TITLE__"))
self.assertTrue(hasattr(about, "__PACKAGE_NAME__"))
self.assertTrue(hasattr(about, "__PACKAGE_DESCRIPTION__"))
self.assertTrue(hasattr(about, "__PACKAGE_URL__"))
self.assertTrue(hasattr(about, "__PLUGIN_NAME__"))
self.assertTrue(hasattr(about, "__PLUGIN_ALIAS__"))
self.assertTrue(hasattr(about, "__PLUGIN_SHORT_DESCRIPTION__"))

def test_application(self):
output = self.runcli()
self.assertIn(PLUGIN_NAME, output)
self.assertIn(PLUGIN_SHORT_DESCRIPTION, output)

def test_application_plugin_list(self):
output = self.runcli("version")
self.assertIn("plugins: {0}".format(PLUGIN_NAME), output)

def test_run_plugin(self):
with capture_log(plg_log_ns) as logs:
self.runcli(PLUGIN_NAME)
self.assertIn("autofix: Done.", "\n".join(logs))

def test_plugin_version(self):
with capture_log(plg_log_ns) as logs:
self.runcli(PLUGIN_NAME, "--version")

versioninfo = "{pt}({pn}) plugin for Beets: v{ver}".format(
pt=PACKAGE_TITLE,
pn=PACKAGE_NAME,
ver=PLUGIN_VERSION
)
self.assertIn(versioninfo, "\n".join(logs))
5 changes: 5 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright: Copyright (c) 2020., Adam Jakab
#
# Author: Adam Jakab <adam at jakab dot pro>
# Created: 3/28/20, 11:21 AM
# License: See LICENSE.txt
9 changes: 6 additions & 3 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
from six import StringIO

from beetsplug import autofix
from beetsplug.autofix import common

logging.getLogger('beets').propagate = True

# Values
PLUGIN_NAME = u'autofix'
PLUGIN_SHORT_NAME = u''
PLUGIN_SHORT_DESCRIPTION = u'fix your library with little effort'
PLUGIN_NAME = common.plg_ns['__PLUGIN_NAME__']
PLUGIN_SHORT_DESCRIPTION = common.plg_ns['__PLUGIN_SHORT_DESCRIPTION__']
PLUGIN_VERSION = common.plg_ns['__version__']
PACKAGE_NAME = common.plg_ns['__PACKAGE_NAME__']
PACKAGE_TITLE = common.plg_ns['__PACKAGE_TITLE__']


class LogCapture(logging.Handler):
Expand Down

0 comments on commit 84c1a6f

Please sign in to comment.