I need a hand integrating this python regex script into the python caller, I basically want to run as per below. I have it all working in python but now have been asked (as work uses FME) to do it here. I'm struggling to understand how to integrate python into the FME workflow.
if you could assist setting this up in a basic manner I'm sure I will get the hang of it, but have been struggling for a day or two.
(Also I know I could not use python, but I want to extend beyond this functionality later and prefer python)
fails.append((filename, 'Not meeting schema or placed incorrect folder. Please amend to this schema.'))
Best answer by david_r
This should work in your PythonCaller:
import fme
import fmeobjects
import re
defcheck_filename(filename):
pattern = r'^([A-Z]{3}-\d{3}-[A-Z]-[A-Z]{3}-MOD-\d{2}-[A-Z]{3}-[A-Z]{3}-\d{4})(-[A-Z]-\d{2})?(\.\w{3,4})$'
match = re.match(pattern, filename)
print(match)
return bool(match), filename ifnot match else""classCheckFilenames(object):"""Template Class Interface:
When using this class, make sure its name is set as the value of the 'Class
to Process Features' transformer parameter.
"""def__init__(self):"""Base constructor for class members."""passdefinput(self, feature):
filename_ = feature.getAttribute('fme_basename')
result, reason = check_filename(filename_)
feature.setAttribute('result', str(result))
feature.setAttribute('reason', reason)
self.pyoutput(feature)
defclose(self):"""This method is called once all the FME Features have been processed
from input().
"""passdefprocess_group(self):"""When 'Group By' attribute(s) are specified, this method is called
once all the FME Features in a current group have been sent to input().
FME Features sent to input() should generally be cached for group-by
processing in this method when knowledge of all Features is required.
The resulting Feature(s) from the group-by processing should be emitted
through self.pyoutput().
FME will continue calling input() a number of times followed
by process_group() for each 'Group By' attribute, so this
implementation should reset any class members for the next group.
"""passdefhas_support_for(self, support_type):"""This method returns whether this PythonCaller supports a certain type.
The only supported type is fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM.
:param int support_type: The support type being queried.
:returns: True if the passed in support type is supported.
:rtype: bool
"""if support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM:
# If this is set to return True, FME will pass features to the input() method that# come from a feature table object. This allows for significant performance gains# when processing large numbers of features.# To enable this, the following conditions must be met:# 1) features passed into the input() method cannot be copied or cached for later use# 2) features cannot be read or modified after being passed to self.pyoutput()# 3) Group Processing must not be enabled# Violations will cause undefined behavior.returnFalsereturnFalse
After the PythonCaller, insert a Tester to check for the value of "result", either "True" or "False".
Important: "fme_basename" does not contain the file extension, so either you'll have to add it manually, or to adapt your regex to acccount for this.
import fme
import fmeobjects
import re
defcheck_filename(filename):
pattern = r'^([A-Z]{3}-\d{3}-[A-Z]-[A-Z]{3}-MOD-\d{2}-[A-Z]{3}-[A-Z]{3}-\d{4})(-[A-Z]-\d{2})?(\.\w{3,4})$'
match = re.match(pattern, filename)
print(match)
return bool(match), filename ifnot match else""classCheckFilenames(object):"""Template Class Interface:
When using this class, make sure its name is set as the value of the 'Class
to Process Features' transformer parameter.
"""def__init__(self):"""Base constructor for class members."""passdefinput(self, feature):
filename_ = feature.getAttribute('fme_basename')
result, reason = check_filename(filename_)
feature.setAttribute('result', str(result))
feature.setAttribute('reason', reason)
self.pyoutput(feature)
defclose(self):"""This method is called once all the FME Features have been processed
from input().
"""passdefprocess_group(self):"""When 'Group By' attribute(s) are specified, this method is called
once all the FME Features in a current group have been sent to input().
FME Features sent to input() should generally be cached for group-by
processing in this method when knowledge of all Features is required.
The resulting Feature(s) from the group-by processing should be emitted
through self.pyoutput().
FME will continue calling input() a number of times followed
by process_group() for each 'Group By' attribute, so this
implementation should reset any class members for the next group.
"""passdefhas_support_for(self, support_type):"""This method returns whether this PythonCaller supports a certain type.
The only supported type is fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM.
:param int support_type: The support type being queried.
:returns: True if the passed in support type is supported.
:rtype: bool
"""if support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM:
# If this is set to return True, FME will pass features to the input() method that# come from a feature table object. This allows for significant performance gains# when processing large numbers of features.# To enable this, the following conditions must be met:# 1) features passed into the input() method cannot be copied or cached for later use# 2) features cannot be read or modified after being passed to self.pyoutput()# 3) Group Processing must not be enabled# Violations will cause undefined behavior.returnFalsereturnFalse
After the PythonCaller, insert a Tester to check for the value of "result", either "True" or "False".
Important: "fme_basename" does not contain the file extension, so either you'll have to add it manually, or to adapt your regex to acccount for this.
import fme
import fmeobjects
import re
defcheck_filename(filename):
pattern = r'^([A-Z]{3}-\d{3}-[A-Z]-[A-Z]{3}-MOD-\d{2}-[A-Z]{3}-[A-Z]{3}-\d{4})(-[A-Z]-\d{2})?(\.\w{3,4})$'
match = re.match(pattern, filename)
print(match)
return bool(match), filename ifnot match else""classCheckFilenames(object):"""Template Class Interface:
When using this class, make sure its name is set as the value of the 'Class
to Process Features' transformer parameter.
"""def__init__(self):"""Base constructor for class members."""passdefinput(self, feature):
filename_ = feature.getAttribute('fme_basename')
result, reason = check_filename(filename_)
feature.setAttribute('result', str(result))
feature.setAttribute('reason', reason)
self.pyoutput(feature)
defclose(self):"""This method is called once all the FME Features have been processed
from input().
"""passdefprocess_group(self):"""When 'Group By' attribute(s) are specified, this method is called
once all the FME Features in a current group have been sent to input().
FME Features sent to input() should generally be cached for group-by
processing in this method when knowledge of all Features is required.
The resulting Feature(s) from the group-by processing should be emitted
through self.pyoutput().
FME will continue calling input() a number of times followed
by process_group() for each 'Group By' attribute, so this
implementation should reset any class members for the next group.
"""passdefhas_support_for(self, support_type):"""This method returns whether this PythonCaller supports a certain type.
The only supported type is fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM.
:param int support_type: The support type being queried.
:returns: True if the passed in support type is supported.
:rtype: bool
"""if support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM:
# If this is set to return True, FME will pass features to the input() method that# come from a feature table object. This allows for significant performance gains# when processing large numbers of features.# To enable this, the following conditions must be met:# 1) features passed into the input() method cannot be copied or cached for later use# 2) features cannot be read or modified after being passed to self.pyoutput()# 3) Group Processing must not be enabled# Violations will cause undefined behavior.returnFalsereturnFalse
After the PythonCaller, insert a Tester to check for the value of "result", either "True" or "False".
Important: "fme_basename" does not contain the file extension, so either you'll have to add it manually, or to adapt your regex to acccount for this.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.