[ADCH++] Pyton scripts

Python Script Releases, known supporting hubsofts for python scripting.

DSHub | ADCH++
Post Reply
Sulan
Junior Member
Posts: 16
Joined: 19 Jan 2009, 20:33

[ADCH++] Pyton scripts

Post by Sulan » 21 Jan 2009, 20:43

Basic script:
Start and run hub.

Here is a basic script to just start and run ADCH++ from a python script. I would like to add that im not that skilled with programming so there might be some stuff that i have got mixed up.

First of all i would like to mention that i had issues when importing pyadchpp.py, i had to edit the line where it imports _pyadchpp.dll

From:

Code: Select all

import _pyadchpp
To:

Code: Select all

try:
	import _pyadchpp
except:
	import imp, os
	_pyadchpp=imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')
	del imp, os
You need the .dll files from ADCH++ and pyadchpp.py in the same directory as the basic script.

Basic script:

Code: Select all

#!/usr/bin/python

#import python adchpp module
import pyadchpp
#import os module
import os
import time

def run():
	#initialize the hubsoft with the complete path to your config directory containing adchpp.xml
	pyadchpp.initialize(os.getcwd()+'\config\\') 
	pyadchpp.startup()
	#create a loop so the program doesnt exit
	while 1:
		time.sleep(0.1)


if __name__== '__main__':
	run()
Last edited by Sulan on 21 Jan 2009, 20:57, edited 1 time in total.

Sulan
Junior Member
Posts: 16
Joined: 19 Jan 2009, 20:33

Re: [ADCH++] Pyton scripts

Post by Sulan » 21 Jan 2009, 20:56

Signal examples:
Used basic script and added some signal handeling as examples.

Code: Select all

#!/usr/bin/python

#import python adchpp module
import pyadchpp
#import os module
import os
import time

def onReceive(client, cmd, n):
	print 'Client object:',client
	print 'AdcCommandObject:',cmd
	print 'Command:',cmd.toString()

def onState(client, state):
	print 'Nick:',client.getField('NI') #Nick not know untill after first INF
	print 'State:', state
	
def run():
	#initialize the hubsoft with the complete path to your config directory containing adchpp.xml
	pyadchpp.initialize(os.getcwd()+'\config\\') 
	pyadchpp.startup()
	#create a loop so the program doesnt exit
	
	#get clientmanager instance
	clientmanager = pyadchpp.getCM()
	
	#register signals
	x = clientmanager.signalReceive().connect(onReceive)
	y = clientmanager.signalState().connect(onState)
	while 1:
		time.sleep(0.1)


if __name__== '__main__':
	run()
	

Sulan
Junior Member
Posts: 16
Joined: 19 Jan 2009, 20:33

Re: [ADCH++] Pyton scripts

Post by Sulan » 25 Jan 2009, 18:23

I made a small script that can load other python modules so they can interact with the hub.
The script will import any pythonscript in a 'pyscripts' directory and execute 'register' function.

AdchCore:

Code: Select all

import threading
import pyadchpp as hub
import os
import sys
import traceback
import time

class ADCH( threading.Thread ):
	
	def __init__ ( self):
		threading.Thread.__init__ ( self )
		self.path = os.getcwd()
		self.scriptDir = 'pyscripts'
		
		hub.initialize(self.path+'\config\\') 
		
		self.keeprunning = True
		self.CM = hub.getCM()
		self.PM = hub.getPM()
		self.SM = hub.getSM() 
		self.newSignals = []
		self.signals = []
		self.start()
		
		
	def importModules(self):
		try:
			if not os.path.exists(self.scriptDir):
				os.mkdir(self.scriptDir)
			if not self.scriptDir in sys.path:
				sys.path.insert(0, self.scriptDir)
				
			
			for script in os.listdir(os.getcwd()+'\\'+self.scriptDir):
				if script.endswith('.py'):
					try:
						
						p = __import__(script[:-3], None, None, [''])
						if hasattr(p, 'register'):
							impfunc = getattr(p, 'register')	
							impfunc(self)
							print 'Loaded module:',p.__name__
					except Exception, err:
						traceback.print_exc(file=sys.stdout)
		except Exception, err:
			traceback.print_exc(file=sys.stdout)
	
	
	def startHub(self):
		try:
			hub.startup()
		except Exception, err:
			traceback.print_exc(file=sys.stdout)
		
	
	def shutdown(self):
		self.newSignals = []
		self.signals = []
		hub.shutdown()
		
	def run (self):	
		self.importModules()
		while self.keeprunning: 
			while len(self.newSignals) >0:
				signal, callback= self.newSignals.pop()
				x=signal(callback)
				self.signals.append(x)
			time.sleep(0.1)

	
			
if __name__ == '__main__':
	hc = ADCH()
	
Testscript to put in to pyscripts:

Code: Select all

import pyadchpp as hub
def testFunc(client, cmd, n):
	CMD = hub.AdcCommand.toString(cmd)
	print CMD

def register(core):
	core.newSignals.append( (core.CM.signalReceive().connect, testFunc) )

poy
Member
Posts: 78
Joined: 26 Nov 2008, 17:04

Re: [ADCH++] Pyton scripts

Post by poy » 01 Feb 2009, 19:58

Sulan wrote:Basic script:
First of all i would like to mention that i had issues when importing pyadchpp.py, i had to edit the line where it imports _pyadchpp.dll

From:

Code: Select all

import _pyadchpp
To:

Code: Select all

try:
	import _pyadchpp
except:
	import imp, os
	_pyadchpp=imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')
	del imp, os
instead of modifying the script generated by SWIG, you can create a different file called "_pyadchpp.py" in which you put your import code:

Code: Select all

import imp
_pyadchpp = imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')

arnetheduck
Newbie
Posts: 8
Joined: 17 Mar 2009, 13:37

Re: [ADCH++] Pyton scripts

Post by arnetheduck » 27 Mar 2009, 18:10

fix in bzr (python modules should be named .pyd on win)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest