斯潘8
斯潘4
This article is part 2 of a 5 part series to help you get started with basic Python and FME.This article is about using the启动python脚本工作台中的功能,包括两个示例。第一个脚本使用启动python脚本在运行转换之前复制文件,第二个脚本将自定义(用户定义)消息写入日志文件。
向工作区添加一个python启动脚本是扩展工作台功能的一个很好的方法。You may want to set-up the environment in a certain way before a translation is run or you may want the increased control over FME that a custom script offers.您可以直接在工作台内定义脚本。在工作台导航器窗格中工作区参数>脚本,您将看到一个名为启动python脚本.右键单击参数并选择编辑参数值打开一个特殊的python编辑窗口。在这里,您可以输入一些将要执行的python代码之前翻译,但是之后脚本化参数的执行。
Users can enter Python code which will execute before the translation in the Startup Python Script parameter within the Navigator pane under Workspace Parameters > Scripting > Startup Python Script.
注:如果您想设置或返回一个FME参数,这不是地方-见脚本化参数文章。
Attached workspace: StartupPython1.fmwt
You may want to create backups of your data before or after you run a translation.使用启动python脚本或关闭python脚本都可以这样做。在这个例子中,you will be creating a zip file backup of your shapefile input dataset.您可以使用附加的工作区模板,启动电源1.fmwt(有关链接,请参阅上面的下载部分)。
打开附加的工作区。The workspace reads the input shapefile dataset and reprojects the data before writing the data to GML.在运行工作区之前,我们希望从源数据集中创建一个zip文件,并将其放在c:drive上的temp文件夹中(如果它不存在的话)。如果已经存在同名的zip文件,the file will not be copied and a message will be printed to the log window.去Navigator > Workspace Parameters > Scripting > Startup Python Script查看脚本或查看以下内容。
#导入FME和关闭模块导入FME,ShutilFrom OS导入路径文件路径硬编码到C:\TempWorkspacePath='C:\\Temp'使用宏值字典的源数据集的文件路径sourceFile=fme.macrovalues['source dataset _esrishape']获取不带扩展名的数据集名称,并附加“_backup”source name=path.splitext(path.basename(sourcefile))[0]+'_backup'带有nam的新zip路径ezipdir='\'。格式(workspacepath,source name)包含源数据集的文件夹根目录source path=path.dirname(source file)如果已经存在同名的zip文件,不要创造,如果path.exists(zipdir+'.zip'):message to log window print(zipdir+'.zip已存在,请将消息添加到日志窗口。没有创建zip。')其他:使用shutil模块创建zip文件shutil.make_archive(zipdir,“拉链”,sourcePath) print('Created backup of ' + path.basename(sourceFile)) print('Path at ' + zipDir + '.zip')# Debugging# print(sourceName)# print(zipDir)
The script imports thefme,舒蒂尔modules and the路径从操作系统模块初始化。这个shutil.make_archive()。(第23行)至少需要3个参数:
我们可以使用python函数和fme宏来查找所有的第一个和第三个参数。在这个例子中,我们要在c:\temp文件夹中创建源数据集的zip文件,该文件与shapefile和文本“_backup”同名。
要查找形状文件名,we can use the宏观价值观字典,使用source dataset参数(脚本中的source file)查找源数据集的文件路径,并仅获取文件路径的最后一部分。
在这个例子中,它是SourceDataset_ESRISHAPE'.源数据集参数可能有另一个名称,您可以通过右键单击“导航器”窗格中的源读卡器并选择编辑用户参数定义在这里可以看到name参数。
源数据集参数的名称通常可以通过在导航器窗格中右键单击读卡器并选择编辑用户参数定义来找到。
要获取不带扩展名的数据集名称(脚本中的sourcename),we can use thepath.basename()函数来自操作系统用于查找文件名的模块。这个SpultExter()函数将在第一个“.”处拆分文本,因为我们只关心文件名而不关心扩展名,我们只返回第一段带有[0]的文本。我们在文件名的末尾附加了“备份”。
我们通过选择目标文件夹(硬编码为工作空间路径)来完成创建完整的zip路径。adding ‘\\',the file name (sourceName) and adding ‘_backup'.注意,因为反斜杠字符是Python中的转义字符,我们需要在文件路径中使用双反斜杠。
Next,我们需要找到包含要压缩的数据的文件夹,因为shapefile是基于文件夹的格式,所以您不能只复制.shp文件。因为我们已经找到了源数据集文件的文件路径,we can usepath.dirname ()函数获取包含数据的文件夹的文件路径(脚本中的sourcepath)。
为了防止覆盖同名的现有数据集,只有当源数据集不存在时,才会创建该数据集的zip文件备份。如果确实如此,只有一条消息将打印到日志中。这是由一个if/else语句。The last three lines are print functions for debugging purposes.
The message that prints in the Translation log pane if a file with the same name already exists in the backup location.In this case,将不会创建zip文件。
Run the workspace (运行>运行工作区或按F5)。工作区运行完成后,go to the C:\TEMP folder.您应该看到一个zip文件,其输入数据集名称的结尾是“_backup”。
注意:您不必给c:\temp写信。您可以将脚本更改为写入另一个硬编码文件路径,或者使用已发布的参数要求用户选择每次运行工作区时希望将zip文件写入的位置。
附加工作区:startuppython2.fmw
A log file contains a lot of information already but what if you want to write custom messages to the log file?您可以使用启动python脚本向日志文件添加自定义消息,皮通卡勒变压器,还有一个关闭python脚本。在这个例子中,you will be adding a short message to the log file with a Startup Python script.
在使用以下启动python脚本运行fme之前,我们可以将自定义消息写入日志文件(请参见StartupPython2.fmw):
#import fme objects moduleimport fmeobjects创建名为loggerlogger=fmeobjects.fmelogfile()调用logmessagestring方法添加自定义的textlogger.logmessagestring(“您可以将自定义消息添加到日志文件”)演示打印语句将只打印到日志paneprint(“这将只打印到转换loG“窗格”
您可以使用FME对象模块访问日志文件,因此我们需要导入FmeObjects.然后通过调用fmeobjects.FMELogFile()班级。然后对象调用日志消息字符串()方法将自定义文本添加到日志文件中。打印功能是演示将消息打印到翻译日志窗格不会出现在翻译日志文件中。
打开附加的工作区。它看起来应该与第一个示例非常相似。Run the workspace (运行>运行工作区或按F5)。转到“翻译日志”窗格,在该窗格中单击鼠标右键。从菜单中选择“find”,然后键入“log”。您应该看到文本“日志”的实例在翻译日志窗格。您将在窗格中看到自定义日志消息和突出显示的打印消息。
第一条突出显示的行是使用logger对象的logMessageString方法创建的。The second highlighted line is created using the print function.
转到保存附加工作区的文件夹位置。日志文件应保存在同一文件夹位置(默认情况下),并且应与工作区同名,但扩展名为.log。用任何文本编辑器打开日志文件。Use the search function to find the custom message within the log.请注意,打印消息没有保存在翻译日志文件中。
Notice the text added using the print function does not appear in the log file.Only text added using the logger object's logMessageString method will appear in log files.
注意:将自定义消息写入日志文件的方法取决于它是在启动还是关闭阶段。在启动脚本中(因为FME已经打开了日志文件,多次打开是很危险的,you should usefmeobjects.FMELogFile().
To access Help documentation for Startup and Shutdown scripts within Workbench,请到:启动和关闭python脚本
有关可用于启动python脚本的FME变量的完整列表,请转到:开始使用python
您可以在这里找到python FME对象API的完整文档:fme对象python api
The data used here originates from open data made available by theCity of Vancouver,不列颠哥伦比亚省。它包含根据开放政府许可证(温哥华)获得许可的信息。
?2019安全亚搏在线软件公司|合法的