vtool.util_file

Class Summary

WatchDirectoryThread() Not developed fully.
FileManager(filepath[, skip_warning]) Convenience to deal with file write/read.
ReadFile(filename) Class to deal with reading a file.
WriteFile(filepath)
VersionFile(filepath) Convenience to version a file or folder.
SettingsFile()
FindUniquePath(directory)
ParsePython(filepath) This needs to be replaced by something that uses the AST instead.
PythonScope(name)

Function Summary

get_vetala_version()
get_vetala_directory()
get_basename(directory) Get the last part of a directory name.
get_basename_no_extension(filepath) Get the last part of a directory name.
get_dirname(directory) Given a directory path, this will return the path above the last thing in the path.
get_user_dir() Get the path to the user directory.
get_temp_dir() Get path to the temp directory.
get_cwd() Get the current working directory.
get_files(directory) Get files found in the directory.
get_folders_without_prefix_dot(directory[, …])
get_folders(directory[, recursive]) Get folders found in the directory.
get_files_and_folders(directory) Get files and folders found in the directory.
get_folders_date_sorted(directory) Get folders date sorted found in the directory.
get_files_date_sorted(directory[, extension]) Get files date sorted found in the directory.
get_latest_file_at_path(path)
get_latest_file(file_paths[, …])
get_files_with_extension(extension, directory) Get files that have the extensions.
get_size(path[, round_value])
get_filesize(filepath[, round_value]) Get the size of a file.
get_folder_size(path[, round_value])
get_last_modified_date(filepath) Get the last date a file was modified.
get_user() Get the current user.
get_file_text(filepath) Get the text directly from a file.
get_file_lines(filepath) Get the text from a file.
get_text_lines(text) Get the text from a file.
get_permission(filepath)
exists(directory)
is_dir(directory)
returns:bool
is_file(filepath)
returns:bool
is_file_in_dir(filename, directory)
param filename:Filename including path.
is_same_date(file1, file2) Check if 2 files have the same data.
is_same_text_content(file1, file2)
inc_path_name(directory[, padding]) Add padding to a name if it is not unique.
open_browser(filepath) Open the file browser to the path specified.
remove_extension(path)
get_common_path(path1, path2)
remove_common_path(path1, path2) Given path1 = pathA/pathB
remove_common_path_simple(path1, path2) This just subtracts a string that is the same at the beginning.
get_installed_programs() Not working at all, very hacky
fix_slashes(directory) Fix slashes in a path so the are all /
set_windows_slashes(directory) Set all the slashes in a name so they are all :returns: The new directory path.
join_path(directory1, directory2) Append directory2 to the end of directory1
rename(directory, name[, make_unique])
param directory:
 Full path to the directory to rename.
move(path1, path2) Move the folder or file pointed to by path1 under the directory path2
comment(filepath, comment, comment_directory) Add a comment to comments.txt
get_comments(comment_directory[, …]) Get the comments from a comments.txt file.
write_lines(filepath, lines[, append]) Write a list of text lines to a file.
create_dir(name[, directory, make_unique])
param name:The name of the new directory.
delete_dir(name, directory) Delete the folder by name in the directory.
delete_read_only_error(action, name, exc) Helper to delete read only files.
refresh_dir(directory) Delete everything in the directory.
create_file(name, directory[, make_unique])
param name:The name of the new file.
delete_file(name, directory) Delete the file by name in the directory.
copy_dir(directory, directory_destination[, …]) Copy the directory to a new directory.
copy_file(filepath, filepath_destination) Copy the file to a new directory.
delete_pyc(python_script) Delete the .pyc file the corresponds to the .py file
import_python_module(module_name, directory)
source_python_module(code_directory)
load_python_module(module_name, directory) Load a module by name and return its instance.
get_package_path_from_name(module_name[, …])
get_line_class_map(lines)
get_line_imports(lines) This needs to be replaced by AST stuff.
get_defined(module_path[, name_only]) Get classes and definitions from the text of a module.
get_defined_classes(module_path)
get_ast_function_name_and_args(function_node)
get_ast_function_args(function_node)
get_ast_class_sub_functions(module_path, …)
get_ast_class_members(class_node[, parents, …])
get_ast_assignment(text, line_number, assignment)
launch_maya(version[, script]) Needs maya installed.
launch_nuke(version[, command]) Needs nuke installed.
run_ffmpeg() Needs ffmpeg installed.

Functions

get_vetala_version()
get_vetala_directory()
get_basename(directory)

Get the last part of a directory name. If the name is C:/goo/foo, this will return foo.

Parameters:directoroy (str) – A directory path.
Returns:The last part of the directory path.
Return type:str
get_basename_no_extension(filepath)

Get the last part of a directory name. If the name is C:/goo/foo.py, this will return foo.

Parameters:directoroy (str) – A directory path.
Returns:The last part of the directory path, without any extensions.
Return type:str
get_dirname(directory)

Given a directory path, this will return the path above the last thing in the path. If C:/goo/foo is give, C:/goo will be returned.

Parameters:directory (str) – A directory path.
Returns:The front portion of the path.
Return type:str
get_user_dir()

Get the path to the user directory.

Returns:The path to the user directory.
Return type:str
get_temp_dir()

Get path to the temp directory.

Returns:The path to the temp directory.
Return type:str
get_cwd()

Get the current working directory.

Returns:The path to the current working directory.
Return type:str
get_files(directory)

Get files found in the directory.

Parameters:directory (str) – A directory path.
Returns:A list of files in the directory.
Return type:list
get_folders_without_prefix_dot(directory, recursive=False, base_directory=None)
get_folders(directory, recursive=False)

Get folders found in the directory.

Parameters:directory (str) – A directory path.
Returns:A list of folders in the directory.
Return type:list
get_files_and_folders(directory)

Get files and folders found in the directory.

Parameters:directory (str) – A directory path.
Returns:A list of files and folders in the directory.
Return type:list
get_folders_date_sorted(directory)

Get folders date sorted found in the directory.

Parameters:directory (str) – A directory path.
Returns:A list of folders date sorted in the directory.
Return type:list
get_files_date_sorted(directory, extension=None)

Get files date sorted found in the directory.

Parameters:directory (str) – A directory path.
Returns:A list of files date sorted in the directory.
Return type:list
get_latest_file_at_path(path)
get_latest_file(file_paths, only_return_one_match=True)
get_files_with_extension(extension, directory, fullpath=False)

Get files that have the extensions.

Parameters:
  • extension (str) – eg. .py, .data, etc.
  • directory (str) – A directory path.
  • fullpath (bool) – Wether to returh the filepath or just the file names.
Returns:

A list of files with the extension.

Return type:

list

get_size(path, round_value=2)
get_filesize(filepath, round_value=2)

Get the size of a file.

Parameters:filepath (str) –
Retrun
float: The size of the file specified by filepath.
get_folder_size(path, round_value=2)
get_last_modified_date(filepath)

Get the last date a file was modified.

Parameters:filepath (str) –
Returns:A formatted date and time.
Return type:str
get_user()

Get the current user.

Returns:The name of the current user.
Return type:str
get_file_text(filepath)

Get the text directly from a file. One long string, no parsing.

get_file_lines(filepath)

Get the text from a file.

Parameters:filepath (str) – The filename and path.
Returns:str
get_text_lines(text)

Get the text from a file. Each line is stored as a different entry in a list.

Parameters:text (str) – Text from get_file_lines
Returns:list
get_permission(filepath)
exists(directory)
is_dir(directory)
Returns:bool
is_file(filepath)
Returns:bool
is_file_in_dir(filename, directory)
Parameters:
  • filename (str) – Filename including path.
  • directory (str) – Directory name including path.
Returns:

Wether the file is in the directory.

Return type:

bool

is_same_date(file1, file2)

Check if 2 files have the same data.

Parameters:
  • file1 (str) – Filename including path.
  • file2 (str) – Filename including path.
Returns:

bool

is_same_text_content(file1, file2)
inc_path_name(directory, padding=0)

Add padding to a name if it is not unique.

Parameters:
  • directory (str) – Directory name including path.
  • padding (int) – Where the padding should start.
Returns:

The new directory with path.

Return type:

str

open_browser(filepath)

Open the file browser to the path specified. Currently only works in windows.

Parameters:filepath (str) – Filename with path.
remove_extension(path)
get_common_path(path1, path2)
remove_common_path(path1, path2)

Given path1 = pathA/pathB and path2 = pathA/pathC

or path1 = pathA and path2 = pathA/pathC

return pathC

remove_common_path_simple(path1, path2)

This just subtracts a string that is the same at the beginning. path1 gets subtracted from path2

get_installed_programs()

Not working at all, very hacky

fix_slashes(directory)

Fix slashes in a path so the are all /

Returns:The new directory path.
Return type:str
set_windows_slashes(directory)

Set all the slashes in a name so they are all :returns: The new directory path. :rtype: str

join_path(directory1, directory2)

Append directory2 to the end of directory1

Returns:The combined directory path.
Return type:str
rename(directory, name, make_unique=False)
Parameters:
  • directory (str) – Full path to the directory to rename.
  • name (str) – The new name.
  • make_unique (bool) – Wether to add a number to the name to make it unique, if needed.
Retrun
str: The path of the renamed folder, or False if rename fails.
move(path1, path2)

Move the folder or file pointed to by path1 under the directory path2

Parameters:
  • path1 (str) – File or folder including path.
  • path2 (str) – Path where path1 should move to.
Returns:

Wether the move was successful.

Return type:

bool

comment(filepath, comment, comment_directory)

Add a comment to comments.txt

Parameters:
  • filepath (str) – Filename and path of the file that is being commented about.
  • comment (str) – The comment
  • comment_directoyr (str) – Directory where the comments.txt file should be saved.
get_comments(comment_directory, comment_filename=None)

Get the comments from a comments.txt file.

Parameters:
  • comment_directory (str) – Directory where the comments.txt file lives.
  • comment_filename (str) – The name of the comment file. By default comments.txt
Returns:

comment dict, keys are filename, and value is (comment, user)

Return type:

dict

write_lines(filepath, lines, append=False)

Write a list of text lines to a file. Every entry in the list is a new line.

Parameters:
  • filepath (str) – filename and path
  • lines (list) – A list of text lines. Each entry is a new line.
  • append (bool) – Wether to append the text or if not replace it.
create_dir(name, directory=None, make_unique=False)
Parameters:
  • name (str) – The name of the new directory.
  • make_unique (bool) – Wether to pad the name with a number to make it unique. Only if the name is taken.
Returns:

The folder name with path. False if create_dir failed.

Return type:

str

delete_dir(name, directory)

Delete the folder by name in the directory.

Parameters:
  • name (str) – The name of the folder to delete.
  • directory (str) – The dirpath where the folder lives.
Returns:

The folder that was deleted with path.

Return type:

str

delete_read_only_error(action, name, exc)

Helper to delete read only files.

refresh_dir(directory)

Delete everything in the directory.

create_file(name, directory, make_unique=False)
Parameters:
  • name (str) – The name of the new file.
  • make_unique (bool) – Wether to pad the name with a number to make it unique. Only if the name is taken.
Returns:

The filename with path. False if create_dir failed.

Return type:

str

delete_file(name, directory)

Delete the file by name in the directory.

Parameters:
  • name (str) – The name of the file to delete.
  • directory (str) – The dirpath where the file lives.
Returns:

The filepath that was deleted.

Return type:

str

copy_dir(directory, directory_destination, ignore_patterns=[])

Copy the directory to a new directory.

Parameters:
  • directory (str) – The directory to copy with path.
  • directory_destination (str) – The destination directory.
  • ignore_patterns (list) – Add txt, py or extensions to ingore them from copying.
  • if py is added to the ignore patterns list, all *.py files will be ignored from the copy. (Eg.) –
Returns:

The destination directory

Return type:

str

copy_file(filepath, filepath_destination)

Copy the file to a new directory.

Parameters:
  • filepath (str) – The file to copy with path.
  • filepath_destination (str) – The destination directory.
Returns:

The destination directory

Return type:

str

delete_pyc(python_script)

Delete the .pyc file the corresponds to the .py file

import_python_module(module_name, directory)
source_python_module(code_directory)
load_python_module(module_name, directory)

Load a module by name and return its instance.

Parameters:
  • module_name (str) – The name of the module found in the directory.
  • directory (str) – The directory path where the module lives.
Returns:

The module instance. With the module instance you can access programattically functions and attributes of the modules.

Return type:

module instance

get_package_path_from_name(module_name, return_module_paths=False)
get_line_class_map(lines)
get_line_imports(lines)

This needs to be replaced by AST stuff.

get_defined(module_path, name_only=False)

Get classes and definitions from the text of a module.

get_defined_classes(module_path)
get_ast_function_name_and_args(function_node)
get_ast_function_args(function_node)
get_ast_class_sub_functions(module_path, class_name)
get_ast_class_members(class_node, parents=[], skip_list=None)
get_ast_assignment(text, line_number, assignment)
launch_maya(version, script=None)

Needs maya installed. If maya is installed in the default directory, will launch the version specified.

launch_nuke(version, command=None)

Needs nuke installed. If nuke is installed in default path, it will launch the version specified.

run_ffmpeg()

Needs ffmpeg installed.