From d1d3e1592e84601f3243cad6f477e94b16ec3db8 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Tue, 17 Jul 2018 18:11:23 -0600 Subject: [PATCH] Added get_process() --- .bin/Scripts/functions/common.py | 33 +++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/.bin/Scripts/functions/common.py b/.bin/Scripts/functions/common.py index 1aa8cfaa..75fe99d1 100644 --- a/.bin/Scripts/functions/common.py +++ b/.bin/Scripts/functions/common.py @@ -197,6 +197,30 @@ def extract_item(item, filter='', silent=False): if not silent: print_warning('WARNING: Errors encountered while exctracting data') +def get_process(name=None): + """Get process by name, returns psutil.Process obj.""" + proc = None + if not name: + raise GenericError + + for p in psutil.process_iter(): + try: + if p.name() == name: + proc = p + except psutil._exceptions.NoSuchProcess: + # Process finished during iteration? Going to ignore + pass + return proc + +def get_simple_string(prompt='Enter string'): + """Get string from user (minimal allowed character set) and return as str.""" + simple_string = None + while simple_string is None: + _input = input('{}: '.format(prompt)) + if re.match(r'^(\w|-|_| )+$', _input, re.ASCII): + simple_string = _input.strip() + return simple_string + def get_ticket_number(): """Get TicketNumber from user, save in LogDir, and return as str.""" if not ENABLED_TICKET_NUMBERS: @@ -213,15 +237,6 @@ def get_ticket_number(): f.write(ticket_number) return ticket_number -def get_simple_string(prompt='Enter string'): - """Get string from user (minimal allowed character set) and return as str.""" - simple_string = None - while simple_string is None: - _input = input('{}: '.format(prompt)) - if re.match(r'^(\w|-|_| )+$', _input, re.ASCII): - simple_string = _input.strip() - return simple_string - def human_readable_size(size, decimals=0): """Convert size in bytes to a human-readable format and return a str.""" # Prep string formatting