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

Commit

Permalink
first working version on /settings url
Browse files Browse the repository at this point in the history
  • Loading branch information
woutervanwijk authored and woutervanwijk committed Sep 18, 2014
1 parent 5c349d1 commit 25e9942
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 168 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ include .travis.yml
include LICENSE
include MANIFEST.in
include README.rst
include mopidy_settings/ext.conf
include mopidy_websettings/ext.conf

recursive-include tests *.py
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ Before starting Mopidy, you can add configuration for
Mopidy-WebSettings to your Mopidy configuration file::

[websettings]
musicbox=false
configfile=/etc/mopidy/mopidy.conf
enabled = true
musicbox = false
configfile = /etc/mopidy/mopidy.conf

Make sure the config file is writable by the user under which mopidy is running!
Make sure the config file is writable by the user under which mopidy is running! And make sure the http extension is working. Go to the ip or url of your mopidy computer and add /settings (e.g. http://musicbox.local/settings or http://192.168.1.10:6680/settings )


Project resources
Expand Down
61 changes: 0 additions & 61 deletions mopidy_settings/__init__.py

This file was deleted.

98 changes: 0 additions & 98 deletions mopidy_settings/old code.py

This file was deleted.

115 changes: 115 additions & 0 deletions mopidy_websettings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from __future__ import unicode_literals

import logging
import os

import tornado.web

from mopidy import config, ext

from configobj import ConfigObj, ConfigObjError
from validate import Validator
import jinja2

__version__ = '0.1.0'

# TODO: If you need to log, use loggers named after the current Python module
logger = logging.getLogger(__name__)

spec_file = os.path.join(os.path.dirname(__file__), 'settingsspec.ini')
template_file = os.path.join(os.path.dirname(__file__), 'index.html')
config_file = '/etc/mopidy/mopidy.conf'

#log_file = '/var/log/mopidy/mopidy.log'

password_mask = '******'

class Extension(ext.Extension):
dist_name = 'Mopidy-WebSettings'
ext_name = 'websettings'
version = __version__

def get_default_config(self):
conf_file = os.path.join(os.path.dirname(__file__), 'ext.conf')
return config.read(conf_file)

def get_config_schema(self):
schema = super(Extension, self).get_config_schema()
schema['musicbox'] = config.String()
schema['config_file'] = config.String()
return schema

def setup(self, registry):
registry.add('http:app', {
#'name': self.ext_name,
'name': 'settings',
'factory': websettings_app_factory,
})

class WebSettingsRequestHandler(tornado.web.RequestHandler):

def initialize(self, core):
self.core = core

def get(self):
templateLoader = jinja2.FileSystemLoader( searchpath = "/" )
templateEnv = jinja2.Environment( loader=templateLoader )
template = templateEnv.get_template(template_file)
error = ''
#read config file
try:
iniconfig = ConfigObj(config_file, configspec=spec_file, file_error=True, encoding='utf8')
except (ConfigObjError, IOError), e:
error = 'Could not load ini file! %s %s %s', e, ConfigObjError, IOError
#read values of valid items (in the spec-file)
validItems = ConfigObj(spec_file, encoding='utf8')
templateVars = {
"error": error
}
#iterate over the valid items to get them into the template
for item in validItems:
for subitem in validItems[item]:
itemName = item + '__' + subitem
try:
configValue = iniconfig[item][subitem]
#compare last 8 caracters of subitemname
if subitem[-8:] == 'password' and configValue != '':
configValue = password_mask
templateVars[itemName] = configValue
except:
pass
self.write(template.render ( templateVars ) )

def post(self):
error = ''
try:
iniconfig = ConfigObj(config_file, configspec=spec_file, file_error=True, encoding='utf8')
except (ConfigObjError, IOError), e:
error = 'Could not load ini file!'
validItems = ConfigObj(spec_file, encoding='utf8')
templateVars = {
"error": error
}
#iterate over the items, so that only valid items are processed
for item in validItems:
for subitem in validItems[item]:
itemName = item + '__' + subitem
argumentItem = self.get_argument(itemName)
if argumentItem:
#don't edit config value if password mask
if subitem[-8:] == 'password':
if argumentItem == password_mask or argumentItem == '':
continue
iniconfig[item][subitem] = argumentItem
iniconfig.write()
logger.info('Rebooting system')
# os.system("sudo shutdown -r now")
# os.system("shutdown -r now")
# os.system("/opt/musicbox/startup.sh")
self.write("<html><body><h1>Settings Saved!</h1><script>toast('Applying changes (reboot)...', 5000);setTimeout(function(){window.location.assign('/');}, 10000);</script><a href='/'>Back</a></body></html>")

def websettings_app_factory(config, core):
config_file = config.get('websettings', 'config_file')
return [
('/', WebSettingsRequestHandler, {'core': core})
]
2 changes: 2 additions & 0 deletions mopidy_websettings/enabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var settingsEnabled = true;
var shutdownEnabled = true;
2 changes: 1 addition & 1 deletion mopidy_settings/ext.conf → mopidy_websettings/ext.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[websettings]
enabled = true
musicbox = false
configfile=/etc/mopidy/mopidy.conf
config_file=/etc/mopidy/mopidy.conf
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
application-import-names = mopidy_settings,tests
application-import-names = mopidy_websettings,tests
exclude = .git,.tox

[wheel]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_version(filename):

setup(
name='Mopidy-WebSettings',
version=get_version('mopidy_settings/__init__.py'),
version=get_version('mopidy_websettings/__init__.py'),
url='https://github.com/woutervanwijk/mopidy-websettings',
license='Apache License, Version 2.0',
author='Wouter van Wijk',
Expand All @@ -37,7 +37,7 @@ def get_version(filename):
],
entry_points={
'mopidy.ext': [
'settings = mopidy_settings:Extension',
'websettings = mopidy_websettings:Extension',
],
},
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deps =
nose
mopidy==dev
install_command = pip install --allow-unverified=mopidy --pre {opts} {packages}
commands = nosetests -v --with-xunit --xunit-file=xunit-{envname}.xml --with-coverage --cover-package=mopidy_settings
commands = nosetests -v --with-xunit --xunit-file=xunit-{envname}.xml --with-coverage --cover-package=mopidy_websettings

[testenv:flake8]
deps =
Expand Down

0 comments on commit 25e9942

Please sign in to comment.