xml_collector.py

xml_collector.py

This file contains the objects for intaking TMC files and generating python interpretations. Db Files can be produced from the interpretation

TmcFile

class pytmc.TmcFile(filename)

Object for handling the reading comprehension comprehension of .tmc files.

all_Symbols

Collection of all Symbols in the document. Must be initialized with isolate_Symbols().

Type

ElementCollector

all_DataTypes

Collection of all DataTypes in the document. Must be initialized with isolate_DataTypes().

Type

ElementCollector

all_SubItems

Collection of all SubItems in the document. Must be initialized with isolate_SubItems().

Type

ElementCollector

all_TmcChains

Collection of all TmcChains in the document. Must be initialized with create_chains(). These chains are NOT SINGULAR.

Type

list

all_singular_TmcChains

Collection of all singularized TmcChains in the document. Must be initialized with isolate_chains().

Type

list

configure_packages()

Apply guessing methods to self.all_RecordPackages.

create_chains()

Add all new TmcChains to this object instance’s all_TmcChains variable

create_packages()

Populate the the self.all_RecordPackages list with no-guessing-applied packages. requires self.all_singular_TmcChains to be populated.

explore_all()

Return a list of ALL paths to leaf-variables in the tmc file.

Returns

For every instantiated variable in the TmcFile, return a list documenting the path to that variable. This path starts at the global level instantiation and tracks through successive levels of encapsulation to find the final value itself. For each value, this list contains a single row.

Return type

list

isolate_DataTypes(process_subitems=True)

Populate all_DataTypes with a DataType representing each DataType in the .tmc file.

Parameters

process_subitems (bool) – If True, automatically process all subitems containted in the datatypes populating all_SubItems.

isolate_SubItems(parent=None)

Populate all_SubItems with a SubItem representing each subitem in the .tmc file.

Parameters

parent (:str) – Specify the name string of the datatype to search for subitems. Subitems are automatically linked to this parent datatype.

isolate_Symbols()

Populate all_Symbols with a Symbol representing each symbol in the .tmc file.

isolate_all()

Shortcut for running isolate_Symbols() and isolate_DataTypes()

isolate_chains()

Populate the self.all_Singular_TmcChains with singularized versions of the the entries in self.all_TmcChains. Requires create_chains to have been run first.

recursive_explore(root_path)

Given a starting Symbol or SubItem, recursively explore the contents of the target and return a list for the path to each final leaf-item.

Parameters

root_path (list) – This is a list leading to the initial item to explore from. The list is composed of Symbol and SubItem instances

Returns

This list contains a list for each leaf-item detected. The individual lists are the paths through the tree to each leaf-item charted by Symbol and SubItem.

Return type

list

recursive_list_SubItems(root_DataType)

For a given DataType, provide all of its SubItems including those derived from inherited DataTypes

Parameters

root_DataType (DataType) – instance of the target datatype

Returns

list of SubItem of contained subItems

Return type

list

render()

Produce .db file as string

resolve_enums()

Identify the SubItems and Datatypes that represent enum types. Requires isolate_Datatypes and isolate_Subitems to have been run first.

ElementCollector

class pytmc.xml_collector.ElementCollector

Dictionary-like object for controlling sets of insntances Symbol, DataType, and SubItem. Each entry’s key is the name of the TwinCAT variable. add() automates this setup and should be used to add entries instead of normal dictionary insertion techniques.

Subclassed from python’s standard dictionary.

add(value)

Include new item in the dictionary.

Parameters

value (Symbol, DataType,or SubItem.) – The instance to add to the ElementCollector

property registered

Return subset of the dictionary including only items marked for pytmc’s comsumption with pragmas.

Returns

TwinCAT variables for pytmc

Return type

dict

TmcChain

class pytmc.xml_collector.TmcChain(chain)

Pointer to the tmc instances and track order. Leaf node is last.

build_singular_chains()

Generate list of all acceptable configurations.

Returns

List of TmcChains. Each chain is bound to one of the possible paths given the configurations available at each step.

Return type

list

forkmap()

Provide a description of the branching hierarchy for entries with multiple Configurations.

Returns

This list contains a list for each element contained in the chain. This interior list documents all configuration names held by that element in the chain.

Return type

list

is_singular()

Determine whether this TmcChain has only a single configuration for each element

Returns

True if all elements in this chain are singular

Return type

bool

property last

Return the last element in the chain.

Returns

The instance (of whetever class) that represents the target variable.

Return type

BaseElement, Symbol, or SubItem

naive_config(cc_symbol=':')

On chains of singular configs, stack up configurations from lowest to highest to generate a guess-free configuration.

Returns

Returns the configuration built from the existing pragmas in the chain.

Return type

Configuration

property name_list

Produce list of the names of the elements in the chain. This name list should be usable by ADS to access the variable.

Returns

Return type

list

BaseRecordPackage

class pytmc.xml_collector.BaseRecordPackage(chain=None, origin=None)

BaseRecordPackage includes some basic funcionality that should be shared across most versions. This includes things like common methods so things like validation can be configured at the __init__ with an instance variable. Overwrite/inherit features as necessary.

ID_type()

Distinguish special record types from one another such as a motor record. Select from the available types of “standard” and “motor record.” Should always return a value. No guessing should be required to use this method

Returns

String name of type

Return type

string

apply_config_validation()

Apply the guessing module. Assert that the proper fields exist.

Returns

a list of the missing requirements

Return type

List

cfg_as_dict()

Produce a jinja-template-compatible dictionary describing this RecordPackage.

Returns

return a dict. Keys are the fields of the jinja template. Contains special ‘field’ key where the value is a dictionary with f_name and f_set as the key/value pairs respectively.

Return type

dict

static generate_files(records)
Parameters

records (list) – list of all incoming Record objects

Returns

The Jinja-made output of the full resulting file.

Return type

str

generate_naive_config()

Create config dictionary from current chain. Move from lowest to highest level to create config (highest level has highest precedence).

Overwrites self.cfg

Returns

Return type

None

generate_record_entry()

apply all jinja functionality to create the template return dict w/ filename as key for each entry in the iterable

guess_DTYP()

Add field specifying DTYP.

The following is taken from the EPICS wiki: “This field specifies the device type for the record. Each record type has its own set of device support routines which are specified in devSup.ASCII. If a record type does not have any associated device support, DTYP and DSET are meaningless.”

Returns

Return a boolean that is True iff a change has been made.

Return type

bool

guess_FTVL()

Add datatype specification field for waveforms

guess_INP_OUT()

Construct, add, INP or OUT field Fields will have this form: “@asyn($(PORT),0,1)ADSPORT=851/Main.bEnableUpdateSine=”

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

guess_NELM()

Add data length secification for waveforms

guess_OZ_NAM()

Add ONAM and ZNAM fields for booleans

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

guess_PREC()

Add precision field for the ai/ao type

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

guess_SCAN()

add field for SCAN field

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

guess_all()

Cycle through guessing methods until none can be applied. guessing methods is a list of functions.

guess_common()

Add fields that are common to all records (PINI, TSE)

guess_io()

Add information indicating io direction if it is not provided.

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

guess_type()

Add information indicating record type (e.g. ai, bo, waveform, etc.)

Returns

Return a boolean that is true iff a change has been made.

Return type

bool

motor_record_as_dict()

Produce a jinja-template-compatible dict describing this RecordPackage

Returns

return a dict. Keys are the fields of the jinja template. Contains special ‘field’ key where the value is a dictionary with f_name and f_set as the key/value pairs respectively.

Return type

dict

render_record()
Returns

Jinja rendered entry for this BaseRecordPackage

Return type

string

render_to_string()

Create the individual record to be inserted in the DB file. To be returned as a string. redundant?