Ejemplo n.º 1
0
	def get_channels(self, key):
		spec = pmxbot.config.get(self.channel_spec_config, {})
		default_channels = spec.get('default', [])
		matching_channels = spec.get(key, default_channels)
		if isinstance(matching_channels, dict) and 'channels' in matching_channels:
			matching_channels = matching_channels['channels']
		return always_iterable(matching_channels)
Ejemplo n.º 2
0
 def part(self, channels, message=""):
     """Send a PART command."""
     channels = always_iterable(channels)
     cmd_parts = ["PART", ",".join(channels)]
     if message:
         cmd_parts.append(message)
     self.send_raw(" ".join(cmd_parts))
Ejemplo n.º 3
0
def generate_results(function):
	"""
	Take a function, which may return an iterator or a static result
	and convert it to a late-dispatched generator.
	"""
	for item in always_iterable(function()):
		yield item
Ejemplo n.º 4
0
Archivo: core.py Proyecto: sd2k/pmxbot
def command(name=None, aliases=None, doc=None):
	handler = CommandHandler(name=name, doc=doc)
	aliases = [
		AliasHandler(name=alias, parent=handler)
		for alias in always_iterable(aliases)
	]
	handler.aliases = aliases
	return handler.decorate
Ejemplo n.º 5
0
def command(name=None, aliases=None, doc=None):
    handler = CommandHandler(name=name, doc=doc)
    aliases = [
        AliasHandler(name=alias, parent=handler)
        for alias in always_iterable(aliases)
    ]
    handler.aliases = aliases
    return handler.decorate
Ejemplo n.º 6
0
 def get_channels(self, key):
     spec = pmxbot.config.get(self.channel_spec_config, {})
     default_channels = spec.get('default', [])
     matching_channels = spec.get(key, default_channels)
     if isinstance(matching_channels,
                   dict) and 'channels' in matching_channels:
         matching_channels = matching_channels['channels']
     return always_iterable(matching_channels)
Ejemplo n.º 7
0
 def list(self, channels=None, server=""):
     """Send a LIST command."""
     command = "LIST"
     channels = ",".join(always_iterable(channels))
     if channels:
         command += ' ' + channels
     if server:
         command = command + " " + server
     self.send_raw(command)
Ejemplo n.º 8
0
 def part(self, channels, message=""):
     """Send a PART command."""
     channels = always_iterable(channels)
     cmd_parts = [
         'PART',
         ','.join(channels),
     ]
     if message: cmd_parts.append(message)
     self.send_raw(' '.join(cmd_parts))
Ejemplo n.º 9
0
def command(name=None, aliases=None, doc=None):
    if callable(name):
        raise ValueError("Name should be a string, did you forget ()?")
    handler = CommandHandler(name=name, doc=doc)
    aliases = [
        AliasHandler(name=alias, parent=handler)
        for alias in always_iterable(aliases)
    ]
    handler.aliases = aliases
    return handler.decorate
Ejemplo n.º 10
0
 def read(self, filenames):
     for filename in always_iterable(filenames):
         # try:
         #     fp = open(filename)
         # except IOError:
         #     continue
         fp = open(filename)
         try:
             self._read(fp, filename)
         finally:
             fp.close()
Ejemplo n.º 11
0
def flatten_mapping(mapping):
    """
	For every key that has an __iter__ method, assign the values
	to a key for each.

	>>> flatten_mapping({'ab': 3, ('c','d'): 4}) == {'ab': 3, 'c': 4, 'd': 4}
	True
	"""
    return {
        key: value
        for keys, value in mapping.items() for key in always_iterable(keys)
    }
Ejemplo n.º 12
0
	def display_message(self, name, url, build, **kwargs):
		"""
		Decides if message should be displayed based on its status and config.
		Mostly done to not display the SUCCESS statuses of periodic builds.
		Options are SUCCESS, UNSTABLE, FAILED, ABORTED, and CYCLE.
		"""
		channel_spec = pmxbot.config.get(self.channel_spec_config, {})
		job_spec = channel_spec.get(name, {})
		allowed_statuses = []
		if isinstance(job_spec, dict) and 'statuses' in job_spec:
			allowed_statuses = always_iterable(job_spec["statuses"])
		return not allowed_statuses or build.get('status') in allowed_statuses
Ejemplo n.º 13
0
def trap_exceptions(results, handler, exceptions=Exception):
	"""
	Iterate through the results, but if an exception occurs, stop
	processing the results and instead replace
	the results with the output from the exception handler.
	"""
	try:
		for result in results:
			yield result
	except exceptions as exc:
		for result in always_iterable(handler(exc)):
			yield result
Ejemplo n.º 14
0
    def display_message(self, name, url, build, **kwargs):
        """
		Decides if message should be displayed based on its status and config.
		Mostly done to not display the SUCCESS statuses of periodic builds.
		Options are SUCCESS, UNSTABLE, FAILED, ABORTED, and CYCLE.
		"""
        channel_spec = pmxbot.config.get(self.channel_spec_config, {})
        job_spec = channel_spec.get(name, {})
        allowed_statuses = []
        if isinstance(job_spec, dict) and 'statuses' in job_spec:
            allowed_statuses = always_iterable(job_spec["statuses"])
        return not allowed_statuses or build.get('status') in allowed_statuses
Ejemplo n.º 15
0
def flatten_mapping(mapping):
	"""
	For every key that has an __iter__ method, assign the values
	to a key for each.

	>>> flatten_mapping({'ab': 3, ('c','d'): 4}) == {'ab': 3, 'c': 4, 'd': 4}
	True
	"""
	return {
		key: value
		for keys, value in mapping.items()
		for key in always_iterable(keys)
	}
Ejemplo n.º 16
0
def package_context(target, action='install'):
    """
    A context for installing the build dependencies for a given target (or
    targets). Uses apt. Removes the dependencies when the context is
    exited. One may prevent the removal of some or all packages by modifying
    the list within the context.
    """
    target = ' '.join(always_iterable(target))
    status = sudo('apt {action} -q -y {target}'.format(**vars()))
    packages = jaraco.apt.parse_new_packages(status)
    try:
        yield packages
    finally:
        remove_packages(packages)
Ejemplo n.º 17
0
def json_in(content_type=[ntou('application/json'),
                          ntou('text/javascript')],
            force=True,
            debug=False,
            processor=json_processor):
    """Add a processor to parse JSON request entities:
    The default processor places the parsed data into request.json.

    Incoming request entities which match the given content_type(s) will
    be deserialized from JSON to the Python equivalent, and the result
    stored at cherrypy.request.json. The 'content_type' argument may
    be a Content-Type string or a list of allowable Content-Type strings.

    If the 'force' argument is True (the default), then entities of other
    content types will not be allowed; "415 Unsupported Media Type" is
    raised instead.

    Supply your own processor to use a custom decoder, or to handle the parsed
    data differently.  The processor can be configured via
    tools.json_in.processor or via the decorator method.

    Note that the deserializer requires the client send a Content-Length
    request header, or it will raise "411 Length Required". If for any
    other reason the request entity cannot be deserialized from JSON,
    it will raise "400 Bad Request: Invalid JSON document".

    You must be using Python 2.6 or greater, or have the 'simplejson'
    package importable; otherwise, ValueError is raised during processing.
    """
    request = cherrypy.serving.request

    content_type = list(always_iterable(content_type))

    if force:
        if debug:
            cherrypy.log(
                'Removing body processors %s' %
                repr(request.body.processors.keys()), 'TOOLS.JSON_IN')
        request.body.processors.clear()
        request.body.default_proc = cherrypy.HTTPError(
            415,
            'Expected an entity of content type %s' % ', '.join(content_type))

    for ct in content_type:
        if debug:
            cherrypy.log('Adding body processor for %s' % ct, 'TOOLS.JSON_IN')
        request.body.processors[ct] = processor
Ejemplo n.º 18
0
def json_in(content_type=[ntou('application/json'), ntou('text/javascript')],
            force=True, debug=False, processor=json_processor):
    """Add a processor to parse JSON request entities:
    The default processor places the parsed data into request.json.

    Incoming request entities which match the given content_type(s) will
    be deserialized from JSON to the Python equivalent, and the result
    stored at cherrypy.request.json. The 'content_type' argument may
    be a Content-Type string or a list of allowable Content-Type strings.

    If the 'force' argument is True (the default), then entities of other
    content types will not be allowed; "415 Unsupported Media Type" is
    raised instead.

    Supply your own processor to use a custom decoder, or to handle the parsed
    data differently.  The processor can be configured via
    tools.json_in.processor or via the decorator method.

    Note that the deserializer requires the client send a Content-Length
    request header, or it will raise "411 Length Required". If for any
    other reason the request entity cannot be deserialized from JSON,
    it will raise "400 Bad Request: Invalid JSON document".

    You must be using Python 2.6 or greater, or have the 'simplejson'
    package importable; otherwise, ValueError is raised during processing.
    """
    request = cherrypy.serving.request

    content_type = list(always_iterable(content_type))

    if force:
        if debug:
            cherrypy.log('Removing body processors %s' %
                         repr(request.body.processors.keys()), 'TOOLS.JSON_IN')
        request.body.processors.clear()
        request.body.default_proc = cherrypy.HTTPError(
            415, 'Expected an entity of content type %s' %
            ', '.join(content_type))

    for ct in content_type:
        if debug:
            cherrypy.log('Adding body processor for %s' % ct, 'TOOLS.JSON_IN')
        request.body.processors[ct] = processor
Ejemplo n.º 19
0
 def names(self, channels=None):
     """Send a NAMES command."""
     self.send_items('NAMES', ','.join(always_iterable(channels)))
Ejemplo n.º 20
0
 def list(self, channels=None, server=""):
     """Send a LIST command."""
     self.send_items('LIST', ','.join(always_iterable(channels)), server)
Ejemplo n.º 21
0
 def whois(self, targets):
     """Send a WHOIS command."""
     self.send_raw("WHOIS " + ",".join(always_iterable(targets)))
Ejemplo n.º 22
0
 def names(self, channels=None):
     """Send a NAMES command."""
     tmpl = "NAMES {channels}" if channels else "NAMES"
     channels = ','.join(always_iterable(channels))
     self.send_raw(tmpl.format(channels=channels))
Ejemplo n.º 23
0
 def from_doc(cls, doc):
     """
     Load instances from the xmltodict result. Always return
     an iterable, even if the result is a singleton.
     """
     return map(cls, always_iterable(doc))
Ejemplo n.º 24
0
 def from_specs(cls, specs):
     return cls(map(RenameSpec.from_spec, always_iterable(specs)))
Ejemplo n.º 25
0
 def from_doc(cls, doc):
     """
     Load instances from the xmltodict result. Always return
     an iterable, even if the result is a singleton.
     """
     return map(cls, always_iterable(doc))
Ejemplo n.º 26
0
	def get_channels(self, key):
		spec = pmxbot.config.get(self.channel_spec_config, {})
		default_channels = spec.get('default', [])
		matching_channels = spec.get(key, default_channels)
		return always_iterable(matching_channels)
Ejemplo n.º 27
0
 def from_specs(cls, specs):
     return cls(map(cls.item, always_iterable(specs)))
Ejemplo n.º 28
0
 def part(self, channels, message=""):
     """Send a PART command."""
     self.send_items('PART', ','.join(always_iterable(channels)), message)
Ejemplo n.º 29
0
 def whois(self, targets):
     """Send a WHOIS command."""
     self.send_items('WHOIS', ",".join(always_iterable(targets)))
Ejemplo n.º 30
0
 def from_specs(cls, specs):
     return cls(map(RenameSpec.from_spec, always_iterable(specs)))