def __init__(self, **kwargs): super(Twitter, self).__init__(**kwargs) consumer_key = kwargs.get('consumer_key', None) consumer_secret = kwargs.get('consumer_secret', None) access_token_key = kwargs.get('access_token_key', None) access_token_secret = kwargs.get('access_token_secret', None) tweet = kwargs.get('tweet', None) if consumer_key is None: raise InvalidParameterException("Twitter needs a consumer_key") if consumer_secret is None: raise InvalidParameterException("Twitter needs a consumer_secret") if access_token_key is None: raise InvalidParameterException( "Twitter needs an access_token_key") if access_token_secret is None: raise InvalidParameterException( "Twitter needs and access_token_secret") if tweet is None: raise InvalidParameterException( "You need to provide something to tweet !") api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token_key=access_token_key, access_token_secret=access_token_secret) status = api.PostUpdate(tweet) message = {"tweet": status.text} self.say(message)
def _is_parameters_ok(self): """ Check that all provided parameters in the neurons are valid :return: True if all check passed """ # URL is mandatory if self.url is None: raise InvalidParameterException("Uri needs an url") # headers can be null, but if provided it must be a list if self.headers is not None: if not isinstance(self.headers, dict): raise InvalidParameterException( "headers must be a list of key: value") # timeout in second must be an integer if self.timeout is not None: if not isinstance(self.timeout, int): raise InvalidParameterException("timeout must be an integer") # data must be loadable with json if self.data is not None: try: json.loads(self.data) except ValueError, e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e)
def _is_parameters_ok(self): """ Check if received links are ok to perform operations :return: true if the neuron is well configured, raise an exception otherwise .. raises:: MissingParameterException """ # with the neuron the user has the choice of a direct link that call another synapse, # or a link with an answer caught from the STT engine # we cannot use at the same time a direct redirection and a link with question if self.direct_link is not None and self.from_answer_link is not None: raise InvalidParameterException( "neurotransmitter cannot be used with both direct_link and from_answer_link" ) if self.direct_link is None and self.from_answer_link is None: raise InvalidParameterException( "neurotransmitter must be used with direct_link or from_answer_link" ) if self.from_answer_link is not None: if self.default is None: raise InvalidParameterException( "default parameter is required and must contain a valid synapse name" ) for el in self.from_answer_link: if "synapse" not in el: raise InvalidParameterException( "Links must contain a synapse name: %s" % el) if "answers" not in el: raise InvalidParameterException( "Links must contain answers: %s" % el) return True
def __init__(self, **kwargs): super(Wake_on_lan, self).__init__(**kwargs) mac_address = kwargs.get('mac_address', None) broadcast_address = kwargs.get('broadcast_address', '255.255.255.255') port = kwargs.get('port', 9) # check we provide a mac address if mac_address is None: raise MissingParameterException("mac_address parameter required") # convert to unicode for testing broadcast_address_unicode = broadcast_address.decode('utf-8') # check the ip address is a valid one ipaddress.ip_address(broadcast_address_unicode) # check the port if type(port) is not int: raise InvalidParameterException( "port argument must be an integer. Remove quotes in your configuration." ) logger.debug( "Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s" % (mac_address, broadcast_address, port))
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: MissingParameterException, InvalidParameterException """ if self.path is None: raise MissingParameterException("You must provide a script path.") if not os.path.isfile(self.path): raise InvalidParameterException( "Script not found or is not a file.") if not os.access(self.path, os.X_OK): raise InvalidParameterException("Script not Executable.") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.query is None: raise InvalidParameterException("Wikipedia needs a query") if self.language is None: raise InvalidParameterException("Wikipedia needs a language") valid_language = wikipedia.languages().keys() if self.language not in valid_language: raise InvalidParameterException( "Wikipedia needs a valid language: %s" % valid_language) return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.consumer_key is None: raise InvalidParameterException("Twitter needs a consumer_key") if self.consumer_secret is None: raise InvalidParameterException("Twitter needs a consumer_secret") if self.access_token_key is None: raise InvalidParameterException( "Twitter needs an access_token_key") if self.access_token_secret is None: raise InvalidParameterException( "Twitter needs and access_token_secret") if self.tweet is None: raise InvalidParameterException( "You need to provide something to tweet !") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.query is None: raise InvalidParameterException("Wikipedia needs a query") if self.language is None: raise InvalidParameterException("Wikipedia needs a language") valid_language = wikipedia.languages().keys() if self.language not in valid_language: raise InvalidParameterException( "Wikipedia needs a valid language: %s" % valid_language) if self.sentences is not None: if not isinstance(self.sentences, int): raise InvalidParameterException( "Number of sentences must be an integer") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException, MissingParameterException """ # check we provide a mac address if self.mac_address is None: raise MissingParameterException("mac_address parameter required") # check the port if type(self.port) is not int: raise InvalidParameterException( "port argument must be an integer. Remove quotes in your configuration." ) return True
class Uri(NeuronModule): def __init__(self, **kwargs): super(Uri, self).__init__(**kwargs) # input variables self.url = kwargs.get('url', None) self.headers = kwargs.get('headers', None) self.data = kwargs.get('data', None) self.data_from_file = kwargs.get('data_from_file', None) self.method = kwargs.get('method', "GET") self.user = kwargs.get('user', None) self.password = kwargs.get('password', None) self.timeout = kwargs.get('timeout', None) # processing parameters self.parameters = None # output variable self.status_code = None self.content = None self.text = None self.response_header = None # this is a switch case option switch_case = { "GET": self.do_get, "POST": self.do_post, "DELETE": self.do_delete, "PUT": self.do_put, "HEAD": self.do_head, "PATCH": self.do_patch, "OPTIONS": self.do_options } # check parameters if self._is_parameters_ok(): # we get parameters that will be passed to the requests lib self.parameters = self.get_parameters() # we call the right method depending of the method selected switch_case[self.method]() message = { "status_code": self.status_code, "content": self.content, "response_header": self.response_header } self.say(message) def do_get(self): logger.debug(self.neuron_name + " do_get method called") r = requests.get(url=self.url, **self.parameters) self.post_processing_request(r) def do_post(self): logger.debug(self.neuron_name + " do_post method called") r = requests.post(url=self.url, **self.parameters) self.post_processing_request(r) def do_delete(self): logger.debug(self.neuron_name + " do_delete method called") r = requests.delete(url=self.url, **self.parameters) self.post_processing_request(r) def do_put(self): logger.debug(self.neuron_name + " do_put method called") r = requests.put(url=self.url, **self.parameters) self.post_processing_request(r) def do_head(self): logger.debug(self.neuron_name + " do_head method called") r = requests.head(url=self.url, **self.parameters) self.post_processing_request(r) def do_patch(self): logger.debug(self.neuron_name + " do_patch method called") r = requests.patch(url=self.url, **self.parameters) self.post_processing_request(r) def do_options(self): logger.debug(self.neuron_name + " do_options method called") r = requests.options(url=self.url, **self.parameters) self.post_processing_request(r) def get_parameters(self): """ :return: Dict of parameters usable by the "requests" lib """ returned_parameters = dict() if self.headers is not None: returned_parameters["headers"] = self.headers if self.timeout is not None: returned_parameters["timeout"] = self.timeout if self.data is not None: returned_parameters["data"] = self.data if self.data_from_file is not None: returned_parameters["data"] = self.data_from_file if self.user is not None: # this implicitly means that the password is set too, the check has been done in _is_parameters_ok returned_parameters["auth"] = self.user, self.password logger.debug(self.neuron_name + " parameters: %s" % returned_parameters) return returned_parameters def post_processing_request(self, r): self.status_code = r.status_code self.content = r.content # we try to load into a json object the content. So Kalliope can use it to talk try: self.content = json.loads(self.content) except ValueError: logger.debug(self.neuron_name + "cannot get a valid json from returned content") pass self.text = r.text self.response_header = r.headers logger.debug(self.neuron_name + " status_code: %s" % self.status_code) logger.debug(self.neuron_name + " content: %s" % self.content) logger.debug(self.neuron_name + " response_header: %s" % self.response_header) def _is_parameters_ok(self): """ Check that all provided parameters in the neurons are valid :return: True if all check passed """ # URL is mandatory if self.url is None: raise InvalidParameterException("Uri needs an url") # headers can be null, but if provided it must be a list if self.headers is not None: if not isinstance(self.headers, dict): raise InvalidParameterException( "headers must be a list of key: value") # timeout in second must be an integer if self.timeout is not None: if not isinstance(self.timeout, int): raise InvalidParameterException("timeout must be an integer") # data must be loadable with json if self.data is not None: try: json.loads(self.data) except ValueError, e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e) # data_from_file path must exist and data inside must be loadable by json if self.data_from_file is not None: # check that the file exist if not os.path.exists(self.data_from_file): raise InvalidParameterException( "error in \"data_file\". File does not exist: %s" % self.data_from_file) # then try to load the json from the file try: self.data_from_file = self.readfile(self.data_from_file) except ValueError, e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e)
# check that the file exist if not os.path.exists(self.data_from_file): raise InvalidParameterException( "error in \"data_file\". File does not exist: %s" % self.data_from_file) # then try to load the json from the file try: self.data_from_file = self.readfile(self.data_from_file) except ValueError, e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e) # we cannot provide both data and data from file if self.data is not None and self.data_from_file is not None: raise InvalidParameterException( "URI can be used with data or data_from_file, not both in same time" ) # the provided method must exist allowed_method = [ "GET", "POST", "DELETE", "PUT", "HEAD", "PATCH", "OPTIONS" ] if self.method not in allowed_method: raise InvalidParameterException("method %s not in: %s" % (self.method, allowed_method)) return True @staticmethod def readfile(file_path): """