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)
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))
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
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
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)
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))
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
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()
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) }
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
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
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)
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
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
def names(self, channels=None): """Send a NAMES command.""" self.send_items('NAMES', ','.join(always_iterable(channels)))
def list(self, channels=None, server=""): """Send a LIST command.""" self.send_items('LIST', ','.join(always_iterable(channels)), server)
def whois(self, targets): """Send a WHOIS command.""" self.send_raw("WHOIS " + ",".join(always_iterable(targets)))
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))
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))
def from_specs(cls, specs): return cls(map(RenameSpec.from_spec, always_iterable(specs)))
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)
def from_specs(cls, specs): return cls(map(cls.item, always_iterable(specs)))
def part(self, channels, message=""): """Send a PART command.""" self.send_items('PART', ','.join(always_iterable(channels)), message)
def whois(self, targets): """Send a WHOIS command.""" self.send_items('WHOIS', ",".join(always_iterable(targets)))