from time import localtime import logging import subprocess # convert list to text seperated by commas # used on group and group member lists def set_groups_data(data): if data == "None": return data else: return "; ".join(data) # end set_groups_data # returns local date as a string # does it need to be a date object? def get_local_date(data_date): try: if data_date is not None: # create time object time_mod = localtime(data_date/1000) # create formatted date (4-15-1999) date_mod = f'{time_mod[1]}-{time_mod[2]}-{time_mod[0]}' return date_mod except Exception: pass return None # end get_local_date def get_edit_dates(properties): # ArcGIS Online style editing_info = getattr(properties, 'editingInfo', None) if editing_info: return { 'dataLastEditDate': editing_info.get('lastEditDate'), 'schemaLastEditDate': editing_info.get('schemaLastEditDate') } # Enterprise fallback editor_tracking = properties.get('editorTrackingInfo') or properties.get('editFieldsInfo') if editor_tracking: return { 'dataLastEditDate': editor_tracking.get('lastEditDate'), 'schemaLastEditDate': None # Not usually available in Enterprise } # No info available return { 'dataLastEditDate': None, 'schemaLastEditDate': None } def overwrite_file(filename, new_content): """ Overwrites the contents of a text file with the passed variable. Parameters: filename (str): Path to the file to be overwritten. new_content (str): The content to write to the file. """ try: with open(filename, 'w') as file: file.write(new_content) logging.info(f"Successfully overwritten the file: {filename}") except Exception as e: logging.info(f"An error occurred while writing to the file: {e}") # end overwrite_file # extract user's full name def get_user_name(username): result = subprocess.run( ["net", "user", username, "/domain"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) output_combined = result.stdout + result.stderr if "The user name could not be found" in output_combined: return "Name Not Found" if result.returncode != 0: logging.info (f"error: Command failed: {result.stderr.strip()}") # Normalize line endings and ensure consistent spacing lines = result.stdout.replace('\r', '').split('\n') full_name = None for line in lines: if line.strip().startswith("Full Name"): parts = line.split("Full Name", 1)[-1].strip() full_name = parts if parts else None break return full_name def extract_user_props(user_list): """ user_list = list object of users from organization """ formatted_users_list = [] for user in user_list: # split on "@" character username = user.split("@")[0] # get user's email address if username != user: if "_" in username: username = username.replace("_", "-") # get email address if "@pa.gov" in user: email = user elif "@pa.lcl_PADOH" in user: email = f'{username}@pa.gov' else: email = '' # run "net user" Windows command utility name = get_user_name(username) # add entry to list formatted_users_list.append(f"{name} ({email})") else: formatted_users_list.append(username) return formatted_users_list