def upload_from_file(self, filename, fileformat=TEXT_FORMAT, commit=True, verbose=False): file_processor = None final_count = 0 final_success = 0 try: name = Store.get_just_filename_from_filepath(filename) file_processor = Store.get_file_processor(fileformat, filename) count, success = file_processor.process_lines(name, self, verbose=verbose) final_count += count final_success += success self.commit(commit) except Exception as e: YLogger.exception_nostack(self, "Error uploading from file", e) self.rollback(commit=commit) finally: if file_processor is not None: file_processor.close() return final_count, final_success
def _load_properties(self, property_filepath): properties = {} if os.path.exists(property_filepath): try: YLogger.debug(self, "Loading properties from [%s]", property_filepath) with open(property_filepath, "r") as props_file: for line in props_file: line = line.strip() if line: if line.startswith( FilePropertyStore.COMMENT) is False: splits = line.split( FilePropertyStore.SPLIT_CHAR) if len(splits) > 1: key = splits[0].strip() val = self.process_value(splits[1:]) if val is not None: properties[key] = val except Exception as excep: YLogger.exception_nostack( self, "Failed to load properties file [%s]", excep, property_filepath) return properties
def _load_file_contents(self, parser, filename): YLogger.debug(self, "Loading file contents from [%s]", filename) try: parser.parse_from_file(filename, userid="*") except Exception as excep: YLogger.exception_nostack(self, "Failed to load cat [%s]", excep, filename)
def save_braintree(self, client_context, pattern_graph): try: self._save(client_context, pattern_graph) except Exception as exc: YLogger.exception_nostack(client_context, "Failed to save Braintree", exc)
def get_that_pattern(self, client_context, srai=False): try: that_question = None if srai is False: that_question = self.previous_nth_question(1) else: if len(self._questions) > 2: for question in reversed(self._questions[:-2]): if question.srai is False and question.has_response(): that_question = question break if that_question is not None: that_sentence = that_question.current_sentence() else: that_sentence = None # If the last response was valid, i.e not none and not empty string, then use # that as the that_pattern, otherwise we default to '*' as pattern if that_sentence is not None and that_sentence.response is not None and that_sentence.response != '': that_pattern = self.parse_last_sentences_from_response( that_sentence.response) YLogger.info(client_context, "That pattern = [%s]", that_pattern) else: YLogger.info(client_context, "That pattern, no response, default to [*]") that_pattern = "*" except Exception as excep: YLogger.exception_nostack(client_context, "No That pattern default to [*]", excep) that_pattern = "*" return that_pattern
def save_binary(self, aiml_parser): try: self._save(aiml_parser) except Exception as excep: YLogger.exception_nostack(self, "Failed to save binary file", excep)
def upload_from_file(self, filename, format=Store.TEXT_FORMAT, commit=True, verbose=False): YLogger.debug(self, "Oploading from file [%s]", filename) file_processor = None try: name = self.get_just_filename_from_filepath(filename) if verbose is True: print(name) file_processor = self.get_file_processor(format, filename) file_processor.process_lines(name, self, verbose=verbose) if commit is True: self.commit() except Exception as e: YLogger.exception_nostack(self, "Error uploading from file [%s]", e, filename) if commit is True: self.rollback() finally: if file_processor is not None: file_processor.close()
def drop(self): try: storage_path = self._get_storage_path() if os.path.exists(storage_path): shutil.rmtree(storage_path) except Exception as e: YLogger.exception_nostack(self, "Error dropping storage", e)
def add_to_collection(self, collection, name, value): try: collection.add_regex(name, re.compile(value, re.IGNORECASE)) except Exception as excep: YLogger.exception_nostack( self, "Error adding regex to collection: [%s]" % value, excep)
def save_braintree(self, client_context, pattern_graph): try: braintree_fullpath = self.storage_engine.configuration.braintree_storage.file YLogger.info(self, "Saving braintree to %s", braintree_fullpath) braintree_dirpath = self._get_dir_from_path(braintree_fullpath) self._ensure_dir_exists(braintree_dirpath) format = self.storage_engine.configuration.braintree_storage.format encoding = self.storage_engine.configuration.braintree_storage.encoding if format == FileStore.TEXT_FORMAT: with open(braintree_fullpath, "w+", encoding=encoding) as dump_file: pattern_graph.dump(output_func=dump_file.write, eol="\n") elif format == FileStore.XML_FORMAT: braintree = '<?xml version="1.0" encoding="%s"?>\n' % encoding braintree += '<aiml>\n' braintree += pattern_graph.root.to_xml(client_context) braintree += '</aiml>\n' with open(braintree_fullpath, "w+", encoding=encoding) as dump_file: dump_file.write(braintree) else: YLogger.error(client_context, "Unknown braintree content type [%s]", format) except Exception as exc: YLogger.exception_nostack(client_context, "Failed to save Braintree", exc)
def remove_user(self, userid, clientid): try: return self._remove_user_from_db(userid, clientid) except Exception as excep: YLogger.exception_nostack(self, "Failed to remove user", excep) return False
def remove_link(self, userid): try: return self._delete_link(userid) except Exception as excep: YLogger.exception_nostack(self, "Failed to remove link", excep) return False
def load_binary(self): try: return self._load() except Exception as excep: YLogger.exception_nostack(self, "Failed to load binary file", excep) return None
def _write_variables(self, variables_filepath, variables): try: with open(variables_filepath, "w+") as vars_file: for key, value in variables.items(): vars_file.write("%s%s%s\n"%(key, FileVariablesStore.SPLIT_CHAR, value)) vars_file.write("\n") except Exception as e: YLogger.exception_nostack(None, "Failed to write variables [%s]", e, variables_filepath)
def primary_account(self, linked_userid): try: db_account = self._storage_engine.session.query(LinkedAccount).filter( LinkedAccount.linked_user == linked_userid).one() return db_account.primary_user except Exception as excep: YLogger.exception_nostack(self, "Failed to find priamry account for userid [%s]", excep, linked_userid) return None
def load_brain_selector(self, configuration): if configuration.brain_selector is None: self._brain_selector = DefaultBrainSelector(configuration, self._brains) else: try: self._brain_selector = ClassLoader.instantiate_class(configuration.brain_selector)(configuration, self._brains) except Exception as excep: YLogger.exception_nostack(self, "Failed to load defined brain selector, loadiing default", excep) self._brain_selector = DefaultBrainSelector(configuration, self._brains)
def _load_file_contents(self, license_collection, filename): try: YLogger.info(self, "Loading license key file: [%s]", filename) with open(filename, "r", encoding="utf-8") as license_file: for line in license_file: self._process_license_key_line(license_collection, line) except Exception as excep: YLogger.exception_nostack(self, "Invalid license key file [%s]", excep, filename)
def handle_trigger(json_data): outputLog(None, "\nTrigger received...") try: outputLog(None, json.dumps(json.loads(json_data), indent=4)) except Exception as excep: YLogger.exception_nostack(None, "Trigger failed", excep) outputLog(None, "Trigger failed [%s]" % str(excep)) return 'OK'
def _get_tuples(self, client_context): raw_tuples = self._tuples.resolve(client_context) try: return TemplateGetNode.decode_tuples(raw_tuples) except Exception as excep: YLogger.exception_nostack(self, "Failed to decode tuples, returning []", excep) return []
def get_link(self, userid): try: link = self._storage_engine.session.query(Link).filter( Link.primary_user == userid).one() return link except Exception as e: YLogger.exception_nostack(self, "Failed to get link", e) return None
def _load_file_contents(self, collection, filename): try: self._load_triggers_from_file(filename, collection) return True except Exception as e: YLogger.exception_nostack(None, "Failed to load triggers [%s]", e, filename) return False
def _exists(self, key): try: return self._storage_engine.session.query(Person2).filter( Denormal.key == key).one() except Exception as excep: YLogger.exception_nostack(self, "Failed to check for existence", excep) return None
def store_last_message_ids(self, last_direct_message_id, last_status_id): self._ensure_dir_exists(self.storage_engine.configuration.twitter_storage.dirs[0]) twitter_ids_file = self._twitter_filename(self.storage_engine.configuration.twitter_storage.dirs[0]) try: with open(twitter_ids_file, "w+", encoding="utf-8") as idfile: idfile.write("%s\n" % last_direct_message_id) idfile.write("%s\n" % last_status_id) except Exception as e: YLogger.exception_nostack(None, "Failed to store last message ids [%s]", e, twitter_ids_file)
def save_errors(self, errors, commit=True): filename = self._get_storage_path() file_dir = self._get_dir_from_path(filename) self._ensure_dir_exists(file_dir) try: YLogger.debug(self, "Saving errors to [%s]", filename) self._write_errors_to_file(filename, errors) except Exception as excep: YLogger.exception_nostack(self, "Failed to write errors file [%s]", excep, filename)
def link_exists(self, userid, provided_key, generated_key): try: self._storage_engine.session.query(Link).filter( Link.primary_user == userid, Link.provided_key == provided_key, Link.generated_key == generated_key).one() return True except Exception as excep: YLogger.exception_nostack(self, "Failed to check link exists", excep) return False
def _load_file_contents(self, collection, filename): YLogger.debug(self, "Loading rdf [%s]", filename) try: self._load_rdfs_from_file(filename, collection) return True except Exception as excep: YLogger.exception_nostack(self, "Failed to load rdf [%s]", excep, filename) return False
def load_properties(self, property_filepath): properties = {} YLogger.debug(self, "Loading properties from [%s]", property_filepath) try: self._load_properties_from_file(property_filepath, properties) except Exception as excep: YLogger.exception_nostack(self, "Failed to load properties file [%s]", excep, property_filepath) return properties
def load_usergroups(self, usersgroupsauthorisor): filename = self.get_storage().file try: self._read_usergroups_from_file(filename, usersgroupsauthorisor) return True except Exception as e: YLogger.exception_nostack( self, "Failed to load usergroups yaml file [%s]", e, filename) return False
def test_ylogger_exception_nostack(self): client_context = ClientContext(TestClient(), "testid") YLogger.reset_snapshot() YLogger.exception_nostack(client_context, "Test Message", Exception("Test error")) snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEqual( str(snapshot), "Critical(0) Fatal(0) Error(0) Exception(1) Warning(0) Info(0), Debug(0)" )
def _write_properties(self, property_filepath, properties): try: with open(property_filepath, "w+") as props_file: for key, value in properties.items(): props_file.write( "%s%s%s\n" % (key, FilePropertyStore.SPLIT_CHAR, value)) props_file.write("\n") except Exception as excep: YLogger.exception_nostack(self, "Failed to write properties file [%s]", excep, property_filepath)