#
#  COPYRIGHT:   (c) Awasu Pty. Ltd. 2002-15 (all rights reserved).
#               Unauthorized use of this code is prohibited.
# 
#  LICENSE:     This software is provided 'as-is', without any express 
#               or implied warranty.
# 
#               In no event will the author be held liable for any damages 
#               arising from the use of this software.
#   
#               Permission is granted to anyone to use this software 
#               for any non-commercial purpose and to alter it and
#               redistribute it freely, subject to the following restrictions: 
# 
#               - The origin of this software must not be misrepresented; 
#                 you must not claim that you wrote the original software. 
#                 If you use this software, an acknowledgment is requested
#                 but not required. 
# 
#               - Altered source versions must be plainly marked as such, 
#                 and must not be misrepresented as being the original software. 
#                 Altered source is encouraged to be submitted back to 
#                 the original author so it can be shared with the community. 
#                 Please share your changes. 
# 
#               - This notice may not be removed or altered from any 
#                 source distribution. 
#

# ---------------------------------------------------------------------

# This is a sample Awasu plugin written in Python. It accepts several
# per-channel parameters and uses them to generate a dummy RSS feed.
# There is a walk-through of this code in the documentation.

import sys
import win32api 
import string

endl = "\n"

# ---------------------------------------------------------------------

# This is our plugin ID. It must correspond to the value configured in the .plugin file.
APP_PLUGIN_ID = "SamplePythonPlugin"

# This is the INI file section that Awasu will pass system parameters to us.
SYSTEM_PARAMETERS_SECTION_NAME = "System"

# This is the INI file section that Awasu will pass global plugin parameters to us.
PLUGIN_PARAMETERS_SECTION_NAME = "PluginParameters"

# This is the INI file section that Awasu will pass per-channel parameters to us.
CHANNEL_PARAMETERS_SECTION_NAME = "ChannelParameters"

# This is the name of our exceptions. Note that raising an exception will
# print an error message to stderr and thus be detected by Awasu as an error.
PluginError = "SamplePythonPlugin Error" 

# ---------------------------------------------------------------------

# This method will generate the plugin's main page i.e. the one that will
# be displayed when it is first opened from the Control Center.

def generateMainPage( configFilename ) :

    # get the app settings 
    appServerUrl = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "AppServerUrl" , "" , configFilename )
    if appServerUrl == "" :
        raise PluginError , "No app server URL was specified."
    appPluginServerUrl = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "AppPluginServerUrl" , "" , configFilename )
    if appPluginServerUrl == "" :
        raise PluginError , "No app plugin server URL was specified."

    # get the plugin settings 
    domainName = win32api.GetProfileVal( PLUGIN_PARAMETERS_SECTION_NAME , "DomainName" , "" , configFilename )
    if domainName == "" :
        raise PluginError , "No domain name was specified."

    # generate a URL that will let the user subscribe to a channel that 
    # we will generate. We embed some information in URL path and parameters
    # for demonstration purposes. These will get passed in to processRequest() 
    # when the channel is updated.
    apiToken = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "ApiToken" , "" , configFilename )
    url = appServerUrl + "/channels/subscribe"
    url = url + "?token=" + apiToken
    url = url + "&url=" + appPluginServerUrl + "/" + APP_PLUGIN_ID + "/foo/bar?p1=Hello%26p2=World"
    
    # generate the main page
    print "<html>"
    print "<body>"
    print "<h2>Sample Python Plugin</h2>"
    print "<p>This is the main page for the sample Python plugin."
    print "<p>The plugin settings are:"
    print "<ul>" 
    print "    <li>DomainName: " + domainName
    print "</ul>"
    print "<p>Click <a href='" + url + "'>here</a> to subscribe to a test channel."
    print "</body>"
    print "</html>"
    
# ---------------------------------------------------------------------


def processRequest( configFilename , pluginRequest , paramString ) :

    # NOTE: We get here when Awasu tries to access the URL we generated 
    # previously in generateMainPage() to get the latest channel feed.
    
    # get the plugin settings 
    domainName = win32api.GetProfileVal( PLUGIN_PARAMETERS_SECTION_NAME , "DomainName" , "" , configFilename )
    if domainName == "" :
        raise PluginError , "No domain name was specified."

    # get the channel settings 
    nItems = win32api.GetProfileVal( CHANNEL_PARAMETERS_SECTION_NAME , "nItems" , 15 , configFilename )
    itemTitleStem = win32api.GetProfileVal( CHANNEL_PARAMETERS_SECTION_NAME , "ItemTitleStem" , "Item Title" , configFilename )
    generateDescriptions = win32api.GetProfileVal( CHANNEL_PARAMETERS_SECTION_NAME , "GenerateDescriptions" , 1 , configFilename )

    # put together a channel description 
    description = "<p>This RSS feed was generated by the sample Python plugin." + endl
    description = description + "<p>The script was invoked with the following parameters:" + endl
    description = description + "<ul>" + endl
    description = description + "    <li>pluginRequest: " + pluginRequest + endl
    description = description + "    <li>paramString: " + paramString + endl
    description = description + "</ul>" + endl
    description = description + "<p>The plugin settings are:" + endl
    description = description + "<ul>" + endl
    description = description + "    <li>DomainName: " + domainName + endl
    description = description + "</ul>" + endl
    description = description + "<p>The channel settings are:" + endl
    description = description + "<ul>" + endl
    description = description + "    <li>nItems: " + str(nItems) + endl
    description = description + "    <li>ItemTitleStem: " + itemTitleStem + endl
    description = description + "    <li>GenerateDescriptions: " + str(generateDescriptions) + endl
    description = description + "</ul>" + endl
   
    # generate the RSS feed 
    print "<rss>" 
    print "<channel>" 
    print 
    print "<title>Sample Python Plugin</title>" 
    print "<link>" + domainName + "</link>"
    print "<description><![CDATA[" + description + "]]></description>" 
    print 
    for i in range(1,nItems+1) :
        print "<item>" 
        print "    <title>" + itemTitleStem + "-" + str(i) + "</title>" 
        print "    <link>" + domainName + "/item" + str(i) + ".html</link>" 
        if generateDescriptions :
            print "    <description>This is a dummy description for item " + str(i) + "</description>"
        print "</item>" 
        print 
    print "</channel>" 
    print "</rss>" 
    
# --- MAIN ------------------------------------------------------------

# get the name of the INI file 
configFilename = sys.argv[1] 

# process the requested command 
scriptCmd = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "Command" , "" , configFilename )
if scriptCmd == "GenerateMainPage" :
    generateMainPage( configFilename )
elif scriptCmd == "ProcessRequest" :
    pluginRequest = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "PluginRequest" , "" , configFilename )
    paramString = win32api.GetProfileVal( SYSTEM_PARAMETERS_SECTION_NAME , "ParamString" , "" , configFilename )
    processRequest( configFilename , pluginRequest , paramString )
else :
    raise PluginError , "Unknown script command: "+scriptCmd

