migration_helpers
The community/migration_helpers directory contains a single file, migration_helpers.py. This file provides functionality to convert CSV files into .talon-list files, which are used by Talon to define lists of words or phrases that can be recognized by voice.
Here's a breakdown of how it works:
-
CSVDatadataclass: This dataclass is used to define how a specific CSV file should be converted. It contains the following information:name: The name of the Talon list that will be generated.path: The path to the CSV file.newpath: The path where the generated.talon-listfile will be created. It can also be a function to dynamically generate the path.is_first_line_header: A boolean that indicates whether the first line of the CSV file is a header that should be skipped.is_spoken_form_first: A boolean that indicates whether the spoken form or value is first in the CSV file.custom_header: An optional function to generate a custom header for the.talon-listfile.custom_value_converter: An optional function to convert the value for the generated.talon-listfile.
-
supported_csv_fileslist: This list containsCSVDataobjects for the CSV files that are automatically migrated by the script. Each entry specifies the location of a CSV file and the corresponding.talon-listthat should be generated. For example,user.git_argumentinapps/git/git_arguments.csvbecomesapps/git/git_argument.talon-list. Theuser.system_pathslist has a lambda which generates a different file name per hostname. -
convert_csv_to_talonlist(input_csv, config)function: This function takes acsv.readerobject and aCSVDataobject and converts the data from the CSV file to the format expected by.talon-listfiles. It skips empty lines, lines starting with#and also performs the following:- Handles cases where the CSV file has one or two columns.
- Supports an optional spoken form.
- Applies custom value converters.
- Adds a header describing the list.
- Ensures that non-printable characters are escaped when generating the talon-list.
-
convert_files(csv_files_list)function: This function iterates through all.csvfiles in the repository, checking them againstsupported_csv_files. It uses theconvert_csv_to_talonlistfunction to convert the data and writes it to the correct location in a.talon-listfile and also renames the.csvfile to a.csv-converted-to-talon-listfile. It will not overwrite a.talon-listfile without making a backup, and will not try to convert CSV files that have been renamed. -
Actionsclass: This class defines themigrate_known_csv_filesaction that can be called from Talon to migrate all known CSV files and amigrate_custom_csvaction that can be called for a single custom CSV. -
on_readyfunction: This function is called when Talon is ready and will trigger the migration of known CSV files to.talon-listfiles. If the Talon action is not available yet, it will print a warning to the user.