try:<EOL><INDENT>is_telegraf = self.core.get_option('<STR_LIT>', "<STR_LIT>")<EOL><DEDENT>except KeyError:<EOL><INDENT>is_telegraf = None<EOL><DEDENT>try:<EOL><INDENT>is_monitoring = self.core.get_option('<STR_LIT>', "<STR_LIT>")<EOL><DEDENT>except KeyError:<EOL><INDENT>is_monitoring = None<EOL><DEDENT>if is_telegraf and is_monitoring:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if is_telegraf and not is_monitoring:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if not is_telegraf and is_monitoring:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if not is_telegraf and not is_monitoring:<EOL><INDENT>try:<EOL><INDENT>is_telegraf_dt = self.core.get_option('<STR_LIT>')<EOL><DEDENT>except NoOptionError:<EOL><INDENT>is_telegraf_dt = None<EOL><DEDENT>try:<EOL><INDENT>is_monitoring_dt = self.core.get_option('<STR_LIT>')<EOL><DEDENT>except BaseException:<EOL><INDENT>is_monitoring_dt = None<EOL><DEDENT>if is_telegraf_dt and is_monitoring_dt:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if is_telegraf_dt and not is_monitoring_dt:<EOL><INDENT>return<EOL><DEDENT>if not is_telegraf_dt and is_monitoring_dt:<EOL><INDENT>self.core.set_option(<EOL>"<STR_LIT>", "<STR_LIT>", is_monitoring_dt)<EOL><DEDENT>if not is_telegraf_dt and not is_monitoring_dt:<EOL><INDENT>return<EOL><DEDENT><DEDENT>

we need to be flexible in order to determine which plugin's configuration
specified and make appropriate configs to metrics collector

:return: SECTION name or None for defaults


def __make_points(self, measurement, additional_tags, ts, fields):

tags = self.tags.copy()<EOL>tags.update(additional_tags)<EOL>return {<EOL>"<STR_LIT>": measurement,<EOL>"<STR_LIT>": tags,<EOL>"<STR_LIT:time>": int(ts),<EOL>"<STR_LIT>": fields,<EOL>}<EOL>

Parameters
----------
measurement : string
    measurement type (e.g. monitoring, overall_meta, net_codes, proto_codes, overall_quantiles)
additional_tags : dict
    custom additional tags for this points
ts : integer
    timestamp
fields : dict
    influxdb columns

Returns
-------
dict
    points for InfluxDB client


if not notify_list:<EOL><INDENT>notify_list = []<EOL><DEDENT>data = {<EOL>'<STR_LIT>': task,<EOL>'<STR_LIT>': person,<EOL>'<STR_LIT>': tank,<EOL>'<STR_LIT:host>': target_host,<EOL>'<STR_LIT:port>': target_port,<EOL>'<STR_LIT>': loadscheme,<EOL>'<STR_LIT>': detailed_time,<EOL>'<STR_LIT>': notify_list<EOL>}<EOL>logger.debug("<STR_LIT>", data)<EOL>api_timeouts = self.api_timeouts()<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>response = self.__post(<EOL>"<STR_LIT>", data, trace=trace)[<NUM_LIT:0>]<EOL>return response['<STR_LIT>'], response['<STR_LIT>']<EOL><DEDENT>except (self.NotAvailable, self.StoppedFromOnline) as e:<EOL><INDENT>try:<EOL><INDENT>timeout = next(api_timeouts)<EOL>logger.warn("<STR_LIT>" % timeout)<EOL>time.sleep(timeout)<EOL>continue<EOL><DEDENT>except StopIteration:<EOL><INDENT>logger.warn('<STR_LIT>')<EOL>raise self.JobNotCreated(e.message)<EOL><DEDENT><DEDENT>except requests.HTTPError as e:<EOL><INDENT>raise self.JobNotCreated('<STR_LIT>'.format(e.response.content))<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.warn('<STR_LIT>')<EOL>logger.warn(repr(e), )<EOL>raise self.JobNotCreated()<EOL><DEDENT><DEDENT>

:return: job_nr, upload_token
:rtype: tuple


request = requests.Request(<EOL>http_method,<EOL>self.writer_url,<EOL>params=params,<EOL>json=json,<EOL>headers={<EOL>'<STR_LIT>': self.user_agent})<EOL>ids = id_gen(str(uuid.uuid4()))<EOL>network_timeouts = self.network_timeouts()<EOL>maintenance_timeouts = self.maintenance_timeouts()<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>response = self.__send_single_request(request, ids.next(), trace=trace)<EOL>return response<EOL><DEDENT>except (Timeout, ConnectionError, ProtocolError):<EOL><INDENT>logger.warn(traceback.format_exc())<EOL>try:<EOL><INDENT>timeout = next(network_timeouts)<EOL>logger.warn(<EOL>"<STR_LIT>" %<EOL>timeout)<EOL>time.sleep(timeout)<EOL>continue<EOL><DEDENT>except StopIteration:<EOL><INDENT>raise self.NetworkError()<EOL><DEDENT><DEDENT>except self.UnderMaintenance as e:<EOL><INDENT>try:<EOL><INDENT>timeout = next(maintenance_timeouts)<EOL>logger.warn(<EOL>"<STR_LIT>" %<EOL>timeout)<EOL>time.sleep(timeout)<EOL>continue<EOL><DEDENT>except StopIteration:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT><DEDENT>


results = []<EOL>for line in lines:<EOL><INDENT>timestamp, rps, instances = line.split("<STR_LIT:\t>")<EOL>curr_ts = int(float(timestamp))  <EOL>if self.__last_ts < curr_ts:<EOL><INDENT>self.__last_ts = curr_ts<EOL>results.append(self.stats_item(self.__last_ts, float(rps), float(instances)))<EOL><DEDENT><DEDENT>return results<EOL>


raise RuntimeError("<STR_LIT>")<EOL>

Render method, fills .lines and .width properties with rendered data


Notification method about new aggregator data


pass<EOL>


right_line = '<STR_LIT>'<EOL>if widget_output:<EOL><INDENT>right_line = widget_output.pop(<NUM_LIT:0>)<EOL>if len(right_line) > self.right_panel_width:<EOL><INDENT>right_line_plain = self.markup.clean_markup(right_line)<EOL>if len(right_line_plain) > self.right_panel_width:<EOL><INDENT>right_line = right_line[:self.right_panel_width] + self.markup.RESET<EOL><DEDENT><DEDENT><DEDENT>return right_line<EOL>


for val in self.get_markup_vars():<EOL><INDENT>orig_str = orig_str.replace(val, '<STR_LIT>')<EOL><DEDENT>return orig_str<EOL>


<EOL>configs = [resource_filename(__name__, '<STR_LIT>')]<EOL>try:<EOL><INDENT>conf_files = sorted(os.listdir(self.baseconfigs_location))<EOL>for filename in conf_files:<EOL><INDENT>if fnmatch.fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>configs += [<EOL>os.path.realpath(<EOL>self.baseconfigs_location + os.sep + filename)<EOL>]<EOL><DEDENT><DEDENT><DEDENT>except OSError:<EOL><INDENT>self.log.warn(<EOL>self.baseconfigs_location + '<STR_LIT>')<EOL><DEDENT>configs += [os.path.expanduser('<STR_LIT>')]<EOL>return configs<EOL>

returns default configs list, from /etc, home dir and package_data


if self.options.get('<STR_LIT>', None):<EOL><INDENT>self.core.apply_shorthand_options(self.options['<STR_LIT>'])<EOL><DEDENT>

override config options with user specified options


PLUGINS_TO_COMBINE = {<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>', True),<EOL>'<STR_LIT>': ('<STR_LIT>', '<STR_LIT>', False)<EOL>}<EOL>plugins = {}<EOL>ready_sections = []<EOL>for section in sections:<EOL><INDENT>if section.plugin in PLUGINS_TO_COMBINE.keys():<EOL><INDENT>try:<EOL><INDENT>plugins[section.plugin].append(section)<EOL><DEDENT>except KeyError:<EOL><INDENT>plugins[section.plugin] = [section]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ready_sections.append(section)<EOL><DEDENT><DEDENT>for plugin_name, _sections in plugins.items():<EOL><INDENT>if isinstance(_sections, list):<EOL><INDENT>parent_name, child_name, is_list = PLUGINS_TO_COMBINE[plugin_name]<EOL>ready_sections.append(Section.from_multiple(_sections, parent_name, child_name, is_list))<EOL><DEDENT><DEDENT>return ready_sections<EOL>

:type sections: list of Section
:rtype: list of Section


DEPRECATED_PLUGINS = ['<STR_LIT>', '<STR_LIT>']<EOL>plugin_instances = [PluginInstance(key.split('<STR_LIT:_>')[<NUM_LIT:1>], value) for key, value in core_opts if<EOL>key.startswith(PLUGIN_PREFIX) and value not in DEPRECATED_PLUGINS]<EOL>enabled_instances = {instance.section_name: instance for instance in plugin_instances if instance.enabled}<EOL>disabled_instances = {instance.section_name: instance for instance in plugin_instances if not instance.enabled}<EOL>for section in sections:<EOL><INDENT>if section.name in enabled_instances.keys():<EOL><INDENT>section.enabled = True<EOL>enabled_instances.pop(section.name)<EOL><DEDENT>elif section.name in disabled_instances.keys():<EOL><INDENT>section.enabled = False<EOL>disabled_instances.pop(section.name)<EOL><DEDENT><DEDENT>for plugin_instance in [i for i in plugin_instances if<EOL>i.section_name in enabled_instances.keys() + disabled_instances.keys()]:<EOL><INDENT>sections.append(Section(plugin_instance.section_name, plugin_instance.plugin_name, [], plugin_instance.enabled))<EOL><DEDENT>return sections<EOL>


return [Section(section.lower(),<EOL>guess_plugin(section.lower()),<EOL>without_defaults(cfg_ini, section))<EOL>for section in cfg_ini.sections()<EOL>if not re.match(CORE_SECTION_PATTERN, section.lower()) and section.lower() not in DEPRECATED_SECTIONS]<EOL>


return filter(lambda option: not is_option_deprecated(plugin, option[<NUM_LIT:0>]), options)<EOL>


prepared_content = content.strip().replace('<STR_LIT:\n>', new_line_replacement).replace('<STR_LIT:\t>', tab_replacement)<EOL>return u'<STR_LIT>'.format(prepared_content, '<STR_LIT:=>' * len(prepared_content))<EOL>

Underlines content with '='. New lines and tabs will be replaced
:param str content:
:param str new_line_replacement:
:param str tab_replacement:
:return: unicode


HEADER = '<STR_LIT>'<EOL>cnt = len(blocks)<EOL>if cnt < <NUM_LIT:2>:<EOL><INDENT>return blocks[<NUM_LIT:0>] if blocks else '<STR_LIT>'<EOL><DEDENT>width = max((len(HEADER), sum([c.padded_width for c in blocks]))) + (cnt + <NUM_LIT:1>)<EOL>height = max([c.height for c in blocks])<EOL>top_bar = '<STR_LIT>'.format('<STR_LIT:->' * (width - <NUM_LIT:2>))<EOL>header_bar = '<STR_LIT>'.format('<STR_LIT:+>'.join(['<STR_LIT:=>' * c.padded_width for c in blocks]))<EOL>bottom_bar = '<STR_LIT>'.format('<STR_LIT:+>'.join(['<STR_LIT:->' * c.padded_width for c in blocks]))<EOL>header = '<STR_LIT>'.format(HEADER.center(width - <NUM_LIT:2>))<EOL>body = '<STR_LIT:\n>'.join(<EOL>['<STR_LIT>'.format('<STR_LIT>'.join([c.get_line_justified(i) for c in blocks])) for i in range(height)])<EOL>return '<STR_LIT:\n>'.join([top_bar,<EOL>header,<EOL>header_bar,<EOL>body,<EOL>bottom_bar])<EOL>


def render_body(renderer, option_kwargs, exclude_keys, special_keys=None):

common_formatters = {<EOL>EXAMPLES: lambda examples: renderer.def_list({renderer.mono(example): annotation for example, annotation in examples.items()})<EOL>}<EOL>def default_fmt(x):<EOL><INDENT>return x<EOL><DEDENT>special_keys = special_keys or {}<EOL>special_part = '<STR_LIT:\n>'.join([special_handler(renderer, option_kwargs[special_key])<EOL>for special_key, special_handler in special_keys.items()<EOL>if special_key in option_kwargs])<EOL>common_part = renderer.field_list({k: common_formatters.get(k, default_fmt)(v) for k, v in option_kwargs.items()<EOL>if k not in exclude_keys + special_keys.keys()})<EOL>return '<STR_LIT:\n>'.join([_ for _ in [common_part, special_part] if _])<EOL>

:type option_kwargs: dict
:type exclude_keys: list
:type special_keys: dict


return '<STR_LIT>' + '<STR_LIT>'.join(block.lines)<EOL>


raise NotImplementedError("<STR_LIT>")<EOL>

long explanation to show after test stop


return self.Info(**self.DEFAULT_INFO)<EOL>


self.log.debug(<EOL>"<STR_LIT>", self.__class__.__name__, key, value)<EOL>self.core.publish(self.__class__.__name__, key, value)<EOL>


pass<EOL>

Release allocated resources here.
Warning: don't do any logic or potentially dangerous operations


return []<EOL>

returns array containing known options for plugin


assert callable(action)<EOL>self._cleanup_actions.append(action)<EOL>


return '<STR_LIT:.>'.join([str(random.randint(<NUM_LIT:0>, <NUM_LIT:255>)) for i in xrange(<NUM_LIT:0>, <NUM_LIT:4>)])<EOL>


if not user:<EOL><INDENT>user = user_name()<EOL><DEDENT>else:<EOL><INDENT>user = user.strip().replace('<STR_LIT:U+0020>', '<STR_LIT:_>').lower()<EOL><DEDENT>return user + '<STR_LIT:@>' + domain_name()<EOL>

Random e-mail address in a hopefully imaginary domain.

If `user` is ``None`` :py:func:`~user_name()` will be used. Otherwise it
will be lowercased and will have spaces replaced with ``_``.

Domain name is created using :py:func:`~domain_name()`.


return random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>


result = random.choice(get_dictionary('<STR_LIT>')).strip()<EOL>result += '<STR_LIT:.>' + top_level_domain()<EOL>return result.lower()<EOL>

Random domain name.

Lowercased result of :py:func:`~forgery_py.forgery.name.company_name()`
plus :py:func:`~top_level_domain()`.


result = words(quantity=words_quantity)<EOL>result += random.choice('<STR_LIT>')<EOL>return result.capitalize()<EOL>

Random sentence to be used as e.g. an e-mail subject.


def paragraph(separator='<STR_LIT>', wrap_start='<STR_LIT>', wrap_end='<STR_LIT>',<EOL>html=False, sentences_quantity=<NUM_LIT:3>):

return paragraphs(quantity=<NUM_LIT:1>, separator=separator, wrap_start=wrap_start,<EOL>wrap_end=wrap_end, html=html,<EOL>sentences_quantity=sentences_quantity)<EOL>


result = [sntc.strip() for sntc in<EOL>random.sample(get_dictionary('<STR_LIT>'), quantity)]<EOL>if as_list:<EOL><INDENT>return result<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT:U+0020>'.join(result)<EOL><DEDENT>


if numerical:<EOL><INDENT>return random.randint(<NUM_LIT:1>, <NUM_LIT:12>)<EOL><DEDENT>else:<EOL><INDENT>if abbr:<EOL><INDENT>return random.choice(MONTHS_ABBR)<EOL><DEDENT>else:<EOL><INDENT>return random.choice(MONTHS)<EOL><DEDENT><DEDENT>

Random (abbreviated if `abbr`) month name or month number if
`numerical`.


def year(past=False, min_delta=<NUM_LIT:0>, max_delta=<NUM_LIT:20>):

return datetime.date.today().year + _delta(past, min_delta, max_delta)<EOL>


return '<STR_LIT>'.join(random.sample(HEX_DIGITS, <NUM_LIT:6>))<EOL>


return '<STR_LIT>'.join(random.sample(HEX_DIGITS, <NUM_LIT:3>))<EOL>


format = '<STR_LIT>'<EOL>result = '<STR_LIT>'<EOL>for item in format:<EOL><INDENT>if item == '<STR_LIT:#>':<EOL><INDENT>result += str(random.randint(<NUM_LIT:0>, <NUM_LIT:9>))<EOL><DEDENT>else:<EOL><INDENT>result += item<EOL><DEDENT><DEDENT>return result<EOL>

Random phone number in `#-(###)###-####` format.


for k, v in u.items():<EOL><INDENT>if isinstance(v, Mapping):<EOL><INDENT>d[k] = deep_update(d.get(k, {}), v)<EOL><DEDENT>elif isinstance(v, list):<EOL><INDENT>existing_elements = d.get(k, [])<EOL>d[k] = existing_elements + [ele for ele in v if ele not in existing_elements]<EOL><DEDENT>else:<EOL><INDENT>d[k] = v<EOL><DEDENT><DEDENT>return d<EOL>

Deeply updates a dictionary. List values are concatenated.

    Args:
      d (dict): First dictionary which will be updated
      u (dict): Second dictionary use to extend the first one

    Returns:
      dict: The merge dictionary


context = super(RenditionAwareStructBlock, self).get_context(value)<EOL>context['<STR_LIT>'] = self.rendition.image_rendition or '<STR_LIT>'<EOL>return context<EOL>

Ensure `image_rendition` is added to the global context.


def register_block(self, block_type, block):

self._verify_block(block_type, block)<EOL>self._registry[block_type] = block<EOL>

Registers `block` to `block_type` in the registry.


async def post_guild_count(<EOL>self,<EOL>shard_count: int = None,<EOL>shard_no: int = None<EOL>):

await self.http.post_guild_count(self.bot_id, self.guild_count(), shard_count, shard_no)<EOL>

This function is a coroutine.

        Posts the guild count to discordbots.org

        .. _0 based indexing : https://en.wikipedia.org/wiki/Zero-based_numbering

        Parameters
        ==========

        shard_count: int[Optional]
            The total number of shards.
        shard_no: int[Optional]
            The index of the current shard. DBL uses `0 based indexing`_ for shards.


async def get_bot_info(self, bot_id: int = None):

if bot_id is None:<EOL><INDENT>bot_id = self.bot_id<EOL><DEDENT>return await self.http.get_bot_info(bot_id)<EOL>

This function is a coroutine.

        Gets information about a bot from discordbots.org

        Parameters
        ==========

        bot_id: int[Optional]
            The bot_id of the bot you want to lookup.

        Returns
        =======

        bot_info: dict
            Information on the bot you looked up.
            https://discordbots.org/api/docs#bots


async def generate_widget_large(<EOL>self,<EOL>bot_id: int = None,<EOL>top: str = '<STR_LIT>',<EOL>mid: str = '<STR_LIT>',<EOL>user: str = '<STR_LIT>',<EOL>cert: str = '<STR_LIT>',<EOL>data: str = '<STR_LIT>',<EOL>label: str = '<STR_LIT>',<EOL>highlight: str = '<STR_LIT>'<EOL>):

url = '<STR_LIT>'<EOL>if bot_id is None:<EOL><INDENT>bot_id = self.bot_id<EOL><DEDENT>url = url.format(bot_id, top, mid, user, cert, data, label, highlight)<EOL>return url<EOL>

This function is a coroutine.

        Generates a custom large widget. Do not add `#` to the color codes (e.g. #FF00FF become FF00FF).

        Parameters
        ==========

        bot_id: int
            The bot_id of the bot you wish to make a widget for.
        top: str
            The hex color code of the top bar.
        mid: str
            The hex color code of the main section.
        user: str
            The hex color code of the username text.
        cert: str
            The hex color code of the certified text (if applicable).
        data: str
            The hex color code of the statistics (numbers only e.g. 44) of the bot.
        label: str
            The hex color code of the description (text e.g. guild count) of the statistics.
        highlight: str
            The hex color code of the data boxes.

        Returns
        =======

        URL of the widget: str


async def get_guild_count(self, bot_id: int=None):

if bot_id is None:<EOL><INDENT>bot_id = self.bot_id<EOL><DEDENT>return await self.http.get_guild_count(bot_id)<EOL>

This function is a coroutine.

        Gets a guild count from discordbots.org

        Parameters
        ==========

        bot_id: int[Optional]
            The bot_id of the bot you want to lookup.
            Defaults to the Bot provided in Client init

        Returns
        =======

        stats: dict
            The guild count and shards of a bot.
            The date object is returned in a datetime.datetime object


duration = int(round(until - time.time()))<EOL>mins = duration / <NUM_LIT><EOL>fmt = '<STR_LIT>'<EOL>log.warn(fmt, duration, mins)<EOL>

Handles the message shown when we are ratelimited


async def request(self, method, url, **kwargs):

rate_limiter = RateLimiter(max_calls=<NUM_LIT>, period=<NUM_LIT>, callback=limited)<EOL>async with rate_limiter:  <EOL><INDENT>if not self.token:<EOL><INDENT>raise UnauthorizedDetected('<STR_LIT>')<EOL><DEDENT>headers = {<EOL>'<STR_LIT>': self.user_agent,<EOL>'<STR_LIT:Content-Type>': '<STR_LIT:application/json>'<EOL>}<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT:data>'] = to_json(kwargs.pop('<STR_LIT>'))<EOL><DEDENT>kwargs['<STR_LIT>'] = headers<EOL>headers['<STR_LIT>'] = self.token<EOL>for tries in range(<NUM_LIT:5>):<EOL><INDENT>async with self.session.request(method, url, **kwargs) as resp:<EOL><INDENT>log.debug('<STR_LIT>', method,<EOL>url, kwargs.get('<STR_LIT:data>'), resp.status)<EOL>data = await json_or_text(resp)<EOL>if <NUM_LIT> > resp.status >= <NUM_LIT:200>:<EOL><INDENT>return data<EOL><DEDENT>if resp.status == <NUM_LIT>:  <EOL><INDENT>fmt = '<STR_LIT>'<EOL>retry_after = json.loads(resp.headers.get('<STR_LIT>'))<EOL>mins = retry_after / <NUM_LIT><EOL>log.warning(fmt, retry_after, mins)<EOL>is_global = True  <EOL>if is_global:<EOL><INDENT>self._global_over.clear()<EOL><DEDENT>await asyncio.sleep(retry_after, loop=self.loop)<EOL>log.debug('<STR_LIT>')<EOL>if is_global:<EOL><INDENT>self._global_over.set()<EOL>log.debug('<STR_LIT>')<EOL><DEDENT>continue<EOL><DEDENT>if resp.status == <NUM_LIT>:<EOL><INDENT>raise HTTPException(resp, data)<EOL><DEDENT>elif resp.status == <NUM_LIT>:<EOL><INDENT>raise Unauthorized(resp, data)<EOL><DEDENT>elif resp.status == <NUM_LIT>:<EOL><INDENT>raise Forbidden(resp, data)<EOL><DEDENT>elif resp.status == <NUM_LIT>:<EOL><INDENT>raise NotFound(resp, data)<EOL><DEDENT>else:<EOL><INDENT>raise HTTPException(resp, data)<EOL><DEDENT><DEDENT><DEDENT>raise HTTPException(resp, data)<EOL><DEDENT>


async def get_bots(self, limit, offset):

if limit > <NUM_LIT>:<EOL><INDENT>limit = <NUM_LIT:50><EOL><DEDENT>return await self.request('<STR_LIT:GET>', '<STR_LIT>'.format(self.BASE, limit, offset))<EOL>


kwargs = {}<EOL>filters = None<EOL>if isinstance(data, dict):<EOL><INDENT>kwargs.update(<EOL>filters=data.get('<STR_LIT>', None),<EOL>output=data.get('<STR_LIT>', None),<EOL>debug=data.get('<STR_LIT>', None),<EOL>extra=data.get('<STR_LIT>', {}),<EOL>config=data.get('<STR_LIT>', {}),<EOL>depends=data.get('<STR_LIT>', None))<EOL><DEDENT>bundle = Bundle(*list(self._yield_bundle_contents(data)), **kwargs)<EOL>return self._auto_filter_bundle(bundle)<EOL>

Return a bundle initialised by the given dict.


if isinstance(data, list):<EOL><INDENT>contents = data<EOL><DEDENT>else:<EOL><INDENT>contents = data.get('<STR_LIT>', [])<EOL>if isinstance(contents, six.string_types):<EOL><INDENT>contents = contents,<EOL><DEDENT><DEDENT>for content in contents:<EOL><INDENT>if isinstance(content, dict):<EOL><INDENT>content = self._create_bundle(content)<EOL><DEDENT>yield content<EOL><DEDENT>

Yield bundle contents from the given dict.

        Each item yielded will be either a string representing a file path
        or a bundle.


node = encode_xml(obj, nsprefix)<EOL>return etree.tostring(node)<EOL>

Encodes an OpenMath element into a string.

    :param obj: Object to encode as string.
    :type obj: OMAny

    :rtype: bytes


def register_to_openmath(self, py_class, converter):

if py_class is not None and not isclass(py_class):<EOL><INDENT>raise TypeError('<STR_LIT>' % py_class)<EOL><DEDENT>if not callable(converter) and not isinstance(converter, om.OMAny):<EOL><INDENT>raise TypeError('<STR_LIT>' % converter)<EOL><DEDENT>self._conv_to_om.append((py_class, converter))<EOL>

Register a conversion from Python to OpenMath

        :param py_class: A Python class the conversion is attached to, or None
        :type py_class: None, type

        :param converter: A conversion function or an OpenMath object
        :type converter: Callable, OMAny

        :rtype: None

        ``converter`` will used to convert any object of type ``py_class``,
        or any object if ``py_class`` is ``None``. If ``converter`` is an
        OpenMath object, it is returned immediately. If it is a callable, it
        is called with the Python object as paramter; in this case, it must
        either return an OpenMath object, or raise an exception.  The
        special exception ``CannotConvertError`` can be used to signify that
        ``converter`` does not know how to convert the current object, and that
        ``to_openmath`` shall continue with the other converters. Any other
        exception stops conversion immediately.

        Converters registered by this function are called in order from the
        most recent to the oldest.


kwargs.update({"<STR_LIT>": self,  '<STR_LIT>': list(args)})<EOL>return OMApplication(**kwargs)<EOL>

Create an OpenMath Application Object using this object


return self.__getattr__(name)<EOL>


if hasattr(x, "<STR_LIT>") and x._ishelper:<EOL><INDENT>return x._toOM()<EOL><DEDENT>elif isinstance(x, om.OMAny):<EOL><INDENT>return x<EOL><DEDENT>elif isinstance(x, six.integer_types):<EOL><INDENT>return om.OMInteger(x)<EOL><DEDENT>elif isinstance(x, float):<EOL><INDENT>return om.OMFloat(x)<EOL><DEDENT>elif isinstance(x, six.string_types):<EOL><INDENT>return om.OMString(x)<EOL><DEDENT>elif isinstance(x, WrappedHelper):<EOL><INDENT>return x.toOM()<EOL><DEDENT>elif inspect.isfunction(x):<EOL><INDENT>paramMap = inspect.signature(x).parameters<EOL>params = [v for k, v in six.iteritems(paramMap)]<EOL>posArgKinds = [inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD]<EOL>if not all([p.kind in posArgKinds for p in params]):<EOL><INDENT>raise CannotInterpretAsOpenMath("<STR_LIT>")<EOL><DEDENT>paramsOM = [om.OMVariable(name=p.name) for p in params]<EOL>bodyOM = interpretAsOpenMath(x(*paramsOM))<EOL>return OMBinding(om.OMSymbol(name="<STR_LIT>", cd="<STR_LIT>", cdbase="<STR_LIT>"), paramsOM, bodyOM)<EOL><DEDENT>else:<EOL><INDENT>raise CannotInterpretAsOpenMath("<STR_LIT>" + str(x))<EOL><DEDENT>

tries to convert a Python object into an OpenMath object
    this is not a replacement for using a Converter for exporting Python objects
    instead, it is used conveniently building OM objects in DSL embedded in Python
    inparticular, it converts Python functions into OMBinding objects using lambdaOM as the binder


if self._cdhook is not None:<EOL><INDENT>return self._cdhook(self._cdbase, name, self._converter, self._symbolhook)<EOL><DEDENT>return CDHelper(self._cdbase, name, self._converter, self._symbolhook)<EOL>

returns a CDHelper object with the given name and this as the base


br_to_quality = {<NUM_LIT>: '<STR_LIT>', <NUM_LIT>: '<STR_LIT>'}<EOL>alters = NetEase().songs_detail_new_api(song_ids)<EOL>urls = [alter['<STR_LIT:url>'] for alter in alters]<EOL>return urls<EOL>


if args.files:<EOL><INDENT>dir_paths = args.files<EOL><DEDENT>else:<EOL><INDENT>dir_paths = [path for path in sys.path<EOL>if os.path.isdir(path)]<EOL><DEDENT>options = []<EOL>if args.expand_star_imports:<EOL><INDENT>options.append('<STR_LIT>')<EOL><DEDENT>if args.imports:<EOL><INDENT>options.append('<STR_LIT>' + args.imports)<EOL><DEDENT>if args.remove_all_unused_imports:<EOL><INDENT>options.append('<STR_LIT>')<EOL><DEDENT>if args.remove_duplicate_keys:<EOL><INDENT>options.append('<STR_LIT>')<EOL><DEDENT>if args.remove_unused_variables:<EOL><INDENT>options.append('<STR_LIT>')<EOL><DEDENT>filenames = dir_paths<EOL>completed_filenames = set()<EOL>while filenames:<EOL><INDENT>try:<EOL><INDENT>name = os.path.realpath(filenames.pop(<NUM_LIT:0>))<EOL>if not os.path.exists(name):<EOL><INDENT>continue<EOL><DEDENT>if name in completed_filenames:<EOL><INDENT>sys.stderr.write(<EOL>colored(<EOL>'<STR_LIT>' + name + '<STR_LIT:\n>',<EOL>YELLOW))<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>completed_filenames.update(name)<EOL><DEDENT>if os.path.isdir(name):<EOL><INDENT>for root, directories, children in os.walk(unicode(name)):<EOL><INDENT>filenames += [os.path.join(root, f) for f in children<EOL>if f.endswith('<STR_LIT>') and<EOL>not f.startswith('<STR_LIT:.>')]<EOL>directories[:] = [d for d in directories<EOL>if not d.startswith('<STR_LIT:.>')]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>verbose_message = '<STR_LIT>' + name<EOL>sys.stderr.write(colored(verbose_message + '<STR_LIT:\n>', YELLOW))<EOL>if not run(os.path.join(name),<EOL>command=args.command,<EOL>verbose=args.verbose,<EOL>options=options):<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>except (UnicodeDecodeError, UnicodeEncodeError) as exception:<EOL><INDENT>print(exception, file=sys.stderr)<EOL>continue<EOL><DEDENT><DEDENT>return True<EOL>

Run recursively run autoflake on directory of files.

    Return False if the fix results in broken syntax.


with autoflake.open_with_encoding(<EOL>filename,<EOL>encoding=autoflake.detect_encoding(filename)) as f:<EOL><INDENT>return len(list(autoflake.check(f.read())))<EOL><DEDENT>


def run(filename, command, verbose=False, options=None):

if not options:<EOL><INDENT>options = []<EOL><DEDENT>import test_autoflake<EOL>with test_autoflake.temporary_directory() as temp_directory:<EOL><INDENT>temp_filename = os.path.join(temp_directory,<EOL>os.path.basename(filename))<EOL>import shutil<EOL>shutil.copyfile(filename, temp_filename)<EOL>if <NUM_LIT:0> != subprocess.call(shlex.split(command) +<EOL>['<STR_LIT>', temp_filename] +<EOL>options):<EOL><INDENT>sys.stderr.write('<STR_LIT>' + filename + '<STR_LIT:\n>')<EOL>return False<EOL><DEDENT>try:<EOL><INDENT>file_diff = diff(filename, temp_filename)<EOL>if verbose:<EOL><INDENT>sys.stderr.write(file_diff)<EOL><DEDENT>if check_syntax(filename):<EOL><INDENT>try:<EOL><INDENT>check_syntax(temp_filename, raise_error=True)<EOL><DEDENT>except (SyntaxError, TypeError,<EOL>UnicodeDecodeError, ValueError) as exception:<EOL><INDENT>sys.stderr.write('<STR_LIT>' + filename + '<STR_LIT:\n>' +<EOL>str(exception) + '<STR_LIT:\n>')<EOL>return False<EOL><DEDENT><DEDENT>before_count = pyflakes_count(filename)<EOL>after_count = pyflakes_count(temp_filename)<EOL>if verbose:<EOL><INDENT>print('<STR_LIT>', (before_count, after_count))<EOL><DEDENT>if file_diff and after_count > before_count:<EOL><INDENT>sys.stderr.write('<STR_LIT>' + filename + '<STR_LIT>')<EOL>return False<EOL><DEDENT><DEDENT>except IOError as exception:<EOL><INDENT>sys.stderr.write(str(exception) + '<STR_LIT:\n>')<EOL><DEDENT><DEDENT>return True<EOL>

Run autoflake on file at filename.

    Return True on success.


return <NUM_LIT:0> if check(process_args()) else <NUM_LIT:1><EOL>


try:<EOL><INDENT>marked_lines = frozenset(useless_pass_line_numbers(source))<EOL><DEDENT>except (SyntaxError, tokenize.TokenError):<EOL><INDENT>marked_lines = frozenset()<EOL><DEDENT>sio = io.StringIO(source)<EOL>for line_number, line in enumerate(sio.readlines(), start=<NUM_LIT:1>):<EOL><INDENT>if line_number not in marked_lines:<EOL><INDENT>yield line<EOL><DEDENT><DEDENT>

Yield code with useless "pass" lines removed.


for name in standard_paths():<EOL><INDENT>if name.startswith('<STR_LIT:_>') or '<STR_LIT:->' in name:<EOL><INDENT>continue<EOL><DEDENT>if '<STR_LIT:.>' in name and name.rsplit('<STR_LIT:.>')[-<NUM_LIT:1>] not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>yield name.split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL><DEDENT>


if is_exclude_file(filename, exclude):<EOL><INDENT>return False<EOL><DEDENT>if not os.path.isdir(filename) and not is_python_file(filename):<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Return True if file is okay for modifying/recursing.


newline = '<STR_LIT:\n>'<EOL>diff = difflib.unified_diff(<EOL>old, new,<EOL>'<STR_LIT>' + filename,<EOL>'<STR_LIT>' + filename,<EOL>lineterm=newline)<EOL>text = '<STR_LIT>'<EOL>for line in diff:<EOL><INDENT>text += line<EOL>if not line.endswith(newline):<EOL><INDENT>text += newline + r'<STR_LIT>' + newline<EOL><DEDENT><DEDENT>return text<EOL>

Return text of unified diff between old and new.


def filter_duplicate_key(line, message, line_number, marked_line_numbers,<EOL>source, previous_line='<STR_LIT>'):

if marked_line_numbers and line_number == sorted(marked_line_numbers)[<NUM_LIT:0>]:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return line<EOL>

Return '' if first occurrence of the key otherwise return `line`.


for is_plat_spec in [True, False]:<EOL><INDENT>path = distutils.sysconfig.get_python_lib(standard_lib=True,<EOL>plat_specific=is_plat_spec)<EOL>for name in os.listdir(path):<EOL><INDENT>yield name<EOL><DEDENT>try:<EOL><INDENT>for name in os.listdir(os.path.join(path, '<STR_LIT>')):<EOL><INDENT>yield name<EOL><DEDENT><DEDENT>except OSError:  <EOL><INDENT>pass<EOL><DEDENT><DEDENT>


line_messages = {}<EOL>for message in messages:<EOL><INDENT>line_messages[message.lineno] = message<EOL><DEDENT>return line_messages<EOL>

Return dictionary that maps line number to message.


pattern = r'<STR_LIT>'<EOL>for message in messages:<EOL><INDENT>if isinstance(message, pyflakes.messages.UnusedImport):<EOL><INDENT>module_name = re.search(pattern, str(message))<EOL>module_name = module_name.group()[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>if module_name:<EOL><INDENT>yield (message.lineno, module_name)<EOL><DEDENT><DEDENT><DEDENT>

Yield line number and module name of unused imports.


for symbol in '<STR_LIT>':<EOL><INDENT>if symbol in line:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>if line.lstrip().startswith('<STR_LIT:>>'):<EOL><INDENT>return True<EOL><DEDENT>return multiline_statement(line, previous_line)<EOL>

Return True if import is spans multiples lines.


assert '<STR_LIT:\\>' not in line<EOL>assert '<STR_LIT:(>' not in line<EOL>assert '<STR_LIT:)>' not in line<EOL>assert '<STR_LIT:;>' not in line<EOL>assert '<STR_LIT:#>' not in line<EOL>assert not line.lstrip().startswith('<STR_LIT>')<EOL>newline = get_line_ending(line)<EOL>if not newline:<EOL><INDENT>return line<EOL><DEDENT>(indentation, imports) = re.split(pattern=r'<STR_LIT>',<EOL>string=line, maxsplit=<NUM_LIT:1>)<EOL>indentation += '<STR_LIT>'<EOL>assert newline<EOL>return '<STR_LIT>'.join([indentation + i.strip() + newline<EOL>for i in sorted(imports.split('<STR_LIT:U+002C>'))])<EOL>

Return line with imports on separate lines.


return set(text.strip() for text in string.split('<STR_LIT:U+002C>') if text.strip())<EOL>


for symbol in '<STR_LIT>':<EOL><INDENT>if symbol in line:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>sio = io.StringIO(line)<EOL>try:<EOL><INDENT>list(tokenize.generate_tokens(sio.readline))<EOL>return previous_line.rstrip().endswith('<STR_LIT:\\>')<EOL><DEDENT>except (SyntaxError, tokenize.TokenError):<EOL><INDENT>return True<EOL><DEDENT>

Return True if this is part of a multiline statement.


for conn in itervalues(self.conns):<EOL><INDENT>if conn.in_flight > <NUM_LIT:0> and conn.in_flight >= (conn.last_rdy * <NUM_LIT>):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Used to identify when buffered messages should be processed and responded to.

When max_in_flight > 1 and you're batching messages together to perform work
is isn't possible to just compare the len of your list of buffered messages against
your configured max_in_flight (because max_in_flight may not be evenly divisible
by the number of producers you're connected to, ie. you might never get that many
messages... it's a *max*).

Example::

    def message_handler(self, nsq_msg, reader):
        # buffer messages
        if reader.is_starved():
            # perform work

    reader = nsq.Reader(...)
    reader.set_message_handler(functools.partial(message_handler, reader=reader))
    nsq.run()


assert isinstance(max_in_flight, int)<EOL>self.max_in_flight = max_in_flight<EOL>if max_in_flight == <NUM_LIT:0>:<EOL><INDENT>for conn in itervalues(self.conns):<EOL><INDENT>if conn.rdy > <NUM_LIT:0>:<EOL><INDENT>logger.debug('<STR_LIT>', conn.id, self.name, conn.rdy)<EOL>self._send_rdy(conn, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>self.total_rdy = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>self.need_rdy_redistributed = True<EOL>self._redistribute_rdy_state()<EOL><DEDENT>

Dynamically adjust the reader max_in_flight. Set to 0 to immediately disable a Reader


return self._async_enabled<EOL>

Returns whether or not asynchronous processing has been enabled.


assert not self._has_responded<EOL>self._has_responded = True<EOL>self.trigger(event.FINISH, message=self)<EOL>

Respond to ``nsqd`` that you've processed this message successfully (or would like
to silently discard it).


return self._has_responded<EOL>

Returns whether or not this message has been responded to.


def trigger(self, name, *args, **kwargs):

for ev in self.__listeners[name]:<EOL><INDENT>ev(*args, **kwargs)<EOL><DEDENT>

Execute the callbacks for the listeners on the specified event with the
supplied arguments.

All extra arguments are passed through to each callback.

:param name: the name of the event
:type name: string


try:<EOL><INDENT>with click.open_file('<STR_LIT>', '<STR_LIT:r>') as f:<EOL><INDENT>contents = f.read()<EOL><DEDENT>app_json = json.loads(contents)<EOL><DEDENT>except IOError:<EOL><INDENT>return None<EOL><DEDENT>repository = app_json.get('<STR_LIT>')<EOL>if not repository:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return app_json.get('<STR_LIT>') + '<STR_LIT>'<EOL><DEDENT>

Returns the tarball URL inferred from an app.json, if present.


try:<EOL><INDENT>with click.open_file('<STR_LIT>', '<STR_LIT:r>') as f:<EOL><INDENT>return f.read().strip()<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>return None<EOL><DEDENT>

Reads the app name from the .happy file.


def create_build(self, tarball_url, env=None, app_name=None):

data = {<EOL>'<STR_LIT>': {<EOL>'<STR_LIT:url>': tarball_url<EOL>}<EOL>}<EOL>if env:<EOL><INDENT>data['<STR_LIT>'] = {'<STR_LIT>': env}<EOL><DEDENT>if app_name:<EOL><INDENT>data['<STR_LIT>'] = {'<STR_LIT:name>': app_name}<EOL><DEDENT>return self.api_request('<STR_LIT:POST>', '<STR_LIT>', data=data)<EOL>

Creates an app-setups build. Returns response data as a dict.

        :param tarball_url: URL of a tarball containing an ``app.json``.
        :param env: Dict containing environment variable overrides.
        :param app_name: Name of the Heroku app to create.
        :returns: Response data as a ``dict``.


self._api.delete_app(app_name=app_name)<EOL>

Deletes a Heroku app.

        :param app_name: Name of the Heroku app to delete.


self._api = Heroku(auth_token=auth_token)<EOL>

Initializes the class.

        :param auth_token: A Heroku API auth token.


def wrapped(func, runner, *args, **kwargs):<EOL><INDENT>with runner.isolated_filesystem():<EOL><INDENT>with open('<STR_LIT>', '<STR_LIT:w>') as f:<EOL><INDENT>f.write('<STR_LIT>')<EOL><DEDENT>return func(runner, *args, **kwargs)<EOL><DEDENT><DEDENT>return decorator.decorator(wrapped, func)<EOL>

Runs a method in an isolated filesystem.

    This also stubs out an app.json for convenience.


message = {<EOL>'<STR_LIT>': self.protocol,<EOL>'<STR_LIT>': self._node,<EOL>'<STR_LIT>': self._chip_id,<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': {},<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': [<EOL>'<STR_LIT>'<EOL>]<EOL>}<EOL>if type(data) is dict:<EOL><INDENT>for k, v in data.items():<EOL><INDENT>if k in message:<EOL><INDENT>message[k] = v<EOL><DEDENT><DEDENT><DEDENT>return message<EOL>


return self._node<EOL>


raise NotImplementedError("<STR_LIT>")<EOL>

prepares empty message adn merge it with data


doc = inspect.cleandoc(doc)<EOL>lines = doc.split('<STR_LIT:\n>')<EOL>section = None<EOL>section_indent = None<EOL>params = {}<EOL>returns = None<EOL>for line in lines:<EOL><INDENT>line = line.rstrip()<EOL>if len(line) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>elif str(line) == '<STR_LIT>':<EOL><INDENT>section = '<STR_LIT:args>'<EOL>section_indent = None<EOL>continue<EOL><DEDENT>elif str(line) == '<STR_LIT>':<EOL><INDENT>section = '<STR_LIT>'<EOL>section_indent = None<EOL>continue<EOL><DEDENT>if section is not None:<EOL><INDENT>stripped = line.lstrip()<EOL>margin = len(line) - len(stripped)<EOL>if section_indent is None:<EOL><INDENT>section_indent = margin<EOL><DEDENT>if margin != section_indent:<EOL><INDENT>continue<EOL><DEDENT>if section == '<STR_LIT:args>':<EOL><INDENT>param_name, type_info = parse_param(stripped)<EOL>params[param_name] = type_info<EOL><DEDENT>elif section == '<STR_LIT>':<EOL><INDENT>returns = parse_return(stripped)<EOL><DEDENT><DEDENT><DEDENT>return params, returns<EOL>

Parse a docstring into ParameterInfo and ReturnInfo objects.


line = '<STR_LIT>'<EOL>for i in range(<NUM_LIT:0>, len(arg), <NUM_LIT:16>):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>line += '<STR_LIT:\n>'<EOL><DEDENT>chunk = arg[i:i + <NUM_LIT:16>]<EOL>hex_chunk = hexlify(chunk).decode('<STR_LIT:utf-8>')<EOL>hex_line = '<STR_LIT:U+0020>'.join(hex_chunk[j:j + <NUM_LIT:2>] for j in range(<NUM_LIT:0>, len(hex_chunk), <NUM_LIT:2>))<EOL>if len(hex_line) < (<NUM_LIT:3> * <NUM_LIT:16>) - <NUM_LIT:1>:<EOL><INDENT>hex_line += '<STR_LIT:U+0020>' * (((<NUM_LIT:3> * <NUM_LIT:16>) - <NUM_LIT:1>) - len(hex_line))<EOL><DEDENT>ascii_line = '<STR_LIT>'.join(_convert_to_ascii(x) for x in chunk)<EOL>offset_line = '<STR_LIT>' % i<EOL>line += "<STR_LIT>" % (offset_line, hex_line, ascii_line)<EOL><DEDENT>return line<EOL>

Convert the bytes object to a hexdump.

    The output format will be:

    <offset, 4-byte>  <16-bytes of output separated by 1 space>  <16 ascii characters>


if arg not in choices:<EOL><INDENT>raise ValueError('<STR_LIT>' % str(choices))<EOL><DEDENT>

Make sure the argument is in the list of choices passed to the function


for name in (x for x in dir(module) if not x.startswith('<STR_LIT:_>')):<EOL><INDENT>typeobj = getattr(module, name)<EOL>try:<EOL><INDENT>self.inject_type(name, typeobj)<EOL><DEDENT>except ArgumentError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Given a module that contains a list of some types find all symbols in the module that
do not start with _ and attempt to import them as types.


type_name = str(type_name)<EOL>if type_name in self.known_types:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Check if type is known to the type system.

        Returns:
            bool: True if the type is a known instantiated simple type, False otherwise


if hasattr(typeobj, '<STR_LIT>'):<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Determine if typeobj is a factory for producing complex types


def instantiate_type(self, typename, base, subtypes):

if base not in self.type_factories:<EOL><INDENT>raise ArgumentError("<STR_LIT>", passed_type=typename, base_type=base)<EOL><DEDENT>base_type = self.type_factories[base]<EOL>for sub_type in subtypes:<EOL><INDENT>try:<EOL><INDENT>self.get_type(sub_type)<EOL><DEDENT>except KeyValueException as exc:<EOL><INDENT>raise ArgumentError("<STR_LIT>", passed_type=typename, sub_type=sub_type, error=exc)<EOL><DEDENT><DEDENT>typeobj = base_type.Build(*subtypes, type_system=self)<EOL>self.inject_type(typename, typeobj)<EOL>


name = self._canonicalize_type(typename)<EOL>if '<STR_LIT:(>' not in name:<EOL><INDENT>return name, False, []<EOL><DEDENT>base, sub = name.split('<STR_LIT:(>')<EOL>if len(sub) == <NUM_LIT:0> or sub[-<NUM_LIT:1>] != '<STR_LIT:)>':<EOL><INDENT>raise ArgumentError("<STR_LIT>", passed_type=typename, basetype=base, subtype_string=sub)<EOL><DEDENT>sub = sub[:-<NUM_LIT:1>]<EOL>subs = sub.split('<STR_LIT:U+002C>')<EOL>return base, True, subs<EOL>

Given a potentially complex type, split it into its base type and specializers


self.interactive = False<EOL>self.known_types = {}<EOL>self.type_factories = {}<EOL>self.logger = logging.getLogger(__name__)<EOL>for arg in args:<EOL><INDENT>self.load_type_module(arg)<EOL><DEDENT>self._lazy_type_sources = []<EOL>self.failed_sources = []<EOL>

Create a TypeSystem by importing all of the types defined in modules passed
as arguments to this function.  Each module is imported using


folder, filename = os.path.split(path)<EOL>try:<EOL><INDENT>fileobj, pathname, description = imp.find_module(filename, [folder])<EOL>mod = imp.load_module(filename, fileobj, pathname, description)<EOL><DEDENT>except ImportError as exc:<EOL><INDENT>raise ArgumentError("<STR_LIT>", module_path=path, parent_directory=folder, module_name=filename, error=str(exc))<EOL><DEDENT>self.load_type_module(mod)<EOL>

Given a path to a python package or module, load that module, search for all defined variables
inside of it that do not start with _ or __ and inject them into the type system.  If any of the
types cannot be injected, silently ignore them unless verbose is True.  If path points to a module
it should not contain the trailing .py since this is added automatically by the python import system


typeobj = self.get_type(type)<EOL>if hasattr(typeobj, '<STR_LIT:size>'):<EOL><INDENT>return typeobj.size()<EOL><DEDENT>return <NUM_LIT:0><EOL>

Get the size of this type for converting a hex string to the
type. Return 0 if the size is not known.


type_name = self._canonicalize_type(type_name)<EOL>if str(type_name) == '<STR_LIT:int>':<EOL><INDENT>type_name = '<STR_LIT>'<EOL><DEDENT>elif str(type_name) == '<STR_LIT:str>':<EOL><INDENT>type_name = '<STR_LIT:string>'<EOL><DEDENT>elif str(type_name) == '<STR_LIT>':<EOL><INDENT>type_name = '<STR_LIT>'<EOL><DEDENT>if self.is_known_type(type_name):<EOL><INDENT>return self.known_types[type_name]<EOL><DEDENT>base_type, is_complex, subtypes = self.split_type(type_name)<EOL>if is_complex and base_type in self.type_factories:<EOL><INDENT>self.instantiate_type(type_name, base_type, subtypes)<EOL>return self.known_types[type_name]<EOL><DEDENT>i = <NUM_LIT:0><EOL>for i, (source, name) in enumerate(self._lazy_type_sources):<EOL><INDENT>if isinstance(source, str):<EOL><INDENT>import pkg_resources<EOL>for entry in pkg_resources.iter_entry_points(source):<EOL><INDENT>try:<EOL><INDENT>mod = entry.load()<EOL>type_system.load_type_module(mod)<EOL><DEDENT>except:  <EOL><INDENT>fail_info = ("<STR_LIT>" % (source, entry.name), sys.exc_info)<EOL>logging.exception("<STR_LIT>", source, entry.name)<EOL>self.failed_sources.append(fail_info)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>source(self)<EOL><DEDENT>except:  <EOL><INDENT>fail_info = ("<STR_LIT>" % name, sys.exc_info)<EOL>logging.exception("<STR_LIT>", source)<EOL>self.failed_sources.append(fail_info)<EOL><DEDENT><DEDENT>if self.is_known_type(type_name) or (is_complex and base_type in self.type_factories):<EOL><INDENT>break<EOL><DEDENT><DEDENT>self._lazy_type_sources = self._lazy_type_sources[i:]<EOL>if not (self.is_known_type(type_name) or (is_complex and base_type in self.type_factories)):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=type_name, failed_external_sources=[x[<NUM_LIT:0>] for x in self.failed_sources])<EOL><DEDENT>return self.get_type(type_name)<EOL>

Return the type object corresponding to a type name.

        If type_name is not found, this triggers the loading of
        external types until a matching type is found or until there
        are no more external type sources.


<EOL>return None<EOL>

Process an @param decorator into a ParameterInfo.


def _context(cls):<EOL><INDENT>annotated(cls, name)<EOL>cls.context = True<EOL>return cls<EOL><DEDENT>return _context<EOL>

Declare that a class defines a context.

    Contexts are for use with HierarchicalShell for discovering
    and using functionality from the command line.

    Args:
        name (str): Optional name for this context if you don't want
            to just use the class name.


doc = inspect.getdoc(func)<EOL>if doc is not None:<EOL><INDENT>doc = inspect.cleandoc(doc)<EOL>lines = doc.splitlines()<EOL>return lines[<NUM_LIT:0>]<EOL><DEDENT>return "<STR_LIT>"<EOL>

Given an object with a docstring, return the first line of the docstring


def param(name, type_name, *validators, **kwargs):

def _param(func):<EOL><INDENT>func = annotated(func)<EOL>valids = _parse_validators(validators)<EOL>func.metadata.add_param(name, type_name, valids, **kwargs)<EOL>if func.decorated:<EOL><INDENT>return func<EOL><DEDENT>func.decorated = True<EOL>return decorate(func, _check_and_execute)<EOL><DEDENT>return _param<EOL>

Decorate a function to give type information about its parameters.

    This function stores a type name, optional description and optional list
    of validation functions along with the decorated function it is called
    on in order to allow run time type conversions and validation.

    Args:
        name (string): name of the parameter
        type_name (string): The name of a type that will be known to the type
            system by the time this function is called for the first time.  Types
            are lazily loaded so it is not required that the type resolve correctly
            at the point in the module where this function is defined.
        validators (list(string or tuple)): A list of validators.  Each validator
            can be defined either using a string name or as an n-tuple of the form
            [name, \\*extra_args].  The name is used to look up a validator function
            of the form validate_name, which is called on the parameters value to
            determine if it is valid.  If extra_args are given, they are passed
            as extra arguments to the validator function, which is called as:

            validator(value, \\*extra_args)
        desc (string): An optional descriptioon for this parameter that must be
            passed as a keyword argument.

    Returns:
        callable: A decorated function with additional type metadata


func = annotated(func)<EOL>func.metadata.load_from_doc = True<EOL>if func.decorated:<EOL><INDENT>return func<EOL><DEDENT>func.decorated = True<EOL>return decorate(func, _check_and_execute)<EOL>

Annotate a function using information from its docstring.

    The annotation actually happens at the time the function is first called
    to improve startup time.  For this function to work, the docstring must be
    formatted correctly.  You should use the typedargs pylint plugin to make
    sure there are no errors in the docstring.


help_text = "<STR_LIT>"<EOL>if isinstance(func, dict):<EOL><INDENT>name = context_name(func)<EOL>help_text = "<STR_LIT:\n>" + name + "<STR_LIT>"<EOL>doc = inspect.getdoc(func)<EOL>if doc is not None:<EOL><INDENT>doc = inspect.cleandoc(doc)<EOL>help_text += doc + '<STR_LIT:\n>'<EOL><DEDENT>return help_text<EOL><DEDENT>sig = func.metadata.signature()<EOL>doc = inspect.getdoc(func)<EOL>if doc is not None:<EOL><INDENT>doc = inspect.cleandoc(doc)<EOL><DEDENT>help_text += "<STR_LIT:\n>" + sig + "<STR_LIT>"<EOL>if doc is not None:<EOL><INDENT>help_text += doc + '<STR_LIT:\n>'<EOL><DEDENT>if inspect.isclass(func):<EOL><INDENT>func = func.__init__<EOL><DEDENT>if func.metadata.load_from_doc:<EOL><INDENT>return help_text<EOL><DEDENT>help_text += "<STR_LIT>"<EOL>for key, info in func.metadata.annotated_params.items():<EOL><INDENT>type_name = info.type_name<EOL>desc = "<STR_LIT>"<EOL>if info.desc is not None:<EOL><INDENT>desc = info.desc<EOL><DEDENT>help_text += "<STR_LIT>" % (key, type_name, desc)<EOL><DEDENT>return help_text<EOL>

Return usage information about a context or function.

    For contexts, just return the context name and its docstring
    For functions, return the function signature as well as its
    argument types.

    Args:
        func (callable): An annotated callable function

    Returns:
        str: The formatted help text


def _returns(func):<EOL><INDENT>annotated(func)<EOL>func.metadata.typed_returnvalue(type_name, formatter)<EOL>return func<EOL><DEDENT>return _returns<EOL>

Specify that this function returns a typed value.

    Args:
        type_name (str): A type name known to the global typedargs type system
        formatter (str): An optional name of a formatting function specified
            for the type given in type_name.


if arg == '<STR_LIT>':<EOL><INDENT>return False<EOL><DEDENT>if not arg.startswith('<STR_LIT:->'):<EOL><INDENT>return False<EOL><DEDENT>if arg.startswith('<STR_LIT>'):<EOL><INDENT>first_char = arg[<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>first_char = arg[<NUM_LIT:1>]<EOL><DEDENT>if not first_char.isalpha():<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Check if an argument is a flag.

        A flag starts with - or -- and the next character must be a letter
        followed by letters, numbers, - or _.  Currently we only check the
        alpha'ness of the first non-dash character to make sure we're not just
        looking at a negative number.

        Returns:
            bool: Whether the argument is a flag.


self.builtins[name] = callable<EOL>

Add a builtin function callable from all contexts.

        Callable should be an annotated function like any other that you
        want to call from a HierarchicalShell

        Args:
            name (str): The name of the function
            callable (callable): The annotated function that we should call


if len(args) == <NUM_LIT:0>:<EOL><INDENT>return self.list_dir(self.contexts[-<NUM_LIT:1>])<EOL><DEDENT>if len(args) == <NUM_LIT:1>:<EOL><INDENT>func = self.find_function(self.contexts[-<NUM_LIT:1>], args[<NUM_LIT:0>])<EOL>return annotate.get_help(func)<EOL><DEDENT>help_text = "<STR_LIT>" + str(args) + "<STR_LIT:\n>"<EOL>help_text += "<STR_LIT>"<EOL>return help_text<EOL>

Return help information for a context or function.


Add entries to root from a dict_line object.


def process_arguments(self, func, args):

pos_args = []<EOL>kw_args = {}<EOL>while len(args) > <NUM_LIT:0>:<EOL><INDENT>if func.metadata.spec_filled(pos_args, kw_args) and not self._is_flag(args[<NUM_LIT:0>]):<EOL><INDENT>break<EOL><DEDENT>arg = args.pop(<NUM_LIT:0>)<EOL>if arg == '<STR_LIT>':<EOL><INDENT>break<EOL><DEDENT>elif self._is_flag(arg):<EOL><INDENT>arg_value = None<EOL>arg_name = None<EOL>if len(arg) == <NUM_LIT:2>:<EOL><INDENT>arg_name = func.metadata.match_shortname(arg[<NUM_LIT:1>:], filled_args=pos_args)<EOL><DEDENT>else:<EOL><INDENT>if not arg.startswith('<STR_LIT>'):<EOL><INDENT>raise ArgumentError("<STR_LIT>", argument=arg)<EOL><DEDENT>arg = arg[<NUM_LIT:2>:]<EOL>if '<STR_LIT:=>' in arg:<EOL><INDENT>arg, arg_value = arg.split('<STR_LIT:=>', <NUM_LIT:1>)<EOL><DEDENT>arg_name = func.metadata.match_shortname(arg, filled_args=pos_args)<EOL><DEDENT>arg_type = func.metadata.param_type(arg_name)<EOL>if arg_type is None:<EOL><INDENT>raise ArgumentError("<STR_LIT>", argument=arg_name)<EOL><DEDENT>if arg_value is None:<EOL><INDENT>arg_value = self._extract_arg_value(arg_name, arg_type, args)<EOL><DEDENT>kw_args[arg_name] = arg_value<EOL><DEDENT>else:<EOL><INDENT>pos_args.append(arg)<EOL><DEDENT><DEDENT>if len(args) > <NUM_LIT:0> and args[<NUM_LIT:0>] == '<STR_LIT>':<EOL><INDENT>args.pop(<NUM_LIT:0>)<EOL><DEDENT>return pos_args, kw_args, args<EOL>

Process arguments from the command line into positional and kw args.

        Arguments are consumed until the argument spec for the function is filled
        or a -- is found or there are no more arguments.  Keyword arguments can be
        specified using --field=value, -f value or --field value.  Positional
        arguments are specified just on the command line itself.

        If a keyword argument (`field`) is a boolean, it can be set to True by just passing
        --field or -f without needing to explicitly pass True unless this would cause
        ambiguity in parsing since the next expected positional argument is also a boolean
        or a string.

        Args:
            func (callable): A function previously annotated with type information
            args (list): A list of all of the potential arguments to this function.

        Returns:
            (args, kw_args, unused args): A tuple with a list of args, a dict of
                keyword args and a list of any unused args that were not processed.


finished = True<EOL>while len(line) > <NUM_LIT:0>:<EOL><INDENT>val, line, finished = self.invoke_one(line)<EOL>if val is not None:<EOL><INDENT>iprint(val)<EOL><DEDENT><DEDENT>return finished<EOL>

Invoke a one or more function given a list of arguments.

        The functions are searched for using the current context on the context stack
        and its annotated type information is used to convert all of the string parameters
        passed in line to appropriate python types.

        Args:
            line (list): The list of command line arguments.

        Returns:
            bool: A boolean specifying if the last function created a new context
                (False if a new context was created) and a list with the remainder of the
                command line if this function did not consume all arguments.)


parts = shlex.split(line, posix=self.posix_lex)<EOL>if not self.posix_lex:<EOL><INDENT>parts = [self._remove_quotes(x) for x in parts]<EOL><DEDENT>return parts<EOL>

Split a line into arguments using shlex and a dequoting routine.


module, _, obj = add_action.partition('<STR_LIT:U+002C>')<EOL>mod = importlib.import_module(module)<EOL>if obj == "<STR_LIT>":<EOL><INDENT>_, con = annotate.context_from_module(mod)<EOL>return con<EOL><DEDENT>if hasattr(mod, obj):<EOL><INDENT>return getattr(mod, obj)<EOL><DEDENT>raise ArgumentError("<STR_LIT>", module=module, object=obj)<EOL>

Lazily load a callable.

        Perform a lazy import of a context so that we don't have a huge initial startup time
        loading all of the modules that someone might want even though they probably only
        will use a few of them.


self.return_info = ReturnInfo(None, str, True, None)<EOL>

Mark the return value as data that should be converted with str.


self._ensure_loaded()<EOL>if name not in self.annotated_params:<EOL><INDENT>return None<EOL><DEDENT>return self.annotated_params[name].type_name<EOL>

Get the parameter type information by name.

        Args:
            name (str): The full name of a parameter.

        Returns:
            str: The type name or None if no type information is given.


def check_spec(self, pos_args, kwargs=None):

if kwargs is None:<EOL><INDENT>kwargs = {}<EOL><DEDENT>if self.varargs is not None or self.kwargs is not None:<EOL><INDENT>raise InternalError("<STR_LIT>")<EOL><DEDENT>missing = object()<EOL>arg_vals = [missing]*len(self.arg_names)<EOL>kw_indices = {name: i for i, name in enumerate(self.arg_names)}<EOL>for i, arg in enumerate(pos_args):<EOL><INDENT>if i >= len(arg_vals):<EOL><INDENT>raise ArgumentError("<STR_LIT>" % str(arg))<EOL><DEDENT>arg_vals[i] = arg<EOL><DEDENT>for arg, val in kwargs.items():<EOL><INDENT>index = kw_indices.get(arg)<EOL>if index is None:<EOL><INDENT>raise ArgumentError("<STR_LIT>" % arg)<EOL><DEDENT>if arg_vals[index] is not missing:<EOL><INDENT>raise ValidationError("<STR_LIT>" % arg)<EOL><DEDENT>arg_vals[index] = val<EOL><DEDENT>if len(self.arg_defaults) > <NUM_LIT:0>:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(self.arg_defaults)):<EOL><INDENT>neg_index = -len(self.arg_defaults) + i<EOL>if arg_vals[neg_index] is missing:<EOL><INDENT>arg_vals[neg_index] = self.arg_defaults[i]<EOL><DEDENT><DEDENT><DEDENT>if missing in arg_vals:<EOL><INDENT>index = arg_vals.index(missing)<EOL>raise ArgumentError("<STR_LIT>" % (index, self.arg_names[index]))<EOL><DEDENT>return {name: val for name, val in zip(self.arg_names, arg_vals)}<EOL>

Check if there are any missing or duplicate arguments.

        Args:
            pos_args (list): A list of arguments that will be passed as positional
                arguments.
            kwargs (dict): A dictionary of the keyword arguments that will be passed.

        Returns:
            dict: A dictionary of argument name to argument value, pulled from either
                the value passed or the default value if no argument is passed.

        Raises:
            ArgumentError: If a positional or keyword argument does not fit in the spec.
            ValidationError: If an argument is passed twice.


def match_shortname(self, name, filled_args=None):

filled_count = <NUM_LIT:0><EOL>if filled_args is not None:<EOL><INDENT>filled_count = len(filled_args)<EOL><DEDENT>possible = [x for x in self.arg_names[filled_count:] if x.startswith(name)]<EOL>if len(possible) == <NUM_LIT:0>:<EOL><INDENT>raise ArgumentError("<STR_LIT>", short_name=name, parameters=self.arg_names)<EOL><DEDENT>elif len(possible) > <NUM_LIT:1>:<EOL><INDENT>raise ArgumentError("<STR_LIT>", short_name=name, possible_matches=possible)<EOL><DEDENT>return possible[<NUM_LIT:0>]<EOL>

Try to convert a prefix into a parameter name.

        If the result could be ambiguous or there is no matching
        parameter, throw an ArgumentError

        Args:
            name (str): A prefix for a parameter name
            filled_args (list): A list of filled positional arguments that will be
                removed from consideration.

        Returns:
            str: The full matching parameter name


self._ensure_loaded()<EOL>if not self.return_info.is_data:<EOL><INDENT>return None<EOL><DEDENT>if self.return_info.type_name is not None:<EOL><INDENT>return typeinfo.type_system.format_value(value, self.return_info.type_name, self.return_info.formatter)<EOL><DEDENT>return self.return_info.formatter(value)<EOL>

Format the return value of this function as a string.

        Args:
            value (object): The return value that we are supposed to format.

        Returns:
            str: The formatted return value, or None if this function indicates
                that it does not return data


def custom_returnvalue(self, printer, desc=None):

self.return_info = ReturnInfo(None, printer, True, desc)<EOL>

Use a custom function to print the return value.

        Args:
            printer (callable): A function that should take in the return
                value and convert it to a string.
            desc (str): An optional description of the return value.


def spec_filled(self, pos_args, kw_args):

req_names = self.arg_names<EOL>if len(self.arg_defaults) > <NUM_LIT:0>:<EOL><INDENT>req_names = req_names[:-len(self.arg_defaults)]<EOL><DEDENT>req = [x for x in req_names if x not in kw_args]<EOL>return len(req) <= len(pos_args)<EOL>

Check if we have enough arguments to call this function.

        Args:
            pos_args (list): A list of all the positional values we have.
            kw_args (dict): A dict of all of the keyword args we have.

        Returns:
            bool: True if we have a filled spec, False otherwise.


return self.kwargs is not None<EOL>

Check if this function supports arbitrary keyword arguments.


def convert_positional_argument(self, index, arg_value):

<EOL>if self._has_self:<EOL><INDENT>if index == <NUM_LIT:0>:<EOL><INDENT>return arg_value<EOL><DEDENT>index -= <NUM_LIT:1><EOL><DEDENT>arg_name = self.arg_names[index]<EOL>return self.convert_argument(arg_name, arg_value)<EOL>

Convert and validate a positional argument.

        Args:
            index (int): The positional index of the argument
            arg_value (object): The value to convert and validate

        Returns:
            object: The converted value.


def _check_and_execute(func, *args, **kwargs):

convargs = []<EOL>for i, arg in enumerate(args):<EOL><INDENT>val = func.metadata.convert_positional_argument(i, arg)<EOL>convargs.append(val)<EOL><DEDENT>convkw = {}<EOL>for key, val in kwargs:<EOL><INDENT>convkw[key] = func.metadata.convert_argument(key, val)<EOL><DEDENT>if not func.metadata.spec_filled(convargs, convkw):<EOL><INDENT>raise ValidationError("<STR_LIT>", function=func.metadata.name, signature=func.metadata.signature())<EOL><DEDENT>retval = func(*convargs, **convkw)<EOL>return retval<EOL>

Check the type of all parameters with type information, converting
as appropriate and then execute the function.


if len(self.maindoc) == <NUM_LIT:0>:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>return self.maindoc[<NUM_LIT:0>].contents<EOL>

One line description of this parsed docstring.

        Returns:
            str


@classmethod<EOL><INDENT>def _join_paragraphs(cls, lines, use_indent=False, leading_blanks=False, trailing_blanks=False):<DEDENT>

curr_para = []<EOL>paragraphs = []<EOL>for line in lines:<EOL><INDENT>if use_indent:<EOL><INDENT>if line.startswith('<STR_LIT:U+0020>'):<EOL><INDENT>curr_para.append(line.lstrip())<EOL>continue<EOL><DEDENT>elif line == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>if len(curr_para) > <NUM_LIT:0>:<EOL><INDENT>paragraphs.append(cls._join_paragraph(curr_para, leading_blanks, trailing_blanks))<EOL><DEDENT>curr_para = [line.lstrip()]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(line) != <NUM_LIT:0>:<EOL><INDENT>curr_para.append(line)<EOL><DEDENT>else:<EOL><INDENT>paragraphs.append(cls._join_paragraph(curr_para, leading_blanks, trailing_blanks))<EOL>curr_para = []<EOL><DEDENT><DEDENT><DEDENT>if len(curr_para) > <NUM_LIT:0>:<EOL><INDENT>paragraphs.append(cls._join_paragraph(curr_para, leading_blanks, trailing_blanks))<EOL><DEDENT>return paragraphs<EOL>

Join adjacent lines together into paragraphs using either a blank line or indent as separator.


param_def, _colon, desc = param.partition('<STR_LIT::>')<EOL>if not include_desc:<EOL><INDENT>desc = None<EOL><DEDENT>else:<EOL><INDENT>desc = desc.lstrip()<EOL><DEDENT>if _colon == "<STR_LIT>":<EOL><INDENT>raise ValidationError("<STR_LIT>", declaration=param)<EOL><DEDENT>param_name, _space, param_type = param_def.partition('<STR_LIT:U+0020>')<EOL>if len(param_type) < <NUM_LIT:2> or param_type[<NUM_LIT:0>] != '<STR_LIT:(>' or param_type[-<NUM_LIT:1>] != '<STR_LIT:)>':<EOL><INDENT>raise ValidationError("<STR_LIT>", param_string=param_def, type_string=param_type)<EOL><DEDENT>param_type = param_type[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>return param_name, ParameterInfo(param_type, [], desc)<EOL>

Parse a single typed parameter statement.


out = {}<EOL>out['<STR_LIT>'] = self.msg<EOL>out['<STR_LIT:type>'] = self.__class__.__name__<EOL>out['<STR_LIT>'] = self.params<EOL>return out<EOL>

Convert this exception to a dictionary.

        Returns:
            dist: A dictionary of information about this exception,
                Has a 'reason' key, a 'type' key  and a dictionary of params


if exclude_class:<EOL><INDENT>msg = self.msg<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" % (self.__class__.__name__, self.msg)<EOL><DEDENT>if len(self.params) != <NUM_LIT:0>:<EOL><INDENT>paramstring = "<STR_LIT:\n>".join([str(key) + "<STR_LIT>" + str(val) for key, val in self.params.items()])<EOL>msg += "<STR_LIT>" + paramstring<EOL><DEDENT>return msg<EOL>

Format this exception as a string including class name.

        Args:
            exclude_class (bool): Whether to exclude the exception class
                name when formatting this exception

        Returns:
            string: a multiline string with the message, class name and
                key value parameters passed to create the exception.


return self._get_falco_rules("<STR_LIT:user>")<EOL>

**Description**
            Get the user falco rules file in use for this customer. See the `Falco wiki <https://github.com/draios/falco/wiki/Falco-Rules>`_ for documentation on the falco rules format.

        **Arguments**
            - None

        **Success Return Value**
            The contents of the user falco rules file.

        **Example**
            `examples/get_secure_user_falco_rules.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_user_falco_rules.py>`_


res = requests.get(self.url + '<STR_LIT>', headers=self.hdrs, verify=self.ssl_verify)<EOL>return self._request_result(res)<EOL>

**Description**
            Get the list of all compliance tasks.

        **Arguments**
            - None

        **Success Return Value**
            A JSON list with the representation of each compliance task.


res = requests.post(self.url + '<STR_LIT>', headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, "<STR_LIT>"]<EOL>

**Description**
            Delete all existing policies. The falco rules file is unchanged.

        **Arguments**
            - None

        **Success Return Value**
            The string "Policies Deleted"

        **Example**
            `examples/delete_all_policies.py <https://github.com/draios/python-sdc-client/blob/master/examples/delete_all_policies.py>`_


return self._set_falco_rules("<STR_LIT>", rules_content)<EOL>

**Description**
            Set the system falco rules file in use for this customer. NOTE: This API endpoint can *only* be used in on-premise deployments. Generally the system falco rules file is only modified in conjunction with Sysdig support. See the `Falco wiki <https://github.com/draios/falco/wiki/Falco-Rules>`_ for documentation on the falco rules format.

        **Arguments**
            - A string containing the system falco rules.

        **Success Return Value**
            The contents of the system falco rules file that were just updated.

        **Example**
            `examples/set_secure_system_falco_rules.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_system_falco_rules.py>`_


res = requests.get(self.url + '<STR_LIT>'.format(id), headers=self.hdrs, verify=self.ssl_verify)<EOL>return self._request_result(res)<EOL>

**Description**
            Get a compliance task.

        **Arguments**
            - id: the id of the compliance task to get.

        **Success Return Value**
            A JSON representation of the compliance task.


def get_sysdig_captures(self, from_sec=None, to_sec=None, scope_filter=None):

url = '<STR_LIT>'.format(<EOL>url=self.url,<EOL>source=self.product,<EOL>frm="<STR_LIT>" % (from_sec * <NUM_LIT:10>**<NUM_LIT:6>) if from_sec else "<STR_LIT>",<EOL>to="<STR_LIT>" % (to_sec * <NUM_LIT:10>**<NUM_LIT:6>) if to_sec else "<STR_LIT>",<EOL>scopeFilter="<STR_LIT>" % scope_filter if scope_filter else "<STR_LIT>")<EOL>res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)<EOL>return self._request_result(res)<EOL>

**Description**
            Returns the list of sysdig captures for the user.

        **Arguments**
            - from_sec: the start of the timerange for which to get the captures
            - end_sec: the end of the timerange for which to get the captures
            - scope_filter: this is a SysdigMonitor-like filter (e.g 'container.image=ubuntu'). When provided, events are filtered by their scope, so only a subset will be returned (e.g. 'container.image=ubuntu' will provide only events that have happened on an ubuntu container).

        **Success Return Value**
            A dictionary containing the list of captures.

        **Example**
            `examples/list_sysdig_captures.py <https://github.com/draios/python-sdc-client/blob/master/examples/list_sysdig_captures.py>`_


res = self.get_teams(name)<EOL>if res[<NUM_LIT:0>] == False:<EOL><INDENT>return res<EOL><DEDENT>for t in res[<NUM_LIT:1>]:<EOL><INDENT>if t['<STR_LIT:name>'] == name:<EOL><INDENT>return [True, t]<EOL><DEDENT><DEDENT>return [False, '<STR_LIT>']<EOL>

**Description**
            Return the team with the specified team name, if it is present.

        **Arguments**
            - **name**: the name of the team to return

        **Success Return Value**
            The requested team.

        **Example**
            `examples/user_team_mgmt.py <https://github.com/draios/python-sdc-client/blob/master/examples/user_team_mgmt.py>`_


def post_event(self, name, description=None, severity=None, event_filter=None, tags=None):

options = {<EOL>'<STR_LIT:name>': name,<EOL>'<STR_LIT:description>': description,<EOL>'<STR_LIT>': severity,<EOL>'<STR_LIT>': event_filter,<EOL>'<STR_LIT>': tags<EOL>}<EOL>edata = {<EOL>'<STR_LIT>': {k: v for k, v in options.items() if v is not None}<EOL>}<EOL>res = requests.post(self.url + '<STR_LIT>', headers=self.hdrs, data=json.dumps(edata), verify=self.ssl_verify)<EOL>return self._request_result(res)<EOL>

**Description**
            Send an event to Sysdig Monitor. The events you post are available in the Events tab in the Sysdig Monitor UI and can be overlied to charts.

        **Arguments**
            - **name**: the name of the new event.
            - **description**: a longer description offering detailed information about the event.
            - **severity**: syslog style from 0 (high) to 7 (low).
            - **event_filter**: metadata, in Sysdig Monitor format, of nodes to associate with the event, e.g. ``host.hostName = 'ip-10-1-1-1' and container.name = 'foo'``.
            - **tags**: a list of key-value dictionaries that can be used to tag the event. Can be used for filtering/segmenting purposes in Sysdig Monitor.

        **Success Return Value**
            A dictionary describing the new event.

        **Examples**
            - `examples/post_event_simple.py <https://github.com/draios/python-sdc-client/blob/master/examples/post_event_simple.py>`_
            - `examples/post_event.py <https://github.com/draios/python-sdc-client/blob/master/examples/post_event.py>`_


**Description**
            Return the list of data retention intervals, with beginning and end UTC time for each of them. Sysdig Monitor performs rollups of the data it stores. This means that data is stored at different time granularities depending on how far back in time it is. This call can be used to know what precision you can expect before you make a call to :func:`~SdcClient.get_data`.

        **Success Return Value**
            A dictionary containing the list of available sampling intervals.

        **Example**
            `examples/print_data_retention_info.py <https://github.com/draios/python-sdc-client/blob/master/examples/print_data_retention_info.py>`_


res = requests.get(self.url + '<STR_LIT>', headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return False, self.lasterr<EOL><DEDENT>ids = []<EOL>if channels is None:<EOL><INDENT>for ch in res.json()["<STR_LIT>"]:<EOL><INDENT>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT>return [True, ids]<EOL><DEDENT>for c in channels:<EOL><INDENT>found = False<EOL>for ch in res.json()["<STR_LIT>"]:<EOL><INDENT>if c['<STR_LIT:type>'] == ch['<STR_LIT:type>']:<EOL><INDENT>if c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>opt = ch['<STR_LIT>']<EOL>if set(opt['<STR_LIT>']) == set(c['<STR_LIT>']):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>opt = ch['<STR_LIT>']<EOL>if '<STR_LIT>' in c:<EOL><INDENT>if set(c['<STR_LIT>']) == set(opt['<STR_LIT>']):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT>elif '<STR_LIT:name>' in c:<EOL><INDENT>if c['<STR_LIT:name>'] == ch.get('<STR_LIT:name>'):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>opt = ch['<STR_LIT>']<EOL>if opt['<STR_LIT>'] == c['<STR_LIT>'] and opt['<STR_LIT>'] == c['<STR_LIT>']:<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>opt = ch['<STR_LIT>']<EOL>if '<STR_LIT>' in opt and opt['<STR_LIT>'] == c['<STR_LIT>']:<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:name>' in c:<EOL><INDENT>if c['<STR_LIT:name>'] == ch.get('<STR_LIT:name>'):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:name>' in c:<EOL><INDENT>if c['<STR_LIT:name>'] == ch.get('<STR_LIT:name>'):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT><DEDENT>elif c['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:name>' in c:<EOL><INDENT>if c['<STR_LIT:name>'] == ch.get('<STR_LIT:name>'):<EOL><INDENT>found = True<EOL>ids.append(ch['<STR_LIT:id>'])<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if not found:<EOL><INDENT>return False, "<STR_LIT>" + str(c)<EOL><DEDENT><DEDENT>return True, ids<EOL>

**Description**
            Get an array of all configured Notification Channel IDs, or a filtered subset of them.

        **Arguments**
            - **channels**: an optional array of dictionaries to limit the set of Notification Channel IDs returned. If not specified, IDs for all configured Notification Channels are returned. Each dictionary contains a ``type`` field that can be one of the available types of Notification Channel (``EMAIL``, ``SNS``, ``PAGER_DUTY``, ``SLACK``, ``OPSGENIE``, ``VICTOROPS``, ``WEBHOOK``) as well as additional elements specific to each channel type.

        **Success Return Value**
            An array of Notification Channel IDs (integers).

        **Examples**
            - `examples/create_alert.py <https://github.com/draios/python-sdc-client/blob/master/examples/create_alert.py>`_
            - `examples/restore_alerts.py <https://github.com/draios/python-sdc-client/blob/master/examples/restore_alerts.py>`_


def get_data(self, metrics, start_ts, end_ts=<NUM_LIT:0>, sampling_s=<NUM_LIT:0>,<EOL>filter='<STR_LIT>', datasource_type='<STR_LIT:host>', paging=None):

reqbody = {<EOL>'<STR_LIT>': metrics,<EOL>'<STR_LIT>': datasource_type,<EOL>}<EOL>if start_ts < <NUM_LIT:0>:<EOL><INDENT>reqbody['<STR_LIT>'] = -start_ts<EOL><DEDENT>elif start_ts == <NUM_LIT:0>:<EOL><INDENT>return [False, "<STR_LIT>"]<EOL><DEDENT>else:<EOL><INDENT>reqbody['<STR_LIT:start>'] = start_ts<EOL>reqbody['<STR_LIT:end>'] = end_ts<EOL><DEDENT>if filter != '<STR_LIT>':<EOL><INDENT>reqbody['<STR_LIT>'] = filter<EOL><DEDENT>if paging is not None:<EOL><INDENT>reqbody['<STR_LIT>'] = paging<EOL><DEDENT>if sampling_s != <NUM_LIT:0>:<EOL><INDENT>reqbody['<STR_LIT>'] = sampling_s<EOL><DEDENT>res = requests.post(self.url + '<STR_LIT>', headers=self.hdrs, data=json.dumps(reqbody), verify=self.ssl_verify)<EOL>return self._request_result(res)<EOL>

**Description**
            Export metric data (both time-series and table-based).

        **Arguments**
            - **metrics**: a list of dictionaries, specifying the metrics and grouping keys that the query will return. A metric is any of the entries that can be found in the *Metrics* section of the Explore page in Sysdig Monitor. Metric entries require an *aggregations* section specifying how to aggregate the metric across time and containers/hosts. A grouping key is any of the entries that can be found in the *Show* or *Segment By* sections of the Explore page in Sysdig Monitor. These entries are used to apply single or hierarchical segmentation to the returned data and don't require the aggregations section. Refer to the Example link below for ready-to-use code snippets.
            - **start_ts**: the UTC time (in seconds) of the beginning of the data window. A negative value can be optionally used to indicate a relative time in the past from now. For example, -3600 means "one hour ago".
            - **end_ts**: the UTC time (in seconds) of the end of the data window, or 0 to indicate "now". A negative value can also be optionally used to indicate a relative time in the past from now. For example, -3600 means "one hour ago".
            - **sampling_s**: the duration of the samples that will be returned. 0 means that the whole data will be returned as a single sample.
            - **filter**: a boolean expression combining Sysdig Monitor segmentation criteria that defines what the query will be applied to. For example: *kubernetes.namespace.name='production' and container.image='nginx'*.
            - **datasource_type**: specify the metric source for the request, can be ``container`` or ``host``. Most metrics, for example ``cpu.used.percent`` or ``memory.bytes.used``, are reported by both hosts and containers. By default, host metrics are used, but if the request contains a container-specific grouping key in the metric list/filter (e.g. ``container.name``), then the container source is used. In cases where grouping keys are missing or apply to both hosts and containers (e.g. ``tag.Name``), *datasource_type* can be explicitly set to avoid any ambiguity and allow the user to select precisely what kind of data should be used for the request. `examples/get_data_datasource.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_data_datasource.py>`_ contains a few examples that should clarify the use of this argument.
            - **paging**: if segmentation of the query generates values for several different entities (e.g. containers/hosts), this parameter specifies which to include in the returned result. It's specified as a dictionary of inclusive values for ``from`` and ``to`` with the default being ``{ "from": 0, "to": 9 }``, which will return values for the "top 10" entities. The meaning of "top" is query-dependent, based on points having been sorted via the specified group aggregation, with the results sorted in ascending order if the group aggregation is ``min`` or ``none``, and descending order otherwise.

        **Success Return Value**
            A dictionary with the requested data. Data is organized in a list of time samples, each of which includes a UTC timestamp and a list of values, whose content and order reflect what was specified in the *metrics* argument.

        **Examples**
            - `examples/get_data_simple.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_data_simple.py>`_
            - `examples/get_data_advanced.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_data_advanced.py>`_
            - `examples/list_hosts.py <https://github.com/draios/python-sdc-client/blob/master/examples/list_hosts.py>`_
            - `examples/get_data_datasource.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_data_datasource.py>`_


body = {<EOL>'<STR_LIT:id>': '<STR_LIT>',<EOL>'<STR_LIT>': [{'<STR_LIT>': []}]<EOL>}<EOL>for item in new_hierarchy:<EOL><INDENT>body['<STR_LIT>'][<NUM_LIT:0>]['<STR_LIT>'].append({'<STR_LIT>': item})<EOL><DEDENT>res = requests.put(self.url + '<STR_LIT>', headers=self.hdrs,<EOL>data=json.dumps(body), verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>else:<EOL><INDENT>return [True, None]<EOL><DEDENT>

**Description**
            Changes the grouping hierarchy in the Explore panel of the current user.

        **Arguments**
            - **new_hierarchy**: a list of sysdig segmentation metrics indicating the new grouping hierarchy.


<EOL>if scope is None or not scope:<EOL><INDENT>return [True, []]<EOL><DEDENT>expressions = []<EOL>string_expressions = scope.strip('<STR_LIT>').split('<STR_LIT>')<EOL>expression_re = re.compile('<STR_LIT>')<EOL>for string_expression in string_expressions:<EOL><INDENT>matches = expression_re.match(string_expression)<EOL>if matches is None:<EOL><INDENT>return [False, '<STR_LIT>']<EOL><DEDENT>is_not_operator = matches.group('<STR_LIT>') is not None<EOL>if matches.group('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>list_value = matches.group('<STR_LIT:value>').strip('<STR_LIT>')<EOL>value_matches = re.findall('<STR_LIT>', list_value)<EOL>if len(value_matches) == <NUM_LIT:0>:<EOL><INDENT>return [False, '<STR_LIT>']<EOL><DEDENT>value_matches = map(lambda v: v[<NUM_LIT:0>] if v[<NUM_LIT:0>] else v[<NUM_LIT:1>], value_matches)<EOL>values = map(lambda v: v.strip('<STR_LIT>'), value_matches)<EOL><DEDENT>else:<EOL><INDENT>values = [matches.group('<STR_LIT:value>').strip('<STR_LIT>')]<EOL><DEDENT>operator_parse_dict = {<EOL>'<STR_LIT>': '<STR_LIT>' if not is_not_operator else '<STR_LIT>',<EOL>'<STR_LIT:=>': '<STR_LIT>' if not is_not_operator else '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>' if not is_not_operator else '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>' if not is_not_operator else '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>}<EOL>operator = operator_parse_dict.get(matches.group('<STR_LIT>'), None)<EOL>if operator is None:<EOL><INDENT>return [False, '<STR_LIT>']<EOL><DEDENT>expressions.append({<EOL>'<STR_LIT>': matches.group('<STR_LIT>'),<EOL>'<STR_LIT>': operator,<EOL>'<STR_LIT:value>': values<EOL>})<EOL><DEDENT>return [True, expressions]<EOL>

**Description**
            Internal function to convert a filter string to a filter object to be used with dashboards.


url = self.url + '<STR_LIT>' + alertid<EOL>res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, res.json()]<EOL>

**Description**
            Retrieve the scanning alert with the given id

        **Arguments**
            - alertid: Unique identifier associated with this alert.

        **Success Return Value**
            A JSON object containing the alert description.


def add_image(self, image, force=False, dockerfile=None, annotations={}, autosubscribe=True):

itype = self._discover_inputimage_format(image)<EOL>if itype != '<STR_LIT>':<EOL><INDENT>return [False, "<STR_LIT>"]<EOL><DEDENT>payload = {}<EOL>if dockerfile:<EOL><INDENT>payload['<STR_LIT>'] = base64.b64encode(dockerfile.encode()).decode("<STR_LIT:utf-8>")<EOL><DEDENT>payload['<STR_LIT>'] = image<EOL>if annotations:<EOL><INDENT>payload['<STR_LIT>'] = annotations<EOL><DEDENT>url = "<STR_LIT>".format(<EOL>base_url=self.url,<EOL>autosubscribe=str(autosubscribe),<EOL>force="<STR_LIT>" if force else "<STR_LIT>")<EOL>res = requests.post(url, data=json.dumps(payload), headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, res.json()]<EOL>

**Description**
            Add an image to the scanner

        **Arguments**
            - image: Input image can be in the following formats: registry/repo:tag
            - dockerfile: The contents of the dockerfile as a str.
            - annotations: A dictionary of annotations {str: str}.
            - autosubscribe: Should active the subscription to this image?

        **Success Return Value**
            A JSON object representing the image that was added.


def deactivate_subscription(self, subscription_type, subscription_key):

return self._update_subscription(subscription_type, subscription_key, False)<EOL>

**Description**
            Deactivate a subscription

        **Arguments**
            - subscription_type: Type of subscription. Valid options:
                - 'tag_update': Receive notification when new image is pushed
                - 'policy_eval': Receive notification when image policy status changes
                - 'vuln_update': Receive notification when vulnerabilities are added, removed or modified
            - subscription_key: Fully qualified name of tag to subscribe to. Eg. docker.io/library/alpine:latest


def activate_subscription(self, subscription_type, subscription_key):

return self._update_subscription(subscription_type, subscription_key, True)<EOL>

**Description**
            Activate a subscription

        **Arguments**
            - subscription_type: Type of subscription. Valid options:
                - 'tag_update': Receive notification when new image is pushed
                - 'policy_eval': Receive notification when image policy status changes
                - 'vuln_update': Receive notification when vulnerabilities are added, removed or modified
            - subscription_key: Fully qualified name of tag to subscribe to. Eg. docker.io/library/alpine:latest


url = self.url + "<STR_LIT>"<EOL>res = requests.post(url, data=json.dumps(image_data), headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, res.json()]<EOL>

**Description**
            Import an image from the scanner export

        **Arguments**
            - image_data: A JSON with the image information.

        **Success Return Value**
            A JSON object representing the image that was imported.


def update_registry(self, registry, registry_user, registry_pass, insecure=False, registry_type="<STR_LIT>", validate=True):

if self._registry_string_is_valid(registry):<EOL><INDENT>return [False, "<STR_LIT>"]<EOL><DEDENT>payload = {<EOL>'<STR_LIT>': registry,<EOL>'<STR_LIT>': registry_user,<EOL>'<STR_LIT>': registry_pass,<EOL>'<STR_LIT>': registry_type,<EOL>'<STR_LIT>': not insecure}<EOL>url = "<STR_LIT>".format(<EOL>base_url=self.url,<EOL>registry=registry,<EOL>validate=validate)<EOL>res = requests.put(url, data=json.dumps(payload), headers=self.hdrs, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, res.json()]<EOL>

**Description**
            Update an existing image registry.

        **Arguments**
            - registry: Full hostname/port of registry. Eg. myrepo.example.com:5000
            - registry_user: Username
            - registry_pass: Password
            - insecure: Allow connection to registry without SSL cert checks (ex: if registry uses a self-signed SSL certificate)
            - registry_type: Specify the registry type. 'docker_v2' and 'awsecr' are supported (default='docker_v2')
            - validate: If set to 'False' will not attempt to validate registry/creds on registry add

        **Success Return Value**
            A JSON object representing the registry.


def list_runtime(self, scope="<STR_LIT>", skip_policy_evaluation=True, start_time=None, end_time=None):

containers = {<EOL>'<STR_LIT>': scope,<EOL>'<STR_LIT>': skip_policy_evaluation<EOL>}<EOL>if start_time or end_time:<EOL><INDENT>containers['<STR_LIT:time>'] = {}<EOL>containers['<STR_LIT:time>']['<STR_LIT>'] = int(start_time * <NUM_LIT>) if start_time else <NUM_LIT:0><EOL>end_time = end_time if end_time else time.time()<EOL>containers['<STR_LIT:time>']['<STR_LIT:to>'] = int(end_time * <NUM_LIT>)<EOL><DEDENT>url = self.url + '<STR_LIT>'<EOL>data = json.dumps(containers)<EOL>res = requests.post(url, headers=self.hdrs, data=data, verify=self.ssl_verify)<EOL>if not self._checkResponse(res):<EOL><INDENT>return [False, self.lasterr]<EOL><DEDENT>return [True, res.json()]<EOL>

**Description**
            List runtime containers

        **Arguments**
            - scope: An AND-composed string of predicates that selects the scope in which the alert will be applied. (like: 'host.domain = "example.com" and container.image != "alpine:latest"')
            - skip_policy_evaluation: If true, no policy evaluations will be triggered for the images.
            - start_time: Start of the time range (integer of unix time).
            - end_time: End of the time range (integer of unix time).

        **Success Return Value**
            A JSON object representing the list of runtime containers.


length = <NUM_LIT:6><EOL>chars = string.uppercase + string.digits[<NUM_LIT:1>:]<EOL>return get_random_string(length=length, allowed_chars=chars)<EOL>

Generates an access code for users' payments as well as their
    fulfilment code for check-in.
    The access code will 4 characters long, which allows for 1,500,625
    unique codes, which really should be enough for anyone.


def pay(self, reference, amount, pre_validate=True):

Testing method for simulating an invoice paymenht by the given
        amount.


def add_to_cart(self, product, quantity):

try:<EOL><INDENT>product_item = commerce.ProductItem.objects.get(<EOL>cart=self.cart,<EOL>product=product)<EOL>old_quantity = product_item.quantity<EOL><DEDENT>except ObjectDoesNotExist:<EOL><INDENT>old_quantity = <NUM_LIT:0><EOL><DEDENT>self.set_quantity(product, old_quantity + quantity)<EOL>

Adds _quantity_ of the given _product_ to the cart. Raises
        ValidationError if constraints are violated.


Adds flag -- PROD_1 is not available unless user is a primary
        presenter of a KIND_2


Adds flag -- PROD_1 is not available unless user is a primary
        presenter of a KIND_1


flag = conditions.ProductFlag.objects.create(<EOL>description="<STR_LIT>",<EOL>condition=condition,<EOL>)<EOL>flag.categories.add(cls.CAT_1)<EOL>flag.enabling_products.add(cls.PROD_3)<EOL>

Adds a product flag condition that operates on a category:
        adding an item from CAT_1 is predicated on adding PROD_3 beforehand


flag = conditions.ProductFlag.objects.create(<EOL>description="<STR_LIT>",<EOL>condition=condition,<EOL>)<EOL>flag.products.add(cls.PROD_1)<EOL>flag.enabling_products.add(cls.PROD_2)<EOL>

Adds a product flag condition: adding PROD_1 to a cart is
        predicated on adding PROD_2 beforehand.


<EOL>def _report(view):<EOL><INDENT>report_view = ReportView(view, title, form_type)<EOL>report_view = user_passes_test(views._staff_only)(report_view)<EOL>report_view = wraps(view)(report_view)<EOL>_all_report_views.append(report_view)<EOL>return report_view<EOL><DEDENT>return _report<EOL>

Decorator that converts a report view function into something that
    displays a Report.

    Arguments:
        title (str):
            The title of the report.
        form_type (Optional[forms.Form]):
            A form class that can make this report display things. If not
            supplied, no form will be displayed.


def __init__(self, inner_view, title, form_type):

<EOL>self.inner_view = inner_view<EOL>self.title = title<EOL>self.form_type = form_type<EOL>

Arguments:
    inner_view: Callable that returns either a Report or a sequence of
        Report objects.

    title: The title that appears at the top of all of the reports.

    form_type: A Form class that can be used to query the report.


raise NotImplementedError<EOL>

Arguments:
    content_type (str): The content-type for the output format of this
    report.

Returns:
    An iterator, which yields each row of the data. Each row should
    be an iterable containing the cells, rendered appropriately for
    content_type.


@report_view("<STR_LIT>", form_type=forms.DiscountForm)<EOL>def discount_status(request, form):

discounts = form.cleaned_data["<STR_LIT>"]<EOL>items = commerce.DiscountItem.objects.filter(<EOL>Q(discount__in=discounts),<EOL>).select_related("<STR_LIT>", "<STR_LIT>", "<STR_LIT>")<EOL>items = group_by_cart_status(<EOL>items,<EOL>["<STR_LIT>"],<EOL>["<STR_LIT>", "<STR_LIT>"],<EOL>)<EOL>headings = [<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>]<EOL>data = []<EOL>for item in items:<EOL><INDENT>data.append([<EOL>item["<STR_LIT>"],<EOL>item["<STR_LIT>"],<EOL>item["<STR_LIT>"],<EOL>item["<STR_LIT>"],<EOL>item["<STR_LIT>"],<EOL>])<EOL><DEDENT>return ListReport("<STR_LIT>", headings, data)<EOL>

Summarises the usage of a given discount.


def value_or_zero(aggregate, key):<EOL><INDENT>return aggregate[key] or <NUM_LIT:0><EOL><DEDENT>def sum_amount(payment_set):<EOL><INDENT>a = payment_set.values("<STR_LIT>").aggregate(total=Sum("<STR_LIT>"))<EOL>return value_or_zero(a, "<STR_LIT>")<EOL><DEDENT>headings = ["<STR_LIT>", "<STR_LIT>"]<EOL>data = []<EOL>sales = commerce.LineItem.objects.filter(<EOL>invoice__status=commerce.Invoice.STATUS_PAID,<EOL>).values(<EOL>"<STR_LIT>", "<STR_LIT>"<EOL>).aggregate(<EOL>total=Sum(F("<STR_LIT>") * F("<STR_LIT>"), output_field=CURRENCY()),<EOL>)<EOL>sales = value_or_zero(sales, "<STR_LIT>")<EOL>all_payments = sum_amount(commerce.PaymentBase.objects.all())<EOL>all_credit_notes = <NUM_LIT:0> - sum_amount(commerce.CreditNote.objects.all())<EOL>unclaimed_credit_notes = <NUM_LIT:0> - sum_amount(commerce.CreditNote.unclaimed())<EOL>claimed_credit_notes = sum_amount(<EOL>commerce.CreditNoteApplication.objects.all()<EOL>)<EOL>refunded_credit_notes = <NUM_LIT:0> - sum_amount(commerce.CreditNote.refunded())<EOL>data.append(["<STR_LIT>", sales])<EOL>data.append(["<STR_LIT>", all_payments])<EOL>data.append(["<STR_LIT>", sales - all_payments])<EOL>data.append(["<STR_LIT>", all_credit_notes])<EOL>data.append(["<STR_LIT>", claimed_credit_notes])<EOL>data.append(["<STR_LIT>", refunded_credit_notes])<EOL>data.append(["<STR_LIT>", unclaimed_credit_notes])<EOL>data.append([<EOL>"<STR_LIT>",<EOL>all_credit_notes - claimed_credit_notes -<EOL>refunded_credit_notes - unclaimed_credit_notes<EOL>])<EOL>return ListReport("<STR_LIT>", headings, data)<EOL>


fields = model._meta.get_fields()<EOL>choices = []<EOL>for field in fields:<EOL><INDENT>if hasattr(field, "<STR_LIT>"):<EOL><INDENT>choices.append((field.name, field.verbose_name))<EOL><DEDENT><DEDENT>class ModelFieldsForm(forms.Form):<EOL><INDENT>fields = forms.MultipleChoiceField(<EOL>choices=choices,<EOL>required=False,<EOL>)<EOL><DEDENT>return ModelFieldsForm<EOL>

Creates a form for specifying fields from a model to display.


try:<EOL><INDENT>tag_name, template_name = token.split_contents()<EOL><DEDENT>except ValueError:<EOL><INDENT>raise template.TemplateSyntaxError("<STR_LIT>" % token.contents.split()[<NUM_LIT:0>])<EOL><DEDENT>return IncludeNode(template_name)<EOL>

Usage: {% include_if_exists "head.html" %}

    This will fail silently if the template doesn't exist. If it does, it will
    be rendered with the current context.

    From: https://djangosnippets.org/snippets/2058/


return ItemController(user_for_context(context)).items_purchased(<EOL>category=category<EOL>)<EOL>

Returns the items purchased for this user.

    The user will be either `context.user`, and `context.request.user` if
    the former is not defined.


return CategoryController.available_categories(user_for_context(context))<EOL>

Gets all of the currently available products.

    Returns:
        [models.inventory.Category, ...]: A list of all of the categories that
            have Products that the current user can reserve.


return ItemController(user_for_context(context)).items_pending()<EOL>

Gets all of the items that the user from this context has reserved.

    The user will be either `context.user`, and `context.request.user` if
    the former is not defined.


return render(<EOL>request,<EOL>"<STR_LIT>",<EOL>{},<EOL>)<EOL>


attendee = people.Attendee.get_instance(request.user)<EOL>try:<EOL><INDENT>profile = attendee.attendeeprofilebase<EOL>profile = people.AttendeeProfileBase.objects.get_subclass(<EOL>pk=profile.id,<EOL>)<EOL><DEDENT>except ObjectDoesNotExist:<EOL><INDENT>profile = None<EOL><DEDENT>try:<EOL><INDENT>speaker_profile = request.user.speaker_profile<EOL>speaker_name = speaker_profile.name<EOL><DEDENT>except ObjectDoesNotExist:<EOL><INDENT>speaker_name = None<EOL><DEDENT>name_field = ProfileForm.Meta.model.name_field()<EOL>initial = {}<EOL>if profile is None and name_field is not None:<EOL><INDENT>initial[name_field] = speaker_name<EOL><DEDENT>form = ProfileForm(<EOL>request.POST or None,<EOL>initial=initial,<EOL>instance=profile,<EOL>prefix=prefix<EOL>)<EOL>handled = True if request.POST else False<EOL>if request.POST and form.is_valid():<EOL><INDENT>form.instance.attendee = attendee<EOL>form.save()<EOL><DEDENT>return form, handled<EOL>

Returns a profile form instance, and a boolean which is true if the
    form was handled.


VOUCHERS_FORM_PREFIX = "<STR_LIT>"<EOL>v = _handle_voucher(request, VOUCHERS_FORM_PREFIX)<EOL>voucher_form, voucher_handled = v<EOL>if voucher_handled:<EOL><INDENT>messages.success(request, "<STR_LIT>")<EOL>return redirect("<STR_LIT>")<EOL><DEDENT>data = {<EOL>"<STR_LIT>": voucher_form,<EOL>}<EOL>return render(request, "<STR_LIT>", data)<EOL>

A view *just* for entering a voucher form.


return user.is_staff<EOL>


form, handled = _handle_profile(request, "<STR_LIT>")<EOL>if handled and not form.errors:<EOL><INDENT>messages.success(<EOL>request,<EOL>"<STR_LIT>",<EOL>)<EOL>return redirect("<STR_LIT>")<EOL><DEDENT>data = {<EOL>"<STR_LIT>": form,<EOL>}<EOL>return render(request, "<STR_LIT>", data)<EOL>

View for editing an attendee's profile

    The user must be logged in to edit their profile.

    Returns:
        redirect or render:
            In the case of a ``POST`` request, it'll redirect to ``dashboard``,
            or otherwise, it will render ``registrasion/profile_form.html``
            with data::

                {
                    "form": form,  # Instance of ATTENDEE_PROFILE_FORM.
                }