ppms.file_utils
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
Function | Parameters | Return Value | Description |
|---|---|---|---|
ask_if_name_okay | name: The filename you want to use |
| Shows a dialog message that asks the user if the given |
create_valid_filename | proposed_filename: The filename you want to use | 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: The path to a file the permissions for which you want to be reset |
| Sets the "read other" and "write other" flags for the given file path |
reset_permissions | file_path: The path to a file the permissions for which you want to be reset |
| This function calls either |
reset_windows_file_permissions | file_path: The path to a file the permissions for which you want to be reset |
| Resets 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('...')