# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2009, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
#
# $
# $Id: Constants.py 10330 2009-12-24 22:13:38Z grisha $
from snaplogic.common.snap_exceptions import SnapObjTypeError
from decimal import Decimal
from datetime import datetime
import time
import snaplogic.components as components
from snaplogic.common.data_types import SnapString,SnapNumber,SnapDateTime
from snaplogic.cc.component_api import ComponentAPI
from snaplogic.cc import prop
from snaplogic.snapi_base import keys
from snaplogic.components import computils
from snaplogic.common.snap_exceptions import SnapComponentError
from snaplogic.common import version_info
INPUT_VIEW = "Input"
OUTPUT_VIEW = "Output"
OUTPUT_FIELD_VALUES = "Output Field Values"
OUTPUT_FIELD_VALUE = "Output Field Value"
OUTPUT_FIELD = "Output Field"
VALUE = "Value"
class Constants(ComponentAPI):
"""
Constants component sends constants to output fields.
Typical use case is taking an input view, and adding one or more fields
that are set to some constant value, or overwriting existing input fields with
constant value.
EXAMPLE
As an example usage consider this pipeline:
CsvRead --> Constants --> CsvWrite
CsvRead outputs the following fields: (name, age)
Constants adds the following field: archived = 1
CsvWrite receives the three fields: (name, age, archived)
Constants component precreates one input view, and one output view,
and sets up the output view to pass through all fields unchanged from the input view.
When using the component one would want to add one or more fields
to the output view, and set the Constants property to tell the component
which constants should be send to which fields.
Constants component has a single property:
"Output Field Values" which is a list of dictionaries.
Each dictionary has two elements:
"Output Field": name of the output field being assigned
"Value" value that should go to the output field
Value is defined in string format, and converted to the approriate type
by the component depending on output field type.
For dates, the following formats are supported:
"yyyy-mm-ddThh:mm:ss", "yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd"
"""
api_version = '1.0'
component_version = '1.1'
capabilities = {
ComponentAPI.CAPABILITY_INPUT_VIEW_LOWER_LIMIT : 1,
ComponentAPI.CAPABILITY_INPUT_VIEW_UPPER_LIMIT : 1,
ComponentAPI.CAPABILITY_OUTPUT_VIEW_LOWER_LIMIT : 1,
ComponentAPI.CAPABILITY_OUTPUT_VIEW_UPPER_LIMIT : 1,
ComponentAPI.CAPABILITY_ALLOW_PASS_THROUGH : True
}
component_description = "Constants component allows to send constant values to output fields"
component_label = "Constants"
component_doc_uri = "https://www.snaplogic.org/trac/wiki/Documentation/%s/ComponentRef/Constants" % \
version_info.doc_uri_version
def create_resource_template(self):
"""
Create Constants resource template.
"""
field = prop.SimpleProp(OUTPUT_FIELD, SnapString, "Destination Output Field",
{'lov': [ keys.CONSTRAINT_LOV_OUTPUT_FIELD] } ,
True)
# Note that "value" isn't required. We set it this way so they can assign empty strings
# to output fields. If we make this a required property the property cannot be set to
# an empty string.
value = prop.SimpleProp(VALUE, SnapString, "Value to assign", None, False)
field_value = prop.DictProp(OUTPUT_FIELD_VALUE, field, "Output Field Value", 2, required=True)
field_value[OUTPUT_FIELD] = field
field_value[VALUE] = value
field_values = prop.ListProp(OUTPUT_FIELD_VALUES, field_value, required=True)
self.set_property_def(OUTPUT_FIELD_VALUES, field_values)
# Predefine the input view with no fields
self.add_record_input_view_def(INPUT_VIEW, [], 'Constants input view')
# Predefine the output view with no fields
self.add_record_output_view_def(OUTPUT_VIEW, [], 'Constants output view')
# Set output view as pass through from input view
self.set_output_view_pass_through(OUTPUT_VIEW, [INPUT_VIEW,])
def validate(self, err_obj):
""" Validate Constants component """
input_view_name = self.list_input_view_names()[keys.SINGLE_VIEW]
output_view_name = self.list_output_view_names()[keys.SINGLE_VIEW]
output_view = self.get_output_view_def(output_view_name)
output_view_fields = output_view[keys.VIEW_FIELDS]
output_view_field_names = [field[0] for field in output_view_fields]
output_view_field_types = [field[1] for field in output_view_fields]
# Validate the output fields property
referenced_fields = []
for i, field_and_value in enumerate(self.get_property_value(OUTPUT_FIELD_VALUES)):
field = field_and_value[OUTPUT_FIELD]
value = field_and_value[VALUE]
# Validate the value
try:
type = output_view_field_types[output_view_field_names.index(field)]
computils.convert_to_field_type(type, value)
except SnapComponentError, e:
err_obj.get_property_err(OUTPUT_FIELD_VALUES)[i][VALUE].set_message(e.message)
if field in referenced_fields:
# Same field referenced more than once
err_obj.get_property_err(OUTPUT_FIELD_VALUES)[i][OUTPUT_FIELD].set_message("Field '%s' referenced more than once" % field)
referenced_fields.append(field)
def execute(self, input_views, output_views):
""" Execute Constants component """
try:
output_view = output_views.values()[keys.SINGLE_VIEW]
except IndexError:
raise SnapComponentError("No output view connected.")
try:
input_view = input_views.values()[keys.SINGLE_VIEW]
except IndexError:
raise SnapComponentError("No input view connected.")
# Prepare a dictionary that maps field name to field value
field_name_to_value = {}
output_view_field_names = list(output_view.field_names)
output_view_field_types = list(output_view.field_types)
for field_and_value in self.get_property_value(OUTPUT_FIELD_VALUES):
field = field_and_value[OUTPUT_FIELD]
value = field_and_value[VALUE]
# Depending on field type make sure the value is converted
# to the right datatype
type = output_view_field_types[output_view_field_names.index(field)]
field_name_to_value[field] = computils.convert_to_field_type(type, value)
# Read all input records and generate output records
while True:
input_record = input_view.read_record()
if input_record is None:
# EOF
break
output_record = output_view.create_record()
# Pass through fields
output_record.transfer_pass_through_fields(input_record)
# Set constant fields in the output view
for field in field_name_to_value:
output_record[field] = field_name_to_value[field]
output_view.write_record(output_record)
# We're done
output_view.completed();
def upgrade_1_0_to_1_1(self):
"""
No-op upgrade only to change component doc URI during the upgrade
which will be by cc_info before calling this method.
"""
pass
|