coreutils
Misc Helper functions for pyRevit.
Examples:
Attributes
DEFAULT_SEPARATOR = ';'
module-attribute
UNICODE_NONPRINTABLE_CHARS = ['\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u200b', '\u200c', '\u200d', '\u200e', '\u200f', '\u2028', '\u2029', '\u202a', '\u202b', '\u202c', '\u202d', '\u202e', '\u202f', '\u205f', '\u2060', '\u2066', '\u2067', '\u2068', '\u2069', '\u206a', '\u206b', '\u206c\u206d', '\u206e', '\u206f']
module-attribute
SPECIAL_CHARS = {' ': '', '~': '', '!': 'EXCLAM', '@': 'AT', '#': 'SHARP', '$': 'DOLLAR', '%': 'PERCENT', '^': '', '&': 'AND', '*': 'STAR', '+': 'PLUS', ';': '', ':': '', ',': '', '"': '', '{': '', '}': '', '[': '', ']': '', '\\(': '', '\\)': '', '-': 'MINUS', '=': 'EQUALS', '<': '', '>': '', '?': 'QMARK', '.': 'DOT', '_': 'UNDERS', '|': 'VERT', '\\/': '', '\\': ''}
module-attribute
Classes
Timer()
Bases: object
Timer class using python native time module.
Examples:
12Initialize and Start Timer.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
Attributes
start = time.time()
instance-attribute
Functions
restart()
ScriptFileParser(file_address)
Bases: object
Parse python script to extract variables and docstrings.
Primarily designed to assist pyRevit in determining script configurations but can work for any python script.
Examples:
"Misc Helper functions for pyRevit." []Initialize and read provided python script.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_address |
str
|
python script file path |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
Attributes
ast_tree = None
instance-attribute
file_addr = file_address
instance-attribute
Functions
extract_node_value(node)
Manual extraction of values from node.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_docstring()
extract_param(param_name, default_value=None)
Find variable and extract its value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param_name |
str
|
variable name |
required |
default_value |
any
|
default value to be returned if variable does not exist |
None
|
Returns:
Type | Description |
---|---|
Any
|
value of the variable or None |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
FileWatcher(filepath)
Bases: object
Simple file version watcher.
This is a simple utility class to look for changes in a file based on its timestamp.
Examples:
TrueInitialize and read timestamp of provided file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
file path |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
Attributes
has_changed
property
Compare current file timestamp to the cached timestamp.
Functions
SafeDict
Bases: dict
Dictionary that does not fail on any key.
This is a dictionary subclass to help with string formatting with unknown key values.
Examples:
string = '{target} {attr} is {color}.'
safedict = SafeDict({'target': 'Apple',
'attr': 'Color'})
string.format(safedict) # will not fail with missing 'color' key
Functions
get_all_subclasses(parent_classes)
Return all subclasses of a python class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent_classes |
list
|
list of python classes |
required |
Returns:
Type | Description |
---|---|
list
|
list of python subclasses |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_sub_folders(search_folder)
Get a list of all subfolders directly inside provided folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
search_folder |
str
|
folder path |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list of subfolder names |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
verify_directory(folder)
Check if the folder exists and if not create the folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder |
str
|
path of folder to verify |
required |
Returns:
Type | Description |
---|---|
str
|
path of verified folder, equals to provided folder |
Raises:
Type | Description |
---|---|
OSError
|
on folder creation error. |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
join_strings(str_list, separator=DEFAULT_SEPARATOR)
Join strings using provided separator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
str_list |
list
|
list of string values |
required |
separator |
str
|
single separator character, defaults to DEFAULT_SEPARATOR |
DEFAULT_SEPARATOR
|
Returns:
Type | Description |
---|---|
str
|
joined string |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
cleanup_string(input_str, skip=None)
Replace special characters in string with another string.
This function was created to help cleanup pyRevit command unique names from any special characters so C# class names can be created based on those unique names.
coreutils.SPECIAL_CHARS
is the conversion table for this function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
input string to be cleaned |
required |
skip |
Container[str]
|
special characters to keep |
None
|
Examples:
"TESTATSomeSTARvalue"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_revit_instance_count()
Return number of open host app instances.
Returns:
Type | Description |
---|---|
int
|
number of open host app instances. |
run_process(proc, cwd='C:')
Run shell process silently.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proc |
str
|
process executive name |
required |
cwd |
str
|
current working directory |
'C:'
|
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
inspect_calling_scope_local_var(variable_name)
Trace back the stack to find the variable in the caller local stack.
PyRevitLoader defines revit in builtins and window in locals. Thus, modules have access to revit but not to window. This function is used to find window in the caller stack.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
variable name to look up in caller local scope |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
inspect_calling_scope_global_var(variable_name)
Trace back the stack to find the variable in the caller global stack.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
variable name to look up in caller global scope |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
make_canonical_name(*args)
Join arguments with dot creating a unique id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
str
|
Variable length argument list |
()
|
Returns:
Type | Description |
---|---|
str
|
dot separated unique name |
Examples:
"somename.someid.txt"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_canonical_parts(canonical_string)
Splots argument using dot, returning all composing parts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
canonical_string |
str
|
Source string e.g. "Config.SubConfig" |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list of composing parts |
Examples:
['Config', 'SubConfig']Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_file_name(file_path)
Return file basename of the given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
file path |
required |
get_str_hash(source_str)
Calculate hash value of given string.
Current implementation uses :func:hashlib.md5
hash function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_str |
str
|
source str |
required |
Returns:
Type | Description |
---|---|
str
|
hash value as string |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
calculate_dir_hash(dir_path, dir_filter, file_filter)
Create a unique hash to represent state of directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path |
str
|
target directory |
required |
dir_filter |
str
|
exclude directories matching this regex |
required |
file_filter |
str
|
exclude files matching this regex |
required |
Returns:
Type | Description |
---|---|
str
|
hash value as string |
Examples:
"1a885a0cae99f53d6088b9f7cee3bf4d"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
prepare_html_str(input_string)
Reformat html string and prepare for pyRevit output window.
pyRevit output window renders html content. But this means that < and >
characters in outputs from python (e.g.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
str
|
input html string |
required |
Examples:
"&clt;p&cgt;Some text&clt;/p&cgt;"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
reverse_html(input_html)
Reformat codified pyRevit output html string back to normal html.
pyRevit output window renders html content. But this means that < and >
characters in outputs from python (e.g.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_html |
str
|
input codified html string |
required |
Examples:
"Some text
"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
escape_for_html(input_string)
can_access_url(url_to_open, timeout=1000)
Check if url is accessible within timeout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url_to_open |
str
|
url to check access for |
required |
timeout |
int
|
timeout in milliseconds |
1000
|
Returns:
Type | Description |
---|---|
bool
|
true if accessible |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
read_url(url_to_open)
Get the url and return response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url_to_open |
str
|
url to check access for |
required |
check_internet_connection(timeout=1000)
Check if internet connection is available.
Pings a few well-known websites to check if internet connection is present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
int
|
timeout in milliseconds |
1000
|
Returns:
Type | Description |
---|---|
str
|
url if internet connection is present, None if no internet. |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
touch(fname, times=None)
Update the timestamp on the given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fname |
str
|
target file path |
required |
times |
int
|
number of times to touch the file |
None
|
read_source_file(source_file_path)
Read text file and return contents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_file_path |
str
|
target file path |
required |
Returns:
Type | Description |
---|---|
str
|
file contents |
Raises:
Type | Description |
---|---|
PyRevitException
|
on read error |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
open_folder_in_explorer(folder_path)
Open given folder in Windows Explorer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder_path |
str
|
directory path |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
show_entry_in_explorer(entry_path)
Show given entry in Windows Explorer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entry_path |
str
|
directory or file path |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
fully_remove_dir(dir_path)
Remove directory recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path |
str
|
directory path |
required |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
cleanup_filename(file_name, windows_safe=False)
Cleanup file name from special characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
file name |
required |
windows_safe |
bool
|
whether to use windows safe characters |
False
|
Returns:
Type | Description |
---|---|
str
|
cleaned up file name |
Examples:
"Myfile(3).txt" "Perforations 18 (New).txt"Source code in pyrevitlib/pyrevit/coreutils/__init__.py
increment_str(input_str, step=1, expand=False)
Incremenet identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
identifier e.g. A310a |
required |
step |
int
|
number of steps to change the identifier |
1
|
expand |
bool
|
removes leading zeroes and duplicate letters |
False
|
Returns:
Type | Description |
---|---|
str
|
modified identifier |
Examples:
'A320a'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
decrement_str(input_str, step=1, shrink=False)
Decrement identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
identifier e.g. A310a |
required |
step |
int
|
number of steps to change the identifier |
1
|
shrink |
bool
|
removes leading zeroes or duplicate letters |
False
|
Returns:
Type | Description |
---|---|
str
|
modified identifier |
Examples:
'A309z'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
extend_counter(input_str, upper=True, use_zero=False)
Add a new level to identifier. e.g. A310 -> A310A.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
identifier e.g. A310 |
required |
upper |
bool
|
use UPPERCASE characters for extension |
True
|
use_zero |
bool
|
start from 0 for numeric extension |
False
|
Returns:
Type | Description |
---|---|
str
|
extended identifier |
Examples:
'A310A' 'A310A0'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
filter_null_items(src_list)
Remove None items in the given list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src_list |
list[Any]
|
list of any items |
required |
Returns:
Type | Description |
---|---|
list[Any]
|
cleaned list |
reverse_dict(input_dict)
Reverse the key, value pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dict |
dict
|
source ordered dict |
required |
Returns:
Type | Description |
---|---|
defaultdict
|
reversed dictionary |
Examples:
defaultdict(Source code in pyrevitlib/pyrevit/coreutils/__init__.py
timestamp()
Return timestamp for current time.
Returns:
Type | Description |
---|---|
str
|
timestamp in string format |
Examples:
'01003075032506808'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
current_time()
Return formatted current time.
Current implementation uses %H:%M:%S to format time.
Returns:
Type | Description |
---|---|
str
|
formatted current time. |
Examples:
'07:50:53'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
current_date()
Return formatted current date.
Current implementation uses %Y-%m-%d to format date.
Returns:
Type | Description |
---|---|
str
|
formatted current date. |
Examples:
'2018-01-03'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
is_blank(input_string)
Check if input string is blank (multiple white spaces is blank).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
str
|
input string |
required |
Returns:
Type | Description |
---|---|
bool
|
True if string is blank |
Examples:
TrueSource code in pyrevitlib/pyrevit/coreutils/__init__.py
is_url_valid(url_string)
Check if given URL is in valid format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url_string |
str
|
URL string |
required |
Returns:
Type | Description |
---|---|
bool
|
True if URL is in valid format |
Examples:
TrueSource code in pyrevitlib/pyrevit/coreutils/__init__.py
reformat_string(orig_str, orig_format, new_format)
Reformat a string into a new format.
Extracts information from a string based on a given pattern, and recreates a new string based on the given new pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
orig_str |
str
|
Original string to be reformatted |
required |
orig_format |
str
|
Pattern of the original str (data to be extracted) |
required |
new_format |
str
|
New pattern (how to recompose the data) |
required |
Returns:
Type | Description |
---|---|
str
|
Reformatted string |
Examples:
reformat_string('150 - FLOOR/CEILING - WD - 1 HR - FLOOR ASSEMBLY',
'{section} - {loc} - {mat} - {rating} - {name}',
'{section}:{mat}:{rating} - {name} ({loc})'))
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_mapped_drives_dict()
Return a dictionary of currently mapped network drives.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
dletter_to_unc(dletter_path)
Convert drive letter path into UNC path of that drive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dletter_path |
str
|
drive letter path |
required |
Returns:
Type | Description |
---|---|
str
|
UNC path |
Examples:
'//filestore/server/jdrive/somefile.txt'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
unc_to_dletter(unc_path)
Convert UNC path into drive letter path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unc_path |
str
|
UNC path |
required |
Returns:
Type | Description |
---|---|
str
|
drive letter path |
Examples:
# assuming J: is mapped to //filestore/server/jdrive
unc_to_dletter('//filestore/server/jdrive/somefile.txt')
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
random_color()
random_alpha()
random_hex_color()
Return a random color in hex format.
Examples:
'#FF0000'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
random_rgb_color()
Return a random color in rgb format.
Examples:
'rgb(255, 0, 0)'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
random_rgba_color()
Return a random color in rgba format.
Examples:
'rgba(255, 0, 0, 0.5)'Source code in pyrevitlib/pyrevit/coreutils/__init__.py
extract_range(formatted_str, max_range=500)
Extract range from formatted string.
String must be formatted as below A103 No range A103-A106 A103 to A106 A103:A106 A103 to A106 A103,A105a A103 and A105a A103;A105a A103 and A105a
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatted_str |
str
|
string specifying range |
required |
max_range |
int
|
maximum number of items to create. |
500
|
Returns:
Type | Description |
---|---|
list[str]
|
names in the specified range |
Examples:
['A103', 'A104', 'A105', 'A106'] ['S203', 'S204', 'S205', 'S206'] ['M00A', 'M00B']Source code in pyrevitlib/pyrevit/coreutils/__init__.py
check_encoding_bom(filename, bom_bytes=codecs.BOM_UTF8)
Check if given file contains the given BOM bytes at the start.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
file path |
required |
bom_bytes |
bytes
|
BOM bytes to check |
BOM_UTF8
|
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
has_nonprintable(input_str)
Check input string for non-printable characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
input string |
required |
Returns:
Type | Description |
---|---|
bool
|
True if contains non-printable characters |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_enum_values(enum_type)
get_enum_value(enum_type, value_string)
Return enum value matching given value string (case insensitive).
get_enum_none(enum_type)
extract_guid(source_str)
Extract GUID number from a string.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
format_hex_rgb(rgb_value)
Formats rgb value as #RGB value string.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
new_uuid()
Create a new UUID (using dotnet Guid.NewGuid).
is_box_visible_on_screens(left, top, width, height)
Check if given box is visible on any screen.
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
fuzzy_search_ratio(target_string, sfilter, regex=False)
Match target string against the filter and return a match ratio.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_string |
str
|
target string |
required |
sfilter |
str
|
search term |
required |
regex |
bool
|
treat the sfilter as regular expression pattern |
False
|
Returns:
Type | Description |
---|---|
int
|
integer between 0 to 100, with 100 being the exact match |
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 |
|
get_exe_version(exepath)
Extract Product Version value from EXE file.
get_reg_key(key, subkey)
Get value of the given Windows registry key and subkey.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
PyHKEY
|
parent registry key |
required |
subkey |
str
|
subkey path |
required |
Returns:
Type | Description |
---|---|
PyHKEY
|
registry key if found, None if not found |
Examples:
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
kill_tasks(task_name)
Kill running tasks matching task_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_name |
str
|
task name |
required |
Examples:
Source code in pyrevitlib/pyrevit/coreutils/__init__.py
int2hex_long(number)
hex2int_long(hex_string)
split_words(input_string)
Splits given string by uppercase characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
str
|
input string |
required |
Returns:
Type | Description |
---|---|
list[str]
|
split string |
Examples:
['UIApplication', 'Application', 'Closing']Source code in pyrevitlib/pyrevit/coreutils/__init__.py
get_paper_sizes(printer_name=None)
Get paper sizes defined on this system.
Returns:
Type | Description |
---|---|
list[]
|
list of papersize instances |