How to run arbitrary scripts within the web2py environment?

389 views
Skip to first unread message

Spencer Underwood

unread,
May 7, 2014, 4:38:52 PM5/7/14
to web...@googlegroups.com

Greetings,

This might be a really dumb or simple question or might better suited to ask in the IRC channel, but I can't seem to find an answer to this anywhere on google. I've been trying to test various bits of code to see how it runs on the web2py framework but without tying it to a specific view. Does anyone know how to run a script / function in a module based off of events such as a button press or a input command to the web shell?

I made a simple function in a module which, provided a valid API Key, Verification Code and Character ID, parses and stores a list of mail messages pulled from the MailMessage function made available by the EVE Online API Server. There is also a function which will do the same as before but for every valid API Key, Verification Code and Character ID stored in the database all at once. I'm working on a means to properly handle the other API calls available and would like to run those on a semi-regular basis, but haven't currently achieved a working result yet. I intended to invoke these method when a user logs into the site, as well as roughly once every twelve hours, to keep a relatively up-to-date database filled with mails for EVE Online. 

All relevant ID columns for things like Character ID, Message ID, etc... from the API server are unique so a quick IS_NOT_IN_DB and a unique key constraint should stop any and all duplicates. I guess later I could log attempts to enter a duplicate value, but right now it's not a concern. 

From my understanding, any code in the Model or Controller folders will be executed each time the page is viewed. This works well for a lot of things, but doesn't seem optimal for relatively slow net and database code. The Scheduler seems like it could be useful for this, but that doesn't seem like it should be placed in a model or a controller file.

Here is what I've used so far to read messages and pull them to a database, it's pretty crude but I just wanted a working example to build off of later.

the EVE API class is a python library (found here) which does the job of parsing the XML data returned from the API server into an iterable data structure. 

# Put your userID and apiKey (full access) here before running this script.
YOUR_KEYID = [redacted]
YOUR_VCODE = "[redacted]"
YOUR_CHARACTERID=[redacted]

from os.path import join, exists
from httplib import HTTPException
from gluon.sql import DAL, Field
from gluon.validators import *

import logging

import eveapi
import datetime

api = eveapi.EVEAPIConnection()
db = DAL('sqlite://test3.sqlite')
#----------------------------------------------------------------------------
message = db.define_table('message', 
Field('messageID','integer', unique=True),
Field('header','string'),Field('body','string'), 
Field('dateTimeSent','datetime'))

#    /account/Characters.xml.aspx
getMailHeader = api("/char/MailMessages", keyID=YOUR_KEYID, vCode=YOUR_VCODE, characterID=YOUR_CHARACTERID)

listOfMessageID = []
listOfHeaders = []

result = getMailHeader.messages.SortedBy("sentDate")
for s in result:
try:
db.message.insert(messageID=s.get("messageID"), header=s.get("title"), dateTimeSent = datetime.datetime.utcfromtimestamp( s.get("sentDate") ) , body="")
listOfMessageID.append(s.get("messageID"))
except Exception as e:
logging.warning("Message with ID of %s already exists in database", s.get("messageID"))

if len(listOfMessageID) > 0:
getMailBody = api("/char/MailBodies", keyID=YOUR_KEYID, vCode=YOUR_VCODE, characterID=YOUR_CHARACTERID, ids = ','.join([ str(i) for i in listOfMessageID]) )
MailMessages = getMailBody.messages.SortedBy("messageID")

for s in MailMessages:
mailID = s.get("messageID")
mailBody = s.get("data").encode("utf-8")
db( db[message].messageID==mailID).update(**{"body":mailBody})
#print s.__str__().encode("utf-8")
#print

for row in db(message).select():
print row
print
db.commit()

Thanks,
Spencer Underwood

Niphlod

unread,
May 7, 2014, 6:01:38 PM5/7/14
to web...@googlegroups.com
ok. now you just have to explain what do you mean by "running in web2py environment".
If you don't want to use models or controllers (and surely do not want views), why do you need an MVC (Model-View-Controller) framework environment to work in?

Anthony

unread,
May 7, 2014, 6:27:50 PM5/7/14
to web...@googlegroups.com
Why can't you use the scheduler for this? You can put your code in a function in a module and then import it into a model file to make it available to the scheduler.

Anthony

Spencer Underwood

unread,
May 8, 2014, 3:41:10 PM5/8/14
to web...@googlegroups.com
I'm not very good at putting into words what I'm thinking, so I do apologize for not being very clear.

I was hoping there might be a way to do "one-off" type scripts using the DAL and Auth features to test code that can print to a console. You are right, for this particular use case an MVC framework isn't needed since it's not web related at all. 

Yesterday the only way I knew how to do such a thing was by running the py.exe executable with an argument for the file you wanted to run, but that doesn't seem to work on non-windows platforms. After looking into it a bit more today, the --run=PYTHON_FILE argument available from command line might be what I need.

Thanks for the tip the other day, by the way, it was a pretty simple question and setting migrate=False worked perfectly.

Spencer Underwood

unread,
May 9, 2014, 9:32:27 AM5/9/14
to web...@googlegroups.com
Sorry for the delay in replying, I added a reply to this yesterday but it doesn't appear to have gone through for some reason. On the other hand, if this is a duplicate of yesterdays post feel free to ignore it.

What I mean by running in the web2py environment is having access to the features such as DAL and Auth to run test code and print the results to a console. You are completely right in that this particular use of testing things to see how it works doesn't need to be in an MVC framework, but if I can get the test code working properly I intended to move it into a module and import it at a controller somewhere. 

After looking a bit more, I noticed there was two command line options (--shell=APPNAME and --run=PYTHON_FILE ) which might be what I'm looking for. I'll give it a shot over this weekend to see if it works as I hope it does.


On Wednesday, 7 May 2014 18:01:38 UTC-4, Niphlod wrote:
Reply all
Reply to author
Forward
0 new messages