Skip to content
1egoman edited this page Jun 20, 2014 · 2 revisions

Parser class

Methods:

  • def validate(self)
  • def parse(self)
  • def get_plugin_dir(self, f)

Variables:

  • query
  • resp
  • info

def validate(self)

The validate() function is one of the functions required in a plugin. If the plugin can parse the query, return True (or something that equates to boolean True). Otherwise, return False.

def parse(self)

The parse() function is the main function in any plugin. If validate() returns True, then parse() is executed to determine what the plugin's actual response is. self.resp is a dictionary that holds the data sent back to the user (more info). Always set the status element to be equal to STATUS_OK unless the plugin failed to produce a result. Set the text element equal to the output of the plugin, or whatever message that you want the user to recieve. If you don't return self.resp from the parse function, strange things will happen!

def get_plugin_dir(self, f)

This function can tell a plugin it's local directory (plugins/plugin's folder) so it has a place to put config files, and other plugin-specific stuff. When calling this function, make sure to specify the argument f (with __file__) for it to work. So, get_plugin_dir(__file__).

query

This class-scoped variable is equal to the user's query as a list of words ('This is a test' -> ['this', 'is', 'a', 'test']). However, some queries will be filtered with more data ('test at 5pm' -> ['test', 'at', {'type': 'time', 'when': [0, 0, 0, 17, 0, 0]}]).

  • date/times are filtered in the format of [year, month, day, hour, minute, second]

resp

This class-scoped variable contains the data sent back to the user as a packet. It is just a normal python dictionary.

###Every plugin must define:

  • resp["status"] - The plugin's status.
    • STATUS_OK for success
    • STATUS_NO_HIT if the plugin cannot find it's target (failed search?)
    • STATUS_ERR for a general error.

If this isn't set, many clients won't understand the packet and your query will fail.

  • resp["text"] - The main response; this should be set to a textual representation of whatever other data is sent in the packet. The default is 'No Response.', but in most cases should be changed to something else. Some clients will only look at this and nothing else in the packet.

###Optional, but highly recommended:

  • resp["type"] - A unique identifier that allows the client (and the server) to determine what plugin responded to which queries. Specifying a type is highly recommended.
  • resp["return"] - A list containing each phrase of the response as an element of the list.

info

Ditionary that contains the parsed contents of a plugin's info.json. Useful for accessing custom keys inside.

Clone this wiki locally