The file_utils module is helpful when you want to write a function that needs to write files on the server. Check the user_interface package for functions for storing/reading files between server and client.

Functions

FunctionParametersReturn ValueDescription
ask_if_name_okay(name)name: The filename you want to useTrue or FalseShows a dialog message that asks the user if the given name is okay. Should be called with the return value from create_valid_filename
create_valid_filename(proposed_filename, convert_spaces=True, platform=None)proposed_filename: The filename you want to use

convert_spaces: If True, spaces will be converted into underscores
platform: The platform on which the file should later be created. Valid values are those described for sys.platform or None if the current server operating system is used.
Returns a sanitized version of the input for the given system or None if the filename isn't valid.Sanitize a filename and make it valid for the operating system
reset_linux_file_permissions(file_path)file_path: The path to a file the permissions for which you want to be resetTrue or False depending on whether the operation succeededSets the "read other" and "write other" flags for the given file path
reset_permissions(file_path, os_name=None)file_path: The path to a file the permissions for which you want to be reset

os_name: The name of the operating system to decide which code to run. Valid values are those described for os.name
True or False depending on whether the operation succeededThis function calls either reset_linux_file_permissions or reset_windows_file_permissions depending on the operating system PLANTA is running on to reset the permissions of the file at the given path.

This function is useful when PLANTA is running with a privileged user and you want to write files that can be read by non-privileged users.
reset_windows_file_permissions(file_path)file_path: The path to a file the permissions for which you want to be resetTrue or False depending on whether the operation succeededResets the ACL for the given file path

Examples

Using file_utils to create a valid filename from user input

from ppms.file_utils import create_valid_filename, ask_if_name_okay


def query_user_for_filename():
    ppms.ui_message_box('Please enter a filename', number_of_input=1)
    msg = ppms.msg_pop()
    user_input = msg.get_input()[0]
    
    return user_input

sanitized_filename = None
while sanitized_filename is None:
    user_input = query_user_for_filename()
    sanitized_filename = create_valid_filename(proposed_filename=user_input)

    if sanitized_filename is not None:
        if sanitized_filename == user_input:  # If the user already entered valid input we can stop asking
            break
        elif ask_if_name_okay(name=sanitized_filename):  # If the sanitized name is different from the user input we ask for permission
            break
        
        sanitized_filename = None  # We end up here when the user isn't okay with the sanitized input
        
with open(sanitized_filename, 'w') as f:
    f.write('...')
PY