Files API¶
Nickname CSV, DataView CDV, PLC data, installed modules, channel parameters, and the PLC name.
pyclickplc.read_csv ¶
read_csv(
path: str | Path,
*,
required: Iterable[str] | str = REQUIRED_COLUMNS,
) -> AddressRecordMap
Read a nickname CSV file into AddressRecords.
The CSV has columns: Address, Data Type, Nickname, Initial Value,
Retentive, Address Comment. A file that starts with a UTF-8 byte order
mark is read as UTF-8. A file without one is read as UTF-8 if it is valid
UTF-8, and as cp1252 if it is not. CLICK exports cp1252. A cp1252 file
whose non-ASCII bytes are also valid UTF-8 is therefore read as UTF-8. If
the only non-ASCII text in the file is é, read_csv returns é.
In a cp1252 file, read_csv reads the bytes 0x81, 0x8D, 0x8F, 0x90 and
0x9D as U+0081, U+008D, U+008F, U+0090 and U+009D. Windows code page 1252
maps them the same way.
From a UTF-8 file, read_csv returns a character that cp1252 does not
have unchanged. CLICK's import replaces that character with a near
character or ?.
Nickname, Address Comment and a Text initial value are kept as written,
spaces included. A Text initial value may be quoted or unquoted. CLICK exports it
without quotes, even when it is a comma, a double quote, a line feed or a
carriage return. read_csv reads such a value correctly. Retentive is on for Yes,
True or 1 in any case, and off for anything else. Blank rows are
skipped. If a nickname repeats, the read does not fail and .addr still
works, but every .tag lookup raises ValueError.
The result's columns lists the columns the file had. A missing
optional column, or a blank cell in one, leaves the default. A blank Data
Type means the bank's own type.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required column is missing, a row has a non-blank cell past the header's columns, a row's Address is not a CLICK address, its Data Type is not one CLICK writes (BIT, INT, INT2, FLOAT, HEX, TEXT, in any case) or not the type of its bank, or the file is UTF-16, starts with a UTF-8 byte order mark but is not UTF-8, or is not CSV. The message names the file and, for a row, its line. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the CSV file. |
required |
required
|
Iterable[str] | str
|
Columns the file must have, or one column name. Defaults to Address and Nickname. |
REQUIRED_COLUMNS
|
Returns:
| Type | Description |
|---|---|
AddressRecordMap
|
AddressRecordMap keyed by addr_key (int) with |
pyclickplc.write_csv ¶
write_csv(
path: str | Path,
records: Mapping[int, AddressRecord]
| Iterable[AddressRecord],
) -> int
Write AddressRecords to a nickname CSV file.
A record is written only if it has a nickname or a comment, or if its initial value or Retentive setting is not the default. Records are sorted by memory type order then address. As in CLICK's export, the file is cp1252 without a byte order mark, lines end in CRLF and an initial value is written without quotes. The function quotes an initial value that holds a comma, double quote or line break. It also quotes a Text value that starts or ends with a space, tab or other character up to 0x20. CLICK exports a Text value that is a space, comma or double quote without quotes, but it can import that value only in quotes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to write the CSV file. |
required |
records
|
Mapping[int, AddressRecord] | Iterable[AddressRecord]
|
Address records to write. Accepts a mapping keyed by addr_key or any iterable of AddressRecord values. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a nickname, comment or initial value has a character that cp1252 does not have, or an initial value is a line feed, a carriage return or the control character 1Ah. The message names the address, and the function writes no file. |
Returns:
| Type | Description |
|---|---|
int
|
Number of rows written. |
pyclickplc.format_csv ¶
Return the nickname CSV text that write_csv writes for records.
If you write the file yourself, encode the text as CSV_ENCODING and
write it unchanged. Python's cp1252 codec cannot encode five characters
that the text can hold. The lines already end in CRLF.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a nickname, comment or initial value has a character that cp1252 does not have, or an initial value is a line feed, a carriage return or the control character 1Ah. The message names the address. |
pyclickplc.first_non_cp1252_char ¶
Return the first character in text that cp1252 does not have, or None.
A nickname CSV is cp1252, so it cannot hold that character. Here cp1252
means Windows code page 1252. Unlike Python's cp1252 codec, it has
U+0081, U+008D, U+008F, U+0090 and U+009D. format_csv
and write_csv raise ValueError for a nickname, comment or initial
value that has one. validate_nickname and validate_comment reject
a nickname or comment that has one. An editor can call this function to
flag the cell before it saves.
pyclickplc.read_mdb_csv ¶
Read CLICK's Address.csv into AddressRecords.
CLICK writes this file beside its project database, as UTF-8 with a byte order mark, with columns AddrKey, MemoryType, Address, DataType, Nickname, Use, InitialValue, Retentive, Comment. Address is the MDB address (XD1 is 2), and Use and Retentive are numbers. DataType is not read. Every record gets its bank's type. Only rows with content are kept. The function skips a row whose MemoryType or Address pyclickplc does not know. It also skips an XD/YD slot that CLICK does not show (MDB 3, 5, ... 15).
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is not UTF-8 or not CSV. The message names the file and, for a row, its line. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the Address.csv file. |
required |
Returns:
| Type | Description |
|---|---|
AddressRecordMap
|
AddressRecordMap keyed by addr_key (int) with |
AddressRecordMap
|
|
pyclickplc.AddressRecordMap ¶
pyclickplc.make_address_record ¶
make_address_record(
address: str,
*,
nickname: str = "",
comment: str = "",
initial_value: str = "",
retentive: bool | None = None,
used: bool | None = None,
) -> AddressRecord
Create an AddressRecord from a display address such as "X001".
The data type comes from the bank. So does retentive, unless you pass it.
pyclickplc.read_cdv ¶
Read a CDV file into a DataViewFile model.
pyclickplc.write_cdv ¶
Write a DataViewFile or list of DataViewRecords to a CDV path.
pyclickplc.verify_cdv ¶
verify_cdv(
path: Path | str,
rows: list[DataViewRecord],
has_new_values: bool | None = None,
) -> list[str]
Compare rows to a CDV file and return a list of the differences.
Values are compared as Python values, not as the text in the file. If has_new_values is None, the function expects the file to be marked as having new values when any non-empty row has a new value.
pyclickplc.check_cdv_file ¶
Validate a single CDV file and return issue strings.
pyclickplc.DataViewFile
dataclass
¶
CDV file model with row data in native Python types.
value_to_display
staticmethod
¶
Render a native value as a display string.
validate_display
staticmethod
¶
Validate display text for a target data type.
try_parse_display
staticmethod
¶
Parse a display string to a native value without raising.
validate_row_display
staticmethod
¶
Validate a user edit for a specific row.
set_row_new_value_from_display
staticmethod
¶
Parse a display string and set it as the row's native new_value.
Raise ValueError if the row is read-only or the string is not valid for its data type.
load
classmethod
¶
Load a CDV file and parse new values into native Python types.
save ¶
Write the rows to a CDV file at path, or at self.path if path is None.
Each new value is converted from its Python type to the text that the CDV file
stores. If the rows and header have not changed since the last load or
save, the bytes of that file are written again unchanged.
verify ¶
Compare this in-memory dataview to a CDV file on disk.
pyclickplc.DataViewRecord
dataclass
¶
Represents a single row in a CLICK DataView.
A dataview row contains an address to monitor and optionally a new value
to write to that address. The nickname and comment are for display only.
The CDV file does not store them, and pyclickplc does not fill them in.
== ignores them. To build a record from a display address such as
"DS1", use make_dataview_record(...).
memory_type
property
¶
Get the memory type prefix (X, Y, DS, etc.) or None if invalid.
address_number
property
¶
Get the address number as a display string, or None if invalid.
from_address
classmethod
¶
Build a DataViewRecord from a display address string.
update_data_type ¶
Update the DataType based on the current address.
Returns:
| Type | Description |
|---|---|
bool
|
True if data_type was updated, False if address is invalid. |
pyclickplc.make_dataview_record ¶
Create a DataViewRecord from a display address such as "DS1".
The data type comes from the address's bank.
pyclickplc.get_data_type_for_address ¶
Get the DataType for an address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
Address string like "X001", "DS1" |
required |
Returns:
| Type | Description |
|---|---|
DataType | None
|
DataType or None if address is invalid. |
pyclickplc.validate_new_value ¶
Validate a user-entered display string for the New Value column.
pyclickplc.Module
dataclass
¶
Installed module identity and physical I/O counts.
A count of zero means the module has no I/O of that kind. Counts describe hardware capacity, not currently assigned channel addresses.
pyclickplc.Modules
dataclass
¶
Installed CPU, CPU slots 0/1, and expansion positions 1..8.
Empty positions are omitted from the read-only mappings. CPU is None if
the project does not identify one. Power supplies are not included.
unknown lists each module whose ID is not in pyclickplc's catalog, in
physical order: CPU, slots, then expansions. Such a module is not in cpu,
slots or expansions.
pyclickplc.UnknownModule
dataclass
¶
A module whose ID is not in pyclickplc's module catalog.
location and position number the module as in ModuleIO: position 0 for
the CPU, 0 or 1 for a CLICK PLUS slot, and 1 to 8 for an expansion. module_id is
the ID that Project.ini lists.
pyclickplc.read_modules ¶
Read the installed CPU and modules, with their I/O counts, from a Project.ini file.
CLICK does not need to be installed. slots uses keys 0 and 1, and
expansions uses keys 1 through 8. Empty positions are left out. So is a slot
that the installed CPU does not have, even if the file still lists a module for it.
This function does not read channel parameters, so a malformed channel record does
not stop it.
If a module's ID is not in pyclickplc's catalog, the function leaves the module
out of cpu, slots and expansions and lists it in unknown. If the
CPU's ID is unknown, cpu is None and the function reads no option slots,
because the CPU model sets how many slots there are.
Raises OSError for unreadable files and ValueError for malformed INI data or a
module ID that is not an integer. If the file names no CPU, cpu is None. If
it lists no installed modules, the mappings are empty.
pyclickplc.ChannelParameters
dataclass
¶
Normalized DF addresses assigned to analog input and output channels.
pyclickplc.read_channel_parameters ¶
Read analog channel assignments for the installed CLICK hardware.
Covers built-in CPU analog channels, expansion positions 1..8, and CLICK PLUS CPU slots 0 and 1. Input and output addresses come back in separate sets. Scaling and electrical ranges are not read. Empty assignments are ignored, and so are settings left behind by modules that were removed or are not analog.
The function skips a module whose ID is not in pyclickplc's catalog and does not
read that module's channel data. read_modules(path).unknown lists the skipped
modules.
Returns empty sets if there are no assigned analog channels. Raises OSError for unreadable files and ValueError for malformed INI data, a module ID that is not an integer, or malformed channel data. No CLICK installation is required.
pyclickplc.ModuleIO
dataclass
¶
One installed module and the addresses of its I/O points.
Position is 0 for the CPU, 0/1 for CLICK PLUS slots, and 1..8 for
expansions. inputs and outputs have one X or Y address for each discrete
point of the module. Each analog tuple has one DF address per channel, in channel
order, or None for a channel with no assignment.
pyclickplc.read_io_layout ¶
Read every installed module with its discrete and analog addresses.
The function returns modules in physical order: CPU, CLICK PLUS slots, then
expansions. It omits empty positions. Discrete addresses are fixed by
position. Analog channels use the DF assignments that
read_channel_parameters reads, one entry per channel. Power supplies
are not included.
The function leaves out a module whose ID is not in pyclickplc's catalog.
read_modules(path).unknown lists the modules that it left out.
Raises OSError for unreadable files and ValueError for malformed INI data, a module ID that is not an integer, or malformed channel data. No CLICK installation is required.
pyclickplc.read_plc_name ¶
Read the PLC name from a CLICK Project.ini file.
The name is [PLCName] PLCName=. CLICK 4 writes it as [Sec20] Key1=.
Section and key names match regardless of case. If a section or key appears
more than once, the function uses the first one. CLICK 4.00 also uses the first
of two [Sec20] sections and the first of two Key1 keys. Returns None if
the project has no PLC name.
If a CLICK 4 file has a name in both [Sec20] and [PLCName], the function
returns the one in [Sec20]. CLICK 4.00 does the same. If only [PLCName]
has a name, the function returns it, so that a project named by pyclickplc 0.9.1
or older keeps its name. CLICK 4.00 shows no name for such a file. If you pass
that name to write_plc_name, it writes the name to [Sec20], where CLICK
4.00 shows it.
Raises OSError for unreadable files and ValueError for malformed INI data. No CLICK installation is required.
pyclickplc.write_plc_name ¶
Set the PLC name in a CLICK Project.ini file.
In a CLICK 4 file, the name goes in [Sec20] Key1=, where CLICK 4.00 shows
it. A [PLCName] section in the same file stays as it was. In any other file,
the name goes in [PLCName] PLCName=. The function changes only that line, or
adds the line if the file does not have it. Every other byte stays as it was.
The function checks the name with validate_click_name and writes it without
surrounding whitespace. It writes the new file to a temporary file in the same
folder, which then replaces Project.ini in one step. If the file already holds
the name there, the function leaves the file alone.
Raises ValueError for an invalid name, for malformed INI data, and for a line indented under an option. Readers may take that line as part of the option's value or as a line of its own, so remove it or outdent it first. Raises OSError if the file can't be read or replaced. If it raises, the file is unchanged.
pyclickplc.read_plc_data ¶
read_plc_data(
path: str | Path, *, skip_default: bool = False
) -> dict[str, bool | int | float | str]
Parse a Click PLC 'Read Data from PLC' CSV dump.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the CSV file exported from Click Programming Software via PLC > Read Data from PLC... > All > Save. |
required |
skip_default
|
bool
|
If True, omit addresses whose value equals the bank
default ( |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, bool | int | float | str]
|
Dict mapping normalized address strings (e.g. |
dict[str, bool | int | float | str]
|
to native Python values ( |
dict[str, bool | int | float | str]
|
|
dict[str, bool | int | float | str]
|
a DF value it can't display. This function returns |
dict[str, bool | int | float | str]
|
cell that CLICK writes as |
dict[str, bool | int | float | str]
|
( |
dict[str, bool | int | float | str]
|
CLICK writes a TXT comma without quotes, so a TXT row that holds a comma has a |
dict[str, bool | int | float | str]
|
cell too many. This function rejoins the cells. If the row also holds an empty |
dict[str, bool | int | float | str]
|
address, the row may fit more than one reading. Then each address whose value |
dict[str, bool | int | float | str]
|
differs between the readings is unknown. An unknown address may hold a comma, |
dict[str, bool | int | float | str]
|
nothing, or a character that the comma moved. |
Raises:
| Type | Description |
|---|---|
PlcDataReadError
|
Some TXT addresses are unknown. The error names them and holds
the values of the other addresses. It is a |
ValueError
|
A row has more or fewer cells than the header has addresses for it, and no reading of TXT commas explains the difference. The message names the row's first address. |
pyclickplc.PlcDataReadError ¶
Bases: ValueError
Raised when read_plc_data can't tell what some TXT addresses in the file hold.
read_plc_data reads the whole file before it raises this error.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
Every other address in the file, as |
|
addresses |
The TXT addresses whose value |
pyclickplc.write_plc_data ¶
write_plc_data(
path: str | Path,
data: Mapping[str, bool | int | float | str],
*,
banks: Iterable[str] | None = None,
) -> None
Write a Click PLC data CSV to load via PLC > Write Data into PLC > File Select.
Warning
CLICK's Write Data into PLC writes every address of each memory type that is in the file. It clears an address that the file leaves blank. This function writes every address of each bank that it includes, with the bank's default where data has no value. To change some addresses and keep the rest, read the PLC's data first, then write it back with your changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
data
|
Mapping[str, bool | int | float | str]
|
Dict mapping normalized address strings (e.g. |
required |
banks
|
Iterable[str] | None
|
Bank names to include, written in the order given (e.g.
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
A DF value is not a number in the float32 range, or is a NaN or an infinity. Or a TXT value is not one character that cp1252 has, or is a comma. The message names the address. CLICK's editor can't enter a NaN or an infinity, though a program or a Modbus write can still put one in the PLC. Nobody has tested whether CLICK's Write Data into PLC reads a TXT comma. If this function raises, it writes nothing. |