def _delete_old_log_files(self, mod_identifier: Union[str, CommonModIdentity]): from sims4communitylib.utils.common_io_utils import CommonIOUtils mod_name = CommonModIdentity._get_mod_name(mod_identifier) files_to_delete = ( CommonLogUtils.get_message_file_path(mod_name), CommonLogUtils.get_exceptions_file_path(mod_name), CommonLogUtils.get_old_message_file_path(mod_name), CommonLogUtils.get_old_exceptions_file_path(mod_name) ) for file_to_delete in files_to_delete: # noinspection PyBroadException try: CommonIOUtils.delete_file(file_to_delete, ignore_errors=True) except: continue
def on_started(self, interaction_sim: Sim, interaction_target: Any) -> bool: self.log.format_with_message('Running \'{}\' on_started.'.format( self.__class__.__name__), interaction_sim=interaction_sim, interaction_target=interaction_target) self.log.enable() self.log.debug('Interactions that can be performed on \'{}\':'.format( interaction_target)) interactions = CommonObjectInteractionUtils.get_all_interactions_registered_to_object_gen( interaction_target) interaction_short_names: List[str] = list() for interaction in interactions: try: interaction_short_names.append( CommonInteractionUtils.get_interaction_short_name( interaction)) except Exception as ex: self.log.error( 'Problem while attempting to handle interaction {}'.format( pformat(interaction)), exception=ex) continue sorted_short_names = sorted(interaction_short_names, key=lambda x: x) self.log.debug(pformat(sorted_short_names)) self.log.debug('Done Logging Available Interactions.'.format()) self.log.disable() CommonBasicNotification( CommonStringId.S4CL_LOG_ALL_INTERACTIONS, CommonStringId.S4CL_DONE_LOGGING_ALL_INTERACTIONS, description_tokens=(CommonLogUtils.get_message_file_path( self.mod_identity), )).show() return True
def _file_path(self, mod_identity: CommonModIdentity, identifier: str = None) -> str: from sims4communitylib.utils.common_log_utils import CommonLogUtils data_name = self._format_data_name(mod_identity, identifier=identifier) if self._per_save: save_slot_id = CommonSaveUtils.get_save_slot_id() save_slot_guid = CommonSaveUtils.get_save_slot_guid() return os.path.join( CommonLogUtils.get_sims_documents_location_path(), 'saves', mod_identity.base_namespace.lower(), f'{data_name}_id_{save_slot_id}_guid_{save_slot_guid}.json') else: return os.path.join( CommonLogUtils.get_sims_documents_location_path(), 'saves', mod_identity.base_namespace.lower(), f'{data_name}.json')
def exceptions_file_path(self) -> str: """The file path exceptions are logged to. :return: The file path exceptions are logged to. :rtype: str """ return CommonLogUtils.get_exceptions_file_path( self.mod_name, custom_file_path=self._custom_file_path)
def messages_file_path(self) -> str: """The file path messages are logged to. :return: The file path messages are logged to. :rtype: str """ return CommonLogUtils.get_message_file_path( self.mod_name, custom_file_path=self._custom_file_path)
def _log_message(self, message_type: str, message: str): from sims4communitylib.utils.common_date_utils import CommonRealDateUtils from pprint import pformat from sims4communitylib.exceptions.common_exceptions_handler import CommonExceptionHandler current_date_time = CommonRealDateUtils.get_current_date_string() new_message = '{} [{}] {}: [{}]: {}\n'.format(current_date_time, self._mod_name, str(message_type), self.name, message) try: from sims4communitylib.utils.common_io_utils import CommonIOUtils file_path = CommonLogUtils.get_message_file_path(self._mod_name) CommonIOUtils.write_to_file(file_path, new_message, ignore_errors=True) except Exception as ex: CommonExceptionHandler.log_exception(self._mod_name, 'Error occurred while attempting to log message: {}'.format(pformat(message)), exception=ex)
def on_started(self, interaction_sim: Sim, interaction_target: Any) -> bool: self.log.enable() object_id = CommonObjectUtils.get_object_id(interaction_target) if interaction_target is not None else -1 definition_id = -1 if isinstance(interaction_target, Sim): object_id = CommonSimUtils.get_sim_id(interaction_target) elif isinstance(interaction_target, GameObject): definition = CommonObjectUtils.get_game_object_definition(interaction_target) if definition is not None: definition_id = definition.id self.log.debug('Interactions that can be performed on \'{}\' id:{} def_id:{}:'.format(interaction_target, object_id, definition_id)) interactions = CommonObjectInteractionUtils.get_all_interactions_registered_to_object_gen(interaction_target) interaction_target: GameObject = interaction_target interaction_short_names: List[str] = list() for interaction in interactions: interaction: Interaction = interaction try: interaction_short_names.append('{} ({})'.format(CommonInteractionUtils.get_interaction_short_name(interaction), CommonInteractionUtils.get_interaction_id(interaction))) except Exception as ex: self.log.error('Problem while attempting to handle interaction {}'.format(pformat(interaction)), exception=ex) continue for component in interaction_target.components: if not hasattr(component, 'component_super_affordances_gen'): continue for affordance in component.component_super_affordances_gen(): try: interaction_short_names.append('{} ({})'.format(CommonInteractionUtils.get_interaction_short_name(affordance), CommonInteractionUtils.get_interaction_id(affordance))) except Exception as ex: self.log.error('Problem while attempting to handle affordance {}'.format(pformat(affordance)), exception=ex) continue sorted_short_names = sorted(interaction_short_names, key=lambda x: x) self.log.format(interactions=sorted_short_names) self.log.debug('Done Logging Available Interactions.') self.log.disable() CommonBasicNotification( CommonStringId.S4CL_LOG_ALL_INTERACTIONS, CommonStringId.S4CL_DONE_LOGGING_ALL_INTERACTIONS, description_tokens=(CommonLogUtils.get_message_file_path(self.mod_identity), ) ).show() return True
def generate_report(self): file_path = CommonLogUtils.get_message_file_path( ModInfo.get_identity().base_namespace) dir_name = os.path.dirname(file_path) file_path = os.path.join( dir_name, ModInfo.get_identity().base_namespace + '_results.html') try: opened_file = open(file_path, mode='a', buffering=1, encoding='utf-8') opened_file.write( '<html><head><title>Tuning Error Notifier Results</title><body>' ) opened_file.write( '<center><font size="+3" color="#E00000">Tuning Error Notifier Report<br></font><font size="+1" color="#C00000">{}</font></center><p>' .format(datetime.datetime.now().strftime('%c'))) for name in self._log_entries: opened_file.write( '<h3><font color="#0000ff">{}</font></h3>'.format( html.escape(name))) opened_file.write('<ul>') for msg in self._log_entries[name]: opened_file.write('<li>{}</li>'.format( html.escape(msg).replace(' ', ' '))) opened_file.write('</ul>') opened_file.write('</body></html>') opened_file.flush() opened_file.close() except Exception as ex: CommonExceptionHandler.log_exception( ModInfo.get_identity(), 'Problem occurred when generating error report.', exception=ex) return False
def _get_save_dir(self) -> str: return os.path.join(CommonLogUtils.get_sims_documents_location_path(), 'saves', 'CNNoRelationshipDecay')