def _gen(d, limit=<NUM_LIT:20>, count=False, grouprefs=None):

if grouprefs is None:<EOL><INDENT>grouprefs = {}<EOL><DEDENT>ret = ['<STR_LIT>']<EOL>strings = <NUM_LIT:0><EOL>literal = False<EOL>for i in d:<EOL><INDENT>if i[<NUM_LIT:0>] == sre_parse.IN:<EOL><INDENT>subs = _in(i[<NUM_LIT:1>])<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.LITERAL:<EOL><INDENT>literal = True<EOL>ret = mappend(ret, unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.CATEGORY:<EOL><INDENT>subs = CATEGORIES.get(i[<NUM_LIT:1>], ['<STR_LIT>'])<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ANY:<EOL><INDENT>subs = CATEGORIES['<STR_LIT>']<EOL>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MAX_REPEAT or i[<NUM_LIT:0>] == sre_parse.MIN_REPEAT:<EOL><INDENT>items = list(i[<NUM_LIT:1>][<NUM_LIT:2>])<EOL>if i[<NUM_LIT:1>][<NUM_LIT:1>] + <NUM_LIT:1> - i[<NUM_LIT:1>][<NUM_LIT:0>] >= limit:<EOL><INDENT>r1 = i[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>r2 = i[<NUM_LIT:1>][<NUM_LIT:0>] + limit<EOL><DEDENT>else:<EOL><INDENT>r1 = i[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>r2 = i[<NUM_LIT:1>][<NUM_LIT:1>] + <NUM_LIT:1><EOL><DEDENT>ran = range(r1, r2)<EOL>if count:<EOL><INDENT>branch_count = <NUM_LIT:0><EOL>for p in ran:<EOL><INDENT>branch_count += pow(_gen(items, limit, True, grouprefs), p)<EOL><DEDENT>strings = (strings or <NUM_LIT:1>) * branch_count<EOL><DEDENT>ret = prods(ret, ran, items, limit, grouprefs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.BRANCH:<EOL><INDENT>if count:<EOL><INDENT>for x in i[<NUM_LIT:1>][<NUM_LIT:1>]:<EOL><INDENT>strings += _gen(x, limit, True, grouprefs) or <NUM_LIT:1><EOL><DEDENT><DEDENT>ret = concit(ret, i[<NUM_LIT:1>][<NUM_LIT:1>], limit, grouprefs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.SUBPATTERN or i[<NUM_LIT:0>] == sre_parse.ASSERT:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:1>]<EOL>if IS_PY36_OR_GREATER and i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:3>]<EOL><DEDENT>if count:<EOL><INDENT>strings = (<EOL>strings or <NUM_LIT:1>) * (sum(ggen([<NUM_LIT:0>], _gen, subexpr, limit=limit, count=True, grouprefs=grouprefs)) or <NUM_LIT:1>)<EOL><DEDENT>ret = ggen(ret, _gen, subexpr, limit=limit, count=False, grouprefs=grouprefs, groupref=i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.AT:<EOL><INDENT>continue<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NOT_LITERAL:<EOL><INDENT>subs = list(CATEGORIES['<STR_LIT>'])<EOL>if unichr(i[<NUM_LIT:1>]) in subs:<EOL><INDENT>subs.remove(unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>if count:<EOL><INDENT>strings = (strings or <NUM_LIT:1>) * len(subs)<EOL><DEDENT>ret = comb(ret, subs)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.GROUPREF:<EOL><INDENT>ret = dappend(ret, grouprefs, i[<NUM_LIT:1>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT_NOT:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>' + repr(i))<EOL><DEDENT><DEDENT>if count:<EOL><INDENT>if strings == <NUM_LIT:0> and literal:<EOL><INDENT>inc = True<EOL>for i in d:<EOL><INDENT>if i[<NUM_LIT:0>] not in (sre_parse.AT, sre_parse.LITERAL):<EOL><INDENT>inc = False<EOL><DEDENT><DEDENT>if inc:<EOL><INDENT>strings = <NUM_LIT:1><EOL><DEDENT><DEDENT>return strings<EOL><DEDENT>return ret<EOL>


ret = u'<STR_LIT>'<EOL>for i in sre_obj:<EOL><INDENT>if i[<NUM_LIT:0>] == sre_parse.IN:<EOL><INDENT>prefix = '<STR_LIT>'<EOL>if len(i[<NUM_LIT:1>]) and i[<NUM_LIT:1>][<NUM_LIT:0>][<NUM_LIT:0>] == sre_parse.NEGATE:<EOL><INDENT>prefix = '<STR_LIT>'<EOL><DEDENT>ret += u'<STR_LIT>'.format(prefix, sre_to_string(i[<NUM_LIT:1>], paren=paren))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.LITERAL:<EOL><INDENT>u = unichr(i[<NUM_LIT:1>])<EOL>ret += u if u not in sre_parse.SPECIAL_CHARS else '<STR_LIT>'.format(u)<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.CATEGORY:<EOL><INDENT>ret += REVERSE_CATEGORIES[i[<NUM_LIT:1>]]<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ANY:<EOL><INDENT>ret += '<STR_LIT:.>'<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.BRANCH:<EOL><INDENT>parts = [sre_to_string(x, paren=paren) for x in i[<NUM_LIT:1>][<NUM_LIT:1>]]<EOL>if not any(parts):<EOL><INDENT>continue<EOL><DEDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>if len(parts) == <NUM_LIT:1>:<EOL><INDENT>paren = False<EOL><DEDENT>prefix = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>prefix = '<STR_LIT>'<EOL><DEDENT>branch = '<STR_LIT:|>'.join(parts)<EOL>if paren:<EOL><INDENT>ret += '<STR_LIT>'.format(prefix, branch)<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(branch)<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:1>]<EOL>if IS_PY36_OR_GREATER and i[<NUM_LIT:0>] == sre_parse.SUBPATTERN:<EOL><INDENT>subexpr = i[<NUM_LIT:1>][<NUM_LIT:3>]<EOL><DEDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(subexpr, paren=False))<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(subexpr, paren=paren))<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NOT_LITERAL:<EOL><INDENT>ret += '<STR_LIT>'.format(unichr(i[<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MAX_REPEAT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == i[<NUM_LIT:1>][<NUM_LIT:1>]:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:0> and i[<NUM_LIT:1>][<NUM_LIT:1>] - i[<NUM_LIT:1>][<NUM_LIT:0>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT:*>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:1> and i[<NUM_LIT:1>][<NUM_LIT:1>] - i[<NUM_LIT:1>][<NUM_LIT:0>] == sre_parse.MAXREPEAT - <NUM_LIT:1>:<EOL><INDENT>range_str = '<STR_LIT:+>'<EOL><DEDENT>else:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>], i[<NUM_LIT:1>][<NUM_LIT:1>])<EOL><DEDENT><DEDENT>ret += sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:2>], paren=paren) + range_str<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.MIN_REPEAT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:0> and i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:0>] == <NUM_LIT:1> and i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>][<NUM_LIT:1>] == sre_parse.MAXREPEAT:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>range_str = '<STR_LIT>'.format(i[<NUM_LIT:1>][<NUM_LIT:0>], i[<NUM_LIT:1>][<NUM_LIT:1>])<EOL><DEDENT>ret += sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:2>], paren=paren) + range_str<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.GROUPREF:<EOL><INDENT>ret += '<STR_LIT>'.format(i[<NUM_LIT:1>])<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.AT:<EOL><INDENT>if i[<NUM_LIT:1>] == sre_parse.AT_BEGINNING:<EOL><INDENT>ret += '<STR_LIT>'<EOL><DEDENT>elif i[<NUM_LIT:1>] == sre_parse.AT_END:<EOL><INDENT>ret += '<STR_LIT:$>'<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.NEGATE:<EOL><INDENT>pass<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.RANGE:<EOL><INDENT>ret += '<STR_LIT>'.format(unichr(i[<NUM_LIT:1>][<NUM_LIT:0>]), unichr(i[<NUM_LIT:1>][<NUM_LIT:1>]))<EOL><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT:<EOL><INDENT>if i[<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:1>], paren=False))<EOL><DEDENT>else:<EOL><INDENT>ret += '<STR_LIT>'.format(sre_to_string(i[<NUM_LIT:1>][<NUM_LIT:1>], paren=paren))<EOL><DEDENT><DEDENT>elif i[<NUM_LIT:0>] == sre_parse.ASSERT_NOT:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>' % str(i))<EOL><DEDENT><DEDENT>return ret<EOL>

sre_parse object to string

    :param sre_obj: Output of sre_parse.parse()
    :type sre_obj: list
    :rtype: str


r = parse(regex_string)<EOL>return sre_to_string(r)<EOL>

Simplify a regular expression

    :param regex_string: Regular expression
    :type regex_string: str
    :rtype: str


if IS_PY3:<EOL><INDENT>r = sre_parse.parse(s, flags=U)<EOL><DEDENT>else:<EOL><INDENT>r = sre_parse.parse(s.decode('<STR_LIT:utf-8>'), flags=U)<EOL><DEDENT>return list(r)<EOL>

Regular expression parser

    :param s: Regular expression
    :type s: str
    :rtype: list


return _gen(parse(s), limit)<EOL>

Creates a generator that generates all matching strings to a given regular expression

    :param s: Regular expression
    :type s: str
    :param limit: Range limit
    :type limit: int
    :returns: string generator object


return _gen(parse(s), limit, count=True)<EOL>

Counts all matching strings to a given regular expression

    :param s: Regular expression
    :type s: str
    :param limit: Range limit
    :type limit: int
    :rtype: int
    :returns: number of matching strings


return _randone(parse(regex_string), limit)<EOL>

Returns a random matching string to a given regular expression


def safe_setting(self, name_hyphen, default=None):

try:<EOL><INDENT>return self.setting(name_hyphen)<EOL><DEDENT>except UserFeedback:<EOL><INDENT>return default<EOL><DEDENT>

Retrieves the setting value, but returns a default value rather than
raising an error if the setting does not exist.


if name_hyphen in self._instance_settings:<EOL><INDENT>value = self._instance_settings[name_hyphen][<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" % name_hyphen<EOL>raise UserFeedback(msg)<EOL><DEDENT>if hasattr(value, '<STR_LIT>') and value.startswith("<STR_LIT:$>"):<EOL><INDENT>env_var = value.lstrip("<STR_LIT:$>")<EOL>if env_var in os.environ:<EOL><INDENT>return os.getenv(env_var)<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" % env_var<EOL>raise UserFeedback(msg)<EOL><DEDENT><DEDENT>elif hasattr(value, '<STR_LIT>') and value.startswith("<STR_LIT>"):<EOL><INDENT>return value.replace("<STR_LIT>", "<STR_LIT:$>")<EOL><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT>

Retrieves the setting value whose name is indicated by name_hyphen.

Values starting with $ are assumed to reference environment variables,
and the value stored in environment variables is retrieved. It's an
error if thes corresponding environment variable it not set.


if not skip:<EOL><INDENT>skip = []<EOL><DEDENT>return dict(<EOL>(k, v[<NUM_LIT:1>])<EOL>for k, v in six.iteritems(self._instance_settings)<EOL>if not k in skip)<EOL>

Returns dict of all setting values (removes the helpstrings).


self._update_settings(new_settings, False)<EOL>

Update settings for this instance based on the provided dictionary of
setting keys: setting values. Values should be a tuple of (helpstring,
value,) unless the setting has already been defined in a parent class,
in which case just pass the desired value.


def _update_settings(self, new_settings, enforce_helpstring=True):

for raw_setting_name, value in six.iteritems(new_settings):<EOL><INDENT>setting_name = raw_setting_name.replace("<STR_LIT:_>", "<STR_LIT:->")<EOL>setting_already_exists = setting_name in self._instance_settings<EOL>value_is_list_len_2 = isinstance(value, list) and len(value) == <NUM_LIT:2><EOL>treat_as_tuple = not setting_already_exists and value_is_list_len_2<EOL>if isinstance(value, tuple) or treat_as_tuple:<EOL><INDENT>self._instance_settings[setting_name] = value<EOL><DEDENT>else:<EOL><INDENT>if setting_name not in self._instance_settings:<EOL><INDENT>if enforce_helpstring:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise InternalCashewException(msg % setting_name)<EOL><DEDENT>else:<EOL><INDENT>self._instance_settings[setting_name] = ('<STR_LIT>', value,)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>orig = self._instance_settings[setting_name]<EOL>self._instance_settings[setting_name] = (orig[<NUM_LIT:0>], value,)<EOL><DEDENT><DEDENT><DEDENT>

This method does the work of updating settings. Can be passed with
enforce_helpstring = False which you may want if allowing end users to
add arbitrary metadata via the settings system.

Preferable to use update_settings (without leading _) in code to do the
right thing and always have docstrings.


attrs = self.setting_values()<EOL>attrs.update(self.__dict__)<EOL>skip = ["<STR_LIT>", "<STR_LIT>"]<EOL>for a in skip:<EOL><INDENT>del attrs[a]<EOL><DEDENT>return attrs<EOL>

Return a combined dictionary of setting values and attribute values.


if isinstance(alias_or_aliases, string_types):<EOL><INDENT>return [alias_or_aliases]<EOL><DEDENT>else:<EOL><INDENT>return alias_or_aliases<EOL><DEDENT>

Make sure we don't attempt to iterate over an alias string thinking
it's an array.


if isinstance(class_or_class_name, type):<EOL><INDENT>return class_or_class_name<EOL><DEDENT>elif isinstance(class_or_class_name, string_types):<EOL><INDENT>if "<STR_LIT::>" in class_or_class_name:<EOL><INDENT>mod_name, class_name = class_or_class_name.split("<STR_LIT::>")<EOL>if not mod_name in sys.modules:<EOL><INDENT>__import__(mod_name)<EOL><DEDENT>mod = sys.modules[mod_name]<EOL>return mod.__dict__[class_name]<EOL><DEDENT>else:<EOL><INDENT>return cls.load_class_from_locals(class_or_class_name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" % type(class_or_class_name)<EOL>raise InternalCashewException(msg)<EOL><DEDENT>

Detect if we get a class or a name, convert a name to a class.


docstring = inspect.getdoc(cls)<EOL>if not docstring:<EOL><INDENT>breadcrumbs = "<STR_LIT>".join(t.__name__ for t in inspect.getmro(cls)[:-<NUM_LIT:1>][::-<NUM_LIT:1>])<EOL>msg = "<STR_LIT>"<EOL>args = (cls.__name__, breadcrumbs, cls.__module__)<EOL>raise InternalCashewException(msg % args)<EOL><DEDENT>max_line_length = cls._class_settings.get('<STR_LIT>')<EOL>if max_line_length:<EOL><INDENT>for i, line in enumerate(docstring.splitlines()):<EOL><INDENT>if len(line) > max_line_length:<EOL><INDENT>msg = "<STR_LIT>" <EOL>args = (i, cls.__name__, len(line) - max_line_length)<EOL>raise Exception(msg % args)<EOL><DEDENT><DEDENT><DEDENT>return docstring<EOL>

Asserts that the class has a docstring, returning it if successful.


return reversed(inspect.getmro(cls)[<NUM_LIT:0>:-<NUM_LIT:2>])<EOL>

Returns MRO in reverse order, skipping 'object/type' class.


processed_aliases = set()<EOL>for alias in sorted(cls.plugins, cmp=lambda x,y: cmp(x.lower(), y.lower())):<EOL><INDENT>if alias in processed_aliases:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>instance = cls.create_instance(alias, *instanceargs)<EOL>instance.alias = instance.standard_alias()<EOL>yield(instance)<EOL>for alias in instance.setting('<STR_LIT>'):<EOL><INDENT>processed_aliases.add(alias)<EOL><DEDENT><DEDENT>except InactivePlugin:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Lets you iterate over instances of all plugins which are not marked as
'inactive'. If there are multiple aliases, the resulting plugin is only
called once.


<EOL>if isinstance(command, (str, unicode)):<EOL><INDENT>splitter = shlex.shlex(command.encode('<STR_LIT:utf-8>'))<EOL>splitter.whitespace = '<STR_LIT:|>'<EOL>splitter.whitespace_split = True<EOL>command = []<EOL>while True:<EOL><INDENT>token = splitter.get_token()<EOL>if token:<EOL><INDENT>command.append(token)<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>command = list(map(shlex.split, command))<EOL><DEDENT>return command<EOL>

Parses command strings and returns a Popen-ready list.


def run(command, data=None, timeout=None, kill_timeout=None, env=None, cwd=None):

command = expand_args(command)<EOL>history = []<EOL>for c in command:<EOL><INDENT>if len(history):<EOL><INDENT>data = history[-<NUM_LIT:1>].std_out[<NUM_LIT:0>:<NUM_LIT:10>*<NUM_LIT>]<EOL><DEDENT>cmd = Command(c)<EOL>try:<EOL><INDENT>out, err = cmd.run(data, timeout, kill_timeout, env, cwd)<EOL>status_code = cmd.returncode<EOL><DEDENT>except OSError as e:<EOL><INDENT>out, err = '<STR_LIT>', u"<STR_LIT:\n>".join([e.strerror, traceback.format_exc()])<EOL>status_code = <NUM_LIT><EOL><DEDENT>r = Response(process=cmd)<EOL>r.command = c<EOL>r.std_out = out<EOL>r.std_err = err<EOL>r.status_code = status_code<EOL>history.append(r)<EOL><DEDENT>r = history.pop()<EOL>r.history = history<EOL>return r<EOL>

Executes a given commmand and returns Response.

    Blocks until process is complete, or timeout is reached.


def connect(command, data=None, env=None, cwd=None):

<EOL>command_str = expand_args(command).pop()<EOL>environ = dict(os.environ)<EOL>environ.update(env or {})<EOL>process = subprocess.Popen(command_str,<EOL>universal_newlines=True,<EOL>shell=False,<EOL>env=environ,<EOL>stdin=subprocess.PIPE,<EOL>stdout=subprocess.PIPE,<EOL>stderr=subprocess.PIPE,<EOL>bufsize=<NUM_LIT:0>,<EOL>cwd=cwd,<EOL>)<EOL>return ConnectedCommand(process=process)<EOL>

Spawns a new process from the given command.


return self._status_code<EOL>

The status code of the process.
        If the code is None, assume that it's still running.


return self._process.pid<EOL>


return self._process.kill()<EOL>


if stream is None:<EOL><INDENT>stream = self.std_out<EOL><DEDENT>

Block until given bytes appear in the stream.


def send(self, str, end='<STR_LIT:\n>'):

return self._process.stdin.write(str+end)<EOL>


self._status_code = self._process.wait()<EOL>

Blocks until command finishes. Returns Response instance.


globals = self.ipy_shell.user_global_ns<EOL>locals = self.ipy_shell.user_ns<EOL>globals.update({<EOL>"<STR_LIT>": None,<EOL>})<EOL>try:<EOL><INDENT>with redirect_stdout(self.stdout):<EOL><INDENT>self.run(cell, globals, locals)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>self.code_error = True<EOL>if self.options.debug:<EOL><INDENT>raise BdbQuit<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>self.finalize()<EOL><DEDENT>

Run the Cell code using the IPython globals and locals

        Args:
            cell (str): Python code to be executed


def user_call(self, frame, argument_list):

self.get_stack_data(frame, None, '<STR_LIT>')<EOL>

This method is called when there is the remote possibility that we
        ever need to stop in this function.


This function is called when we stop or break at this line.


def user_return(self, frame, return_value):

This function is called when a return trap is set here.


def user_exception(self, frame, exc_info):

pass<EOL>

This function is called if an exception occurs,
        but only if we are to stop at or just below this level.


return "<STR_LIT>" in frame.f_globals.keys()<EOL>

Return True if the current frame belongs to the notebook, else
        False


return frame.f_code.co_filename.startswith("<STR_LIT>")<EOL>

Return True if the current frame belongs to the Cell, else
        False


def get_stack_data(self, frame, traceback, event_type):

heap_data = Heap(self.options)<EOL>stack_data = StackFrames(self.options)<EOL>stack_frames, cur_frame_ind = self.get_stack(frame, traceback)<EOL>for frame_ind, (frame, lineno) in enumerate(stack_frames):<EOL><INDENT>skip_this_stack = False<EOL>if frame_ind == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>if len(stack_data) > self.options.depth:<EOL><INDENT>skip_this_stack = True<EOL>break<EOL><DEDENT>if (not self.is_notebook_frame(frame) or<EOL>self.is_other_cell_frame(frame)):<EOL><INDENT>if not self.options.step_all:<EOL><INDENT>skip_this_stack = True<EOL>break<EOL><DEDENT>lineno = <NUM_LIT:0>  <EOL><DEDENT>else:<EOL><INDENT>lineno += <NUM_LIT:1>  <EOL><DEDENT>user_locals = filter_dict(<EOL>frame.f_locals,<EOL>ignore_vars + list(self.ipy_shell.user_ns_hidden.keys())<EOL>)<EOL>stack_data.add(frame, lineno, event_type, user_locals)<EOL>heap_data.add(user_locals)<EOL><DEDENT>if not skip_this_stack and not stack_data.is_empty():<EOL><INDENT>self.trace_history.append(<EOL>stack_data,<EOL>heap_data,<EOL>self.stdout.getvalue()<EOL>)<EOL><DEDENT>

Get the stack frames data at each of the hooks above (Ie. for each
        line of the Python code)


self.trace_history.sort_frame_locals()<EOL>

Finalize the trace history data after execution is complete


ret = {}<EOL>for key, value in d.items():<EOL><INDENT>if key not in exclude:<EOL><INDENT>ret.update({key: value})<EOL><DEDENT><DEDENT>return ret<EOL>

Return a new dict with specified keys excluded from the origional dict

    Args:
        d (dict): origional dict
        exclude (list): The keys that are excluded


old_stdout, sys.stdout = sys.stdout, new_stdout<EOL>try:<EOL><INDENT>yield None<EOL><DEDENT>finally:<EOL><INDENT>sys.stdout = old_stdout<EOL><DEDENT>

Redirect the stdout

    Args:
        new_stdout (io.StringIO): New stdout to use instead


formatters = {<EOL>float_types: lambda x: '<STR_LIT>'.format(x, options.digits),<EOL>}<EOL>for _types, fmtr in formatters.items():<EOL><INDENT>if isinstance(obj, _types):<EOL><INDENT>return fmtr(obj)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>if six.PY2 and isinstance(obj, six.string_types):<EOL><INDENT>return str(obj.encode('<STR_LIT:utf-8>'))<EOL><DEDENT>return str(obj)<EOL><DEDENT>except:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

Return a string representation of the Python object

    Args:
        obj: The Python object
        options: Format options


if isinstance(obj, primitive_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, sequence_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, array_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, key_value_types):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, types.ModuleType):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, (types.FunctionType, types.MethodType)):<EOL><INDENT>return ('<STR_LIT>', type(obj).__name__)<EOL><DEDENT>if isinstance(obj, type):<EOL><INDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>return ('<STR_LIT:class>', obj.__name__)<EOL><DEDENT><DEDENT>if isinstance(type(obj), type):<EOL><INDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>cls_name = type(obj).__name__<EOL>if cls_name == '<STR_LIT>':<EOL><INDENT>cls_name = obj.__name__<EOL>return ('<STR_LIT:class>', '<STR_LIT:{}>'.format(cls_name))<EOL><DEDENT>if cls_name == '<STR_LIT>':<EOL><INDENT>cls_name = obj.__class__.__name__<EOL><DEDENT>return ('<STR_LIT>', '<STR_LIT>'.format(cls_name))<EOL><DEDENT><DEDENT>return ('<STR_LIT>', type(obj).__name__)<EOL>

Get type information for a Python object

    Args:
        obj: The Python object

    Returns:
        tuple: (object type "catagory", object type name)


if os.path.split(program)[<NUM_LIT:0>]:<EOL><INDENT>program_path = find_exe(program)<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for path in get_path_list():<EOL><INDENT>program_path = find_exe(os.path.join(path, program))<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>

Identify the location of an executable file.


if sys.version_info[<NUM_LIT:0>] < <NUM_LIT:3>:<EOL><INDENT>text = text.encode('<STR_LIT:utf-8>')<EOL><DEDENT>print(text)<EOL>


while stdout.readline():<EOL><INDENT>pass<EOL><DEDENT>

Consume/ignore the rest of the server output.

    Without this, the server will end up hanging due to the buffer
    filling up.


def correct(text: str, matches: [Match]) -> str:

ltext = list(text)<EOL>matches = [match for match in matches if match.replacements]<EOL>errors = [ltext[match.offset:match.offset + match.errorlength]<EOL>for match in matches]<EOL>correct_offset = <NUM_LIT:0><EOL>for n, match in enumerate(matches):<EOL><INDENT>frompos, topos = (correct_offset + match.offset,<EOL>correct_offset + match.offset + match.errorlength)<EOL>if ltext[frompos:topos] != errors[n]:<EOL><INDENT>continue<EOL><DEDENT>repl = match.replacements[<NUM_LIT:0>]<EOL>ltext[frompos:topos] = list(repl)<EOL>correct_offset += len(repl) - len(errors[n])<EOL><DEDENT>return '<STR_LIT>'.join(ltext)<EOL>

Automatically apply suggestions to the text.


version = _get_attrib().get('<STR_LIT:version>')<EOL>if not version:<EOL><INDENT>match = re.search(r"<STR_LIT>", get_directory())<EOL>if match:<EOL><INDENT>version = match.group(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>return version<EOL>


return _get_attrib().get('<STR_LIT>')<EOL>


try:<EOL><INDENT>languages = cache['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>languages = LanguageTool._get_languages()<EOL>cache['<STR_LIT>'] = languages<EOL><DEDENT>return languages<EOL>


try:<EOL><INDENT>language_check_dir = cache['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>def version_key(string):<EOL><INDENT>return [int(e) if e.isdigit() else e<EOL>for e in re.split(r"<STR_LIT>", string)]<EOL><DEDENT>def get_lt_dir(base_dir):<EOL><INDENT>paths = [<EOL>path for path in<EOL>glob.glob(os.path.join(base_dir, '<STR_LIT>'))<EOL>if os.path.isdir(path)<EOL>]<EOL>return max(paths, key=version_key) if paths else None<EOL><DEDENT>base_dir = os.path.dirname(sys.argv[<NUM_LIT:0>])<EOL>language_check_dir = get_lt_dir(base_dir)<EOL>if not language_check_dir:<EOL><INDENT>try:<EOL><INDENT>base_dir = os.path.dirname(os.path.abspath(__file__))<EOL><DEDENT>except NameError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>language_check_dir = get_lt_dir(base_dir)<EOL><DEDENT>if not language_check_dir:<EOL><INDENT>raise PathError("<STR_LIT>"<EOL>.format(base_dir))<EOL><DEDENT><DEDENT>cache['<STR_LIT>'] = language_check_dir<EOL><DEDENT>return language_check_dir<EOL>


old_path = get_directory()<EOL>terminate_server()<EOL>cache.clear()<EOL>if path:<EOL><INDENT>cache['<STR_LIT>'] = path<EOL>try:<EOL><INDENT>get_jar_info()<EOL><DEDENT>except Error:<EOL><INDENT>cache['<STR_LIT>'] = old_path<EOL>raise<EOL><DEDENT><DEDENT>


return locale.getlocale()[<NUM_LIT:0>] or locale.getdefaultlocale()[<NUM_LIT:0>]<EOL>

Get the language code for the current locale setting.


if LanguageTool._server_is_alive():<EOL><INDENT>LanguageTool._terminate_server()<EOL><DEDENT>


return self._language<EOL>


return self._motherTongue<EOL>

The user's mother tongue or None.

        The mother tongue may also be used as a source language for
        checking bilingual texts.


def check(self, text: str, srctext=None) -> [Match]:

root = self._get_root(self._url, self._encode(text, srctext))<EOL>return [Match(e.attrib) for e in root if e.tag == '<STR_LIT:error>']<EOL>


def _check_api(self, text: str, srctext=None) -> bytes:

root = self._get_root(self._url, self._encode(text, srctext))<EOL>return (b'<STR_LIT>' +<EOL>ElementTree.tostring(root) + b"<STR_LIT:\n>")<EOL>

Match text against enabled rules (result in XML format).


def correct(self, text: str, srctext=None) -> str:

return correct(text, self.check(text, srctext))<EOL>


self.disabled.difference_update(self._spell_checking_rules)<EOL>


self.disabled.update(self._spell_checking_rules)<EOL>


@classmethod<EOL><INDENT>def _get_languages(cls) -> set:<DEDENT>

cls._start_server_if_needed()<EOL>url = urllib.parse.urljoin(cls._url, '<STR_LIT>')<EOL>languages = set()<EOL>for e in cls._get_root(url, num_tries=<NUM_LIT:1>):<EOL><INDENT>languages.add(e.get('<STR_LIT>'))<EOL>languages.add(e.get('<STR_LIT>'))<EOL><DEDENT>return languages<EOL>

Get supported languages (by querying the server).


cls._start_server_if_needed()<EOL>params = {'<STR_LIT>': FAILSAFE_LANGUAGE, '<STR_LIT:text>': '<STR_LIT>'}<EOL>data = urllib.parse.urlencode(params).encode()<EOL>root = cls._get_root(cls._url, data, num_tries=<NUM_LIT:1>)<EOL>return root.attrib<EOL>


match = re.search(JAVA_VERSION_REGEX, version_text)<EOL>if not match:<EOL><INDENT>raise SystemExit(<EOL>'<STR_LIT>'.format(version_text))<EOL><DEDENT>return (int(match.group('<STR_LIT>')), int(match.group('<STR_LIT>')))<EOL>

Return Java version (major1, major2).

    >>> parse_java_version('''java version "1.6.0_65"
    ... Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
    ... Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode))
    ... ''')
    (1, 6)

    >>> parse_java_version('''
    ... openjdk version "1.8.0_60"
    ... OpenJDK Runtime Environment (build 1.8.0_60-b27)
    ... OpenJDK 64-Bit Server VM (build 25.60-b23, mixed mode))
    ... ''')
    (1, 8)


java_path = find_executable('<STR_LIT>')<EOL>if not java_path:<EOL><INDENT>return JAVA_6_COMPATIBLE_VERSION<EOL><DEDENT>output = subprocess.check_output([java_path, '<STR_LIT>'],<EOL>stderr=subprocess.STDOUT,<EOL>universal_newlines=True)<EOL>java_version = parse_java_version(output)<EOL>if java_version >= (<NUM_LIT:1>, <NUM_LIT:8>):<EOL><INDENT>return LATEST_VERSION<EOL><DEDENT>elif java_version >= (<NUM_LIT:1>, <NUM_LIT:7>):<EOL><INDENT>return JAVA_7_COMPATIBLE_VERSION<EOL><DEDENT>elif java_version >= (<NUM_LIT:1>, <NUM_LIT:6>):<EOL><INDENT>warn('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>return JAVA_6_COMPATIBLE_VERSION<EOL><DEDENT>else:<EOL><INDENT>raise SystemExit(<EOL>'<STR_LIT>')<EOL><DEDENT>

Return newest compatible version.

    >>> version = get_newest_possible_languagetool_version()
    >>> version in [JAVA_6_COMPATIBLE_VERSION,
    ...             JAVA_7_COMPATIBLE_VERSION,
    ...             LATEST_VERSION]
    True


name_list = z.namelist()<EOL>if name_list and all(n.startswith(name_list[<NUM_LIT:0>]) for n in name_list[<NUM_LIT:1>:]):<EOL><INDENT>return name_list[<NUM_LIT:0>]<EOL><DEDENT>return None<EOL>

Get common directory in a zip file if any.


with io.open('<STR_LIT>', encoding='<STR_LIT:utf-8>') as input_file:<EOL><INDENT>for line in input_file:<EOL><INDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>return ast.parse(line).body[<NUM_LIT:0>].value.s<EOL><DEDENT><DEDENT><DEDENT>


def is_exe(path):<EOL><INDENT>return os.path.isfile(path) and os.access(path, os.X_OK)<EOL><DEDENT>def _get_path_list():<EOL><INDENT>return os.environ['<STR_LIT>'].split(os.pathsep)<EOL><DEDENT>if os.name == '<STR_LIT>':<EOL><INDENT>def find_exe(program):<EOL><INDENT>root, ext = os.path.splitext(program)<EOL>if ext:<EOL><INDENT>if is_exe(program):<EOL><INDENT>return program<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for ext in os.environ['<STR_LIT>'].split(os.pathsep):<EOL><INDENT>program_path = root + ext.lower()<EOL>if is_exe(program_path):<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL><DEDENT>def get_path_list():<EOL><INDENT>paths = _get_path_list()<EOL>if win_allow_cross_arch:<EOL><INDENT>alt_sys_path = os.path.expandvars(r"<STR_LIT>")<EOL>if os.path.isdir(alt_sys_path):<EOL><INDENT>paths.insert(<NUM_LIT:0>, alt_sys_path)<EOL><DEDENT>else:<EOL><INDENT>alt_sys_path = os.path.expandvars(r"<STR_LIT>")<EOL>if os.path.isdir(alt_sys_path):<EOL><INDENT>paths.append(alt_sys_path)<EOL><DEDENT><DEDENT><DEDENT>return paths<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def find_exe(program):<EOL><INDENT>return program if is_exe(program) else None<EOL><DEDENT>get_path_list = _get_path_list<EOL><DEDENT>if os.path.split(program)[<NUM_LIT:0>]:<EOL><INDENT>program_path = find_exe(program)<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for path in get_path_list():<EOL><INDENT>program_path = find_exe(os.path.join(path, program))<EOL>if program_path:<EOL><INDENT>return program_path<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>


return [element for element in (line.strip() for line in value.split('<STR_LIT:\n>'))<EOL>if element]<EOL>

Split a multiline string into a list, excluding blank lines.


items = [v.strip() for v in value.split('<STR_LIT:U+002C>')]<EOL>if len(items) == <NUM_LIT:1>:<EOL><INDENT>items = value.split()<EOL><DEDENT>return items<EOL>

Split a string with comma or space-separated elements into a list.


def eval_environ_str(value):<EOL><INDENT>parts = value.split('<STR_LIT:;>')<EOL>if len(parts) < <NUM_LIT:2>:<EOL><INDENT>return value<EOL><DEDENT>expr = parts[<NUM_LIT:1>].lstrip()<EOL>if not re.match("<STR_LIT>"<EOL>'<STR_LIT>'<EOL>"<STR_LIT>"<EOL>'<STR_LIT>', expr):<EOL><INDENT>raise ValueError('<STR_LIT>' % expr)<EOL><DEDENT>expr = re.sub(r"<STR_LIT>", r"<STR_LIT>", expr)<EOL>return parts[<NUM_LIT:0>] if eval(expr) else '<STR_LIT>'<EOL><DEDENT>if isinstance(value, list):<EOL><INDENT>new_value = []<EOL>for element in value:<EOL><INDENT>element = eval_environ_str(element)<EOL>if element:<EOL><INDENT>new_value.append(element)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(value, str):<EOL><INDENT>new_value = eval_environ_str(value)<EOL><DEDENT>else:<EOL><INDENT>new_value = value<EOL><DEDENT>return new_value<EOL>


def get_cfg_value(config, section, option):

try:<EOL><INDENT>value = config[section][option]<EOL><DEDENT>except KeyError:<EOL><INDENT>if (section, option) in MULTI_OPTIONS:<EOL><INDENT>return []<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>if (section, option) in MULTI_OPTIONS:<EOL><INDENT>value = split_multiline(value)<EOL><DEDENT>if (section, option) in ENVIRON_OPTIONS:<EOL><INDENT>value = eval_environ(value)<EOL><DEDENT>return value<EOL>


def set_cfg_value(config, section, option, value):

if isinstance(value, list):<EOL><INDENT>value = '<STR_LIT:\n>'.join(value)<EOL><DEDENT>config[section][option] = value<EOL>


kwargs = {}<EOL>opts_to_args = {<EOL>'<STR_LIT>': [<EOL>('<STR_LIT:name>', '<STR_LIT:name>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT:url>'),<EOL>('<STR_LIT>', '<STR_LIT:description>'),<EOL>('<STR_LIT:description>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>],<EOL>'<STR_LIT>': [<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>],<EOL>}<EOL>opts_to_args['<STR_LIT>'].append(('<STR_LIT>', '<STR_LIT>'))<EOL>if IS_PY2K and not which('<STR_LIT>'):<EOL><INDENT>kwargs['<STR_LIT>'] = ['<STR_LIT>']<EOL><DEDENT>kwargs['<STR_LIT>'] = False<EOL>for section in opts_to_args:<EOL><INDENT>for option, argname in opts_to_args[section]:<EOL><INDENT>value = get_cfg_value(config, section, option)<EOL>if value:<EOL><INDENT>kwargs[argname] = value<EOL><DEDENT><DEDENT><DEDENT>if '<STR_LIT>' not in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = read_description_file(config)<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = {'<STR_LIT>': kwargs['<STR_LIT>']}<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = split_elements(kwargs['<STR_LIT>'])<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = get_package_data(kwargs['<STR_LIT>'])<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = get_data_files(kwargs['<STR_LIT>'])<EOL><DEDENT>kwargs['<STR_LIT:version>'] = get_version()<EOL>if not IS_PY2K:<EOL><INDENT>kwargs['<STR_LIT>'] = '<STR_LIT:test>'<EOL><DEDENT>return kwargs<EOL>

Compatibility helper to use setup.cfg in setup.py.


args = BASE_ARGS_3TO2 if args is None else BASE_ARGS_3TO2 + args<EOL>try:<EOL><INDENT>proc = subprocess.Popen(['<STR_LIT>'] + args, stderr=subprocess.PIPE)<EOL><DEDENT>except OSError:<EOL><INDENT>for path in glob.glob('<STR_LIT>'):<EOL><INDENT>if os.path.isdir(path) and path not in sys.path:<EOL><INDENT>sys.path.append(path)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>from lib3to2.main import main as lib3to2_main<EOL><DEDENT>except ImportError:<EOL><INDENT>raise OSError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>if lib3to2_main('<STR_LIT>', args):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>num_errors = <NUM_LIT:0><EOL>while proc.poll() is None:<EOL><INDENT>line = proc.stderr.readline()<EOL>sys.stderr.write(line)<EOL>num_errors += line.count('<STR_LIT>')<EOL><DEDENT>if proc.returncode or num_errors:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>


if not isinstance(file_list, list):<EOL><INDENT>file_list = [file_list]<EOL><DEDENT>python_re = re.compile(br"<STR_LIT>")<EOL>coding_re = re.compile(br"<STR_LIT>")<EOL>new_line_re = re.compile(br"<STR_LIT>")<EOL>version_3 = LooseVersion('<STR_LIT:3>')<EOL>for file in file_list:<EOL><INDENT>if not os.path.getsize(file):<EOL><INDENT>continue<EOL><DEDENT>rewrite_needed = False<EOL>python_found = False<EOL>coding_found = False<EOL>lines = []<EOL>f = open(file, '<STR_LIT:rb>')<EOL>try:<EOL><INDENT>while len(lines) < <NUM_LIT:2>:<EOL><INDENT>line = f.readline()<EOL>match = python_re.match(line)<EOL>if match:<EOL><INDENT>python_found = True<EOL>version = LooseVersion(match.group(<NUM_LIT:2>).decode() or '<STR_LIT:2>')<EOL>try:<EOL><INDENT>version_test = version >= version_3<EOL><DEDENT>except TypeError:<EOL><INDENT>version_test = True<EOL><DEDENT>if version_test:<EOL><INDENT>line = python_re.sub(br"<STR_LIT>", line)<EOL>rewrite_needed = True<EOL><DEDENT><DEDENT>elif coding_re.search(line):<EOL><INDENT>coding_found = True<EOL><DEDENT>lines.append(line)<EOL><DEDENT>if not coding_found:<EOL><INDENT>match = new_line_re.search(lines[<NUM_LIT:0>])<EOL>newline = match.group(<NUM_LIT:1>) if match else b"<STR_LIT:\n>"<EOL>line = b"<STR_LIT>" + newline<EOL>lines.insert(<NUM_LIT:1> if python_found else <NUM_LIT:0>, line)<EOL>rewrite_needed = True<EOL><DEDENT>if rewrite_needed:<EOL><INDENT>lines += f.readlines()<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>if rewrite_needed:<EOL><INDENT>f = open(file, '<STR_LIT:wb>')<EOL>try:<EOL><INDENT>f.writelines(lines)<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT><DEDENT><DEDENT>

Write Python 2 shebang and add encoding cookie if needed.


if (any(arg.startswith('<STR_LIT>') for arg in sys.argv) and<EOL>os.path.isdir(PY2K_DIR) != IS_PY2K and os.path.isdir(LIB_DIR)):<EOL><INDENT>shutil.rmtree(LIB_DIR)<EOL><DEDENT>if IS_PY2K and any(arg.startswith(('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:test>'))<EOL>for arg in sys.argv):<EOL><INDENT>generate_py2k(config)<EOL>packages_root = get_cfg_value(config, '<STR_LIT>', '<STR_LIT>')<EOL>packages_root = os.path.join(PY2K_DIR, packages_root)<EOL>set_cfg_value(config, '<STR_LIT>', '<STR_LIT>', packages_root)<EOL><DEDENT>


config = load_config()<EOL>run_setup_hooks(config)<EOL>setup(**cfg_to_args(config))<EOL>


args = (sys.executable,) + args<EOL>return subprocess.call(args) == <NUM_LIT:0><EOL>


class ContextualZipFile(zipfile.ZipFile):<EOL><INDENT>def __enter__(self):<EOL><INDENT>return self<EOL><DEDENT>def __exit__(self, type, value, traceback):<EOL><INDENT>self.close<EOL><DEDENT><DEDENT>return zipfile.ZipFile if hasattr(zipfile.ZipFile, '<STR_LIT>') elseContextualZipFile<EOL>

Supplement ZipFile class to support context manager for Python 2.6


try:<EOL><INDENT>subprocess.check_call(cmd)<EOL><DEDENT>except subprocess.CalledProcessError:<EOL><INDENT>if os.access(target, os.F_OK):<EOL><INDENT>os.unlink(target)<EOL><DEDENT>raise<EOL><DEDENT>

Run the command to download target. If the command fails, clean up before
re-raising the error.


target = os.path.abspath(target)<EOL>cmd = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>"<STR_LIT>" % vars(),<EOL>]<EOL>_clean_check(cmd, target)<EOL>

Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.


try:<EOL><INDENT>from urllib.request import urlopen<EOL><DEDENT>except ImportError:<EOL><INDENT>from urllib2 import urlopen<EOL><DEDENT>src = dst = None<EOL>try:<EOL><INDENT>src = urlopen(url)<EOL>data = src.read()<EOL>dst = open(target, "<STR_LIT:wb>")<EOL>dst.write(data)<EOL><DEDENT>finally:<EOL><INDENT>if src:<EOL><INDENT>src.close()<EOL><DEDENT>if dst:<EOL><INDENT>dst.close()<EOL><DEDENT><DEDENT>

Use Python to download the file, even though it cannot authenticate the
connection.


def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,<EOL>to_dir=os.curdir, delay=<NUM_LIT:15>, downloader_factory=get_best_downloader):

<EOL>to_dir = os.path.abspath(to_dir)<EOL>zip_name = "<STR_LIT>" % version<EOL>url = download_base + zip_name<EOL>saveto = os.path.join(to_dir, zip_name)<EOL>if not os.path.exists(saveto):  <EOL><INDENT>log.warn("<STR_LIT>", url)<EOL>downloader = downloader_factory()<EOL>downloader(url, saveto)<EOL><DEDENT>return os.path.realpath(saveto)<EOL>

Download setuptools from a specified location and return its filename

`version` should be a valid setuptools version number that is available
as an egg for download under the `download_base` URL (which should end
with a '/'). `to_dir` is the directory where the egg will be downloaded.
`delay` is the number of seconds to pause before an actual download
attempt.

``downloader_factory`` should be a function taking no arguments and
returning a function for downloading a URL to a target.


return ['<STR_LIT>'] if options.user_install else []<EOL>

Build the arguments to 'python setup.py install' on the setuptools package


parser = optparse.OptionParser()<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', metavar="<STR_LIT>",<EOL>default=DEFAULT_URL,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT>',<EOL>const=lambda: download_file_insecure, default=get_best_downloader,<EOL>help='<STR_LIT>'<EOL>)<EOL>parser.add_option(<EOL>'<STR_LIT>', help="<STR_LIT>",<EOL>default=DEFAULT_VERSION,<EOL>)<EOL>options, args = parser.parse_args()<EOL>return options<EOL>


options = _parse_args()<EOL>archive = download_setuptools(<EOL>version=options.version,<EOL>download_base=options.download_base,<EOL>downloader_factory=options.downloader_factory,<EOL>)<EOL>return _install(archive, _build_install_args(options))<EOL>

Install or upgrade setuptools and EasyInstall


return value.tzinfo is not None and value.tzinfo.utcoffset(value) is not None<EOL>

Determines if a given datetime.datetime is aware.

The logic is described in Python's docs:
http://docs.python.org/library/datetime.html#datetime.tzinfo


return value.tzinfo is None or value.tzinfo.utcoffset(value) is None<EOL>

Determines if a given datetime.datetime is naive.

The logic is described in Python's docs:
http://docs.python.org/library/datetime.html#datetime.tzinfo


if hasattr(timezone, '<STR_LIT>') and value not in (datetime.datetime.min, datetime.datetime.max):<EOL><INDENT>return timezone.localize(value, is_dst=None)<EOL><DEDENT>else:<EOL><INDENT>return value.replace(tzinfo=timezone)<EOL><DEDENT>

Makes a naive datetime.datetime in a given time zone aware.


value = value.astimezone(timezone)<EOL>if hasattr(timezone, '<STR_LIT>'):<EOL><INDENT>value = timezone.normalize(value)<EOL><DEDENT>return value.replace(tzinfo=None)<EOL>

Makes an aware datetime.datetime naive in a given time zone.


assert root.tag == '<STR_LIT>'<EOL>if root.xpath('<STR_LIT>'):<EOL><INDENT>return _ScheduleIntervals(root, timezone)<EOL><DEDENT>elif root.xpath('<STR_LIT>'):<EOL><INDENT>return _ScheduleRecurring(root, timezone)<EOL><DEDENT>raise NotImplementedError<EOL>

Return a Schedule object based on an lxml Element for the <schedule>
        tag. timezone is a tzinfo object, ideally from pytz.


if timezone.is_aware(dt):<EOL><INDENT>return dt.astimezone(self.timezone)<EOL><DEDENT>else:<EOL><INDENT>return timezone.make_aware(dt, self.timezone)<EOL><DEDENT>

Converts a datetime to the timezone of this Schedule.


def intervals(self, range_start=datetime.datetime.min, range_end=datetime.datetime.max):

raise NotImplementedError<EOL>

Returns a list of tuples of start/end datetimes for when the schedule
        is active during the provided range.


if after is None:<EOL><INDENT>after = timezone.now()<EOL><DEDENT>after = self.to_timezone(after)<EOL>return next(self.intervals(range_start=after), None)<EOL>

Returns the next Period this event is in effect, or None if the event
        has no remaining periods.


def active_within_range(self, query_start, query_end):

return any(self.intervals(query_start, query_end))<EOL>

Is the event ever active within the given range?


query = self.to_timezone(query)<EOL>return any(self.intervals(range_start=query, range_end=query))<EOL>

Does this schedule include the provided time?
        query should be a datetime (naive or timezone-aware)


return bool(self.next_interval(after))<EOL>

Is this schedule ever in effect at or after the given time?
        If no time is given, uses the current time.


ex = {}<EOL>for sd in self.root.xpath('<STR_LIT>'):<EOL><INDENT>bits = str(sd.text).split('<STR_LIT:U+0020>')<EOL>date = text_to_date(bits.pop(<NUM_LIT:0>))<EOL>ex.setdefault(date, []).extend([<EOL>_time_text_to_period(t)<EOL>for t in bits<EOL>])<EOL><DEDENT>return ex<EOL>

A dict of dates -> [Period time tuples] representing exceptions
        to the base recurrence pattern.


def exception_periods(self, range_start=datetime.date.min, range_end=datetime.date.max):

periods = []<EOL>for exception_date, exception_times in self.exceptions.items():<EOL><INDENT>if exception_date >= range_start and exception_date <= range_end:<EOL><INDENT>for exception_time in exception_times:<EOL><INDENT>periods.append(<EOL>Period(<EOL>self.timezone.localize(datetime.datetime.combine(exception_date, exception_time.start)),<EOL>self.timezone.localize(datetime.datetime.combine(exception_date, exception_time.end))<EOL>)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>periods.sort()<EOL>return periods<EOL>

Returns a list of Period tuples for each period represented in an <exception>
        that falls between range_start and range_end.


query = self.to_timezone(query)<EOL>query_date = query.date()<EOL>query_time = query.time()<EOL>specific = self.exceptions.get(query_date)<EOL>if specific is not None:<EOL><INDENT>if len(specific) == <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT>for period in specific:<EOL><INDENT>if query_time >= period.start and query_time <= period.end:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>return any(sched.includes(query_date, query_time) for sched in self._recurring_schedules)<EOL>


def _daily_periods(self, range_start, range_end):

specific = set(self.exceptions.keys())<EOL>return heapq.merge(self.exception_periods(range_start, range_end), *[<EOL>sched.daily_periods(range_start=range_start, range_end=range_end, exclude_dates=specific)<EOL>for sched in self._recurring_schedules<EOL>])<EOL>

Returns an iterator of Period tuples for every day this event is in effect, between range_start
        and range_end.


<EOL>current_period = None<EOL>max_continuous_days = <NUM_LIT><EOL>range_start = self.to_timezone(range_start)<EOL>range_end = self.to_timezone(range_end)<EOL>for period in self._daily_periods(range_start.date(), range_end.date()):<EOL><INDENT>if period.end < range_start or period.start > range_end:<EOL><INDENT>continue<EOL><DEDENT>if current_period is None:<EOL><INDENT>current_period = period<EOL><DEDENT>else:<EOL><INDENT>if ( ((period.start < current_period.end)<EOL>or (period.start - current_period.end) <= datetime.timedelta(minutes=<NUM_LIT:1>))<EOL>and (current_period.end - current_period.start) < datetime.timedelta(days=max_continuous_days)):<EOL><INDENT>current_period = Period(current_period.start, period.end)<EOL><DEDENT>else:<EOL><INDENT>yield current_period<EOL>current_period = period<EOL><DEDENT><DEDENT><DEDENT>if current_period:<EOL><INDENT>yield current_period<EOL><DEDENT>

Returns an iterator of Period tuples for continuous stretches of time during
        which this event is in effect, between range_start and range_end.


def includes(self, query_date, query_time=None):

if self.start_date and query_date < self.start_date:<EOL><INDENT>return False<EOL><DEDENT>if self.end_date and query_date > self.end_date:<EOL><INDENT>return False<EOL><DEDENT>if query_date.weekday() not in self.weekdays:<EOL><INDENT>return False<EOL><DEDENT>if not query_time:<EOL><INDENT>return True<EOL><DEDENT>if query_time >= self.period.start and query_time <= self.period.end:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Does this schedule include the provided time?
        query_date and query_time are date and time objects, interpreted
        in this schedule's timezone


def daily_periods(self, range_start=datetime.date.min, range_end=datetime.date.max, exclude_dates=tuple()):

tz = self.timezone<EOL>period = self.period<EOL>weekdays = self.weekdays<EOL>current_date = max(range_start, self.start_date)<EOL>end_date = range_end<EOL>if self.end_date:<EOL><INDENT>end_date = min(end_date, self.end_date)<EOL><DEDENT>while current_date <= end_date:<EOL><INDENT>if current_date.weekday() in weekdays and current_date not in exclude_dates:<EOL><INDENT>yield Period(<EOL>tz.localize(datetime.datetime.combine(current_date, period.start)),<EOL>tz.localize(datetime.datetime.combine(current_date, period.end))<EOL>)<EOL><DEDENT>current_date += datetime.timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>

Returns an iterator of Period tuples for every day this schedule is in effect, between range_start
        and range_end.


start_time = self.root.findtext('<STR_LIT>')<EOL>if start_time:<EOL><INDENT>return Period(text_to_time(start_time), text_to_time(self.root.findtext('<STR_LIT>')))<EOL><DEDENT>return Period(datetime.time(<NUM_LIT:0>, <NUM_LIT:0>), datetime.time(<NUM_LIT>, <NUM_LIT>))<EOL>

A Period tuple representing the daily start and end time.


if not self.root.xpath('<STR_LIT>'):<EOL><INDENT>return set(range(<NUM_LIT:7>))<EOL><DEDENT>return set(int(d) - <NUM_LIT:1> for d in self.root.xpath('<STR_LIT>'))<EOL>

A set of integers representing the weekdays the schedule recurs on,
        with Monday = 0 and Sunday = 6.


return text_to_date(self.root.findtext('<STR_LIT>'))<EOL>

Start date of event recurrence, as datetime.date or None.


End date of event recurrence, as datetime.date or None.


def _tmdd_datetime_to_iso(dt, include_offset=True, include_seconds=True):

datestring = dt.findtext('<STR_LIT:date>')<EOL>timestring = dt.findtext('<STR_LIT:time>')<EOL>assert len(datestring) == <NUM_LIT:8><EOL>assert len(timestring) >= <NUM_LIT:6><EOL>iso = datestring[<NUM_LIT:0>:<NUM_LIT:4>] + '<STR_LIT:->' + datestring[<NUM_LIT:4>:<NUM_LIT:6>] + '<STR_LIT:->' + datestring[<NUM_LIT:6>:<NUM_LIT:8>] + '<STR_LIT:T>'+ timestring[<NUM_LIT:0>:<NUM_LIT:2>] + '<STR_LIT::>' + timestring[<NUM_LIT:2>:<NUM_LIT:4>]<EOL>if include_seconds:<EOL><INDENT>iso += '<STR_LIT::>' + timestring[<NUM_LIT:4>:<NUM_LIT:6>]<EOL><DEDENT>if include_offset:<EOL><INDENT>offset = dt.findtext('<STR_LIT>')<EOL>if offset:<EOL><INDENT>assert len(offset) == <NUM_LIT:5><EOL>iso += offset[<NUM_LIT:0>:<NUM_LIT:3>] + '<STR_LIT::>' + offset[<NUM_LIT:3>:<NUM_LIT:5>]<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % etree.tostring(dt))<EOL><DEDENT><DEDENT>return iso<EOL>

dt is an xml Element with <date>, <time>, and optionally <offset> children.
returns an ISO8601 string


<EOL>active_flag = _xpath_or_none(c.feu, '<STR_LIT>')<EOL>return '<STR_LIT>' if active_flag in ('<STR_LIT>', '<STR_LIT:2>') else '<STR_LIT>'<EOL>

Returns ACTIVE or ARCHIVED, the Open511 <status> field.


<EOL>headline = c.data['<STR_LIT>'].replace('<STR_LIT:_>', '<STR_LIT:U+0020>').title()<EOL>if c.data['<STR_LIT>']:<EOL><INDENT>headline += '<STR_LIT>' + c.data['<STR_LIT>'][<NUM_LIT:0>]['<STR_LIT:name>']<EOL>direction = c.data['<STR_LIT>'][<NUM_LIT:0>].get('<STR_LIT>')<EOL>if direction and direction not in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>headline += '<STR_LIT:U+0020>' + direction<EOL><DEDENT><DEDENT>return headline<EOL>

The only field that maps closely to Open511 <headline>, a required field, is optional
    in TMDD. So we sometimes need to generate our own.


severities = c.feu.xpath('<STR_LIT>')<EOL>impacts = c.feu.xpath('<STR_LIT>')<EOL>severities = [convert_severity[s] for s in severities]<EOL>impacts = [convert_impact[i] for i in impacts]<EOL>return ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'][max(itertools.chain(severities, impacts))]<EOL>

1. Collect all <severity> and <impact-level> values.
2. Convert impact-level of 1-3 to MINOR, 4-7 to MODERATE, 8-10 to MAJOR
3. Map severity -> none to MINOR, natural-disaster to MAJOR, other to UNKNOWN
4. Pick the highest severity.


objs = []<EOL>for feu in doc.xpath('<STR_LIT>'):<EOL><INDENT>detail_els = feu.xpath('<STR_LIT>')<EOL>for idx, detail in enumerate(detail_els):<EOL><INDENT>objs.append(cls(feu, detail, id_suffix=idx, number_in_group=len(detail_els)))<EOL><DEDENT><DEDENT>return objs<EOL>

Returns a list of TMDDEventConverter elements.

        doc is an XML Element containing one or more <FEU> events


if not geo_location.xpath('<STR_LIT>') and geo_location.xpath('<STR_LIT>'):<EOL><INDENT>raise Exception("<STR_LIT>" % etree.tostring(geo_location))<EOL><DEDENT>if _xpath_or_none(geo_location, '<STR_LIT>') not in ('<STR_LIT>', None):<EOL><INDENT>logger.warning("<STR_LIT>" % etree.tostring(geo_location))<EOL>return<EOL><DEDENT>point = (<EOL>float(_xpath_or_none(geo_location, '<STR_LIT>')) / <NUM_LIT>,<EOL>float(_xpath_or_none(geo_location, '<STR_LIT>')) / <NUM_LIT><EOL>)<EOL>self.points.add(point)<EOL>

Saves a <geo-location> Element, to be incoporated into the Open511
geometry field.


assert format in ('<STR_LIT>', '<STR_LIT>')<EOL>if getattr(doc, '<STR_LIT>', None) == '<STR_LIT>':<EOL><INDENT>if format == '<STR_LIT>':<EOL><INDENT>return xml_to_json(doc)<EOL><DEDENT><DEDENT>elif isinstance(doc, dict) and '<STR_LIT>' in doc:<EOL><INDENT>if format == '<STR_LIT>':<EOL><INDENT>return json_doc_to_xml(doc)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return doc<EOL>

Ensures that the provided document is an lxml Element or json dict.


def open511_convert(input_doc, output_format, serialize=True, **kwargs):

try:<EOL><INDENT>output_format_info = FORMATS[output_format]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ValueError("<STR_LIT>" % output_format)<EOL><DEDENT>input_doc = ensure_format(input_doc, output_format_info.input_format)<EOL>result = output_format_info.func(input_doc, **kwargs)<EOL>if serialize:<EOL><INDENT>result = output_format_info.serializer(result)<EOL><DEDENT>return result<EOL>

Convert an Open511 document between formats.
input_doc - either an lxml open511 Element or a deserialized JSON dict
output_format - short string name of a valid output format, as listed above


j = {}<EOL>if len(root) == <NUM_LIT:0>:  <EOL><INDENT>return _maybe_intify(root.text)<EOL><DEDENT>if len(root) == <NUM_LIT:1> and root[<NUM_LIT:0>].tag.startswith('<STR_LIT:{>' + NS_GML):  <EOL><INDENT>return gml_to_geojson(root[<NUM_LIT:0>])<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'] = {'<STR_LIT:version>': root.get('<STR_LIT:version>')}<EOL><DEDENT>for elem in root:<EOL><INDENT>name = elem.tag<EOL>if name == '<STR_LIT>' and elem.get('<STR_LIT>'):<EOL><INDENT>name = elem.get('<STR_LIT>') + '<STR_LIT>'<EOL>if name == '<STR_LIT>':<EOL><INDENT>name = '<STR_LIT:url>'<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'][name] = elem.get('<STR_LIT>')<EOL>continue<EOL><DEDENT><DEDENT>elif name.startswith('<STR_LIT:{>' + NS_PROTECTED):<EOL><INDENT>name = '<STR_LIT:!>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:] <EOL><DEDENT>elif name[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>name = '<STR_LIT:+>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:]<EOL><DEDENT>if name in j:<EOL><INDENT>continue  <EOL><DEDENT>elif elem.tag == '<STR_LIT>' and not elem.text:<EOL><INDENT>j[name] = elem.get('<STR_LIT>')<EOL><DEDENT>elif len(elem):<EOL><INDENT>if name == '<STR_LIT>':<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=False) for child in elem]<EOL><DEDENT>elif name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=True) for child in elem]<EOL><DEDENT>elif all((name == pluralize(child.tag) for child in elem)):<EOL><INDENT>j[name] = [xml_to_json(child) for child in elem]<EOL><DEDENT>else:<EOL><INDENT>j[name] = xml_to_json(elem)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if root.tag == '<STR_LIT>' and name.endswith('<STR_LIT:s>') and not elem.text:<EOL><INDENT>j[name] = []<EOL><DEDENT>else:<EOL><INDENT>j[name] = _maybe_intify(elem.text)<EOL><DEDENT><DEDENT><DEDENT>return j<EOL>

Convert an Open511 XML document or document fragment to JSON.

    Takes an lxml Element object. Returns a dict ready to be JSON-serialized.


if el.get('<STR_LIT>') not in ('<STR_LIT>', None):<EOL><INDENT>if el.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>return _gmlv2_to_geojson(el)<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>" % el.get('<STR_LIT>'))<EOL><DEDENT><DEDENT>tag = el.tag.replace('<STR_LIT>' % NS_GML, '<STR_LIT>')<EOL>if tag == '<STR_LIT>':<EOL><INDENT>coordinates = _reverse_gml_coords(el.findtext('<STR_LIT>' % NS_GML))[<NUM_LIT:0>]<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = _reverse_gml_coords(el.findtext('<STR_LIT>' % NS_GML))<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = []<EOL>for ring in el.xpath('<STR_LIT>', namespaces=NSMAP)+ el.xpath('<STR_LIT>', namespaces=NSMAP):<EOL><INDENT>coordinates.append(_reverse_gml_coords(ring.text))<EOL><DEDENT><DEDENT>elif tag in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>single_type = tag[<NUM_LIT:5>:]<EOL>member_tag = single_type[<NUM_LIT:0>].lower() + single_type[<NUM_LIT:1>:] + '<STR_LIT>'<EOL>coordinates = [<EOL>gml_to_geojson(member)['<STR_LIT>']<EOL>for member in el.xpath('<STR_LIT>' % (member_tag, single_type), namespaces=NSMAP)<EOL>]<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return {<EOL>'<STR_LIT:type>': tag,<EOL>'<STR_LIT>': coordinates<EOL>}<EOL>

Given an lxml Element of a GML geometry, returns a dict in GeoJSON format.


tag = el.tag.replace('<STR_LIT>' % NS_GML, '<STR_LIT>')<EOL>if tag == '<STR_LIT>':<EOL><INDENT>coordinates = [float(c) for c in el.findtext('<STR_LIT>' % NS_GML).split('<STR_LIT:U+002C>')]<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = [<EOL>[float(x) for x in pair.split('<STR_LIT:U+002C>')]<EOL>for pair in el.findtext('<STR_LIT>' % NS_GML).split('<STR_LIT:U+0020>')<EOL>]<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>coordinates = []<EOL>for ring in el.xpath('<STR_LIT>', namespaces=NSMAP)+ el.xpath('<STR_LIT>', namespaces=NSMAP):<EOL><INDENT>coordinates.append([<EOL>[float(x) for x in pair.split('<STR_LIT:U+002C>')]<EOL>for pair in ring.text.split('<STR_LIT:U+0020>')<EOL>])<EOL><DEDENT><DEDENT>elif tag in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if tag == '<STR_LIT>':<EOL><INDENT>single_type = '<STR_LIT>'<EOL>member_tag = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>single_type = tag[<NUM_LIT:5>:]<EOL>member_tag = single_type[<NUM_LIT:0>].lower() + single_type[<NUM_LIT:1>:] + '<STR_LIT>'<EOL><DEDENT>coordinates = [<EOL>gml_to_geojson(member)['<STR_LIT>']<EOL>for member in el.xpath('<STR_LIT>' % (member_tag, single_type), namespaces=NSMAP)<EOL>]<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return {<EOL>'<STR_LIT:type>': tag,<EOL>'<STR_LIT>': coordinates<EOL>}<EOL>

Translates a deprecated GML 2.0 geometry to GeoJSON


def json_doc_to_xml(json_obj, lang='<STR_LIT>', custom_namespace=None):

if '<STR_LIT>' not in json_obj:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>json_obj = dict(json_obj)<EOL>meta = json_obj.pop('<STR_LIT>')<EOL>elem = get_base_open511_element(lang=lang, version=meta.pop('<STR_LIT:version>'))<EOL>pagination = json_obj.pop('<STR_LIT>', None)<EOL>json_struct_to_xml(json_obj, elem, custom_namespace=custom_namespace)<EOL>if pagination:<EOL><INDENT>elem.append(json_struct_to_xml(pagination, '<STR_LIT>', custom_namespace=custom_namespace))<EOL><DEDENT>json_struct_to_xml(meta, elem)<EOL>return elem<EOL>

Converts a Open511 JSON document to XML.

    lang: the appropriate language code

    Takes a dict deserialized from JSON, returns an lxml Element.

    Accepts only the full root-level JSON object from an Open511 response.


def json_struct_to_xml(json_obj, root, custom_namespace=None):

if isinstance(root, (str, unicode)):<EOL><INDENT>if root.startswith('<STR_LIT:!>'):<EOL><INDENT>root = etree.Element('<STR_LIT>' % (NS_PROTECTED, root[<NUM_LIT:1>:]))<EOL><DEDENT>elif root.startswith('<STR_LIT:+>'):<EOL><INDENT>if not custom_namespace:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>root = etree.Element('<STR_LIT>' % (custom_namespace, root[<NUM_LIT:1>:]))<EOL><DEDENT>else:<EOL><INDENT>root = etree.Element(root)<EOL><DEDENT><DEDENT>if root.tag in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>for link in json_obj:<EOL><INDENT>root.append(json_link_to_xml(link))<EOL><DEDENT><DEDENT>elif isinstance(json_obj, (str, unicode)):<EOL><INDENT>root.text = json_obj<EOL><DEDENT>elif isinstance(json_obj, (int, float)):<EOL><INDENT>root.text = unicode(json_obj)<EOL><DEDENT>elif isinstance(json_obj, dict):<EOL><INDENT>if frozenset(json_obj.keys()) == frozenset(('<STR_LIT:type>', '<STR_LIT>')):<EOL><INDENT>root.append(geojson_to_gml(json_obj))<EOL><DEDENT>else:<EOL><INDENT>for key, val in json_obj.items():<EOL><INDENT>if key == '<STR_LIT:url>' or key.endswith('<STR_LIT>'):<EOL><INDENT>el = json_link_to_xml(val, json_link_key_to_xml_rel(key))<EOL><DEDENT>else:<EOL><INDENT>el = json_struct_to_xml(val, key, custom_namespace=custom_namespace)<EOL><DEDENT>if el is not None:<EOL><INDENT>root.append(el)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif isinstance(json_obj, list):<EOL><INDENT>tag_name = root.tag<EOL>if tag_name.endswith('<STR_LIT>'):<EOL><INDENT>tag_name = tag_name[:-<NUM_LIT:3>] + '<STR_LIT:y>'<EOL><DEDENT>elif tag_name.endswith('<STR_LIT:s>'):<EOL><INDENT>tag_name = tag_name[:-<NUM_LIT:1>]<EOL><DEDENT>for val in json_obj:<EOL><INDENT>el = json_struct_to_xml(val, tag_name, custom_namespace=custom_namespace)<EOL>if el is not None:<EOL><INDENT>root.append(el)<EOL><DEDENT><DEDENT><DEDENT>elif json_obj is None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return root<EOL>

Converts a Open511 JSON fragment to XML.

    Takes a dict deserialized from JSON, returns an lxml Element.

    This won't provide a conforming document if you pass in a full JSON document;
    it's for translating little fragments, and is mostly used internally.


tag = G(gj['<STR_LIT:type>'])<EOL>if set_srs:<EOL><INDENT>tag.set('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>if gj['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>tag.append(G.pos(_reverse_geojson_coords(gj['<STR_LIT>'])))<EOL><DEDENT>elif gj['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>tag.append(G.posList('<STR_LIT:U+0020>'.join(_reverse_geojson_coords(ll) for ll in gj['<STR_LIT>'])))<EOL><DEDENT>elif gj['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>rings = [<EOL>G.LinearRing(<EOL>G.posList('<STR_LIT:U+0020>'.join(_reverse_geojson_coords(ll) for ll in ring))<EOL>) for ring in gj['<STR_LIT>']<EOL>]<EOL>tag.append(G.exterior(rings.pop(<NUM_LIT:0>)))<EOL>for ring in rings:<EOL><INDENT>tag.append(G.interior(ring))<EOL><DEDENT><DEDENT>elif gj['<STR_LIT:type>'] in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>single_type = gj['<STR_LIT:type>'][<NUM_LIT:5>:]<EOL>member_tag = single_type[<NUM_LIT:0>].lower() + single_type[<NUM_LIT:1>:] + '<STR_LIT>'<EOL>for coord in gj['<STR_LIT>']:<EOL><INDENT>tag.append(<EOL>G(member_tag, geojson_to_gml({'<STR_LIT:type>': single_type, '<STR_LIT>': coord}, set_srs=False))<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>return tag<EOL>

Given a dict deserialized from a GeoJSON object, returns an lxml Element
    of the corresponding GML geometry.


if geom.srs.srid != <NUM_LIT>:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>return geojson_to_gml(json.loads(geom.geojson))<EOL>

Transform a GEOS or OGR geometry object into an lxml Element
    for the GML geometry.


pending_updates = []<EOL>url = PATHS['<STR_LIT>'] % self.profile_id<EOL>response = self.api.get(url=url)<EOL>for update in response['<STR_LIT>']:<EOL><INDENT>pending_updates.append(Update(api=self.api, raw_response=update))<EOL><DEDENT>self.__pending = pending_updates<EOL>return self.__pending<EOL>

Returns an array of updates that are currently in the buffer for an
individual social media profile.


sent_updates = []<EOL>url = PATHS['<STR_LIT>'] % self.profile_id<EOL>response = self.api.get(url=url)<EOL>for update in response['<STR_LIT>']:<EOL><INDENT>sent_updates.append(Update(api=self.api, raw_response=update))<EOL><DEDENT>self.__sent = sent_updates<EOL>return self.__sent<EOL>

Returns an array of updates that have been sent from the buffer for an
individual social media profile.


def shuffle(self, count=None, utc=None):

url = PATHS['<STR_LIT>'] % self.profile_id<EOL>post_data = '<STR_LIT>'<EOL>if count:<EOL><INDENT>post_data += '<STR_LIT>' % count<EOL><DEDENT>if utc:<EOL><INDENT>post_data += '<STR_LIT>' % utc<EOL><DEDENT>return self.api.post(url=url, data=post_data)<EOL>

Randomize the order at which statuses for the specified social media
profile will be sent out of the buffer.


def reorder(self, updates_ids, offset=None, utc=None):

url = PATHS['<STR_LIT>'] % self.profile_id<EOL>order_format = "<STR_LIT>"<EOL>post_data = '<STR_LIT>'<EOL>if offset:<EOL><INDENT>post_data += '<STR_LIT>' % offset<EOL><DEDENT>if utc:<EOL><INDENT>post_data += '<STR_LIT>' % utc<EOL><DEDENT>for update in updates_ids:<EOL><INDENT>post_data += order_format % update<EOL><DEDENT>return self.api.post(url=url, data=post_data)<EOL>

Edit the order at which statuses for the specified social media profile will
be sent out of the buffer.


def new(self, text, shorten=None, now=None, top=None, media=None, when=None):

url = PATHS['<STR_LIT>']<EOL>post_data = "<STR_LIT>" % text<EOL>post_data += "<STR_LIT>" % self.profile_id<EOL>if shorten:<EOL><INDENT>post_data += "<STR_LIT>" % shorten<EOL><DEDENT>if now:<EOL><INDENT>post_data += "<STR_LIT>" % now<EOL><DEDENT>if top:<EOL><INDENT>post_data += "<STR_LIT>" % top<EOL><DEDENT>if when:<EOL><INDENT>post_data += "<STR_LIT>" % str(when)<EOL><DEDENT>if media:<EOL><INDENT>media_format = "<STR_LIT>"<EOL>for media_type, media_item in media.iteritems():<EOL><INDENT>post_data += media_format % (media_type, media_item)<EOL><DEDENT><DEDENT>response = self.api.post(url=url, data=post_data)<EOL>new_update = Update(api=self.api, raw_response=response['<STR_LIT>'][<NUM_LIT:0>])<EOL>self.append(new_update)<EOL>return new_update<EOL>


response = self.api.get(url=PATHS['<STR_LIT>'])<EOL>for raw_profile in response:<EOL><INDENT>self.append(Profile(self.api, raw_profile))<EOL><DEDENT>return self<EOL>


if not len(self):<EOL><INDENT>self.all()<EOL><DEDENT>new_list = filter(lambda item: [True for arg in kwargs if item[arg] == kwargs[arg]] != [], self)<EOL>return Profiles(self.api, new_list)<EOL>

Based on some criteria, filter the profiles and return a new Profiles
Manager containing only the chosen items

If the manager doen't have any items, get all the profiles from Buffer


interactions = []<EOL>url = PATHS['<STR_LIT>'] % self.id<EOL>response = self.api.get(url=url)<EOL>for interaction in response['<STR_LIT>']:<EOL><INDENT>interactions.append(ResponseObject(interaction))<EOL><DEDENT>self.__interactions = interactions<EOL>return self.__interactions<EOL>

Returns the detailed information on individual interactions with the social
media update such as favorites, retweets and likes.


def edit(self, text, media=None, utc=None, now=None):

url = PATHS['<STR_LIT>'] % self.id<EOL>post_data = "<STR_LIT>" % text<EOL>if now:<EOL><INDENT>post_data += "<STR_LIT>" % now<EOL><DEDENT>if utc:<EOL><INDENT>post_data += "<STR_LIT>" % utc<EOL><DEDENT>if media:<EOL><INDENT>media_format = "<STR_LIT>"<EOL>for media_type, media_item in media.iteritems():<EOL><INDENT>post_data += media_format % (media_type, media_item)<EOL><DEDENT><DEDENT>response = self.api.post(url=url, data=post_data)<EOL>return Update(api=self.api, raw_response=response['<STR_LIT>'])<EOL>

Edit an existing, individual status update.


url = PATHS['<STR_LIT>'] % self.id<EOL>return self.api.post(url=url)<EOL>

Immediately shares a single pending update and recalculates times for
updates remaining in the queue.


Permanently delete an existing status update.


url = PATHS['<STR_LIT>'] % self.id<EOL>response = self.api.post(url=url)<EOL>return Update(api=self.api, raw_response=response)<EOL>

Move an existing status update to the top of the queue and recalculate
times for all updates in the queue. Returns the update with its new
posting time.


self.shares = self.api.get(url=PATHS['<STR_LIT>'] % self.url)['<STR_LIT>']<EOL>return self.shares<EOL>

Returns an object with a the numbers of shares a link has had using
Buffer.

www will be stripped, but other subdomains will not.


url = PATHS['<STR_LIT>'] % self.id<EOL>self.__schedules = self.api.get(url=url)<EOL>return self.__schedules<EOL>

Returns details of the posting schedules associated with a social media
profile.


url = PATHS['<STR_LIT>'] % self.id<EOL>data_format = "<STR_LIT>"<EOL>post_data = "<STR_LIT>"<EOL>for format_type, values in schedules.iteritems():<EOL><INDENT>for value in values:<EOL><INDENT>post_data += data_format % (format_type, value)<EOL><DEDENT><DEDENT>self.api.post(url=url, data=post_data)<EOL>

Set the posting schedules for the specified social media profile.


response = self.get(url=PATHS['<STR_LIT>'])<EOL>return ResponseObject(response)<EOL>

Returns an object with the current configuration that Buffer is using,
including supported services, their icons and the varying limits of
character and schedules.

The services keys map directly to those on profiles and updates so that
you can easily show the correct icon or calculate the correct character
length for an update.


if hasattr(sys.stderr, '<STR_LIT>') and sys.stderr.isatty():<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>request="<STR_LIT>" % (self.method, self.url),<EOL>http_code = self.http_code,<EOL>error_code = self.error_code,<EOL>description=self.description<EOL>))<EOL>return msg<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>".format(self.http_code, self.error_code, self.description)<EOL><DEDENT>

:return: if on TTY, a friendly string conversion for the object,
else a one liner indicating the error


for key in root_dict:<EOL><INDENT>if isinstance(root_dict[key], dict):<EOL><INDENT>root_dict[key] = ResponseObject(root_dict[key])<EOL><DEDENT><DEDENT>return root_dict<EOL>

Used to check if there is a dict in a dict


apps = []<EOL>for app_name in settings.INSTALLED_APPS:<EOL><INDENT>mongoadmin = "<STR_LIT>".format(app_name)<EOL>try:<EOL><INDENT>module = import_module(mongoadmin)<EOL><DEDENT>except ImportError as e:<EOL><INDENT>if str(e).startswith("<STR_LIT>"):<EOL><INDENT>continue<EOL><DEDENT>raise e<EOL><DEDENT>app_store = AppStore(module)<EOL>apps.append(dict(<EOL>app_name=app_name,<EOL>obj=app_store<EOL>))<EOL><DEDENT>return apps<EOL>

Returns a list of all mongoadmin implementations for the site


if hasattr(self, "<STR_LIT>"):<EOL><INDENT>return None<EOL><DEDENT>self.app_label = self.kwargs.get('<STR_LIT>')<EOL>self.document_name = self.kwargs.get('<STR_LIT>')<EOL>self.models_name = self.kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>self.model_name = "<STR_LIT>".format(self.app_label, self.models_name)<EOL>self.models = import_module(self.model_name)<EOL>

Sets a number of commonly used attributes


if hasattr(self, "<STR_LIT>"):<EOL><INDENT>return None<EOL><DEDENT>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>self.set_mongonaut_base()<EOL><DEDENT>for mongoadmin in self.get_mongoadmins():<EOL><INDENT>for model in mongoadmin['<STR_LIT>'].models:<EOL><INDENT>if model.name == self.document_name:<EOL><INDENT>self.mongoadmin = model.mongoadmin<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>raise NoMongoAdminSpecified("<STR_LIT>".format(self.app_label, self.document_name))<EOL><DEDENT>

Returns the MongoAdmin object for an app_label/document_name style view


context['<STR_LIT>'] = self.mongoadmin.has_view_permission(self.request)<EOL>context['<STR_LIT>'] = self.mongoadmin.has_edit_permission(self.request)<EOL>context['<STR_LIT>'] = self.mongoadmin.has_add_permission(self.request)<EOL>context['<STR_LIT>'] = self.mongoadmin.has_delete_permission(self.request)<EOL>return context<EOL>

Provides permissions for mongoadmin for use in the context


<EOL>if not hasattr(self, '<STR_LIT>') or self.document is None:<EOL><INDENT>self.document = self.document_type()<EOL><DEDENT>self.form = MongoModelForm(model=self.document_type, instance=self.document,<EOL>form_post_data=self.request.POST).get_form()<EOL>self.form.is_bound = True<EOL>if self.form.is_valid():<EOL><INDENT>self.document_map_dict = MongoModelForm(model=self.document_type).create_document_dictionary(self.document_type)<EOL>self.new_document = self.document_type<EOL>self.embedded_list_docs = {}<EOL>if self.new_document is None:<EOL><INDENT>messages.error(self.request, u"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.new_document = self.new_document()<EOL>for form_key in self.form.cleaned_data.keys():<EOL><INDENT>if form_key == '<STR_LIT:id>' and hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.new_document.id = self.document.id<EOL>continue<EOL><DEDENT>self.process_document(self.new_document, form_key, None)<EOL><DEDENT>self.new_document.save()<EOL>if success_message:<EOL><INDENT>messages.success(self.request, success_message)<EOL><DEDENT><DEDENT><DEDENT>return self.form<EOL>

As long as the form is set on the view this method will validate the form
and save the submitted data.  Only call this if you are posting data.
The given success_message will be used with the djanog messages framework
if the posted data sucessfully submits.


def process_document(self, document, form_key, passed_key):

if passed_key is not None:<EOL><INDENT>current_key, remaining_key_array = trim_field_key(document, passed_key)<EOL><DEDENT>else:<EOL><INDENT>current_key, remaining_key_array = trim_field_key(document, form_key)<EOL><DEDENT>key_array_digit = remaining_key_array[-<NUM_LIT:1>] if remaining_key_array and has_digit(remaining_key_array) else None<EOL>remaining_key = make_key(remaining_key_array)<EOL>if current_key.lower() == '<STR_LIT:id>':<EOL><INDENT>raise KeyError(u"<STR_LIT>")<EOL><DEDENT>is_embedded_doc = (isinstance(document._fields.get(current_key, None), EmbeddedDocumentField)<EOL>if hasattr(document, '<STR_LIT>') else False)<EOL>is_list = not key_array_digit is None<EOL>key_in_fields = current_key in document._fields.keys() if hasattr(document, '<STR_LIT>') else False<EOL>if key_in_fields:<EOL><INDENT>if is_embedded_doc:<EOL><INDENT>self.set_embedded_doc(document, form_key, current_key, remaining_key)<EOL><DEDENT>elif is_list:<EOL><INDENT>self.set_list_field(document, form_key, current_key, remaining_key, key_array_digit)<EOL><DEDENT>else:<EOL><INDENT>value = translate_value(document._fields[current_key],<EOL>self.form.cleaned_data[form_key])<EOL>setattr(document, current_key, value)<EOL><DEDENT><DEDENT>

Given the form_key will evaluate the document and set values correctly for
the document given.


def set_embedded_doc(self, document, form_key, current_key, remaining_key):

embedded_doc = getattr(document, current_key, False)<EOL>if not embedded_doc:<EOL><INDENT>embedded_doc = document._fields[current_key].document_type_obj()<EOL><DEDENT>new_key, new_remaining_key_array = trim_field_key(embedded_doc, remaining_key)<EOL>self.process_document(embedded_doc, form_key, make_key(new_key, new_remaining_key_array))<EOL>setattr(document, current_key, embedded_doc)<EOL>

Get the existing embedded document if it exists, else created it.


def set_list_field(self, document, form_key, current_key, remaining_key, key_array_digit):

document_field = document._fields.get(current_key)<EOL>list_value = translate_value(document_field.field, self.form.cleaned_data[form_key])<EOL>if list_value is None or (not list_value and not bool(list_value)):<EOL><INDENT>return None<EOL><DEDENT>current_list = getattr(document, current_key, None)<EOL>if isinstance(document_field.field, EmbeddedDocumentField):<EOL><INDENT>embedded_list_key = u"<STR_LIT>".format(current_key, key_array_digit)<EOL>embedded_list_document = self.embedded_list_docs.get(embedded_list_key, None)<EOL>if embedded_list_document is None:<EOL><INDENT>embedded_list_document = document_field.field.document_type_obj()<EOL><DEDENT>new_key, new_remaining_key_array = trim_field_key(embedded_list_document, remaining_key)<EOL>self.process_document(embedded_list_document, form_key, new_key)<EOL>list_value = embedded_list_document<EOL>self.embedded_list_docs[embedded_list_key] = embedded_list_document<EOL>if isinstance(current_list, list):<EOL><INDENT>if embedded_list_document not in current_list:<EOL><INDENT>current_list.append(embedded_list_document)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>setattr(document, current_key, [embedded_list_document])<EOL><DEDENT><DEDENT>elif isinstance(current_list, list):<EOL><INDENT>current_list.append(list_value)<EOL><DEDENT>else:<EOL><INDENT>setattr(document, current_key, [list_value])<EOL><DEDENT>

1. Figures out what value the list ought to have
           2. Sets the list


value = getattr(document, key)<EOL>if isinstance(value, ObjectId):<EOL><INDENT>return value<EOL><DEDENT>if isinstance(document._fields.get(key), URLField):<EOL><INDENT>return mark_safe("""<STR_LIT>""".format(value, value))<EOL><DEDENT>if isinstance(value, Document):<EOL><INDENT>app_label = value.__module__.replace("<STR_LIT>", "<STR_LIT>")<EOL>document_name = value._class_name<EOL>url = reverse(<EOL>"<STR_LIT>",<EOL>kwargs={'<STR_LIT>': app_label, '<STR_LIT>': document_name,<EOL>'<STR_LIT:id>': value.id})<EOL>return mark_safe("""<STR_LIT>""".format(url, value))<EOL><DEDENT>return value<EOL>

Returns the display value of a field for a particular MongoDB document.


if self.mongoadmin.search_fields and q:<EOL><INDENT>params = {}<EOL>for field in self.mongoadmin.search_fields:<EOL><INDENT>if field == '<STR_LIT:id>':<EOL><INDENT>if is_valid_object_id(q):<EOL><INDENT>return queryset.filter(pk=q)<EOL><DEDENT>continue<EOL><DEDENT>search_key = "<STR_LIT>".format(field=field)<EOL>params[search_key] = q<EOL><DEDENT>queryset = queryset.filter(**params)<EOL><DEDENT>return queryset<EOL>

Performs filtering against the default queryset returned by
            mongoengine.


if hasattr(self, "<STR_LIT>") and self.queryset:<EOL><INDENT>return self.queryset<EOL><DEDENT>self.set_mongonaut_base()<EOL>self.set_mongoadmin()<EOL>self.document = getattr(self.models, self.document_name)<EOL>queryset = self.document.objects.all()<EOL>if self.mongoadmin.ordering:<EOL><INDENT>queryset = queryset.order_by(*self.mongoadmin.ordering)<EOL><DEDENT>q = self.request.GET.get('<STR_LIT:q>')<EOL>queryset = self.get_qset(queryset, q)<EOL>try:<EOL><INDENT>self.page = int(self.request.GET.get('<STR_LIT>', '<STR_LIT:1>'))<EOL><DEDENT>except ValueError:<EOL><INDENT>self.page = <NUM_LIT:1><EOL><DEDENT>obj_count = queryset.count()<EOL>self.total_pages = math.ceil(obj_count / self.documents_per_page)<EOL>if self.page > self.total_pages:<EOL><INDENT>self.page = self.total_pages<EOL><DEDENT>if self.page < <NUM_LIT:1>:<EOL><INDENT>self.page = <NUM_LIT:1><EOL><DEDENT>start = (self.page - <NUM_LIT:1>) * self.documents_per_page<EOL>end = self.page * self.documents_per_page<EOL>queryset = queryset[start:end] if obj_count else queryset<EOL>self.queryset = queryset<EOL>return queryset<EOL>

Replicates Django CBV `get_queryset()` method, but for MongoEngine.


self.query = self.get_queryset()<EOL>mongo_ids = {'<STR_LIT>': [str(x.id) for x in self.query]}<EOL>return mongo_ids<EOL>


context = super(DocumentListView, self).get_context_data(**kwargs)<EOL>context = self.set_permissions_in_context(context)<EOL>if not context['<STR_LIT>']:<EOL><INDENT>return HttpResponseForbidden("<STR_LIT>")<EOL><DEDENT>context['<STR_LIT>'] = self.get_queryset()<EOL>context['<STR_LIT>'] = self.document<EOL>context['<STR_LIT>'] = self.app_label<EOL>context['<STR_LIT>'] = self.document_name<EOL>context['<STR_LIT>'] = self.request<EOL>context['<STR_LIT>'] = self.page<EOL>context['<STR_LIT>'] = self.documents_per_page<EOL>if self.page > <NUM_LIT:1>:<EOL><INDENT>previous_page_number = self.page - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>previous_page_number = None<EOL><DEDENT>if self.page < self.total_pages:<EOL><INDENT>next_page_number = self.page + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>next_page_number = None<EOL><DEDENT>context['<STR_LIT>'] = previous_page_number<EOL>context['<STR_LIT>'] = previous_page_number is not None<EOL>context['<STR_LIT>'] = next_page_number<EOL>context['<STR_LIT>'] = next_page_number is not None<EOL>context['<STR_LIT>'] = self.total_pages<EOL>if self.queryset.count():<EOL><INDENT>context['<STR_LIT>'] = ['<STR_LIT:id>', ]<EOL>for key in [x for x in self.mongoadmin.list_fields if x != '<STR_LIT:id>' and x in self.document._fields.keys()]:<EOL><INDENT>if isinstance(self.document._fields[key], EmbeddedDocumentField):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(self.document._fields[key], ListField):<EOL><INDENT>continue<EOL><DEDENT>context['<STR_LIT>'].append(key)<EOL><DEDENT><DEDENT>if self.mongoadmin.search_fields:<EOL><INDENT>context['<STR_LIT>'] = True<EOL><DEDENT>return context<EOL>

Injects data into the context to replicate CBV ListView.


def post(self, request, *args, **kwargs):

<EOL>form_class = self.get_form_class()<EOL>form = self.get_form(form_class)<EOL>mongo_ids = self.get_initial()['<STR_LIT>']<EOL>for form_mongo_id in form.data.getlist('<STR_LIT>'):<EOL><INDENT>for mongo_id in mongo_ids:<EOL><INDENT>if form_mongo_id == mongo_id:<EOL><INDENT>self.document.objects.get(pk=mongo_id).delete()<EOL><DEDENT><DEDENT><DEDENT>return self.form_invalid(form)<EOL>


context = super(DocumentAddFormView, self).get_context_data(**kwargs)<EOL>self.set_mongoadmin()<EOL>context = self.set_permissions_in_context(context)<EOL>self.document_type = getattr(self.models, self.document_name)<EOL>context['<STR_LIT>'] = self.app_label<EOL>context['<STR_LIT>'] = self.document_name<EOL>context['<STR_LIT>'] = reverse('<STR_LIT>', args=[self.kwargs.get('<STR_LIT>'),<EOL>self.kwargs.get('<STR_LIT>')])<EOL>return context<EOL>

TODO - possibly inherit this from DocumentEditFormView. This is same thing minus:
            self.ident = self.kwargs.get('id')
            self.document = self.document_type.objects.get(pk=self.ident)


return request.user.is_authenticated and request.user.is_active<EOL>

Returns True if the given HttpRequest has permission to view
*at least one* page in the mongonaut site.


return request.user.is_authenticated and request.user.is_active and request.user.is_staff<EOL>


return request.user.is_authenticated and request.user.is_active and request.user.is_superuser<EOL>


if isinstance(string_or_list, (tuple, list)):<EOL><INDENT>list_length = len(string_or_list)<EOL>if list_length:<EOL><INDENT>return six.text_type(string_or_list[-<NUM_LIT:1>]).isdigit()<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return has_digit(string_or_list.split(sep))<EOL><DEDENT>

Given a string or a list will return true if the last word or
element is a digit.  sep is used when a string is given to know
what separates one word from another.


sep = kwargs.get('<STR_LIT>', u"<STR_LIT:_>")<EOL>exclude_last_string = kwargs.get('<STR_LIT>', False)<EOL>string_array = []<EOL>for arg in args:<EOL><INDENT>if isinstance(arg, list):<EOL><INDENT>string_array.append(six.text_type(sep.join(arg)))<EOL><DEDENT>else:<EOL><INDENT>if exclude_last_string:<EOL><INDENT>new_key_array = arg.split(sep)[:-<NUM_LIT:1>]<EOL>if len(new_key_array) > <NUM_LIT:0>:<EOL><INDENT>string_array.append(make_key(new_key_array))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>string_array.append(six.text_type(arg))<EOL><DEDENT><DEDENT><DEDENT>return sep.join(string_array)<EOL>

Given any number of lists and strings will join them in order as one
string separated by the sep kwarg.  sep defaults to u"_".

Add exclude_last_string=True as a kwarg to exclude the last item in a
given string after being split by sep.  Note if you only have one word
in your string you can end up getting an empty string.

Example uses:

>>> from mongonaut.forms.form_utils import make_key
>>> make_key('hi', 'my', 'firend')
>>> u'hi_my_firend'

>>> make_key('hi', 'my', 'firend', sep='i')
>>> 'hiimyifirend'

>>> make_key('hi', 'my', 'firend',['this', 'be', 'what'], sep='i')
>>> 'hiimyifirendithisibeiwhat'

>>> make_key('hi', 'my', 'firend',['this', 'be', 'what'])
>>> u'hi_my_firend_this_be_what'


def __init__(self, form_post_data=None, *args, **kwargs):

kwargs.update({'<STR_LIT>': form_post_data})<EOL>super(MongoModelForm, self).__init__(*args, **kwargs)<EOL>

Overriding init so we can set the post vars like a normal form and generate
the form the same way Django does.


<EOL>if self.is_initialized:<EOL><INDENT>self.model_map_dict = self.create_document_dictionary(self.model_instance)<EOL><DEDENT>else:<EOL><INDENT>self.model_map_dict = self.create_document_dictionary(self.model)<EOL><DEDENT>form_field_dict = self.get_form_field_dict(self.model_map_dict)<EOL>self.set_form_fields(form_field_dict)<EOL>


self.form.data = self.post_data_dict<EOL>for field_key, field in self.form.fields.items():<EOL><INDENT>if has_digit(field_key):<EOL><INDENT>base_key = make_key(field_key, exclude_last_string=True)<EOL>for key in self.post_data_dict.keys():<EOL><INDENT>if base_key in key:<EOL><INDENT>self.form.fields.update({key: field})<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Need to set form data so that validation on all post data occurs and
places newly entered form data on the form object.


self.set_fields()<EOL>if self.post_data_dict is not None:<EOL><INDENT>self.set_post_data()<EOL><DEDENT>return self.form<EOL>


def create_doc_dict(self, document, doc_key=None, owner_document=None):

<EOL>if owner_document:<EOL><INDENT>doc_field = owner_document._fields.get(doc_key, None) if doc_key else None<EOL><DEDENT>else:<EOL><INDENT>doc_field = document._fields.get(doc_key, None) if doc_key else None<EOL><DEDENT>doc_dict = {"<STR_LIT>": document if owner_document is None else owner_document,<EOL>"<STR_LIT>": document.__class__.__name__.lower() if doc_key is None else doc_key,<EOL>"<STR_LIT>": doc_field}<EOL>if not isinstance(document, TopLevelDocumentMetaclass) and doc_key:<EOL><INDENT>doc_dict.update({"<STR_LIT>": EmbeddedDocumentField})<EOL><DEDENT>for key, field in document._fields.items():<EOL><INDENT>doc_dict[key] = field<EOL><DEDENT>return doc_dict<EOL>

Generate a dictionary representation of the document.  (no recursion)

DO NOT CALL DIRECTLY


def create_list_dict(self, document, list_field, doc_key):

list_dict = {"<STR_LIT>": document}<EOL>if isinstance(list_field.field, EmbeddedDocumentField):<EOL><INDENT>list_dict.update(self.create_document_dictionary(document=list_field.field.document_type_obj,<EOL>owner_document=document))<EOL><DEDENT>list_dict.update({"<STR_LIT>": list_field.field,<EOL>"<STR_LIT>": doc_key,<EOL>"<STR_LIT>": ListField,<EOL>"<STR_LIT>": get_widget(list_field.field),<EOL>"<STR_LIT>": getattr(document, doc_key, None)})<EOL>return list_dict<EOL>

Genereates a dictionary representation of the list field. Document
should be the document the list_field comes from.

DO NOT CALL DIRECTLY


def create_document_dictionary(self, document, document_key=None,<EOL>owner_document=None):

doc_dict = self.create_doc_dict(document, document_key, owner_document)<EOL>for doc_key, doc_field in doc_dict.items():<EOL><INDENT>if doc_key.startswith("<STR_LIT:_>"):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(doc_field, ListField):<EOL><INDENT>doc_dict[doc_key] = self.create_list_dict(document, doc_field, doc_key)<EOL><DEDENT>elif isinstance(doc_field, EmbeddedDocumentField):<EOL><INDENT>doc_dict[doc_key] = self.create_document_dictionary(doc_dict[doc_key].document_type_obj,<EOL>doc_key)<EOL><DEDENT>else:<EOL><INDENT>doc_dict[doc_key] = {"<STR_LIT>": document,<EOL>"<STR_LIT>": doc_key,<EOL>"<STR_LIT>": doc_field,<EOL>"<STR_LIT>": get_widget(doc_dict[doc_key], getattr(doc_field, '<STR_LIT>', False))}<EOL><DEDENT><DEDENT>return doc_dict<EOL>

Given document generates a dictionary representation of the document.
Includes the widget for each for each field in the document.


attrs = get_attrs(model_field, disabled)<EOL>if hasattr(model_field, "<STR_LIT:max_length>") and not model_field.max_length:<EOL><INDENT>return forms.Textarea(attrs=attrs)<EOL><DEDENT>elif isinstance(model_field, DateTimeField):<EOL><INDENT>return forms.DateTimeInput(attrs=attrs)<EOL><DEDENT>elif isinstance(model_field, BooleanField):<EOL><INDENT>return forms.CheckboxInput(attrs=attrs)<EOL><DEDENT>elif isinstance(model_field, ReferenceField) or model_field.choices:<EOL><INDENT>return forms.Select(attrs=attrs)<EOL><DEDENT>elif (isinstance(model_field, ListField) or<EOL>isinstance(model_field, EmbeddedDocumentField) or<EOL>isinstance(model_field, GeoPointField)):<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return forms.TextInput(attrs=attrs)<EOL><DEDENT>

Choose which widget to display for a field.


attrs = {}<EOL>attrs['<STR_LIT:class>'] = '<STR_LIT>'<EOL>if disabled or isinstance(model_field, ObjectIdField):<EOL><INDENT>attrs['<STR_LIT:class>'] += '<STR_LIT>'<EOL>attrs['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>return attrs<EOL>


FIELD_MAPPING = {<EOL>IntField: forms.IntegerField,<EOL>StringField: forms.CharField,<EOL>FloatField: forms.FloatField,<EOL>BooleanField: forms.BooleanField,<EOL>DateTimeField: forms.DateTimeField,<EOL>DecimalField: forms.DecimalField,<EOL>URLField: forms.URLField,<EOL>EmailField: forms.EmailField<EOL>}<EOL>return FIELD_MAPPING.get(model_field.__class__, forms.CharField)<EOL>

Gets the default form field  for a mongoenigne field.


try:<EOL><INDENT>return document.__unicode__()<EOL><DEDENT>except AttributeError:<EOL><INDENT>return six.text_type(document)<EOL><DEDENT>

Safely converts MongoDB document strings to unicode.


def __init__(self, model, instance=None, form_post_data=None):

self.model = model<EOL>self.model_instance = instance<EOL>self.post_data_dict = form_post_data<EOL>self.is_initialized = False if instance is None else True<EOL>self.form = forms.Form()<EOL>if not isinstance(self.model, TopLevelDocumentMetaclass):<EOL><INDENT>raise TypeError(u"<STR_LIT>")<EOL><DEDENT>if self.is_initialized and not isinstance(self.model_instance, self.model):<EOL><INDENT>raise TypeError(u"<STR_LIT>")<EOL><DEDENT>if self.post_data_dict is not None and not isinstance(self.post_data_dict, dict):<EOL><INDENT>raise TypeError(u"<STR_LIT>")<EOL><DEDENT>

Params:
    model          -- The model class to create the form with
    instance       -- An instance of the model class can be used to
                      initialize data.
    form_post_data -- Values given by request.POST


return_dict = OrderedDict()<EOL>if hasattr(self.model, '<STR_LIT:Meta>') and hasattr(self.model.Meta, '<STR_LIT>'):<EOL><INDENT>field_order_list = tuple(form_field for form_field<EOL>in self.model.Meta.form_fields_ordering<EOL>if form_field in model_dict.iterkeys())<EOL>order_dict = OrderedDict.fromkeys(field_order_list)<EOL>return_dict = order_dict<EOL><DEDENT>for field_key, field_dict in sorted(model_dict.items()):<EOL><INDENT>if not field_key.startswith("<STR_LIT:_>"):<EOL><INDENT>widget = field_dict.get('<STR_LIT>', None)<EOL>if widget is None:<EOL><INDENT>return_dict[field_key] = self.get_form_field_dict(field_dict)<EOL>return_dict[field_key].update({'<STR_LIT>': field_dict.get('<STR_LIT>', None)})<EOL><DEDENT>else:<EOL><INDENT>return_dict[field_key] = FieldTuple(widget,<EOL>field_dict.get('<STR_LIT>', None),<EOL>field_dict.get('<STR_LIT>', None),<EOL>field_dict.get('<STR_LIT>', None))<EOL><DEDENT><DEDENT><DEDENT>return return_dict<EOL>

Takes a model dictionary representation and creates a dictionary
keyed by form field.  Each value is a  keyed 4 tuple of:
(widget, mode_field_instance, model_field_type, field_key)


def set_form_fields(self, form_field_dict, parent_key=None, field_type=None):

for form_key, field_value in form_field_dict.items():<EOL><INDENT>form_key = make_key(parent_key, form_key) if parent_key is not None else form_key<EOL>if isinstance(field_value, tuple):<EOL><INDENT>set_list_class = False<EOL>base_key = form_key<EOL>if ListField in (field_value.field_type, field_type):<EOL><INDENT>if parent_key is None or ListField == field_value.field_type:<EOL><INDENT>if field_type != EmbeddedDocumentField:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(form_key)<EOL><DEDENT>set_list_class = True<EOL><DEDENT>else:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'<EOL><DEDENT>list_keys = [field_key for field_key in self.form.fields.keys()<EOL>if has_digit(field_key)]<EOL>key_int = <NUM_LIT:0><EOL>while form_key in list_keys:<EOL><INDENT>key_int += <NUM_LIT:1><EOL><DEDENT>form_key = make_key(form_key, key_int)<EOL><DEDENT>if parent_key is not None:<EOL><INDENT>valid_base_keys = [model_key for model_key in self.model_map_dict.keys()<EOL>if not model_key.startswith("<STR_LIT:_>")]<EOL>while base_key not in valid_base_keys and base_key:<EOL><INDENT>base_key = make_key(base_key, exclude_last_string=True)<EOL><DEDENT>embedded_key_class = None<EOL>if set_list_class:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += "<STR_LIT>".format(base_key)<EOL>embedded_key_class = make_key(field_key, exclude_last_string=True)<EOL><DEDENT>field_value.widget.attrs['<STR_LIT:class>'] += "<STR_LIT>"<EOL>if base_key == parent_key:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(base_key)<EOL><DEDENT>else:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(base_key, parent_key)<EOL><DEDENT>if embedded_key_class is not None:<EOL><INDENT>field_value.widget.attrs['<STR_LIT:class>'] += '<STR_LIT>'.format(embedded_key_class)<EOL><DEDENT><DEDENT>default_value = self.get_field_value(form_key)<EOL>if isinstance(default_value, list) and len(default_value) > <NUM_LIT:0>:<EOL><INDENT>key_index = int(form_key.split("<STR_LIT:_>")[-<NUM_LIT:1>])<EOL>new_base_key = make_key(form_key, exclude_last_string=True)<EOL>for list_value in default_value:<EOL><INDENT>list_widget = deepcopy(field_value.widget)<EOL>new_key = make_key(new_base_key, six.text_type(key_index))<EOL>list_widget.attrs['<STR_LIT:class>'] += "<STR_LIT>".format(make_key(base_key, key_index))<EOL>self.set_form_field(list_widget, field_value.document_field, new_key, list_value)<EOL>key_index += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.set_form_field(field_value.widget, field_value.document_field,<EOL>form_key, default_value)<EOL><DEDENT><DEDENT>elif isinstance(field_value, dict):<EOL><INDENT>self.set_form_fields(field_value, form_key, field_value.get("<STR_LIT>", None))<EOL><DEDENT><DEDENT>

Set the form fields for every key in the form_field_dict.

Params:
  form_field_dict -- a dictionary created by get_form_field_dict
  parent_key -- the key for the previous key in the recursive call
  field_type -- used to determine what kind of field we are setting


def set_form_field(self, widget, model_field, field_key, default_value):

<EOL>if default_value == []:<EOL><INDENT>default_value = None<EOL><DEDENT>if widget and isinstance(widget, forms.widgets.Select):<EOL><INDENT>self.form.fields[field_key] = forms.ChoiceField(label=model_field.name,<EOL>required=model_field.required,<EOL>widget=widget)<EOL><DEDENT>else:<EOL><INDENT>field_class = get_form_field_class(model_field)<EOL>self.form.fields[field_key] = field_class(label=model_field.name,<EOL>required=model_field.required,<EOL>widget=widget)<EOL><DEDENT>if default_value is not None:<EOL><INDENT>if isinstance(default_value, Document):<EOL><INDENT>self.form.fields[field_key].initial = getattr(default_value, '<STR_LIT:id>', None)<EOL><DEDENT>else:<EOL><INDENT>self.form.fields[field_key].initial = default_value<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.form.fields[field_key].initial = getattr(model_field, '<STR_LIT:default>', None)<EOL><DEDENT>if isinstance(model_field, ReferenceField):<EOL><INDENT>self.form.fields[field_key].choices = [(six.text_type(x.id), get_document_unicode(x))<EOL>for x in model_field.document_type.objects.all()]<EOL>self.form.fields[field_key].choices.insert(<NUM_LIT:0>, ("<STR_LIT>", "<STR_LIT>"))<EOL><DEDENT>elif model_field.choices:<EOL><INDENT>self.form.fields[field_key].choices = model_field.choices<EOL><DEDENT>for key, form_attr in CHECK_ATTRS.items():<EOL><INDENT>if hasattr(model_field, key):<EOL><INDENT>value = getattr(model_field, key)<EOL>setattr(self.form.fields[field_key], key, value)<EOL><DEDENT><DEDENT>

Parmams:
    widget -- the widget to use for displyaing the model_field
    model_field -- the field on the model to create a form field with
    field_key -- the name for the field on the form
    default_value -- the value to give for the field
                     Default: None


def get_value(document, field_key):<EOL><INDENT>if document is None:<EOL><INDENT>return None<EOL><DEDENT>current_key, new_key_array = trim_field_key(document, field_key)<EOL>key_array_digit = int(new_key_array[-<NUM_LIT:1>]) if new_key_array and has_digit(new_key_array) else None<EOL>new_key = make_key(new_key_array)<EOL>if key_array_digit is not None and len(new_key_array) > <NUM_LIT:0>:<EOL><INDENT>if len(new_key_array) == <NUM_LIT:1>:<EOL><INDENT>return_data = document._data.get(current_key, [])<EOL><DEDENT>elif isinstance(document, BaseList):<EOL><INDENT>return_list = []<EOL>if len(document) > <NUM_LIT:0>:<EOL><INDENT>return_list = [get_value(doc, new_key) for doc in document]<EOL><DEDENT>return_data = return_list<EOL><DEDENT>else:<EOL><INDENT>return_data = get_value(getattr(document, current_key), new_key)<EOL><DEDENT><DEDENT>elif len(new_key_array) > <NUM_LIT:0>:<EOL><INDENT>return_data = get_value(document._data.get(current_key), new_key)<EOL><DEDENT>else:<EOL><INDENT>try: <EOL><INDENT>return_data = (document._data.get(None, None) if current_key == "<STR_LIT:id>" else<EOL>document._data.get(current_key, None))<EOL><DEDENT>except: <EOL><INDENT>return_data = document._data.get(current_key, None)<EOL><DEDENT><DEDENT>return return_data<EOL><DEDENT>if self.is_initialized:<EOL><INDENT>return get_value(self.model_instance, field_key)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Given field_key will return value held at self.model_instance.  If
model_instance has not been provided will return None.


try:<EOL><INDENT>OBJECT_ID.validate(value)<EOL>return True<EOL><DEDENT>except ValidationError:<EOL><INDENT>return False<EOL><DEDENT>

Validates BSON IDs using mongoengine's ObjectIdField field.


value = form_value<EOL>if isinstance(document_field, ReferenceField):<EOL><INDENT>value = document_field.document_type.objects.get(id=form_value) if form_value else None<EOL><DEDENT>return value<EOL>

Given a document_field and a form_value this will translate the value
to the correct result for mongo to use.


trimming = True<EOL>left_over_key_values = []<EOL>current_key = field_key<EOL>while trimming and current_key:<EOL><INDENT>if hasattr(document, current_key):<EOL><INDENT>trimming = False<EOL><DEDENT>else:<EOL><INDENT>key_array = current_key.split("<STR_LIT:_>")<EOL>left_over_key_values.append(key_array.pop())<EOL>current_key = u"<STR_LIT:_>".join(key_array)<EOL><DEDENT><DEDENT>left_over_key_values.reverse()<EOL>return current_key, left_over_key_values<EOL>

Returns the smallest delimited version of field_key that
is an attribute on document.

return (key, left_over_array)