 |
The TinTin++ message board
|
 |
| View previous topic :: View next topic |
| Author |
Message |
Penquincoder
Joined: 29 Jul 2009 Posts: 9
|
Posted: Wed Jul 29, 2009 12:14 pm Post subject: Using TinTin++ with an External script |
|
|
Greetings TinTin users!
I have a simple question really, but can't find the answer in the forum or documentation.
I want to use TinTin++ is some complex ways, utilzing Python scripting. The easiest way I see to do this, is via an external event driven script file.
Essentially, I would like to have a trigger (action) that calls a script function from the loaded .py file. Is this possible??
I've tried loading a script file :
#script {~/path/to/file.py)
But it doesn't do anything that I can see.
Also, how do I CALL the function? Or post to the script, an event/notification?
IE
| Code: | #action {This is a line of text} {#event some_fun()}
|
<Script file>
| Code: | #!/usr/bin/python
def some_fun():
print '#showme This is a test function!' |
</script file>
Would I have to parse each and every function as an argument to the script?
Thank you in advance for your responses. |
|
| Back to top |
|
 |
Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3274
|
|
| Back to top |
|
 |
Penquincoder
Joined: 29 Jul 2009 Posts: 9
|
Posted: Fri Jul 31, 2009 8:50 pm Post subject: |
|
|
Well, I don't think that's doing what I expect it should do, or what I need to do.
I have the following script in python,
| Code: |
#run python python
def tintin_show(ses, cmd):
print "%s - %s" % (ses, cmd)
#test
#python tintin_show("test","#showme Today is %s" % time)
|
To see if the simple script can even run. Now, when I start up tt++ with the following command (being in the same directory): ./tt++ test.tin , I get the following output:
| Code: |
def tintin_show(ses, cmd):
print "%s - %s" % (ses, cmd)
tintin_show("test","#showme Today is %s" % time)
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def tintin_show(ses, cmd):
... print "%s - %s" % (ses, cmd)
File "<stdin>", line 2
print "%s - %s" % (ses, cmd)
^
IndentationError: expected an indented block
>>> tintin_show("test","#showme Today is %s" % time)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tintin_show' is not defined
>>>
|
Now...my understanding is that using the TinTin function #run python python, feeds everything that's in the script file DIRECT to python?? Well the proper syntax in Python is to use whitespace as indentation, and my spacing is correct.... So, why is this NOT parsing correctly through TinTin??
Is there something else I need to do in order to use python scripting IN TINTIN? |
|
| Back to top |
|
 |
Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3274
|
Posted: Fri Jul 31, 2009 11:10 pm Post subject: |
|
|
You'd probably want to use: print "#%s - %s" % (ses, cmd)
I'm quite sure that tintin messes up the indentation when using #read. Is there a way to import a script file in python?
Something like:
| Code: |
#run python python
execfile("test.py")
#test
#python tintin_show("test","#showme Today is %s" % time)
|
|
|
| Back to top |
|
 |
Penquincoder
Joined: 29 Jul 2009 Posts: 9
|
Posted: Sat Aug 01, 2009 9:31 am Post subject: |
|
|
Okay, I've managed to actually return results with the following!
TinTin file:
| Code: |
#split
#config regexp on
#run python python
import sys
sys.path.append( "~/Documents" )
import test
#python test.timestamp()
|
The path.append is to make python search the current directory, to import our custom written PY file.
The Python file:
| Code: |
#! /usr/bin/env python
#
import sys
import time
from datetime import datetime
def tintin_show(ses, cmd):
print "%s - %s" % (ses, cmd)
def timestamp():
dt = datetime.now()
ms = float(dt.microsecond) / 100000
tintin_show('main', '#show %s' % (time.strftime(" <%H:%M:%S:" + str(ms) + ">")))
|
And, the output from tintin is:
| Code: |
import sys
sys.path.append( "~/Documents" )
import test
test.timestamp()
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append( "~/Documents" )
>>> import test
>>> test.timestamp()
main - #show <10:27:45:1.33798>
>>>
|
Which ...is somewhat good! It shows that the python interpreter is being called by TinTin, and the code is executed. HOWEVER, TinTin just calls up the python editor, and leaves it in that state? IT doesn't actually just send the command or function to be parsed by it, and then return function to itself? Seems like this is not what I want, in terms of being able to call python functions from an external script, WITHIN TinTin.
If I wanted an entire MUD session in the python interpreter, I'd just get or write a python mud client!
Is there anyway to have TinTin use python functions, FROM an external script??
Example:
#import scriptfile
#action {A prompt line here} {#exec prompt()}
Which would load the script file, then when the trigger 'A prompt line here' is matched, it would execute the function prompt, from the pre-loaded script file? |
|
| Back to top |
|
 |
Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3274
|
Posted: Sat Aug 01, 2009 6:53 pm Post subject: |
|
|
You need a trigger in the python session to capture the output of the python script and feed it back to the calling session.
The following might work:
| Code: |
#run python python
import sys
sys.path.append( "~/Documents" )
import test
#action {^tintin (%1) (%2)$}
{
#%1 %2
}
#nop switch back to the startup session:
#gts
#python test.timestamp('gts')
|
test.py
| Code: |
def tintin(ses, cmd):
print "tintin (%s) (%s)" % (ses, cmd)
def timestamp(ses):
dt = datetime.now()
ms = float(dt.microsecond) / 100000
tintin('ses', '#show %s' % (time.strftime(" <%H:%M:%S:" + str(ms) + ">")))
|
You can do the time in tintin++ as well using
| Code: |
#format utime %U;
#format {time} {%t:%m} {{%H:%M:%S} {$utime % 1000000 / 100000}}
|
|
|
| Back to top |
|
 |
mrbigtaco
Joined: 26 Jun 2011 Posts: 35
|
Posted: Sun Jun 26, 2011 6:33 am Post subject: |
|
|
I hate to resurrect an old post, but I had trouble getting python to work using the examples here. Like you mentioned, #read flubbed all the white space formatting for python so that was out of the question, but if I used import instead it would recognize that the file I was importing was valid, but for some reason I couldn't access the methods added to the name space. The only thing that I could get to work was manually typing out #run and the defs for a simple hello world without using #read.
I don't have the exact error handy, but test.timestamp() etc when used with the import method gave a generic error about modules and not having that function defined.
Since there's not many other posts on this board specifically calling out python examples with #run, has anyone had success with using #run python?
I was able to run the Lua example fine. |
|
| Back to top |
|
 |
Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3274
|
Posted: Sun Jun 26, 2011 11:37 am Post subject: |
|
|
Might be easier to use #script instead of #run. You call a python script with #script and the result gets stored into a variable, indexed by line. For example:
| Code: |
#script {temp} {python -c "print 'hello world\nhello tintin'"}
#showme $temp[1]
#showme $temp[2]
|
If you want to directly execute the python echo as a command you'd use:
| Code: |
#script
{
python -c "print '#showme hello world';print '#showme hello tintin'"
}
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
TinTin++ Homepage
Powered by phpBB © 2001, 2002 phpBB Group
|