Dynamically updating configurations.yml

Introduction

On this page, we will cover the following:

  1. Adding a new dependent field to exclude_file_ext and file dynamic choice fields
  2. Updating the local template.json to grab the new field
  3. Using the newly added field

Step 1 - Adding a new dependent field to exclude_file_ext and file dynamic choice fields

The exclude_file_ext dynamic choice command runs in the same directory as the workspace, which is why the output shows uip, vscode, cfg, and py. To grab the file extensions in some other directory, we need a new field.

At the time of writing this document, the Controller does NOT allow setting the runtime directory when executing a dynamic choice command. To keep it consistent, the VSCode Plugin also doesn’t allow this. Thus, the best way to set a target directory is to add a new field and have exclude_file_ext depend on it.

Using the UIP: Push All command (See Extension Development), push the extension and template to the Controller. Once uploaded, add a new text field called target_directory as shown below:


Then, double-click on the
exclude_file_ext field and set Text Field 2 as a dependent field:


Repeat the same for the
file field.

Step 2 - Updating the local template.json to grab the new field

Now, go back to VSCode and run UIP: Pull. You should see the following changes:


Whenever
template.json changes, the .schema file under .uip/debug also changes. .schema is used by configurations.yml to validate the field types, offer code-completion etc. In this case, updating template.json results in some problems because exclude_file_ext was changed to depend on target_directory, but in configurations.yml, we haven’t specified a value for it yet. Go ahead and add the target_directory field (the value can be any directory, as long as it has some files in it) as shown below:

configurations.yml
properties:
  agent:
    log_level: Info
api:
  dynamic_choice_commands:
    exclude_file_ext:
      - name: efe1
        log_level: Inherited
		fields:
			target_directory: 
      - name: efe2
        log_level: Trace
		fields:
			target_directory: 

Step 3 - Using the newly added field

Make the following changes to list_of_file_extensions method in extension.py to use the new target_directory field:

extension.py::list_of_file_extensions
@dynamic_choice_command('exclude_file_ext')
def list_of_file_extensions(self, fields):
    print('ENTRY: list_of_file_extensions')
    logger.info('ENTRY: list_of_file_extensions')
 
    exts = []

	target_dir = fields.get('target_directory', os.getcwd())
 
    for f in os.listdir(target_dir):
        if '.' in f:
            ext = f.split('.')[-1]
            if ext not in exts:
                exts.append(ext)
 
    print('EXIT: list_of_file_extensions')
    logger.info('EXIT: list_of_file_extensions')
 
    return ExtensionResult(
        values=exts
    )


Now, press
F5 (efe1 should still be the default) and it should hit the breakpoint. Let the dynamic choice command run to completion, and the output should show all the file extensions in target_directory. In my case, the output is:


Now, let’s add a couple of configurations for the
file dynamic choice command, which will retrieve the list of files in target_directory:


configurations.yml
properties:
  agent:
    log_level: Info
api:
  dynamic_choice_commands:
    exclude_file_ext:
      - name: efe1
        log_level: Inherited
        fields:
          target_directory: 
      - name: efe2
        log_level: Trace
        fields:
          target_directory: 
    file:
      - name: list_all_files
        log_level: Inherited
        fields:
          exclude_file_ext:
            - ""
          target_directory: 
      - name: exclude_json_files
        log_level: Inherited
        fields:
          exclude_file_ext:
            - json
          target_directory: 

The list_all_files configuration should list all the files in target_directory whereas exclude_json_files will list all files except the ones that end in .json. You may have to change exclude_json_files depending on the types of file that your target_directory contains.


Modify the list_files method in extension.py to use the target_directory as well:

extension.py::list_files
@dynamic_choice_command('file')
def list_files(self, fields):
    to_exclude = fields.get('exclude_file_ext', [])
    files = []

	target_dir = fields.get('target_directory', os.getcwd())

    for f in os.listdir(target_dir):
        if f.split('.')[-1] not in to_exclude:
            files.append(f)

    return ExtensionResult(
        values=files
    )


Now, go ahead and launch
list_all_files:



Since a breakpoint wasn't set in list_files, the debugger will attach and finish almost immediately. Upon completion, press Shift+Alt+3, and you should see a listing of all the files in your target_directory.
Do the same with exclude_json_files (or whatever you named it), and it should show all the files except the ones that end in .json.

< Previous     Next >