<commit_before>#!/usr/bin/env python

# Static Routes http://stackoverflow.com/questions/10486224/bottle-static-files

from bottle import route, static_file, debug, run, get, view, redirect
from bottle import post, request, response
import os, inspect, json

#enable bottle debug
debug(True)

# WebApp route path
routePath = '/pyWebMOC'
# get directory of WebApp (pyWebMOC.py's dir)
rootPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

@route(routePath)
def rootHome():
    return redirect(routePath+'/index.html')

@view(routePath + '/index.html')
def home():
    return static_file("index.html", root=rootPath)

@route(routePath + '/<filename:re:.*\.html>')
def html_file(filename):
    return static_file(filename, root=rootPath)

@get(routePath + '/assets/<filepath:path>')
def assets_file(filepath):
    return static_file(filepath, root=rootPath+'/assets')

#http://gotofritz.net/blog/weekly-challenge/restful-python-api-bottle/
"""
POST action
curl -i -X GET http://localhost:8080/pyWebMOC/jsonGET

GET action
curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"OopsMonk"}' http://localhost:8080/pyWebMOC/jsonPOST
"""
@get(routePath + '/json')
def testJsonGET():
    print dict(request.headers)
    return {"id":1,"name":"Sam"}

@post(routePath + '/json')
def testJsonPost():
    #print dict(request.headers)
    data = request.json
    print data
    if data == None:
        return json.dumps({'Status':"Failed!"})
    else:
        return json.dumps({'Status':"Success!"})

@get(routePath + '/gettest')
def gettest():
    return "get test return!!!\n"

run(host='localhost', port=8080, reloader=True)
<commit_msg>add function list.<commit_after>#!/usr/bin/env python

# Static Routes http://stackoverflow.com/questions/10486224/bottle-static-files

from bottle import route, static_file, debug, run, get, view, redirect
from bottle import post, request, response
import os, inspect, json
import moc

#enable bottle debug
debug(True)

# WebApp route path
routePath = '/pyWebMOC'
# get directory of WebApp (pyWebMOC.py's dir)
rootPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

@route(routePath)
def rootHome():
    return redirect(routePath+'/index.html')

@view(routePath + '/index.html')
def home():
    return static_file("index.html", root=rootPath)

@route(routePath + '/<filename:re:.*\.html>')
def html_file(filename):
    return static_file(filename, root=rootPath)

@get(routePath + '/assets/<filepath:path>')
def assets_file(filepath):
    return static_file(filepath, root=rootPath+'/assets')

"""
Function list
* Player control GET
    * return moc server state (play/stop/pause/not running)

* Player control POST
    * Previous
    * Play / Resume
    * Next
    * Pause
    * Stop
    * volume

* Player info
    * Song name
    * current time
    * duration
    * 
"""

#control GET API return 
@get(routePath + '/ctl')
def MOCControlGET():
    """
    -1: moc server not running 
    0: Stopping
    1: Pause
    2: Playing
    """
    st = moc.get_state()
    print "Get moc server status : %d" % st
    return json.dumps({'Status':st})

#handle control post action
def CtlHandle(data):
    """
    request:
    {'do':action}
    action:
    0: Prev 
    1: Play/Pause
    2: Next
    3: Stop
    4: Volume
    5: Add playlist
    6: append playlist
    7: clear playlist
    8:
    response:
    {'ack': error}
    error:
    0: Success
    not zero: Failed.
    """

    return json.dumps({'ack':0})

#control POST API
@post(routePath + '/ctl')
def MOCControlPOST():
    data = request.json
    if data == None:
        return json.dumps({'ack': -1})

    print "Get cmd : %s" % data 
    return CtlHandle(data)

#http://gotofritz.net/blog/weekly-challenge/restful-python-api-bottle/
"""
POST action
curl -i -X GET http://localhost:8080/pyWebMOC/jsonGET

GET action
curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"OopsMonk"}' http://localhost:8080/pyWebMOC/jsonPOST
"""
@get(routePath + '/json')
def testJsonGET():
    print dict(request.headers)
    return {"id":1,"name":"Sam"}

@post(routePath + '/json')
def testJsonPost():
    #print dict(request.headers)
    data = request.json
    print data
    if data == None:
        return json.dumps({'Status':"Failed!"})
    else:
        return json.dumps({'Status':"Success!"})

@get(routePath + '/gettest')
def gettest():
    return "get test return!!!\n"

run(host='localhost', port=8080, reloader=True)

{'fraction_non_alphanumeric': 0.15554072096128171, 'fraction_numerical': 0.01335113484646195, 'mean_word_length': 3.822961373390558, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5027547', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1527, 'n_words': 372}