endpoint = self.lookupd_http_addresses[self.lookupd_query_index]<EOL>self.lookupd_query_index = (self.lookupd_query_index + <NUM_LIT:1>) % len(self.lookupd_http_addresses)<EOL>if '<STR_LIT>' not in endpoint:<EOL><INDENT>endpoint = '<STR_LIT>' + endpoint<EOL><DEDENT>scheme, netloc, path, query, fragment = urlparse.urlsplit(endpoint)<EOL>if not path or path == "<STR_LIT:/>":<EOL><INDENT>path = "<STR_LIT>"<EOL><DEDENT>params = parse_qs(query)<EOL>params['<STR_LIT>'] = self.topic<EOL>query = urlencode(_utf8_params(params), doseq=<NUM_LIT:1>)<EOL>lookupd_url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))<EOL>req = tornado.httpclient.HTTPRequest(<EOL>lookupd_url, method='<STR_LIT:GET>',<EOL>headers={'<STR_LIT>': '<STR_LIT>'},<EOL>connect_timeout=self.lookupd_connect_timeout,<EOL>request_timeout=self.lookupd_request_timeout)<EOL>callback = functools.partial(self._finish_query_lookupd, lookupd_url=lookupd_url)<EOL>self.http_client.fetch(req, callback=callback)<EOL>

Trigger a query of the configured ``nsq_lookupd_http_addresses``.


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.message_handler(message)<EOL>

Called when a message is received in order to execute the configured ``message_handler``

This is useful to subclass and override if you want to change how your
message handlers are called.

:param message: the :class:`nsq.Message` received


logger.warning('<STR_LIT>',<EOL>self.name, message.id, message.attempts, self.max_tries, message.body)<EOL>

Called when a message has been received where ``msg.attempts > max_tries``

This is useful to subclass and override to perform a task (such as writing to disk, etc.)

:param message: the :class:`nsq.Message` received


return False<EOL>

Called as part of RDY handling to identify whether this Reader has been disabled

This is useful to subclass and override to examine a file on disk or a key in cache
to identify if this reader should pause execution (during a deploy, etc.).

Note: deprecated. Use set_max_in_flight(0)


signal.signal(signal.SIGTERM, _handle_term_signal)<EOL>signal.signal(signal.SIGINT, _handle_term_signal)<EOL>tornado.ioloop.IOLoop.instance().start()<EOL>

Starts any instantiated :class:`nsq.Reader` or :class:`nsq.Writer`


self._async_enabled = True<EOL>

Enables asynchronous processing for this message.

:class:`nsq.Reader` will not automatically respond to the message
upon return of ``message_handler``.


return self._async_enabled<EOL>

Returns whether or not asynchronous processing has been enabled.


return self._has_responded<EOL>

Returns whether or not this message has been responded to.


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).


<EOL>if '<STR_LIT>' in kwargs and isinstance(kwargs['<STR_LIT>'], int) and kwargs['<STR_LIT>'] >= <NUM_LIT:0>:<EOL><INDENT>kwargs['<STR_LIT>'] = kwargs['<STR_LIT>'] * <NUM_LIT:1000><EOL><DEDENT>assert not self._has_responded<EOL>self._has_responded = True<EOL>self.trigger(event.REQUEUE, message=self, **kwargs)<EOL>

Respond to ``nsqd`` that you've failed to process this message successfully (and would
like it to be requeued).

:param backoff: whether or not :class:`nsq.Reader` should apply backoff handling
:type backoff: bool

:param delay: the amount of time (in seconds) that this message should be delayed
    if -1 it will be calculated based on # of attempts
:type delay: int


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

Respond to ``nsqd`` that you need more time to process the message.


def pub(self, topic, msg, callback=None):

self._pub('<STR_LIT>', topic, msg, callback=callback)<EOL>

publish a message to nsq

:param topic: nsq topic
:param msg: message body (bytes)
:param callback: function which takes (conn, data) (data may be nsq.Error)


def mpub(self, topic, msg, callback=None):

if isinstance(msg, bytes_types):<EOL><INDENT>msg = [msg]<EOL><DEDENT>assert isinstance(msg, (list, set, tuple))<EOL>self._pub('<STR_LIT>', topic, msg, callback=callback)<EOL>

publish multiple messages in one command (efficiently)

:param topic: nsq topic
:param msg: list of messages bodies (which are bytes)
:param callback: function which takes (conn, data) (data may be nsq.Error)


def dpub(self, topic, delay_ms, msg, callback=None):

self._pub('<STR_LIT>', topic, msg, delay_ms, callback=callback)<EOL>

publish multiple messages in one command (efficiently)

:param topic: nsq topic
:param delay_ms: tell nsqd to delay delivery for this long (integer milliseconds)
:param msg: message body (bytes)
:param callback: function which takes (conn, data) (data may be nsq.Error)


assert callable(callback), '<STR_LIT>'<EOL>if callback in self.__listeners[name]:<EOL><INDENT>raise DuplicateListenerError<EOL><DEDENT>self.__listeners[name].append(callback)<EOL>

Listen for the named event with the specified callback.

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

:param callback: the callback to execute when the event is triggered
:type callback: callable


if callback not in self.__listeners[name]:<EOL><INDENT>raise InvalidListenerError<EOL><DEDENT>self.__listeners[name].remove(callback)<EOL>

Stop listening for the named event via the specified callback.

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

:param callback: the callback that was originally used
:type callback: callable


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


def create_lang_instance(var_map = None):

def py_bool_to_lit(py_bool):<EOL><INDENT>return parse( '<STR_LIT:true>' if py_bool else '<STR_LIT:false>', BooleanLiteral)<EOL><DEDENT>if not var_map:<EOL><INDENT>class Identifier(str):<EOL><INDENT>grammar = re.compile(r'<STR_LIT>') <EOL><DEDENT><DEDENT>else:<EOL><INDENT>class Identifier(Keyword):<EOL><INDENT>grammar = Enum(*[K(v) for v in var_map.iterkeys()])<EOL><DEDENT><DEDENT>class StringLiteral(str):<EOL><INDENT>def __new__(cls, s):<EOL><INDENT>return super(StringLiteral, cls).__new__(cls, ast.literal_eval(s))<EOL><DEDENT>grammar = [re.compile(r'<STR_LIT>'), re.compile(r"<STR_LIT>")]<EOL><DEDENT>class IntegerLiteral(int):<EOL><INDENT>grammar = re.compile(r'<STR_LIT>')<EOL><DEDENT>class FloatLiteral(float):<EOL><INDENT>grammar = re.compile(r'<STR_LIT>')<EOL><DEDENT>class BooleanLiteral(Keyword):<EOL><INDENT>grammar = Enum(K('<STR_LIT:true>'), K('<STR_LIT:false>'))<EOL><DEDENT>class NullLiteral(Keyword):<EOL><INDENT>grammar = Enum(K('<STR_LIT:null>'))<EOL><DEDENT>Comparable = [NullLiteral, FloatLiteral, IntegerLiteral, <EOL>StringLiteral, Identifier]<EOL>class ListOfComparables(List):<EOL><INDENT>pass<EOL><DEDENT>ListOfComparables.grammar = (<EOL>'<STR_LIT:(>',<EOL>Comparable,<EOL>maybe_some(<EOL>'<STR_LIT:U+002C>',<EOL>blank,<EOL>Comparable,<EOL>),<EOL>optional('<STR_LIT:U+002C>'),<EOL>'<STR_LIT:)>'<EOL>)<EOL>class ComparisonOperator(str):<EOL><INDENT>grammar = re.compile(r'<STR_LIT>')<EOL><DEDENT>class BooleanFunctionName(Keyword):<EOL><INDENT>grammar = Enum(K('<STR_LIT>'), K('<STR_LIT>'))<EOL><DEDENT>class ComparisonOperation(List):<EOL><INDENT>pass<EOL><DEDENT>ComparisonOperation.grammar = (<EOL>Comparable,<EOL>blank,<EOL>attr('<STR_LIT>', ComparisonOperator),<EOL>blank,<EOL>[Comparable, ListOfComparables],<EOL>)<EOL>class BooleanOperationSimple(List):<EOL>e flag() pypeg2 function works great when parsing but does not work when<EOL>mposing (the flag gets output whether it was in the source text or not). So<EOL>workaround is this:<EOL><INDENT>grammar = (<EOL>attr('<STR_LIT>', optional(RE_NOT)),<EOL>ComparisonOperation,<EOL>)<EOL><DEDENT>class BooleanOperation(List):<EOL><INDENT>pass<EOL><DEDENT>BooleanOperation.grammar = (<EOL>BooleanOperationSimple,<EOL>maybe_some(<EOL>blank,<EOL>BooleanFunctionName,<EOL>blank,<EOL>BooleanOperationSimple,<EOL>),<EOL>)<EOL>class Expression(List):<EOL><INDENT>pass<EOL><DEDENT>Expression.grammar = (<EOL>[BooleanOperationSimple, ('<STR_LIT:(>', Expression, '<STR_LIT:)>')],<EOL>maybe_some(<EOL>blank,<EOL>BooleanFunctionName,<EOL>blank,<EOL>[BooleanOperationSimple, ('<STR_LIT:(>', Expression, '<STR_LIT:)>')],<EOL>),<EOL>)<EOL>def eval_node(node):<EOL><INDENT>en = lambda n: eval_node(n)<EOL>if isinstance(node, Identifier):<EOL><INDENT>return var_map[node]<EOL><DEDENT>elif isinstance(node, StringLiteral):<EOL><INDENT>return node<EOL><DEDENT>elif isinstance(node, IntegerLiteral):<EOL><INDENT>return node<EOL><DEDENT>elif isinstance(node, FloatLiteral):<EOL><INDENT>return node<EOL><DEDENT>elif isinstance(node, BooleanLiteral):<EOL><INDENT>if node == '<STR_LIT:true>':<EOL><INDENT>return True<EOL><DEDENT>elif node == '<STR_LIT:false>':<EOL><INDENT>return False<EOL><DEDENT><DEDENT>elif isinstance(node, NullLiteral):<EOL><INDENT>return None<EOL><DEDENT>elif isinstance(node, ListOfComparables):<EOL><INDENT>return node<EOL><DEDENT>elif isinstance(node, ComparisonOperation):<EOL><INDENT>opa, opb = node[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>if node.comp_op == '<STR_LIT:=>':<EOL><INDENT>return en(opa) == en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT:>>':<EOL><INDENT>return en(opa) >  en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT:<>':<EOL><INDENT>return en(opa) <  en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT>':<EOL><INDENT>return en(opa) != en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT>':<EOL><INDENT>return en(opa) >= en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT>':<EOL><INDENT>return en(opa) <= en(opb)<EOL><DEDENT>elif node.comp_op == '<STR_LIT>':<EOL><INDENT>enopa = en(opa)<EOL>enopb = en(opb)<EOL>for other_node in list(enopb):<EOL><INDENT>virtual_node = ComparisonOperation([opa, other_node])<EOL>virtual_node.comp_op = '<STR_LIT:=>'<EOL>if en(virtual_node):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT><DEDENT>elif isinstance(node, BooleanOperationSimple):<EOL><INDENT>a = en(node[<NUM_LIT:0>])<EOL>if node.negated:<EOL><INDENT>a = not a<EOL><DEDENT>return a<EOL><DEDENT>elif isinstance(node, BooleanOperation):<EOL><INDENT>if len(node) == <NUM_LIT:1>:<EOL><INDENT>return en(node[<NUM_LIT:0>])<EOL><DEDENT>fn_map = {<EOL>'<STR_LIT>': lambda a,b: a and b,<EOL>'<STR_LIT>':  lambda a,b: a or  b,<EOL>}<EOL>def simple_eval(tr):<EOL><INDENT>return py_bool_to_lit(fn_map[tr[<NUM_LIT:1>]]( en(tr[<NUM_LIT:0>]), en(tr[<NUM_LIT:2>]))) <EOL><DEDENT>for fname in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>for i in xrange(<NUM_LIT:1>, len(node), <NUM_LIT:2>):<EOL><INDENT>if node[i] == fname:<EOL><INDENT>new_self = (<EOL>node[:i-<NUM_LIT:1>]<EOL>+ [simple_eval(node[i-<NUM_LIT:1>:i+<NUM_LIT:2>])]<EOL>+ node[i+<NUM_LIT:2>:]<EOL>)<EOL>return en(BooleanOperation(new_self))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif isinstance(node, Expression):<EOL><INDENT>def iter_over_relevant():<EOL><INDENT>for eli, el in enumerate(node):<EOL><INDENT>if eli % <NUM_LIT:2> == <NUM_LIT:0>:<EOL><INDENT>yield eli, el<EOL><DEDENT><DEDENT><DEDENT>if all(<EOL>isinstance(el, BooleanOperationSimple) <EOL>or<EOL>isinstance(el, BooleanLiteral) <EOL>for eli, el in iter_over_relevant()<EOL>):<EOL><INDENT>res = en(BooleanOperation(node))<EOL>return res<EOL><DEDENT>else:<EOL><INDENT>for eli, el in iter_over_relevant():<EOL><INDENT>if isinstance(el, Expression):<EOL><INDENT>new_self = (<EOL>node[:eli]<EOL>+ [py_bool_to_lit(en(el))]<EOL>+ node[eli+<NUM_LIT:1>:]<EOL>)<EOL>return en(Expression(new_self))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>def compose_node_to_python(node):<EOL><INDENT>return compose(node)<EOL><DEDENT>def compose_node_to_sql(node):<EOL><INDENT>return compose(node)<EOL><DEDENT>def aml_compile(source):<EOL><INDENT>return parse(source, Expression)<EOL><DEDENT>def aml_evaluate(aml_c):<EOL><INDENT>result = eval_node(aml_c)<EOL>return result<EOL><DEDENT>def aml_translate_python(aml_c):<EOL><INDENT>def comp_op_compose(self, *args, **kwargs):<EOL><INDENT>if self == '<STR_LIT:=>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return self<EOL><DEDENT><DEDENT>def null_compose(self, *args, **kwargs):<EOL><INDENT>return '<STR_LIT:None>'<EOL><DEDENT>def string_compose(self, *args, **kwargs):<EOL><INDENT>return '<STR_LIT:">' + self.replace('<STR_LIT:">', r'<STR_LIT>') + '<STR_LIT:">'<EOL><DEDENT>ComparisonOperator.compose = comp_op_compose<EOL>NullLiteral.compose = null_compose<EOL>StringLiteral.compose = string_compose<EOL>result = compose_node_to_python(aml_c)<EOL>delattr(ComparisonOperator, '<STR_LIT>')<EOL>delattr(NullLiteral, '<STR_LIT>')<EOL>delattr(StringLiteral, '<STR_LIT>')<EOL>return result<EOL><DEDENT>def aml_translate_sql(aml_c):<EOL><INDENT>def comp_op_compose(self, *args, **kwargs):<EOL><INDENT>if self == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return self<EOL><DEDENT><DEDENT>def null_compose(self, *args, **kwargs):<EOL><INDENT>return '<STR_LIT:null>'<EOL><DEDENT>def string_compose(self, *args, **kwargs):<EOL><INDENT>return "<STR_LIT:'>" + self.replace("<STR_LIT:'>", "<STR_LIT>") + "<STR_LIT:'>"<EOL><DEDENT>def comp_operation_compose(self, *args, **kwargs):<EOL><INDENT>if (<EOL>(<EOL>isinstance(self[<NUM_LIT:0>], NullLiteral) <EOL>or<EOL>isinstance(self[<NUM_LIT:1>], NullLiteral)<EOL>)<EOL>and<EOL>(<EOL>self.comp_op in ('<STR_LIT:=>', '<STR_LIT>')<EOL>)<EOL>):<EOL><INDENT>if self.comp_op == '<STR_LIT:=>':<EOL><INDENT>middle = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>middle = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>middle = compose(self.comp_op)<EOL><DEDENT>return '<STR_LIT:U+0020>'.join([<EOL>compose(self[<NUM_LIT:0>]),<EOL>middle,<EOL>compose(self[<NUM_LIT:1>]),<EOL>])<EOL><DEDENT>ComparisonOperator.compose = comp_op_compose<EOL>NullLiteral.compose = null_compose<EOL>ComparisonOperation.compose = comp_operation_compose<EOL>StringLiteral.compose = string_compose<EOL>result = compose_node_to_sql(aml_c)<EOL>delattr(ComparisonOperator, '<STR_LIT>')<EOL>delattr(NullLiteral, '<STR_LIT>')<EOL>delattr(ComparisonOperation, '<STR_LIT>')<EOL>delattr(StringLiteral, '<STR_LIT>')<EOL>return result<EOL><DEDENT>def aml_suggest(source):<EOL><INDENT>suggestions = [ ]<EOL>if var_map:<EOL><INDENT>if not source:<EOL><INDENT>suggestions = list(var_map.iterkeys())<EOL><DEDENT>else:<EOL><INDENT>split = [el for el in re.split(r'<STR_LIT>', source) if el]<EOL>if split:<EOL><INDENT>for candidate in var_map.iterkeys():<EOL><INDENT>if candidate.lower().startswith(split[-<NUM_LIT:1>].lower()):<EOL><INDENT>suggestions.append(candidate)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>suggestions.sort()<EOL>return suggestions<EOL><DEDENT>lang_instance = LangInstance()<EOL>lang_instance.aml_compile = aml_compile<EOL>lang_instance.aml_evaluate = aml_evaluate<EOL>lang_instance.aml_translate_python = aml_translate_python<EOL>lang_instance.aml_translate_sql = aml_translate_sql<EOL>lang_instance.aml_suggest = aml_suggest<EOL>return lang_instance<EOL>

>>> lang_instance = create_lang_instance()
>>> lang_instance.aml_evaluate(lang_instance.aml_compile('1 = 1'))
True
>>> li = create_lang_instance()
>>> c = li.aml_compile
>>> e = li.aml_evaluate
>>> p = li.aml_translate_python
>>> s = li.aml_translate_sql
>>> u = li.aml_suggest
>>> e(c('1 = 0'))
False
>>> e(c('"1" = "1"'))
True
>>> e(c('(1=1)'))
True
>>> e(c('1 > 1'))
False
>>> e(c('not 1 > 1'))
True
>>> e(c('1 != 1'))
False
>>> e(c('-2 = -2'))
True
>>> eval(p(c('-2 = -2')))
True
>>> eval(p(c('-2 >= -1')))
False
>>> eval(p(c('-2 <= -1')))
True
>>> eval(p(c('2 >= 1')))
True
>>> eval(p(c('2 <= 1')))
False
>>> eval(p(c('null = null')))
True
>>> eval(p(c('1 = null')))
False
>>> e(c('"foo" = "foo"'))
True
>>> e(c('"foo"'    '='    "'foo'"))
True
>>> e(c('"foo" = \\'foo\\''))
True
>>> e(c('"fo\\'o" = "fo\\'o"'))
True
>>> e(c("'foo'" + '=' + '"foo"'))
True
>>> li = create_lang_instance({'foo' : 1});
>>> c = li.aml_compile
>>> e = li.aml_evaluate
>>> e(c('foo = 1'))
True
>>> li = create_lang_instance({'foo' : 1.00})
>>> c = li.aml_compile
>>> e = li.aml_evaluate
>>> e(c('foo = 1'))
True
>>> li = create_lang_instance({'foo' : 2.24})
>>> c = li.aml_compile
>>> e = li.aml_evaluate
>>> e(c('foo = 2.24'))
True
>>> e(c('foo = 2.2399 or foo = 2.24'))
True
>>> e(c('foo = 2.2399 or foo = 2.2401'))
False
>>> e(c('foo in (2.2399, 2.24, null,)'))
True
>>> e(c('foo in (2.2399, 2.2401, null,)'))
False
>>> e(c('null in (2.2399, 2.2401, null)'))
True
>>> e(c('"null" in (2.2399, 2.2401, null)'))
False
>>> e(c('"null"' 'in' "(2.2399, 'null', null)"))
True
>>> li = create_lang_instance({'foo' : 'foo'})
>>> c = li.aml_compile
>>> e = li.aml_evaluate
>>> e(c('foo = "foo"'))
True
>>> li = create_lang_instance()
>>> c = li.aml_compile
>>> p = li.aml_translate_python
>>> s = li.aml_translate_sql
>>> s(c('null = null'))
u'null is null'
>>> p(c('null = null'))
u'None == None'
>>> s(c('null != null'))
u'null is not null'
>>> p(c('null != null'))
u'None != None'
>>> s(c('5 != 3'))
u'5 <> 3'
>>> p(c('5 != 3'))
u'5 != 3'
>>> p(c('5 in (3, 4, 5)'))
u'5 in (3, 4, 5,)'
>>> p(s('5 in (3, 4, 5)'))
u'5 in (3, 4, 5)'
>>> li = create_lang_instance({'foo' : 'bar', 'fo2' : 'ba2'})
>>> c = li.aml_compile
>>> p = li.aml_translate_python
>>> e = li.aml_evaluate
>>> u = li.aml_suggest
>>> u('1 = fo')
[u'fo2', u'foo']
>>> u('1 = FO')
[u'fo2', u'foo']
>>> p(c('null = null'))
u'None == None'
>>> e(c('foo = "bar"'))
True
>>> e(c('fo2 = "ba2"'))
True


if len(left_hash) != len(right_hash):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return sum(map(lambda x: <NUM_LIT:0> if x[<NUM_LIT:0>] == x[<NUM_LIT:1>] else <NUM_LIT:1>, zip(left_hash, right_hash)))<EOL>

Compute the hamming distance between two hashes


def hashes_are_similar(left_hash, right_hash, tolerance=<NUM_LIT:6>):

return hash_distance(left_hash, right_hash) <= tolerance<EOL>

Return True if the hamming distance between
the image hashes are less than the given tolerance.


with open(image_path, '<STR_LIT:rb>') as f:<EOL><INDENT>image = Image.open(f).resize((hash_size, hash_size), Image.ANTIALIAS).convert('<STR_LIT:L>')<EOL>pixels = list(image.getdata())<EOL><DEDENT>avg = sum(pixels) / len(pixels)<EOL>bits = "<STR_LIT>".join(map(lambda pixel: '<STR_LIT:1>' if pixel > avg else '<STR_LIT:0>', pixels))<EOL>hashformat = "<STR_LIT>".format(hashlength=hash_size ** <NUM_LIT:2> // <NUM_LIT:4>)<EOL>return int(bits, <NUM_LIT:2>).__format__(hashformat)<EOL>

Compute the average hash of the given image.


image_hash = average_hash(image_path)<EOL>other_image_hash = average_hash(other_image_path)<EOL>return hash_distance(image_hash, other_image_hash)<EOL>

Compute the hamming distance between two images


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.


with click.open_file('<STR_LIT>', '<STR_LIT:w>') as f:<EOL><INDENT>f.write(str(app_name))<EOL><DEDENT>


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.


try:<EOL><INDENT>os.remove('<STR_LIT>')<EOL><DEDENT>except (IOError, OSError):<EOL><INDENT>pass<EOL><DEDENT>


Quickly set up and tear down Heroku apps!


@cli.command(name='<STR_LIT>')<EOL>@click.option('<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', multiple=True, help='<STR_LIT>')<EOL>@click.argument('<STR_LIT>', required=False)<EOL>def up(tarball_url, auth_token, env, app_name):

tarball_url = tarball_url or _infer_tarball_url()<EOL>if not tarball_url:<EOL><INDENT>click.echo('<STR_LIT>')<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>if env:<EOL><INDENT>env = {<EOL>arg.split('<STR_LIT:=>')[<NUM_LIT:0>]: arg.split('<STR_LIT:=>')[<NUM_LIT:1>]<EOL>for arg in env<EOL>}<EOL><DEDENT>happy = Happy(auth_token=auth_token)<EOL>click.echo('<STR_LIT>', nl=False)<EOL>build_id, app_name = happy.create(<EOL>tarball_url=tarball_url,<EOL>env=env,<EOL>app_name=app_name,<EOL>)<EOL>click.echo(app_name)<EOL>click.echo('<STR_LIT>', nl=False)<EOL>happy.wait(build_id)<EOL>_write_app_name(app_name)<EOL>click.echo('<STR_LIT>')<EOL>click.echo("<STR_LIT>" % app_name)<EOL>


@cli.command(name='<STR_LIT>')<EOL>@click.option('<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', is_flag=True, help='<STR_LIT>')<EOL>@click.argument('<STR_LIT>', required=False)<EOL>def down(auth_token, force, app_name):

if not app_name:<EOL><INDENT>click.echo(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>app_name = app_name or _read_app_name()<EOL>if not app_name:<EOL><INDENT>click.echo('<STR_LIT>')<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>if not force:<EOL><INDENT>click.confirm(<EOL>'<STR_LIT>' % app_name,<EOL>abort=True,<EOL>)<EOL><DEDENT>happy = Happy(auth_token=auth_token)<EOL>click.echo('<STR_LIT>' % app_name, nl=False)<EOL>happy.delete(app_name=app_name)<EOL>_delete_app_name_file()<EOL>click.echo('<STR_LIT>')<EOL>click.echo("<STR_LIT>")<EOL>


self._auth_token = auth_token<EOL>

Intialize the class.

        :param auth_token: A Heroku API auth token.


session = Session()<EOL>session.headers = {<EOL>'<STR_LIT>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>if self._auth_token:<EOL><INDENT>session.trust_env = False  <EOL>session.headers['<STR_LIT>'] = '<STR_LIT>' % self._auth_token<EOL><DEDENT>return session<EOL>

Returns a prepared ``Session`` instance.


def api_request(self, method, endpoint, data=None, *args, **kwargs):

session = self._get_session()<EOL>api_root = '<STR_LIT>'<EOL>url = api_root + endpoint<EOL>if data:<EOL><INDENT>data = json.dumps(data)<EOL><DEDENT>response = session.request(method, url, data=data, *args, **kwargs)<EOL>if not response.ok:<EOL><INDENT>try:<EOL><INDENT>message = response.json().get('<STR_LIT:message>')<EOL><DEDENT>except ValueError:<EOL><INDENT>message = response.content<EOL><DEDENT>raise APIError(message)<EOL><DEDENT>return response.json()<EOL>

Sends an API request to Heroku.

        :param method: HTTP method.
        :param endpoint: API endpoint, e.g. ``/apps``.
        :param data: A dict sent as JSON in the body of the request.
        :returns: A dict represntation of the JSON response.


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``.


data = self.api_request('<STR_LIT:GET>', '<STR_LIT>' % build_id)<EOL>status = data.get('<STR_LIT:status>')<EOL>if status == '<STR_LIT>':<EOL><INDENT>return False<EOL><DEDENT>elif status == '<STR_LIT>':<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>raise BuildError(str(data))<EOL><DEDENT>

Checks the status of an app-setups build.

        :param build_id: ID of the build to check.
        :returns: ``True`` if succeeded, ``False`` if pending.


self.api_request('<STR_LIT>', '<STR_LIT>' % app_name)<EOL>

Deletes an app.

        :param app_name: Name of the app to delete.


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

Initializes the class.

        :param auth_token: A Heroku API auth token.


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

data = self._api.create_build(<EOL>tarball_url=tarball_url,<EOL>env=env,<EOL>app_name=app_name,<EOL>)<EOL>return (data['<STR_LIT:id>'], data['<STR_LIT>']['<STR_LIT:name>'])<EOL>

Creates a Heroku app-setup build.

        :param tarball_url: URL of a tarball containing an ``app.json``.
        :param env: (optional) Dict containing environment variable overrides.
        :param app_name: (optional) Name of the Heroku app to create.
        :returns: A tuple with ``(build_id, app_name)``.


while True:<EOL><INDENT>if self._api.check_build_status(build_id):<EOL><INDENT>break<EOL><DEDENT>sleep(<NUM_LIT:3>)<EOL><DEDENT>

Waits for an app-setup build to finish.

        :param build_id: ID of the app-setup build for which to wait.


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

Deletes a Heroku app.

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


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.


return self._node<EOL>


return self._chip_id<EOL>


if '<STR_LIT>' in os.name:<EOL><INDENT>return subprocess.getoutput('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return subprocess.getoutput('<STR_LIT>')<EOL><DEDENT>


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>


try:<EOL><INDENT>message = json.loads(message)<EOL>if not self._validate_message(message):<EOL><INDENT>message = None<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>message = None<EOL><DEDENT>return message<EOL>

Decode json string to dict. Validate against node name(targets) and protocol version
:return dict | None


if '<STR_LIT>' not in message or '<STR_LIT>' not in message ortype(message['<STR_LIT>']) is not list:<EOL><INDENT>return False<EOL><DEDENT>if message['<STR_LIT>'] != self.protocol:<EOL><INDENT>return False<EOL><DEDENT>if self.node not in message['<STR_LIT>'] and '<STR_LIT>' not in message['<STR_LIT>']:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>


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

prepares empty message adn merge it with data


from string decode to json, check protocol and targets


with open(os.path.join(*paths), '<STR_LIT:r>') as file_handler:<EOL><INDENT>return file_handler.read()<EOL><DEDENT>

Build a file path from *paths* and return the contents.


fileObj = open(filename, '<STR_LIT:wb>')<EOL>pickle.dump(variable, fileObj)<EOL>fileObj.close()<EOL>

Save variable on given path using Pickle

    Args:
        variable: what to save
        path (str): path of the output


fileObj = open(filename, '<STR_LIT:rb>')<EOL>variable = pickle.load(fileObj)<EOL>fileObj.close()<EOL>return variable<EOL>

Load variable from Pickle file

    Args:
        path (str): path of the file to load

    Returns:
        variable read from path


parser = ArgumentParser(description="<STR_LIT>")<EOL>parser.add_argument('<STR_LIT>', dest='<STR_LIT:path>', help="<STR_LIT>", nargs=<NUM_LIT:1>, action="<STR_LIT:store>", default=getcwd())<EOL>parser.add_argument('<STR_LIT>', dest='<STR_LIT>', help="<STR_LIT>", action='<STR_LIT:store_true>', default=True)<EOL>parser.add_argument('<STR_LIT>', dest='<STR_LIT>', help="<STR_LIT>", nargs=<NUM_LIT:1>, action="<STR_LIT:store>", default=["<STR_LIT>"])<EOL>parser.add_argument('<STR_LIT>', dest='<STR_LIT>', help="<STR_LIT>", action='<STR_LIT:store_true>', default=False)<EOL>parser.add_argument('<STR_LIT>', dest='<STR_LIT>', help="<STR_LIT>", action="<STR_LIT:store>", default=<NUM_LIT:10>)<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:+>', help="<STR_LIT>", action="<STR_LIT:store>")<EOL>args = parser.parse_args()<EOL>if args.verbose:<EOL><INDENT>verbose = <NUM_LIT:2><EOL>pprint(args)<EOL><DEDENT>else:<EOL><INDENT>verbose = <NUM_LIT:0><EOL><DEDENT>query = args.query[<NUM_LIT:0>]<EOL>for arg in args.query[<NUM_LIT:1>:]:<EOL><INDENT>query = query + "<STR_LIT:U+0020>" + arg<EOL><DEDENT>slb = min([len(w) for w in query.split("<STR_LIT:U+0020>")])<EOL>files = Files(path=args.path, filetype=args.filetype[<NUM_LIT:0>], exclude=[], update=args.update, verbose=verbose)<EOL>index = Index(files, slb=slb, verbose=verbose)<EOL>results = index.search(query, verbose=verbose)<EOL>Handler(results, results_number=int(args.results))<EOL>


def search(self, query, verbose=<NUM_LIT:0>):

if verbose > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" + query)<EOL><DEDENT>query = query.lower()<EOL>qgram = ng(query, self.slb)<EOL>qocument = set()<EOL>for q in qgram:<EOL><INDENT>if q in self.ngrams.keys():<EOL><INDENT>for i in self.ngrams[q]:<EOL><INDENT>qocument.add(i)<EOL><DEDENT><DEDENT><DEDENT>self.qocument = qocument<EOL>results = {}<EOL>for i in qocument:<EOL><INDENT>for j in self.D[i].keys():<EOL><INDENT>if not j in results.keys():<EOL><INDENT>results[j] = <NUM_LIT:0><EOL><DEDENT>results[j] = results[j] + self.D[i][j]<EOL><DEDENT><DEDENT>sorted_results = sorted(results.items(), key=operator.itemgetter(<NUM_LIT:1>), reverse=True)<EOL>return [self.elements[f[<NUM_LIT:0>]] for f in sorted_results]<EOL>

Searches files satisfying query

        It first decompose the query in ngrams, then score each document containing
        at least one ngram with the number. The ten document having the most ngrams
        in common with the query are selected.

        Args:
             query (str): what to search;
             results_number (int): number of results to return (default: 10)


current_os = platform.system()<EOL>tuple_xy = None<EOL>if current_os == '<STR_LIT>':<EOL><INDENT>tuple_xy = _get_terminal_size_windows()<EOL>if tuple_xy is None:<EOL><INDENT>tuple_xy = _get_terminal_size_tput()<EOL><DEDENT><DEDENT>if current_os in ['<STR_LIT>', '<STR_LIT>'] or current_os.startswith('<STR_LIT>'):<EOL><INDENT>tuple_xy = _get_terminal_size_linux()<EOL><DEDENT>if tuple_xy is None:<EOL><INDENT>tuple_xy = (<NUM_LIT>, <NUM_LIT>)      <EOL><DEDENT>return tuple_xy<EOL>

getTerminalSize()
     - get width and height of console
     - works on linux,os x,windows,cygwin(windows)
     originally retrieved from:
     http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python


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.


return hexlify(arg).decode('<STR_LIT:utf-8>')<EOL>

Convert the bytes object to a hex string.


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


if len(arg) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>


if type_system.interactive:<EOL><INDENT>print(stringable)<EOL><DEDENT>

A simple function to only print text if in an interactive session.


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


def register_type_source(self, source, name=None):

self._lazy_type_sources.append((source, name))<EOL>

Register an external source of types.

        This function does not actually load any external types, it just
        keeps track of the source, which must either be a str that is
        interpreted as a python entry_point group or a callable that
        will be passed an instance of this type system.

        External type sources will only be loaded in an as needed basis when
        a type is encountered (and needed) that is not currently known.  At
        that point external type sources will be considered until the type
        in question is found.

        If an external type source fails to load for some reason, it is logged
        but the error is not fatal.

        Args:
            source (str or callable): Either a pkg_resources entry_point
                group that will be searched for external types or a callable
                function that will be called as source(self) where self
                refers to this TypeSystem object.
            name (str): Optional short name to use for reporting errors having
                to do with this source of types.  This is most useful to pass
                when source is a callable.


def convert_to_type(self, value, typename, **kwargs):

try:<EOL><INDENT>if isinstance(value, bytearray):<EOL><INDENT>return self.convert_from_binary(value, typename, **kwargs)<EOL><DEDENT>typeobj = self.get_type(typename)<EOL>conv = typeobj.convert(value, **kwargs)<EOL>return conv<EOL><DEDENT>except (ValueError, TypeError) as exc:<EOL><INDENT>raise ValidationError("<STR_LIT>", type=typename, value=value, error_message=str(exc))<EOL><DEDENT>

Convert value to type 'typename'

If the conversion routine takes various kwargs to
modify the conversion process, \\**kwargs is passed
through to the underlying conversion function


def convert_from_binary(self, binvalue, type, **kwargs):

size = self.get_type_size(type)<EOL>if size > <NUM_LIT:0> and len(binvalue) != size:<EOL><INDENT>raise ArgumentError("<STR_LIT>", required_size=size, actual_size=len(binvalue), type=type)<EOL><DEDENT>typeobj = self.get_type(type)<EOL>if not hasattr(typeobj, '<STR_LIT>'):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=type)<EOL><DEDENT>return typeobj.convert_binary(binvalue, **kwargs)<EOL>

Convert binary data to type 'type'.

'type' must have a convert_binary function.  If 'type'
supports size checking, the size function is called to ensure
that binvalue is the correct size for deserialization


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.


def format_value(self, value, type, format=None, **kwargs):

typed_val = self.convert_to_type(value, type, **kwargs)<EOL>typeobj = self.get_type(type)<EOL>if format is None:<EOL><INDENT>if hasattr(typeobj, '<STR_LIT>'):<EOL><INDENT>format_func = getattr(typeobj, '<STR_LIT>')<EOL>return format_func(typed_val, **kwargs)<EOL><DEDENT>return str(typed_val)<EOL><DEDENT>formatter = "<STR_LIT>" % str(format)<EOL>if not hasattr(typeobj, formatter):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=type, format=format, formatter_function=formatter)<EOL><DEDENT>format_func = getattr(typeobj, formatter)<EOL>return format_func(typed_val, **kwargs)<EOL>

Convert value to type and format it as a string

type must be a known type in the type system and format,
if given, must specify a valid formatting option for the
specified type.


if not (hasattr(typeobj, "<STR_LIT>") or hasattr(typeobj, "<STR_LIT>")):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=typeobj, methods=dir(typeobj))<EOL><DEDENT>if not hasattr(typeobj, "<STR_LIT>"):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=typeobj, methods=dir(typeobj))<EOL><DEDENT>

Validate that all required type methods are implemented.

At minimum a type must have:
- a convert() or convert_binary() function
- a default_formatter() function

Raises an ArgumentError if the type is not valid


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


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


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>


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

Determine if typeobj is a factory for producing complex types


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.


def is_known_format(self, type, format):

typeobj = self.get_type(type)<EOL>formatter = "<STR_LIT>" % str(format)<EOL>if not hasattr(typeobj, formatter):<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Check if format is known for given type.

Returns boolean indicating if format is valid for the specified type.


name = self._canonicalize_type(name)<EOL>_, is_complex, _ = self.split_type(name)<EOL>if self.is_known_type(name):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=name)<EOL><DEDENT>if (not is_complex) and self._is_factory(typeobj):<EOL><INDENT>if name in self.type_factories:<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=name)<EOL><DEDENT>self.type_factories[name] = typeobj<EOL><DEDENT>else:<EOL><INDENT>self._validate_type(typeobj)<EOL>self.known_types[name] = typeobj<EOL><DEDENT>if not hasattr(typeobj, "<STR_LIT>"):<EOL><INDENT>raise ArgumentError("<STR_LIT>", type=typeobj, methods=dir(typeobj))<EOL><DEDENT>

Given a module-like object that defines a type, add it to our type system so that
it can be used with the iotile tool and with other annotated API functions.


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.


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


<EOL>return None<EOL>

Process an @param decorator into a ParameterInfo.


ret_type = return_type.args[<NUM_LIT:0>]<EOL>return None<EOL>


con = find_all(module)<EOL>if hasattr(module, "<STR_LIT>"):<EOL><INDENT>setattr(con, "<STR_LIT>", module.__doc__)<EOL><DEDENT>name = module.__name__<EOL>if hasattr(module, "<STR_LIT>"):<EOL><INDENT>name = module._name_  <EOL><DEDENT>con = annotated(con, name)<EOL>setattr(con, '<STR_LIT>', True)<EOL>return name, con<EOL>

Given a module, create a context from all of the top level annotated
symbols in that module.


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 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


def returns(desc=None, printer=None, data=True):

if data is False:<EOL><INDENT>raise ArgumentError("<STR_LIT>")<EOL><DEDENT>def _returns(func):<EOL><INDENT>annotated(func)<EOL>func.custom_returnvalue(printer, desc)<EOL>return func<EOL><DEDENT>return _returns<EOL>

Specify how the return value of this function should be handled.

    Args:
        desc (str): A deprecated description of the return value
        printer (callable): A callable function that can format this return value
        data (bool): A deprecated parameter for specifying that this function
            returns data.


func = annotated(func)<EOL>func.metadata.string_returnvalue()<EOL>return func<EOL>

Specify that the return value should just be printed as a string.

    Args:
        func (callable): The function that we wish to annotate.


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.


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.


func = annotated(func)<EOL>func.finalizer = True<EOL>return func<EOL>

Indicate that this function destroys the context in which it is invoked, such as a quit method
on a subprocess or a delete method on an object.


func = annotated(func)<EOL>func.takes_cmdline = True<EOL>return func<EOL>

Annotate that a function should take the entire command line.


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.


if hasattr(func, '<STR_LIT>'):<EOL><INDENT>if name is not None:<EOL><INDENT>func.metadata = AnnotatedMetadata(func, name)<EOL><DEDENT>return func<EOL><DEDENT>func.metadata = AnnotatedMetadata(func, name)<EOL>func.finalizer = False<EOL>func.takes_cmdline = False<EOL>func.decorated = False<EOL>func.context = False<EOL>return func<EOL>

Mark a function as callable from the command line.

    This function is meant to be called as decorator.  This function
    also initializes metadata about the function's arguments that is
    built up by the param decorator.

    Args:
        func (callable): The function that we wish to mark as callable
            from the command line.
        name (str): Optional string that will override the function's
            built-in 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


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


Add entries to root from a dict_line object.


self.root[name] = value<EOL>

Add a single function to the root context.

        Args:
            name (str): The name of the callable to add
            value (str or callable): The callable function or a string to
                lazily resolve to the callable later.


return utils.context_name(self.contexts[-<NUM_LIT:1>])<EOL>

Get the string name of the current context.


return len(self.contexts) == <NUM_LIT:0><EOL>

Check if we have finalized all contexts.

        Returns:
            bool: True if there are no nested contexts left, False otherwise


funcs = list(utils.find_all(self.contexts[-<NUM_LIT:1>])) + list(self.builtins)<EOL>return funcs<EOL>

Get a list of all valid identifiers for the current context.

        Returns:
            list(str): A list of all of the valid identifiers for this context


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.


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.


path = "<STR_LIT:.>".join([annotate.context_name(x) for x in self.contexts])<EOL>old_interactive = type_system.interactive<EOL>type_system.interactive = False<EOL>for key, cmds in self.init_commands.items():<EOL><INDENT>if path.endswith(key):<EOL><INDENT>for cmd in cmds:<EOL><INDENT>line = self._split_line(cmd)<EOL>self.invoke(line)<EOL><DEDENT><DEDENT><DEDENT>type_system.interactive = old_interactive<EOL>

Check if our context matches something that we have initialization commands
for.  If so, run them to initialize the context before proceeding with other
commands.


pass<EOL>

Pop the current context and return to its parent.


del self.contexts[:]<EOL>


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.


def find_function(self, context, funname):

if funname in self.builtins:<EOL><INDENT>return self.builtins[funname]<EOL><DEDENT>func = None<EOL>if isinstance(context, dict):<EOL><INDENT>if funname in context:<EOL><INDENT>func = context[funname]<EOL>if isinstance(func, str):<EOL><INDENT>func = self._deferred_add(func)<EOL>context[funname] = func<EOL><DEDENT><DEDENT><DEDENT>elif hasattr(context, funname):<EOL><INDENT>func = getattr(context, funname)<EOL><DEDENT>if func is None:<EOL><INDENT>raise NotFoundError("<STR_LIT>", function=funname)<EOL><DEDENT>return func<EOL>

Find a function in the given context by name.

        This function will first search the list of builtins and if the
        desired function is not a builtin, it will continue to search
        the given context.

        Args:
            context (object): A dict or class that is a typedargs context
            funname (str): The name of the function to find

        Returns:
            callable: The found function.


doc = inspect.getdoc(context)<EOL>listing = "<STR_LIT>"<EOL>listing += "<STR_LIT:\n>"<EOL>listing += annotate.context_name(context) + "<STR_LIT:\n>"<EOL>if doc is not None:<EOL><INDENT>doc = inspect.cleandoc(doc)<EOL>listing += doc + "<STR_LIT:\n>"<EOL><DEDENT>listing += "<STR_LIT>"<EOL>is_dict = False<EOL>if isinstance(context, dict):<EOL><INDENT>funs = context.keys()<EOL>is_dict = True<EOL><DEDENT>else:<EOL><INDENT>funs = utils.find_all(context)<EOL><DEDENT>for fun in sorted(funs):<EOL><INDENT>override_name = None<EOL>if is_dict:<EOL><INDENT>override_name = fun<EOL><DEDENT>fun = self.find_function(context, fun)<EOL>if isinstance(fun, dict):<EOL><INDENT>if is_dict:<EOL><INDENT>listing += "<STR_LIT>" + override_name + '<STR_LIT:\n>'<EOL><DEDENT>else:<EOL><INDENT>listing += "<STR_LIT>" + fun.metadata.name + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>listing += "<STR_LIT>" + fun.metadata.signature(name=override_name) + '<STR_LIT:\n>'<EOL><DEDENT>if annotate.short_description(fun) != "<STR_LIT>":<EOL><INDENT>listing += "<STR_LIT:U+0020>" + annotate.short_description(fun) + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>listing += "<STR_LIT>"<EOL>for bif in sorted(self.builtins.keys()):<EOL><INDENT>listing += '<STR_LIT>' + bif + '<STR_LIT:\n>'<EOL><DEDENT>listing += '<STR_LIT:\n>'<EOL>return listing<EOL>

Return a listing of all of the functions in this context including builtins.

        Args:
            context (object): The context to print a directory for.

        Returns:
            str


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.


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.


@classmethod<EOL><INDENT>def _extract_arg_value(cls, arg_name, arg_type, remaining):<DEDENT>

next_arg = None<EOL>should_consume = False<EOL>if len(remaining) > <NUM_LIT:0>:<EOL><INDENT>next_arg = remaining[<NUM_LIT:0>]<EOL>should_consume = True<EOL>if next_arg == '<STR_LIT>':<EOL><INDENT>next_arg = None<EOL><DEDENT><DEDENT>if arg_type == "<STR_LIT:bool>":<EOL><INDENT>if next_arg is None or next_arg.startswith('<STR_LIT:->'):<EOL><INDENT>next_arg = True<EOL>should_consume = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if next_arg is None:<EOL><INDENT>raise ArgumentError("<STR_LIT>", argument=arg_name)<EOL><DEDENT><DEDENT>if should_consume:<EOL><INDENT>remaining.pop(<NUM_LIT:0>)<EOL><DEDENT>return next_arg<EOL>

Try to find the value for a keyword argument.


funname = line.pop(<NUM_LIT:0>)<EOL>context = self.contexts[-<NUM_LIT:1>]<EOL>func = self.find_function(context, funname)<EOL>if isinstance(func, dict):<EOL><INDENT>self.contexts.append(func)<EOL>self._check_initialize_context()<EOL>return None, line, False<EOL><DEDENT>if func.takes_cmdline is True:<EOL><INDENT>val = func(line)<EOL>line = []<EOL><DEDENT>else:<EOL><INDENT>posargs, kwargs, line = self.process_arguments(func, line)<EOL>if inspect.isclass(func) and not func.metadata.spec_filled(posargs, kwargs):<EOL><INDENT>raise ValidationError("<STR_LIT>", function=func.metadata.name, signature=func.metadata.signature())<EOL><DEDENT>val = func(*posargs, **kwargs)<EOL><DEDENT>finished = True<EOL>if func.finalizer is True:<EOL><INDENT>self.contexts.pop()<EOL><DEDENT>elif val is not None:<EOL><INDENT>if func.metadata.returns_data():<EOL><INDENT>val = func.metadata.format_returnvalue(val)<EOL><DEDENT>else:<EOL><INDENT>self.contexts.append(val)<EOL>self._check_initialize_context()<EOL>finished = False<EOL>val = None<EOL><DEDENT><DEDENT>return val, line, finished<EOL>

Invoke a function given a list of arguments with the function listed first.

        The function is 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:
            (object, list, bool): A tuple containing the return value of the function, if any,
                a boolean specifying if the 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.


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.)


<EOL>line = str(line)<EOL>if len(line) == <NUM_LIT:0>:<EOL><INDENT>return True<EOL><DEDENT>if line[<NUM_LIT:0>] == u'<STR_LIT:#>':<EOL><INDENT>return True<EOL><DEDENT>args = self._split_line(line)<EOL>return self.invoke(args)<EOL>

Parse and invoke a string line.

        Args:
            line (str): The line that we want to parse and invoke.

        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.)


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.


def add_param(self, name, type_name, validators, desc=None):

if name in self.annotated_params:<EOL><INDENT>raise TypeSystemError("<STR_LIT>", param=name)<EOL><DEDENT>if name not in self.arg_names and name != self.varargs and name != self.kwargs:<EOL><INDENT>raise TypeSystemError("<STR_LIT>", param=name)<EOL><DEDENT>info = ParameterInfo(type_name, validators, desc)<EOL>self.annotated_params[name] = info<EOL>

Add type information for a parameter by name.

        Args:
            name (str): The name of the parameter we wish to annotate
            type_name (str): The name of the parameter's type
            validators (list): A list of either strings or n tuples that each
                specify a validator defined for type_name.  If a string is passed,
                the validator is invoked with no extra arguments.  If a tuple is
                passed, the validator will be invoked with the extra arguments.
            desc (str): Optional parameter description.


def typed_returnvalue(self, type_name, formatter=None):

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

Add type information to the return value of this function.

        Args:
            type_name (str): The name of the type of the return value.
            formatter (str): An optional name of a formatting function specified
                for the type given in type_name.


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

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


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.


return self.varargs is not None<EOL>

Check if this function supports variable arguments.


return self.kwargs is not None<EOL>

Check if this function supports arbitrary keyword arguments.


self._ensure_loaded()<EOL>return self.return_info.is_data<EOL>


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 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.


self._ensure_loaded()<EOL>if name is None:<EOL><INDENT>name = self.name<EOL><DEDENT>num_args = len(self.arg_names)<EOL>num_def = <NUM_LIT:0><EOL>if self.arg_defaults is not None:<EOL><INDENT>num_def = len(self.arg_defaults)<EOL><DEDENT>num_no_def = num_args - num_def<EOL>args = []<EOL>for i in range(<NUM_LIT:0>, len(self.arg_names)):<EOL><INDENT>typestr = "<STR_LIT>"<EOL>if self.arg_names[i] in self.annotated_params:<EOL><INDENT>typestr = "<STR_LIT>".format(self.annotated_params[self.arg_names[i]].type_name)<EOL><DEDENT>if i >= num_no_def:<EOL><INDENT>default = str(self.arg_defaults[i-num_no_def])<EOL>if len(default) == <NUM_LIT:0>:<EOL><INDENT>default = "<STR_LIT>"<EOL><DEDENT>args.append("<STR_LIT>".format(typestr, str(self.arg_names[i]), default))<EOL><DEDENT>else:<EOL><INDENT>args.append(typestr + str(self.arg_names[i]))<EOL><DEDENT><DEDENT>return "<STR_LIT>".format(name, "<STR_LIT:U+002CU+0020>".join(args))<EOL>

Return our function signature as a string.

        By default this function uses the annotated name of the function
        however if you need to override that with a custom name you can
        pass name=<custom name>

        Args:
            name (str): Optional name to override the default name given
                in the function signature.

        Returns:
            str: The formatted function signature


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 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_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 convert_argument(self, arg_name, arg_value):

self._ensure_loaded()<EOL>type_name = self.param_type(arg_name)<EOL>if type_name is None:<EOL><INDENT>return arg_value<EOL><DEDENT>val = typeinfo.type_system.convert_to_type(arg_value, type_name)<EOL>validators = self.annotated_params[arg_name].validators<EOL>if len(validators) == <NUM_LIT:0>:<EOL><INDENT>return val<EOL><DEDENT>type_obj = typeinfo.type_system.get_type(type_name)<EOL>try:<EOL><INDENT>for validator_name, extra_args in validators:<EOL><INDENT>if not hasattr(type_obj, validator_name):<EOL><INDENT>raise ValidationError("<STR_LIT>", argument=arg_name, validator_name=validator_name, type=str(type_obj), method=dir(type_obj))<EOL><DEDENT>validator = getattr(type_obj, validator_name)<EOL>validator(val, *extra_args)<EOL><DEDENT><DEDENT>except (ValueError, TypeError) as exc:<EOL><INDENT>raise ValidationError(exc.args[<NUM_LIT:0>], argument=arg_name, arg_value=val)<EOL><DEDENT>return val<EOL>

Given a parameter with type information, convert and validate it.

        Args:
            arg_name (str): The name of the argument to convert and validate
            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.


outvals = []<EOL>for val in valids:<EOL><INDENT>if isinstance(val, str):<EOL><INDENT>args = []<EOL><DEDENT>elif len(val) > <NUM_LIT:1>:<EOL><INDENT>args = val[<NUM_LIT:1>:]<EOL>val = val[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>raise ValidationError("<STR_LIT>", validator=val)<EOL><DEDENT>name = "<STR_LIT>" % str(val)<EOL>outvals.append((name, args))<EOL><DEDENT>return outvals<EOL>

Parse a list of validator names or n-tuples, checking for errors.

    Returns:
        list((func_name, [args...])): A list of validator function names and a
            potentially empty list of optional parameters for each function.


if hasattr(con, '<STR_LIT>'):<EOL><INDENT>return con.metadata.name<EOL><DEDENT>return str(con)<EOL>

Given a context, return its proper name as a string.


if isinstance(container, dict):<EOL><INDENT>names = container.keys()<EOL><DEDENT>else:<EOL><INDENT>names = dir(container)<EOL><DEDENT>built_context = BasicContext()<EOL>for name in names:<EOL><INDENT>if name.startswith('<STR_LIT:_>'):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(container, dict):<EOL><INDENT>obj = container[name]<EOL><DEDENT>else:<EOL><INDENT>obj = getattr(container, name)<EOL><DEDENT>if isinstance(container, dict) and isinstance(obj, str):<EOL><INDENT>built_context[name] = obj<EOL><DEDENT>elif hasattr(obj, '<STR_LIT>') and isinstance(getattr(obj, '<STR_LIT>'), AnnotatedMetadata):<EOL><INDENT>built_context[name] = obj<EOL><DEDENT><DEDENT>return built_context<EOL>

Find all annotated function inside of a container.

    Annotated functions are identified as those that:
    - do not start with a _ character
    - are either annotated with metadata
    - or strings that point to lazily loaded modules

    Args:
        container (object): The container to search for annotated functions.

    Returns:
        dict: A dict with all of the found functions in it.


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.


ret_def, _colon, desc = return_line.partition('<STR_LIT::>')<EOL>if _colon == "<STR_LIT>":<EOL><INDENT>raise ValidationError("<STR_LIT>", declaration=ret_def)<EOL><DEDENT>if not include_desc:<EOL><INDENT>desc = None<EOL><DEDENT>if '<STR_LIT>' in ret_def:<EOL><INDENT>ret_type, _showas, show_type = ret_def.partition('<STR_LIT>')<EOL>ret_type = ret_type.strip()<EOL>show_type = show_type.strip()<EOL>if show_type not in ('<STR_LIT:string>', '<STR_LIT>'):<EOL><INDENT>raise ValidationError("<STR_LIT>", found=show_type, expected=['<STR_LIT:string>', '<STR_LIT>'])<EOL><DEDENT>if show_type == '<STR_LIT:string>':<EOL><INDENT>return ReturnInfo(None, str, True, desc)<EOL><DEDENT>return ReturnInfo(None, None, False, desc)<EOL><DEDENT>if '<STR_LIT>' in ret_def:<EOL><INDENT>ret_type, _showas, formatter = ret_def.partition('<STR_LIT>')<EOL>ret_type = ret_type.strip()<EOL>formatter = formatter.strip()<EOL>return ReturnInfo(ret_type, formatter, True, desc)<EOL><DEDENT>return ReturnInfo(ret_def, None, True, desc)<EOL>

Parse a single return statement declaration.

    The valid types of return declarion are a Returns: section heading
    followed a line that looks like:
    type [format-as formatter]: description

    OR

    type [show-as (string | context)]: description sentence


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


name = section.lower()<EOL>if name in frozenset(['<STR_LIT:args>', '<STR_LIT>', "<STR_LIT>", "<STR_LIT>"]):<EOL><INDENT>return cls.ARGS_SECTION<EOL><DEDENT>if name in frozenset(['<STR_LIT>', '<STR_LIT>']):<EOL><INDENT>return cls.RETURN_SECTION<EOL><DEDENT>if name in frozenset(['<STR_LIT>']):<EOL><INDENT>return cls.MAIN_SECTION<EOL><DEDENT>return None<EOL>

Attempt to find the canonical name of this section.


line = line.rstrip()<EOL>if len(line) == <NUM_LIT:0>:<EOL><INDENT>return BlankLine('<STR_LIT>')<EOL><DEDENT>if '<STR_LIT:U+0020>' not in line and line.endswith('<STR_LIT::>'):<EOL><INDENT>name = line[:-<NUM_LIT:1>]<EOL>return SectionHeader(name)<EOL><DEDENT>if line.startswith('<STR_LIT:U+0020>'):<EOL><INDENT>return ContinuationLine(line.lstrip())<EOL><DEDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>return ListItem('<STR_LIT:->', line[<NUM_LIT:3>:].lstrip())<EOL><DEDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>return ListItem('<STR_LIT:->', line[<NUM_LIT:2>:].lstrip())<EOL><DEDENT>return Line(line)<EOL>


@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.


def wrap_and_format(self, width=None, include_params=False, include_return=False, excluded_params=None):

if excluded_params is None:<EOL><INDENT>excluded_params = []<EOL><DEDENT>out = StringIO()<EOL>if width is None:<EOL><INDENT>width, _height = get_terminal_size()<EOL><DEDENT>for line in self.maindoc:<EOL><INDENT>if isinstance(line, Line):<EOL><INDENT>out.write(fill(line.contents, width=width))<EOL>out.write('<STR_LIT:\n>')<EOL><DEDENT>elif isinstance(line, BlankLine):<EOL><INDENT>out.write('<STR_LIT:\n>')<EOL><DEDENT>elif isinstance(line, ListItem):<EOL><INDENT>out.write(fill(line.contents, initial_indent="<STR_LIT>" % line.marker[<NUM_LIT:0>], subsequent_indent="<STR_LIT:U+0020>", width=width))<EOL>out.write('<STR_LIT:\n>')<EOL><DEDENT><DEDENT>if include_params:<EOL><INDENT>included_params = set(self.param_info) - set(excluded_params)<EOL>if len(included_params) > <NUM_LIT:0>:<EOL><INDENT>out.write("<STR_LIT>")<EOL>for param in included_params:<EOL><INDENT>info = self.param_info[param]<EOL>out.write("<STR_LIT>" % (param, info.type_name))<EOL>out.write(fill(info.desc, initial_indent="<STR_LIT:U+0020>", subsequent_indent="<STR_LIT:U+0020>", width=width))<EOL>out.write('<STR_LIT:\n>')<EOL><DEDENT><DEDENT><DEDENT>if include_return:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT:U+0020>" + self.return_info.type_name)<EOL><DEDENT>return out.getvalue()<EOL>

Wrap, format and print this docstring for a specific width.

        Args:
            width (int): The number of characters per line.  If set to None
                this will be inferred from the terminal width and default
                to 80 if not passed or if passed as None and the terminal
                width cannot be determined.
            include_return (bool): Include the return information section
                in the output.
            include_params (bool): Include a parameter information section
                in the output.
            excluded_params (list): An optional list of parameter names to exclude.
                Options for excluding things are, for example, 'self' or 'cls'.


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.format(exclude_class=True)<EOL>

Format this exception as a string excluding class name.

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


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


@param("<STR_LIT>", "<STR_LIT>")<EOL>@param("<STR_LIT>", "<STR_LIT:bool>")<EOL>@param("<STR_LIT>", "<STR_LIT:string>")<EOL>@return_type("<STR_LIT:string>")<EOL>def func(arg1, force=False, arg2="<STR_LIT:hello>"):

return (arg1, force, arg2)<EOL>


return DemoClass(<NUM_LIT:1>)<EOL>


return self.value<EOL>


@stringable<EOL><INDENT>def return_one(self):  <DEDENT>

return <NUM_LIT:1><EOL>


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

**Description**
            Get the system 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 system falco rules file.

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


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>`_


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>`_


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

**Description**
            Set 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**
            - A string containing the user falco rules.

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

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


res = self._get_falco_rules_files("<STR_LIT:default>")<EOL>if not res[<NUM_LIT:0>]:<EOL><INDENT>return res<EOL><DEDENT>else:<EOL><INDENT>res_obj = res[<NUM_LIT:1>]["<STR_LIT>"]<EOL>ret = {}<EOL>if "<STR_LIT>" in res_obj:<EOL><INDENT>ret["<STR_LIT>"] = res_obj["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in res_obj:<EOL><INDENT>ret["<STR_LIT>"] = res_obj["<STR_LIT>"]<EOL><DEDENT>return [True, ret]<EOL><DEDENT>

**Description**
            Get the set of falco rules files from the backend. The _files programs and endpoints are a
               replacement for the system_file endpoints and allow for publishing multiple files instead
               of a single file as well as publishing multiple variants of a given file that are compatible
               with different agent versions.

        **Arguments**
            - None

        **Success Return Value**
            A dict with the following keys:
               - tag: A string used to uniquely identify this set of rules. It is recommended that this tag change every time the set of rules is updated.
               - files: An array of dicts. Each dict has the following keys:
                     - name: the name of the file
                     - variants: An array of dicts with the following keys:
                          - requiredEngineVersion: the minimum falco engine version that can read this file
                          - content: the falco rules content
            An example would be:
               {'tag': 'v1.5.9',
                'files': [
                    {
                        'name': 'falco_rules.yaml',
                        'variants': [
                            {
                                'content': '- required_engine_version: 29\n\n- list: foo\n',
                                'requiredEngineVersion': 29
                            },
                            {
                                'content': '- required_engine_version: 1\n\n- list: foo\n',
                                'requiredEngineVersion': 1
                            }
                        ]
                    },
                    {
                        'name': 'k8s_audit_rules.yaml',
                        'variants': [
                            {
                                'content': '# some comment\n',
                                'requiredEngineVersion': 0
                            }
                        ]
                    }
                ]
              }

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


def save_default_falco_rules_files(self, fsobj, save_dir):

if os.path.exists(save_dir):<EOL><INDENT>try:<EOL><INDENT>if os.path.isdir(save_dir):<EOL><INDENT>shutil.rmtree(save_dir)<EOL><DEDENT>else:<EOL><INDENT>os.unlink(save_dir)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(save_dir, str(e))]<EOL><DEDENT><DEDENT>prefix = os.path.join(save_dir, fsobj["<STR_LIT>"])<EOL>try:<EOL><INDENT>os.makedirs(prefix)<EOL><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(prefix, str(e))]<EOL><DEDENT>if "<STR_LIT>" in fsobj:<EOL><INDENT>for fobj in fsobj["<STR_LIT>"]:<EOL><INDENT>fprefix = os.path.join(prefix, fobj["<STR_LIT:name>"])<EOL>try:<EOL><INDENT>os.makedirs(fprefix)<EOL><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(fprefix, str(e))]<EOL><DEDENT>for variant in fobj["<STR_LIT>"]:<EOL><INDENT>vprefix = os.path.join(fprefix, str(variant["<STR_LIT>"]))<EOL>try:<EOL><INDENT>os.makedirs(vprefix)<EOL><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(vprefix, str(e))]<EOL><DEDENT>cpath = os.path.join(vprefix, "<STR_LIT:content>")<EOL>try:<EOL><INDENT>with open(cpath, "<STR_LIT:w>") as cfile:<EOL><INDENT>cfile.write(variant["<STR_LIT:content>"])<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(cfile, str(e))]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return [True, None]<EOL>

**Description**
            Given a dict returned from get_default_falco_rules_files, save those files to a set of files below save_dir.
               The first level below save_dir is a directory with the tag name. The second level is a directory per file.
               The third level is a directory per variant. Finally the files are at the lowest level, in a file called "content".
            For example, using the example dict in get_default_falco_rules_files(), the directory layout would look like:
                save_dir/
                    v1.5.9/
                        falco_rules.yaml/
                            29/
                                content: a file containing "- required_engine_version: 29\n\n- list: foo\n"
                            1/
                                content: a file containing "- required_engine_version: 1\n\n- list: foo\n"
                        k8s_audit_rules.yaml/
                            0/
                                content: a file containing "# some comment"
        **Arguments**
            - fsobj: a python dict matching the structure returned by get_default_falco_rules_files()
            - save_dir: a directory path under which to save the files. If the path already exists, it will be removed first.

        **Success Return Value**
            - None

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


return self._set_falco_rules_files("<STR_LIT:default>", rules_files)<EOL>

**Description**
            Update the set of falco rules files to the provided set of files. See the `Falco wiki <https://github.com/draios/falco/wiki/Falco-Rules>`_ for documentation on the falco rules format.
               The _files programs and endpoints are a replacement for the system_file endpoints and
               allow for publishing multiple files instead of a single file as well as publishing
               multiple variants of a given file that are compatible with different agent versions.

        **Arguments**
            - rules_files: a dict with the same structure as returned by get_default_falco_rules_files.

        **Success Return Value**
            The contents of the default falco rules files that were just updated.

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


tags = os.listdir(save_dir)<EOL>if len(tags) != <NUM_LIT:1>:<EOL><INDENT>return [False, "<STR_LIT>".format(save_dir)]<EOL><DEDENT>tpath = os.path.join(save_dir, tags[<NUM_LIT:0>])<EOL>if not os.path.isdir(tpath):<EOL><INDENT>return [False, "<STR_LIT>".format(tpath)]<EOL><DEDENT>ret = {"<STR_LIT>": os.path.basename(tpath), "<STR_LIT>": []}<EOL>for fdir in os.listdir(tpath):<EOL><INDENT>fpath = os.path.join(tpath, fdir)<EOL>if not os.path.isdir(fpath):<EOL><INDENT>return [False, "<STR_LIT>".format(fpath)]<EOL><DEDENT>fobj = {"<STR_LIT:name>": os.path.basename(fpath), "<STR_LIT>": []}<EOL>for vdir in os.listdir(fpath):<EOL><INDENT>vpath = os.path.join(fpath, vdir)<EOL>if not os.path.isdir(vpath):<EOL><INDENT>return [False, "<STR_LIT>".format(vpath)]<EOL><DEDENT>cpath = os.path.join(vpath, "<STR_LIT:content>")<EOL>try:<EOL><INDENT>with open(cpath, '<STR_LIT:r>') as content_file:<EOL><INDENT>try:<EOL><INDENT>required_engine_version = int(os.path.basename(vpath))<EOL>if vpath < <NUM_LIT:0>:<EOL><INDENT>return [False, "<STR_LIT>".format(vpath)]<EOL><DEDENT>fobj["<STR_LIT>"].append({<EOL>"<STR_LIT>": required_engine_version,<EOL>"<STR_LIT:content>": content_file.read()<EOL>})<EOL><DEDENT>except ValueError:<EOL><INDENT>return [False, "<STR_LIT>".format(vpath)]<EOL><DEDENT><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>return [False, "<STR_LIT>".format(cpath, str(e))]<EOL><DEDENT><DEDENT>ret["<STR_LIT>"].append(fobj)<EOL><DEDENT>return [True, ret]<EOL>

**Description**
            Given a file and directory layout as described in save_default_falco_rules_files(), load those files and
            return a dict representing the contents. This dict is suitable for passing to set_default_falco_rules_files().

        **Arguments**
            - save_dir: a directory path from which to load the files.

        **Success Return Value**
            - A dict matching the format described in get_default_falco_rules_files.

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


def get_policy_events_range(self, from_sec, to_sec, sampling=None, aggregations=None, scope_filter=None, event_filter=None):

options = {"<STR_LIT>": int(from_sec) * <NUM_LIT>,<EOL>"<STR_LIT:to>": int(to_sec) * <NUM_LIT>,<EOL>"<STR_LIT>": <NUM_LIT:0>,<EOL>"<STR_LIT>": <NUM_LIT:1000>,<EOL>"<STR_LIT>": sampling,<EOL>"<STR_LIT>": aggregations,<EOL>"<STR_LIT>": scope_filter,<EOL>"<STR_LIT>": event_filter}<EOL>ctx = {k: v for k, v in options.items() if v is not None}<EOL>return self._get_policy_events_int(ctx)<EOL>

**Description**
            Fetch all policy events that occurred in the time range [from_sec:to_sec]. This method is used in conjunction
            with :func:`~sdcclient.SdSecureClient.get_more_policy_events` to provide paginated access to policy events.

        **Arguments**
            - from_sec: the start of the timerange for which to get events
            - end_sec: the end of the timerange for which to get events
            - sampling: sample all policy events using *sampling* interval.
            - aggregations: When present it specifies how to aggregate events (sampling does not need to be specified, because when it's present it automatically means events will be aggregated). This field can either be a list of scope metrics or a list of policyEvents fields but (currently) not a mix of the two. When policy events fields are specified, only these can be used= severity, agentId, containerId, policyId, ruleType.
            - 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).
            - event_filter: this is a SysdigMonitor-like filter (e.g. policyEvent.policyId=3). When provided, events are filtered by some of their properties. Currently the supported set of filters is policyEvent.all(which can be used just with matches, policyEvent.policyId, policyEvent.id, policyEvent.severity, policyEvent.ruleTye, policyEvent.ruleSubtype.

        **Success Return Value**
            An array containing:
              - A context object that should be passed to later calls to get_more_policy_events.
              - An array of policy events, in JSON format. See :func:`~sdcclient.SdSecureClient.get_more_policy_events`
                for details on the contents of policy events.

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