Skip to content

Commit

Permalink
Don't require config for some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Newton committed Jan 20, 2018
1 parent efd2dec commit ef3680a
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 9 deletions.
35 changes: 29 additions & 6 deletions unsonic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,33 @@
VERSION = "unsonic (%s, protocol: %s, subsonic protocol: %s) mishmash (%s)" % \
(version.VERSION, version.UNSONIC_PROTOCOL_VERSION,
version.PROTOCOL_VERSION, __about__.__version__)
APP = None


class Unsonic(MishMash):

def __init__(self):
super().__init__(progname="unsonic")
self.cfg_found = False
self._orig_main_func = self._main_func
self._main_func = self.mainWrapper


def mainWrapper(self, args):
# Handle mishmash commands which assume a default config
# Unsonic commands are handled in the unsonic base command class
need_cfg = False
if (hasattr(self.args, "command_func") and
hasattr(self.args.command_func, "__self__")):
need_cfg = ("mishmash.commands" in
self.args.command_func.__self__.__module__)
if not self.cfg_found and need_cfg:
print("Could not find a standardly located config. "
"You must specify the config file with -c argument, "
"example: unsonic -c /etc/unsonic.ini ...")
sys.exit(-1)

return self._orig_main_func(args)


# hack in the unsonic version
Expand All @@ -31,24 +52,26 @@ def _addArguments(self, parser):


def buildApp():
global APP
if "web" in Command._all_commands:
del Command._all_commands["web"]
if mishmash.commands.web.Web.name in Command._all_commands:
del Command._all_commands[mishmash.commands.web.Web.name]
return Unsonic()
APP = Unsonic()
return APP


def adjustCmdline(parser):
path = config.findConfig(parser)
if path is False:
print("Could not find a standardly located config. "
"You must specify the config file with -c argument, "
"example: unsonic -c /etc/unsonic.ini ...")
sys.exit(-1)
APP.cfg_found = False
elif path is not True:
# Append the found config file
sys.argv = sys.argv[:1] + ["-c", path] + sys.argv[1:]
# else its already supplied, just carry on
APP.cfg_found = True
else:
# its already supplied, just carry on
APP.cfg_found = True


def run(args=None):
Expand Down
14 changes: 13 additions & 1 deletion unsonic/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# flake8: noqa: F401

import sys

from nicfit.command import register
from mishmash import core

from unsonic import __main__


class Command(core.Command):
CFG_NEEDED = True

def _run(self, args=None):
initAlembic(self.config.get("mishmash", "sqlalchemy.url"))
if self.CFG_NEEDED:
if not __main__.APP.cfg_found:
print("Could not find a standardly located config. "
"You must specify the config file with -c argument, "
"example: unsonic -c /etc/unsonic.ini ...")
sys.exit(-1)
initAlembic(self.config.get("mishmash", "sqlalchemy.url"))


from ..models import initAlembic
Expand Down
3 changes: 2 additions & 1 deletion unsonic/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Install(Command):
NAME = "install"
HELP = "Install the Unsonic service."
DESC = "Install the Unsonic service. Requires root permissions."

CFG_NEEDED = False

def _initArgParser(self, parser):
parser.add_argument("-u", "--user", default="unsonic",
Expand All @@ -28,6 +28,7 @@ def _initArgParser(self, parser):


def run(self, args, config):
super()._run()
cmd = "/bin/bash %s %s %s %s %s" % (
os.path.join(unsonic.INSTALL, "etc/install.sh"),
unsonic.CMD, unsonic.INSTALL, args.user, args.rundir)
Expand Down
2 changes: 2 additions & 0 deletions unsonic/commands/man.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Man(Command):
HELP = "Show the manpages for Unsonic."
DESC = HELP
INSTALL = os.path.join(unsonic.INSTALL, "docs/man")
CFG_NEEDED = False


def _initArgParser(self, parser):
Expand All @@ -21,6 +22,7 @@ def _initArgParser(self, parser):


def run(self, args, config):
super()._run()
if args.list:
files = glob.glob(os.path.join(self.INSTALL, "*.1"))
files = [".".join(f.split(".")[:-1]) for f in files]
Expand Down
1 change: 1 addition & 0 deletions unsonic/commands/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def _initArgParser(self, parser):


def run(self, args, config):
super()._run()
pargs = args.pserve_args

if not config.filename:
Expand Down
132 changes: 132 additions & 0 deletions unsonic/etc/example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
###
# Unsonic configuration
###

[unsonic]
name = Unsonic!

# Location of a web client, like Jamstash
ui = %(install)s/../Jamstash/dist


###
# MishMash configuration
###

[mishmash]
various_artists_name = Various Artists
sqlalchemy.url = sqlite:///%(here)s/../../venv/unsonic.sqlite
sqlalchemy.convert_unicode = true
sqlalchemy.encoding = utf8

[library:Music]
paths = %(here)s/../../test/music
sync = true



###
# The rest of this file is pyramid/pylons/logging configuration and in most
# cases do not need to be touched
###


###
# main app configuration
###

[app:main]
use = egg:unsonic

pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = pyramid_tm

# SQLAlchemy engine settings for pyramid are copied from mishmash section


###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
listen = 0.0.0.0:6543 [::]:6543

###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###

[loggers]
keys = root, unsonic, sqlalchemy, mishmash, wsgi, alembic

[handlers]
# Add the appropriate handler for your setup
# filelog, systemd, syslog
keys = syslog

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = syslog

[logger_unsonic]
level = NOTSET
handlers =
qualname = unsonic

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither. (Recommended for production systems.)

[logger_mishmash]
level = NOTSET
handlers =
qualname = mishmash

[logger_wsgi]
level = NOTSET
handlers =
qualname = wsgi

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[handler_syslog]
class = logging.handlers.SysLogHandler
args = ()
level = NOTSET
formatter = generic

[handler_systemd]
class = systemd.journal.JournalHandler
args = (INFO, SYSLOG_IDENTIFIER='unsonic')
level = NOTSET
formatter = generic

[handler_filelog]
class = logging.handlers.RotatingFileHandler
args = ('/var/log/unsonic/unsonic.log','a', 10485760, 10)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
11 changes: 11 additions & 0 deletions unsonic/etc/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ DIST="$2"
USER="$3"
HOME="$4"

echo "*******************************************************"
echo " This will install Unsonic as a service."
read -p " Do you wish to continue? (Y/y) " CONTINUE

if [[ "$CONTINUE" != "y" && "$CONTINUE" != "Y" ]]; then
echo "Exiting..."
exit -1
fi

echo

grep -sq $USER /etc/passwd
if [ $? != 0 ]; then
echo "** Adding user $USER as a service account"
Expand Down
2 changes: 1 addition & 1 deletion unsonic/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = "0.1"
VERSION = "0.1.1"
UNSONIC_PROTOCOL_VERSION = "0.1"
PROTOCOL_VERSION = "1.15.0"

0 comments on commit ef3680a

Please sign in to comment.