This article describes how to create a Python script to log recognition event information to a Comma Separated Values (CSV) file. The script is generalized so that you can pass any number of fields along with the column labels.  This way, you can change the columns that are written simply by editing the SAFRActions.config file.


By default the script writes to the application directory for Actions Relay Event Service (ARES). Here's an example log generated by the script:


Set up the script with SAFR actions


To 

  1. Configure SAFR Actions
  2. Publish Script to Scripts directory
  3. Verify script


For this article, we set up a script that simply writes information from events to a CSV file.


Install the script

Add the attached script (write_log.py) to the SAFR Actions Scripts directory in following locations:

  • Mac: /Library/RealNetworks/SAFR/ares/scripts
  • Linux: /opt/RealNetworks/SAFR/ares/scripts
  • Windows: C:\Program Files\RealNetworks\SAFR\ares\scripts


Following are contents of script with comments

######### SETTINGS ##########
# Directory to write log file.  Default (empty string) writes to SAFR Actions Application directory.
# For windows use 'C:\\Users\\Public\\Documents\\'.  For Linux/Mac use  '/path/dir'
log_basepath=""
#############################

import os  # Split filenames
import sys  # For parsing arguments passed by safr actions
# For creating unique dir name for untar dir
from datetime import datetime

def writeLine(fname, str):
  with open(fname, "a") as f:
    f.write(str + "\n")

# Assign cmd line args
vals = sys.argv[1]
hdrs = sys.argv[2]
# Uncomment to test outside SAFR Actions:
# vals = "Steve|Cam1|Building1"; hdrs = "firstName|source|site"

# Create filename in format of YY_MM_DD for log file rolling
log_filename = log_basepath + "events" + datetime.now().strftime('%Y_%m_%d') + ".csv"

if not os.path.exists(log_filename):
  writeLine(log_filename, hdrs.replace('|',','))
print(os.getcwd())
# build log string
vals = vals.split('|') # Split into list
logstr = "\"" + '\",\"'.join(vals) + "\"" # combine back info CSV format
# Print log
writeLine(log_filename, logstr)



SAFR actions configuration

Open the SAFR Actions GUI application and edit the SAFRActions.config as follows:


Replace the userId and userPwd above with your username and password. If you're using an on-premise license, set the environment field to "SAFR Custom...".  


Edit the actions -> Item 1 field to fit your need.  The contents of the  actions -> Item 1 field shown above is as follows:

python ./scripts/write_log.py "#D|#E|#N|#T|#O|#a|#S|#I|#A|#G" "personId|personExternalId|name|personType|company|idClass|source|site|age|gender"


The command has 2 arguments.  

  • The first is a list of tokens that are replaced by SAFR Actions.  The definitions of the tokens are found in Action and Reply Message Escape Sequences.   For convenience, you can use the attached helper tool (SAFRActionsTokens.xlsx) to generate the command string.  Hopefully the spreadsheet is self-explanatory.
  • The 2nd line is the list of headings used in the CSV file.  This can be any strings you like.


As an alternative, you can edit the attached "SAFRActions.config" file and paste in the contents of the attached.  The SAFR Actions config file (SAFRActions.config) is located in following: 

  • Mac: /Library/RealNetworks/SAFR/ares/config/
  • Linux: /opt/RealNetworks/SAFR/ares/config/
  • Windows: C:\ProgramData\RealNetworks\SAFR\ares\config\

After editing the file, start SAFR Actions to reload the file.


Testing the script

Please refer to How to trigger a script with SAFR actions for details on testing the script.


The log file should be written to the SAFR Actions application directory by default (same place you put the script but one folder level up).