Skip to content

egse.counter

This module manages files that have a counter in their filename.

Functions:

Name Description
counter_exists

Returns True if the given file exists, False otherwise.

counter_filename

Creates an absolute filename to be used as a counter file. A counter file usually has a 'count' extension

get_next_counter

Read the counter from a dedicated file, add one and save the counter back to the file.

new_counter

Create a counter based on the files that already exist for the given pattern.

counter_exists

counter_exists(filename)

Returns True if the given file exists, False otherwise.

Parameters:

Name Type Description Default
filename Path

path of the counter file

required

Returns:

Type Description
bool

True if the given filename exists, False otherwise.

Note

No checking is done if the file is indeed a counter file, i.e. if it contains the correct content. So, this function basically only checks if the given Path exists and if it is a regular file.

counter_filename

counter_filename(location, filename)

Creates an absolute filename to be used as a counter file. A counter file usually has a 'count' extension but that is not enforced by this module. The location can be a relative path, even '.' or '..' are accepted.

Parameters:

Name Type Description Default
location Path

the location of the counter file.

required
filename Path | str

the name of the counter file, use the '.count' extension.

required

Returns:

Type Description
Path

An absolute filename.

Note

If the file doesn't exist, it is NOT created.

determine_counter_from_dir_list

determine_counter_from_dir_list(
    location, pattern, index=-1
)

Determine counter for a new file at the given location and with the given pattern. The next counter is determined from the sorted list of files that match the given pattern.

Parameters:

Name Type Description Default
location

Location where the file should be stored.

required
pattern

Pattern for the filename.

required
index int

the location of the counter in the filename after it is split on '_' [default=-1]

-1

Returns:

Type Description
int

The value of the next counter, 1 if no previous files were found or if an error occurred.

get_next_counter

get_next_counter(filename)

Read the counter from a dedicated file, add one and save the counter back to the file.

Parameters:

Name Type Description Default
file_path

full pathname of the file that contains the required counter

required

Returns:

Type Description
int

The value of the next counter, 1 if no previous files were found or if an error occurred.

Note

This will create the counter file if it doesn't exist.

new_counter

new_counter(filename, pattern)

Create a counter based on the files that already exist for the given pattern.

Parameters:

Name Type Description Default
filename Path

the name of the counter file

required
pattern str

a pattern to match the filenames

required

Returns:

Type Description
int

The next counter value as an integer.