Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: awilliams1024Hi,
You may need to import data from a file. However, in some situations the file could be present but it could be empty.
In the case where the file is not empty the file contents are to be imported.
If the file is empty, the contents of a default file are to be read (which could contain, say, a header record but no data records).
The following Python-based node checks the size of a specified file and if the file is not empty it outputs the file path.
If the file is empty the node outputs the specified default file path.
node:Select_File_Using_File_Size
bretype:core::Composite
editor:Label=Select File Using File Size
editor:sortkey=57627c2a0ea96330
editor:icon=Python.ico
output:57627c4b7c2740af/=
outlink:0=/=testFileSizeImp.510d1e8b08c5651f
outputxy:0=510,130
outputrotation:0=0
inclockxy:Inclock=20,30
outclockxy:Outclock=400,30
prop:DefaultAction=Edit
editor:XY=600,160
editor:UsageDoc=Checks the size of a file and outputs the file name if the file is not empty or outputs the specified default file name.
editor:Doc=Checks the size of a file and outputs the file name if the file is not empty or outputs the specified default file name.
editor:propdef=File Name|filename|General||Not Blank
editor:propdef=Default File Name|filename|General||Not Blank
editor:propdoc=File Name=The name of the file whose file size is to be checked.
editor:propdoc=File Name=
editor:propdoc=File Name=Mandatory property.
editor:propdoc=Default File Name=The name of the file to be selected if the file
editor:propdoc=Default File Name=specified by the 'File Name' property is empty.
editor:propdoc=Default File Name=
editor:propdoc=Default File Name=Mandatory Property.
node:testFileSizeImp
bretype:core::Python
editor:Label=testFileSizeImp
editor:sortkey=510d1e0e27e55c56_2
output:510d1e8b08c5651f/out1=
prop:DefaultFileName={{^Default File Name^}}
prop:FileName={{^File Name^}}
prop:Mandatory=false
prop:Python2Implementation=<<EOX
import braininfo
import os
PROPERTY_BASE = "ls.brain.node.testFileSize"
def setup(brainNodeControlObj, BrainNodeClass):
''' Must return a class that inherits from BrainNodeClass. '''
class BrainNode(BrainNodeClass):
def initialize(self):
''' Called at node initialization, before first pump.'''
super(BrainNode, self).initialize()
self.logMedium("Initializing the testFileSize node")
# Obtain the values set on the node parameters
self.FileName = self.properties.getString(PROPERTY_BASE + ".FileName")
self.DefaultFileName = self.properties.getString(PROPERTY_BASE + ".DefaultFileName")
# Specify the name of the output field to store the selected file
self.outputFileName = "FileName"
#Setup the output metadata
om = self.newMetadata()
om.append(self.outputFileName, "string")
self.outputs[0].metadata = om
def finalize(self, val):
''' Called at node end, after last pump call. '''
super(BrainNode, self).finalize(val)
def pump(self, quant):
''' Will continue to be called until False is returned.
Only a small amount of work should be done in pump without checking
quant.permitsRunning. When that returns False, return from pump
with a True value to be called again, a False value when finished.'''
while quant.permitsRunning(self):
# Get the path of the file to be examined and open it for reading
testFilePath = self.FileName
# Replace any Windows-style path separators
testFilePath = testFilePath.replace("\\" ,"/")
# Get the file properties and check if the file is empty
statinfo = os.stat(testFilePath)
if statinfo.st_size > 0:
# The file has some contents
selectedFile = testFilePath
else:
# Use the default file
selectedFile = self.DefaultFileName
# Now output the selected file path
outputRec = self.outputs[0].newRecord()
outputRec[0] = str(selectedFile)
self.outputs[0].write(outputRec)
return False
# finished your quantum, you want pump called again
return True
# If you implement your logic here, you don't need code below. If you want
# to inherit, and add more logic at next level, uncomment below code
# py3File = brainNodeControlObj.properties.getString("ls.brain.node.BrainPython.python3ImplementationFile")
# braininfo.loadModule(py3File, "py3", brainNodeControlObj)
# import py3
# BrainNodeImpl = py3.setup(brainNodeControlObj, BrainNode)
# return BrainNodeImpl
return BrainNode
EOX
editor:XY=390,130
editor:propdef=FileName|string|General|ls.brain.node.testFileSize.FileName|Not Blank
editor:propdef=DefaultFileName|string|General|ls.brain.node.testFileSize.DefaultFileName|Not Blank
end:testFileSizeImp
end:Select_File_Using_File_Size_2
If required you could modify the Python code to perform other checks. The relevant section starts at line 44.
Regards,
Adrian