system.file.getTempFile not working

#1

Hi everybody,
In a gateway tag change event script, my code below is not working (line 29) whereas it works in script console.(Ignition version 7.9.4)

import StringIO
import csv
import os
import ftplib
import sys
import system
from datetime import date
from datetime import datetime
from datetime import timedelta

if newValue.value==0:
	sys.exit()

today=date.today() #Récupère la date du jour au format 'aaaa-mm-jj'
datejour= str(today).replace('-','') #Remplace les - par rien
yesterday=date.today() - timedelta(days=1) #Récupère la date de la veille
yesterday_jour=str(yesterday).replace('-','') #Remplace les - par rien
#print datejour,yesterday_jour


ftp = ftplib.FTP('1.1.1.1') 
ftp.login('user','password')
files = [] #créé liste vide
ftp.retrlines('NLST', files.append) #Récupère la liste des fichiers dans le dossier du FTP et alimente la liste

#print files

def callback(buf):
	ftmp=system.file.getTempFile("data")
	#print ftmp
	
	f=open(ftmp, 'wb')
	f.write(buf)
	system.file.writeFile("C:\\TempTest\\NewTest.data",buf)
	#print buf
	
	dataIn=	StringIO.StringIO(buf)
	reader=csv.reader(dataIn, delimiter=';', quotechar="\"")
	dataOut=[]
	headers=["Poste","Nom","Date","DJU"]
	for row in reader:
		dataOut.append(row)
	#print dataOut
	
	date=datetime.strptime(dataOut[-1][2]+'000000','%Y%m%d%H%M%S')
	dju=dataOut[-1][3].replace(',','.')
	#print dataOut[-1][2], dju,date
	
	
	f.close()
	
	query="INSERT INTO dju (t_stamp,dju) VALUES (?,?)"
	system.db.runPrepUpdate(query,[date,dju])


filename="DJU_GRAND_CLOS_"+yesterday_jour+".data"
#print filename
ftp.retrbinary('RETR '+filename, callback)
ftp.voidcmd('QUIT')
ftp.close()

The error below

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 58, in File "user-lib\pylib\ftplib.py", line 395, in retrbinary callback(data) File "", line 29, in callback NameError: global name 'system' is not defined
at org.python.core.Py.NameError(Py.java:260)
at org.python.core.PyFrame.getglobal(PyFrame.java:265)
at org.python.pycode._pyx104.callback$1(:53)
at org.python.pycode._pyx104.call_function()
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
at org.python.core.PyFunction.__call__(PyFunction.java:317)
at ftplib$py.retrbinary$29(user-lib\pylib\ftplib.py:397)
at ftplib$py.call_function(user-lib\pylib\ftplib.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
at org.python.core.PyBaseCode.call(PyBaseCode.java:157)
at org.python.core.PyFunction.__call__(PyFunction.java:338)
at org.python.core.PyMethod.__call__(PyMethod.java:139)
at org.python.pycode._pyx104.f$0(:60)
at org.python.pycode._pyx104.call_function()
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:657)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:616)
at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:187)
at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:134)
at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:99)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 58, in File "user-lib\pylib\ftplib.py", line 395, in retrbinary callback(data) File "", line 29, in callback NameError: global name 'system' is not defined
... 29 common frames omitted

Thanks !

0 Likes

#2

Try moving import system into the function where system is used.

0 Likes

#3

Ok it’s working :no_mouth::thinking:
Meaningless but ok
Thanks

0 Likes

#4

It’s an artifact of legacy scripting. You can avoid such problems by delegating event handling to a project script function. In the event itself, you just have a one-liner calling project.someScript.someFunction(event....).

0 Likes