How to make your own module - Hellowlol/HTPC-Manager GitHub Wiki
This intended to be a quick guide on how to make your own module. Its a work in process.
Make a new file in your favorite editor and paste this in
#
Is a comment in python. Thus you can write what ever you want on that line
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import htpc # always imported as db access is here and other stuff
import cherrypy # webserver/framework
import logging
from cherrypy.lib.auth2 import require # decorator protecting htpc manager
from htpc.helpers import serve_template, fix_basepath, striphttp # helper functions
import requests # If you need to get something from the interwebz
# Replace Mynewmodule with your modulename
class Mynewmodule(object):
def __init__(self):
self.logger = logging.getLogger('modules.mynewmodule')
htpc.MODULES.append({
# Name is used for the settings list
'name': 'Mynewmodule',
# Adds a button next to save/cancel, if that one is pressed is calls function
'test': htpc.WEBDIR + "mynewmodule/test",
# is used for the menu bar
'id': 'mynewmodule',
### Some field explainations ###
#type: referes to html input type ex text, option, password or bool (True/False)
#label: The "name" that you see to the left of input field
#name: is the key that the db use to find stuff.
#desc: Add a description about that option
#fbutton: Adds a button(link) next to the input example xxx
'fields': [
{'type': 'bool', 'label': 'Enable', 'name': 'mynewmodule_enable'},
{'type': 'text', 'label': 'Menu name', 'name': 'mynewmodule_name'},
{'type': 'text', 'label': 'Host', 'name': 'mynewmodule_host'},
{'type': 'text', 'label': 'port', 'name': 'mynewmodule_port'},
{'type': 'text', 'label': 'Apikey', 'name': 'mynewmodule_apikey'},
{'type': 'bool', 'label': 'Use SSL', 'name': 'mynewmodule_ssl'}
]
})
In Htpc.py, find the function load_module, after the last line of the function add:
from modules.mynewmodule import Mynewmodule
htpc.ROOT.mynewmodule = Mynewmodule()
Great! You have just made a settings page to your module! Lets check it out.
It should look something like this:
Next step is to make the module page "work". Add this two line below the rest of you code in mynewmodule.py. Remember the indents!
@cherrypy.expose() # exposes the module to the handler. (So /mynewmodule return something)
@require() # Adds user control so this is protected
def index(self):
return "HELLO! :)"
It should look like this: Thats isnt very pretty
Lets make it look like HTPC-Manager! Open a new tab in your editor, paste the code below. Save it as mynewmodule.html and while you at it in /interfaces/default/html folder, make a empty js and css file in the js and css folder. Use the same name, just remember to replace the extention
<%inherit file="base.html"/>
<% settings = self.attr.settings %>
<div class="container">
<div class="content maincontent">
<h1 class="page-header page-title">
<!-- The line below grabs the value of mynewmodule_host from the db and sets it as href "Awesome" will be use if that value does not exist
-->
<a href="http://${settings.get('mynewmodule_host')}" target="_blank">${settings.get('mynewmodule_name', 'Awesome')}</a>
</h1>
</div>
</div>
In newmodule.py, we need to replace the last line in the index function with this:
# Fetches the html page your created in /interfaces/default/html, the js file in /interface/default/js and css in /interface/default/css (more on that later)
return serve_template('mynewmodule.html', scriptname='mynewmodule')
Does it look like this? If not make sure you have pasted it correctly. (Your code code should look like this: link to gist/commit)
Usually the module fetch something from a web service. So let make sure the test function works. Add:
@cherrypy.expose()
@require()
def test(self):
# Pressing the test button on the settings page calls this function
# htpc.settings.get('keyname', 'default_value') is used for database access
ssl = 's' if htpc.settings.get('mynewmodule_ssl', '') else ''
url = 'http%s://%s:%s' % (ssl, htpc.settings.get('mynewmodule_host'), htpc.settings.get('mynewmodule_host'))
result = requests.get(url)
return result.content
Go to the settings page and enter the host and port. Press the test button, if it was a success it should look like this: