# Path: uzbl/arguments.py
# def match(s):
# def lex(s):
# def parse(l):
#     def __new__(cls, s):
#     def raw(self, frm=0, to=None):
#     def safe_raw(self, frm=0, to=None):
# def is_quoted(s):
# def unquote(s):
# class Arguments(tuple):
# 
# Path: uzbl/plugins/cmd_expand.py
# def send_user_command(uzbl, cmd, args):
#     if cmd[0] == 'event':
#         has_var = any('@' in x for x in cmd)
#         event = cmd[1]
#         args = ' '.join(format_arg(c, args) for c in cmd[2:])
#         if not has_var:
#             # Bypass the roundtrip to uzbl and dispatch immediately
#             uzbl.event(event, args)
#         else:
#             uzbl.send(' '.join(('event', event, args)))
#     else:
#         cmd = ' '.join((cmd_expand(cmd[0], args),) +
#                        tuple(format_arg(c, args) for c in cmd[1:]))
#         uzbl.send(cmd)
# 
# Path: uzbl/ext.py
# class PerInstancePlugin(BasePlugin):
#     """Base class for plugins which instantiate once per uzbl instance"""
# 
#     def __init__(self, uzbl):
#         self.uzbl = uzbl
#         self.plugin_config = uzbl.parent.get_plugin_config(self.CONFIG_SECTION)
#         self.logger = uzbl.logger  # we can also append plugin name to logger
# 
#     def cleanup(self):
#         """Cleanup state after instance is gone
# 
#         Default function avoids cyclic refrences, so don't forget to call
#         super() if overriding
#         """
#         del self.uzbl
# 
#     @classmethod
#     def _get_instance(cls, owner):
#         """Returns instance of the plugin
# 
#         This method should be private to not violate TOOWTDI
#         """
#         if not isinstance(owner, Uzbl):
#             raise ValueError("Can only get {0} instance for uzbl, not {1}"
#                 .format(cls.__name__, type(owner).__name__))
#         # TODO(tailhook) probably subclasses can be returned as well
#         return owner.plugins[cls]


import fnmatch
from functools import partial
from uzbl.arguments import splitquoted
from .cmd_expand import send_user_command
from uzbl.ext import PerInstancePlugin

Formatting options:
  %s = space separated string of the arguments
  %r = escaped and quoted version of %s
  %1 = argument 1
  %2 = argument 2
  %n = argument n

Usage:
  event ON_EVENT LINK_HOVER set selected_uri $1
    --> LINK_HOVER http://uzbl.org/
    <-- set selected_uri http://uzbl.org/

  event ON_EVENT CONFIG_CHANGED print Config changed: %1 %2
    --> CONFIG_CHANGED selected_uri http://uzbl.org/
    <-- print Config changed: selected_uri http://uzbl.org/
'''




def match_args(pattern, args):
    if len(pattern) > len(args):
        return False
    for p, a in zip(pattern, args):
        if not fnmatch.fnmatch(a, p):
            return False
    return True



# Path: uzbl/arguments.py
# def match(s):
# def lex(s):
# def parse(l):
#     def __new__(cls, s):
#     def raw(self, frm=0, to=None):
#     def safe_raw(self, frm=0, to=None):
# def is_quoted(s):
# def unquote(s):
# class Arguments(tuple):
# 
# Path: uzbl/plugins/cmd_expand.py
# def send_user_command(uzbl, cmd, args):
#     if cmd[0] == 'event':
#         has_var = any('@' in x for x in cmd)
#         event = cmd[1]
#         args = ' '.join(format_arg(c, args) for c in cmd[2:])
#         if not has_var:
#             # Bypass the roundtrip to uzbl and dispatch immediately
#             uzbl.event(event, args)
#         else:
#             uzbl.send(' '.join(('event', event, args)))
#     else:
#         cmd = ' '.join((cmd_expand(cmd[0], args),) +
#                        tuple(format_arg(c, args) for c in cmd[1:]))
#         uzbl.send(cmd)
# 
# Path: uzbl/ext.py
# class PerInstancePlugin(BasePlugin):
#     """Base class for plugins which instantiate once per uzbl instance"""
# 
#     def __init__(self, uzbl):
#         self.uzbl = uzbl
#         self.plugin_config = uzbl.parent.get_plugin_config(self.CONFIG_SECTION)
#         self.logger = uzbl.logger  # we can also append plugin name to logger
# 
#     def cleanup(self):
#         """Cleanup state after instance is gone
# 
#         Default function avoids cyclic refrences, so don't forget to call
#         super() if overriding
#         """
#         del self.uzbl
# 
#     @classmethod
#     def _get_instance(cls, owner):
#         """Returns instance of the plugin
# 
#         This method should be private to not violate TOOWTDI
#         """
#         if not isinstance(owner, Uzbl):
#             raise ValueError("Can only get {0} instance for uzbl, not {1}"
#                 .format(cls.__name__, type(owner).__name__))
#         # TODO(tailhook) probably subclasses can be returned as well
#         return owner.plugins[cls]


# Path: uzbl/plugins/on_event.py
import fnmatch
from functools import partial
from uzbl.arguments import splitquoted
from .cmd_expand import send_user_command
from uzbl.ext import PerInstancePlugin


Formatting options:
  %s = space separated string of the arguments
  %r = escaped and quoted version of %s
  %1 = argument 1
  %2 = argument 2
  %n = argument n

Usage:
  event ON_EVENT LINK_HOVER set selected_uri $1
    --> LINK_HOVER http://uzbl.org/
    <-- set selected_uri http://uzbl.org/

  event ON_EVENT CONFIG_CHANGED print Config changed: %1 %2
    --> CONFIG_CHANGED selected_uri http://uzbl.org/
    <-- print Config changed: selected_uri http://uzbl.org/
'''




def match_args(pattern, args):
    if len(pattern) > len(args):
        return False
    for p, a in zip(pattern, args):
        if not fnmatch.fnmatch(a, p):
            return False
    return True



class OnEventPlugin(PerInstancePlugin):