user_id = UserID(chat_id=entity.id.chat_id,<EOL>gaia_id=entity.id.gaia_id)<EOL>return User(user_id, entity.properties.display_name,<EOL>entity.properties.first_name,<EOL>entity.properties.photo_url,<EOL>entity.properties.email,<EOL>(self_user_id == user_id) or (self_user_id is None))<EOL>

Construct user from ``Entity`` message.

        Args:
            entity: ``Entity`` message.
            self_user_id (~hangups.user.UserID or None): The ID of the current
                user. If ``None``, assume ``entity`` is the current user.

        Returns:
            :class:`~hangups.user.User` object.


return _replace_words(HANGOUTS_EMOTICONS_TO_EMOJI, string)<EOL>

Replace emoticon words in string with corresponding emoji.


output_lines = []<EOL>for line in string.split('<STR_LIT:\n>'):<EOL><INDENT>output_words = []<EOL>for word in line.split('<STR_LIT:U+0020>'):<EOL><INDENT>new_word = replacements.get(word, word)<EOL>output_words.append(new_word)<EOL><DEDENT>output_lines.append(output_words)<EOL><DEDENT>return '<STR_LIT:\n>'.join('<STR_LIT:U+0020>'.join(output_words) for output_words in output_lines)<EOL>

Replace words with corresponding values in replacements dict.

    Words must be separated by spaces or newlines.


try:<EOL><INDENT>conv_events = await self._conversation.get_events(<EOL>self._conversation.events[<NUM_LIT:0>].id_<EOL>)<EOL><DEDENT>except (IndexError, hangups.NetworkError):<EOL><INDENT>conv_events = []<EOL><DEDENT>if not conv_events:<EOL><INDENT>self._first_loaded = True<EOL><DEDENT>if self._focus_position == self.POSITION_LOADING and conv_events:<EOL><INDENT>self.set_focus(conv_events[-<NUM_LIT:1>].id_)<EOL><DEDENT>else:<EOL><INDENT>self._modified()<EOL><DEDENT>self._refresh_watermarked_events()<EOL>self._is_loading = False<EOL>


<EOL>if not text:<EOL><INDENT>return<EOL><DEDENT>elif text.startswith('<STR_LIT>') and len(text.split('<STR_LIT:U+0020>')) == <NUM_LIT:2>:<EOL><INDENT>filename = text.split('<STR_LIT:U+0020>')[<NUM_LIT:1>]<EOL>image_file = open(filename, '<STR_LIT:rb>')<EOL>text = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>image_file = None<EOL><DEDENT>text = replace_emoticons(text)<EOL>segments = hangups.ChatMessageSegment.from_str(text)<EOL>self._coroutine_queue.put(<EOL>self._handle_send_message(<EOL>self._conversation.send_message(<EOL>segments, image_file=image_file<EOL>)<EOL>)<EOL>)<EOL>

Called when the user presses return on the send message widget.


if conv_id not in self._conv_widgets:<EOL><INDENT>set_title_cb = (lambda widget, title:<EOL>self._tabbed_window.set_tab(widget, title=title))<EOL>widget = ConversationWidget(<EOL>self._client, self._coroutine_queue,<EOL>self._conv_list.get(conv_id), set_title_cb, self._keys,<EOL>self._datetimefmt<EOL>)<EOL>self._conv_widgets[conv_id] = widget<EOL><DEDENT>return self._conv_widgets[conv_id]<EOL>

Return an existing or new ConversationWidget.


return get_conv_name(self._conversation, show_unread=True)<EOL>

Return the button's label generated from the conversation.


return ConversationMenu(<EOL>self._coroutine_queue, self._conversation, close_callback,<EOL>self._keys<EOL>)<EOL>

Return the menu widget associated with this widget.


self._button.set_label(self._get_label())<EOL>

Update the button's label when an event occurs.


<EOL>return super().keypress(size, key)<EOL>


self._user_list, self._conv_list = (<EOL>await hangups.build_user_conversation_list(self._client)<EOL>)<EOL>self._conv_list.on_event.add_observer(self._on_event)<EOL>conv_picker = ConversationPickerWidget(self._conv_list,<EOL>self.on_select_conversation,<EOL>self._keys)<EOL>self._tabbed_window = TabbedWindowWidget(self._keys)<EOL>self._tabbed_window.set_tab(conv_picker, switch=True,<EOL>title='<STR_LIT>')<EOL>self._urwid_loop.widget = self._tabbed_window<EOL>


<EOL>assert asyncio.iscoroutine(coro)<EOL>self._queue.put_nowait(coro)<EOL>

Put a coroutine in the queue to be executed.


return self._get_position(position, prev=True)<EOL>

Return the position above position or raise IndexError.


self._typing_statuses[typing_message.user_id] = typing_message.status<EOL>self._update()<EOL>


self._is_connected = True<EOL>self._update()<EOL>

Hide reconnecting message when reconnected.


directory = os.path.dirname(path)<EOL>if directory != '<STR_LIT>' and not os.path.isdir(directory):<EOL><INDENT>try:<EOL><INDENT>os.makedirs(directory)<EOL><DEDENT>except OSError as e:<EOL><INDENT>sys.exit('<STR_LIT>'.format(e))<EOL><DEDENT><DEDENT>

Create a directory if it does not exist.


return self._get_position(position)<EOL>

Return the position below position or raise IndexError.


self._urwid_loop.widget = self._tabbed_window<EOL>


<EOL>current_widget = self._tabbed_window.get_current_widget()<EOL>if hasattr(current_widget, '<STR_LIT>'):<EOL><INDENT>menu_widget = current_widget.get_menu_widget(self._hide_menu)<EOL>overlay = urwid.Overlay(menu_widget, self._tabbed_window,<EOL>align='<STR_LIT>', width=('<STR_LIT>', <NUM_LIT>),<EOL>valign='<STR_LIT>', height=('<STR_LIT>', <NUM_LIT>))<EOL>self._urwid_loop.widget = overlay<EOL><DEDENT>


self._is_connected = False<EOL>self._update()<EOL>

Show reconnecting message when disconnected.


sys.stdout.write('<STR_LIT>')<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>sys.stdout.write('<STR_LIT>')<EOL><DEDENT>

Context manager for enabling/disabling bracketed paste mode.


self._message = None<EOL>self._message_handle = None<EOL>self._update()<EOL>


typing_users = [self._conversation.get_user(user_id)<EOL>for user_id, status in self._typing_statuses.items()<EOL>if status == hangups.TYPING_TYPE_STARTED]<EOL>displayed_names = [user.first_name for user in typing_users<EOL>if not user.is_self]<EOL>if displayed_names:<EOL><INDENT>typing_message = '<STR_LIT>'.format(<EOL>'<STR_LIT:U+002CU+0020>'.join(sorted(displayed_names)),<EOL>'<STR_LIT>' if len(displayed_names) == <NUM_LIT:1> else '<STR_LIT>'<EOL>)<EOL><DEDENT>else:<EOL><INDENT>typing_message = '<STR_LIT>'<EOL><DEDENT>if not self._is_connected:<EOL><INDENT>self._widget.set_text("<STR_LIT>")<EOL><DEDENT>elif self._message is not None:<EOL><INDENT>self._widget.set_text(self._message)<EOL><DEDENT>else:<EOL><INDENT>self._widget.set_text(typing_message)<EOL><DEDENT>


@staticmethod<EOL><INDENT>def _get_date_str(timestamp, datetimefmt, show_date=False):<DEDENT>

fmt = '<STR_LIT>'<EOL>if show_date:<EOL><INDENT>fmt += '<STR_LIT:\n>'+datetimefmt.get('<STR_LIT:date>', '<STR_LIT>')+'<STR_LIT:\n>'<EOL><DEDENT>fmt += datetimefmt.get('<STR_LIT:time>', '<STR_LIT>')<EOL>return timestamp.astimezone(tz=None).strftime(fmt)<EOL>

Convert UTC datetime into user interface string.


while True:<EOL><INDENT>coro = await self._queue.get()<EOL>assert asyncio.iscoroutine(coro)<EOL>await coro<EOL><DEDENT>

Consume coroutines from the queue by executing them.


def _exception_handler(self, _loop, context):

<EOL>self._coroutine_queue.put(self._client.disconnect())<EOL>default_exception = Exception(context.get('<STR_LIT:message>'))<EOL>self._exception = context.get('<STR_LIT>', default_exception)<EOL>

Handle exceptions from the asyncio loop.


@staticmethod<EOL><INDENT>def from_conversation_event(conversation, conv_event, prev_conv_event,<EOL>datetimefmt, watermark_users=None):<DEDENT>

user = conversation.get_user(conv_event.user_id)<EOL>if prev_conv_event is not None:<EOL><INDENT>is_new_day = (conv_event.timestamp.astimezone(tz=None).date() !=<EOL>prev_conv_event.timestamp.astimezone(tz=None).date())<EOL><DEDENT>else:<EOL><INDENT>is_new_day = False<EOL><DEDENT>if isinstance(conv_event, hangups.ChatMessageEvent):<EOL><INDENT>return MessageWidget(conv_event.timestamp, conv_event.text,<EOL>datetimefmt, user, show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>elif isinstance(conv_event, hangups.RenameEvent):<EOL><INDENT>if conv_event.new_name == '<STR_LIT>':<EOL><INDENT>text = ('<STR_LIT>'<EOL>.format(user.first_name))<EOL><DEDENT>else:<EOL><INDENT>text = ('<STR_LIT>'<EOL>.format(user.first_name, conv_event.new_name))<EOL><DEDENT>return MessageWidget(conv_event.timestamp, text, datetimefmt,<EOL>show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>elif isinstance(conv_event, hangups.MembershipChangeEvent):<EOL><INDENT>event_users = [conversation.get_user(user_id) for user_id<EOL>in conv_event.participant_ids]<EOL>names = '<STR_LIT:U+002CU+0020>'.join([user.full_name for user in event_users])<EOL>if conv_event.type_ == hangups.MEMBERSHIP_CHANGE_TYPE_JOIN:<EOL><INDENT>text = ('<STR_LIT>'<EOL>.format(user.first_name, names))<EOL><DEDENT>else:  <EOL><INDENT>text = ('<STR_LIT>'.format(names))<EOL><DEDENT>return MessageWidget(conv_event.timestamp, text, datetimefmt,<EOL>show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>elif isinstance(conv_event, hangups.HangoutEvent):<EOL><INDENT>text = {<EOL>hangups.HANGOUT_EVENT_TYPE_START: (<EOL>'<STR_LIT>'<EOL>),<EOL>hangups.HANGOUT_EVENT_TYPE_END: (<EOL>'<STR_LIT>'<EOL>),<EOL>hangups.HANGOUT_EVENT_TYPE_ONGOING: (<EOL>'<STR_LIT>'<EOL>),<EOL>}.get(conv_event.event_type, '<STR_LIT>')<EOL>return MessageWidget(conv_event.timestamp, text, datetimefmt,<EOL>show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>elif isinstance(conv_event, hangups.GroupLinkSharingModificationEvent):<EOL><INDENT>status_on = hangups.GROUP_LINK_SHARING_STATUS_ON<EOL>status_text = ('<STR_LIT>' if conv_event.new_status == status_on<EOL>else '<STR_LIT>')<EOL>text = '<STR_LIT>'.format(user.first_name,<EOL>status_text)<EOL>return MessageWidget(conv_event.timestamp, text, datetimefmt,<EOL>show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>else:<EOL><INDENT>text = '<STR_LIT>'<EOL>return MessageWidget(conv_event.timestamp, text, datetimefmt,<EOL>show_date=is_new_day,<EOL>watermark_users=watermark_users)<EOL><DEDENT>

Return MessageWidget representing a ConversationEvent.

        Returns None if the ConversationEvent does not have a widget
        representation.


<EOL>self._coroutine_queue.put(self._client.set_active())<EOL>self._coroutine_queue.put(self._conversation.update_read_timestamp())<EOL>return super().keypress(size, key)<EOL>

Handle marking messages as read and keeping client active.


if isinstance(conv_event, hangups.ChatMessageEvent):<EOL><INDENT>self._typing_statuses[conv_event.user_id] = (<EOL>hangups.TYPING_TYPE_STOPPED<EOL>)<EOL>self._update()<EOL><DEDENT>

Make users stop typing when they send a message.


return (self[self._focus_position], self._focus_position)<EOL>


sys.stdout.write("<STR_LIT>".format(title))<EOL>

Use an xterm escape sequence to set the terminal title.


def add_conversation_tab(self, conv_id, switch=False):

conv_widget = self.get_conv_widget(conv_id)<EOL>self._tabbed_window.set_tab(conv_widget, switch=switch,<EOL>title=conv_widget.title)<EOL>

Add conversation tab if not present, and optionally switch to it.


if keys == [self._keys['<STR_LIT>']]:<EOL><INDENT>if self._urwid_loop.widget == self._tabbed_window:<EOL><INDENT>self._show_menu()<EOL><DEDENT>else:<EOL><INDENT>self._hide_menu()<EOL><DEDENT><DEDENT>elif keys == [self._keys['<STR_LIT>']]:<EOL><INDENT>self._coroutine_queue.put(self._client.disconnect())<EOL><DEDENT>else:<EOL><INDENT>return keys<EOL><DEDENT>


def add_color_to_scheme(scheme, name, foreground, background, palette_colors):

if foreground is None and background is None:<EOL><INDENT>return scheme<EOL><DEDENT>new_scheme = []<EOL>for item in scheme:<EOL><INDENT>if item[<NUM_LIT:0>] == name:<EOL><INDENT>if foreground is None:<EOL><INDENT>foreground = item[<NUM_LIT:1>]<EOL><DEDENT>if background is None:<EOL><INDENT>background = item[<NUM_LIT:2>]<EOL><DEDENT>if palette_colors > <NUM_LIT:16>:<EOL><INDENT>new_scheme.append((name, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', foreground, background))<EOL><DEDENT>else:<EOL><INDENT>new_scheme.append((name, foreground, background))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>new_scheme.append(item)<EOL><DEDENT><DEDENT>return new_scheme<EOL>

Add foreground and background colours to a color scheme


def get_conv_name(conv, truncate=False, show_unread=False):

num_unread = len([conv_event for conv_event in conv.unread_events if<EOL>isinstance(conv_event, hangups.ChatMessageEvent) and<EOL>not conv.get_user(conv_event.user_id).is_self])<EOL>if show_unread and num_unread > <NUM_LIT:0>:<EOL><INDENT>postfix = '<STR_LIT>'.format(num_unread)<EOL><DEDENT>else:<EOL><INDENT>postfix = '<STR_LIT>'<EOL><DEDENT>if conv.name is not None:<EOL><INDENT>return conv.name + postfix<EOL><DEDENT>else:<EOL><INDENT>participants = sorted(<EOL>(user for user in conv.users if not user.is_self),<EOL>key=lambda user: user.id_<EOL>)<EOL>names = [user.first_name for user in participants]<EOL>if not participants:<EOL><INDENT>return "<STR_LIT>" + postfix<EOL><DEDENT>if len(participants) == <NUM_LIT:1>:<EOL><INDENT>return participants[<NUM_LIT:0>].full_name + postfix<EOL><DEDENT>elif truncate and len(participants) > <NUM_LIT:2>:<EOL><INDENT>return ('<STR_LIT:U+002CU+0020>'.join(names[:<NUM_LIT:2>] + ['<STR_LIT>'.format(len(names) - <NUM_LIT:2>)]) +<EOL>postfix)<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT:U+002CU+0020>'.join(names) + postfix<EOL><DEDENT><DEDENT>

Return a readable name for a conversation.

    If the conversation has a custom name, use the custom name. Otherwise, for
    one-to-one conversations, the name is the full name of the other user. For
    group conversations, the name is a comma-separated list of first names. If
    the group conversation is empty, the name is "Empty Conversation".

    If truncate is true, only show up to two names in a group conversation.

    If show_unread is True, if there are unread chat messages, show the number
    of unread chat messages in parentheses after the conversation name.


if callback not in self._observers:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>.format(callback, self))<EOL><DEDENT>self._observers.remove(callback)<EOL>

Remove an observer from this event.

        Args:
            callback: A function or coroutine callback to remove from this
                event.

        Raises:
            ValueError: If the callback is not an observer of this event.


raw_attachments = self._event.chat_message.message_content.attachment<EOL>if raw_attachments is None:<EOL><INDENT>raw_attachments = []<EOL><DEDENT>attachments = []<EOL>for attachment in raw_attachments:<EOL><INDENT>for embed_item_type in attachment.embed_item.type:<EOL><INDENT>known_types = [<EOL>hangouts_pb2.ITEM_TYPE_PLUS_PHOTO,<EOL>hangouts_pb2.ITEM_TYPE_PLACE_V2,<EOL>hangouts_pb2.ITEM_TYPE_PLACE,<EOL>hangouts_pb2.ITEM_TYPE_THING,<EOL>]<EOL>if embed_item_type not in known_types:<EOL><INDENT>logger.warning('<STR_LIT>'<EOL>'<STR_LIT>', embed_item_type)<EOL><DEDENT><DEDENT>if attachment.embed_item.HasField('<STR_LIT>'):<EOL><INDENT>attachments.append(<EOL>attachment.embed_item.plus_photo.thumbnail.image_url<EOL>)<EOL><DEDENT><DEDENT>return attachments<EOL>

List of attachments in the message (:class:`list`).


return [user.UserID(chat_id=id_.chat_id, gaia_id=id_.gaia_id)<EOL>for id_ in self._event.membership_change.participant_ids]<EOL>

:class:`~hangups.user.UserID` of users involved (:class:`list`).


return self._event.otr_modification.old_otr_status<EOL>

The conversation's old OTR status.

        May be either ``OFF_THE_RECORD_STATUS_OFF_THE_RECORD`` or
        ``OFF_THE_RECORD_STATUS_ON_THE_RECORD``.


segment_list = chat_message_parser.parse(text)<EOL>return [ChatMessageSegment(segment.text, **segment.params)<EOL>for segment in segment_list]<EOL>

Construct :class:`ChatMessageSegment` list parsed from a string.

        Args:
            text (str): Text to parse. May contain line breaks, URLs and
                formatting markup (simplified Markdown and HTML) to be
                converted into equivalent segments.

        Returns:
            List of :class:`ChatMessageSegment` objects.


return self._event.membership_change.type<EOL>

The type of membership change.

        May be either ``MEMBERSHIP_CHANGE_TYPE_JOIN`` or
        ``MEMBERSHIP_CHANGE_TYPE_LEAVE``.


return self._event.conversation_rename.old_name<EOL>

The conversation's old name (:class:`str`).

        May be an empty string if the conversation had no previous name.


def __init__(self, text, segment_type=None,<EOL>is_bold=False, is_italic=False, is_strikethrough=False,<EOL>is_underline=False, link_target=None):

if segment_type is not None:<EOL><INDENT>self.type_ = segment_type<EOL><DEDENT>elif link_target is not None:<EOL><INDENT>self.type_ = hangouts_pb2.SEGMENT_TYPE_LINK<EOL><DEDENT>else:<EOL><INDENT>self.type_ = hangouts_pb2.SEGMENT_TYPE_TEXT<EOL><DEDENT>self.text = text<EOL>self.is_bold = is_bold<EOL>self.is_italic = is_italic<EOL>self.is_strikethrough = is_strikethrough<EOL>self.is_underline = is_underline<EOL>self.link_target = link_target<EOL>


async def lookup_entities(client, args):

lookup_spec = _get_lookup_spec(args.entity_identifier)<EOL>request = hangups.hangouts_pb2.GetEntityByIdRequest(<EOL>request_header=client.get_request_header(),<EOL>batch_lookup_spec=[lookup_spec],<EOL>)<EOL>res = await client.get_entity_by_id(request)<EOL>for entity_result in res.entity_result:<EOL><INDENT>for entity in entity_result.entity:<EOL><INDENT>print(entity)<EOL><DEDENT><DEDENT>

Search for entities by phone number, email, or gaia_id.


async def _async_main(example_coroutine, client, args):

<EOL>task = asyncio.ensure_future(client.connect())<EOL>on_connect = asyncio.Future()<EOL>client.on_connect.add_observer(lambda: on_connect.set_result(None))<EOL>done, _ = await asyncio.wait(<EOL>(on_connect, task), return_when=asyncio.FIRST_COMPLETED<EOL>)<EOL>await asyncio.gather(*done)<EOL>try:<EOL><INDENT>await example_coroutine(client, args)<EOL><DEDENT>except asyncio.CancelledError:<EOL><INDENT>pass<EOL><DEDENT>finally:<EOL><INDENT>await client.disconnect()<EOL>await task<EOL><DEDENT>


parser = argparse.ArgumentParser(<EOL>formatter_class=argparse.ArgumentDefaultsHelpFormatter,<EOL>)<EOL>dirs = appdirs.AppDirs('<STR_LIT>', '<STR_LIT>')<EOL>default_token_path = os.path.join(dirs.user_cache_dir, '<STR_LIT>')<EOL>parser.add_argument(<EOL>'<STR_LIT>', default=default_token_path,<EOL>help='<STR_LIT>'<EOL>)<EOL>parser.add_argument(<EOL>'<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>',<EOL>help='<STR_LIT>'<EOL>)<EOL>for extra_arg in extra_args:<EOL><INDENT>parser.add_argument(extra_arg, required=True)<EOL><DEDENT>return parser<EOL>

Return ArgumentParser with any extra arguments.


args = _get_parser(extra_args).parse_args()<EOL>logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARNING)<EOL>cookies = hangups.auth.get_auth_stdin(args.token_path)<EOL>client = hangups.Client(cookies)<EOL>loop = asyncio.get_event_loop()<EOL>task = asyncio.ensure_future(_async_main(example_coroutine, client, args),<EOL>loop=loop)<EOL>try:<EOL><INDENT>loop.run_until_complete(task)<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>task.cancel()<EOL>loop.run_until_complete(task)<EOL><DEDENT>finally:<EOL><INDENT>loop.close()<EOL><DEDENT>

Run a hangups example coroutine.

    Args:
        example_coroutine (coroutine): Coroutine to run with a connected
            hangups client and arguments namespace as arguments.
        extra_args (str): Any extra command line arguments required by the
            example.


col_widths = [max(len(str(row[col])) for row in [col_tuple] + row_tuples)<EOL>for col in range(len(col_tuple))]<EOL>format_str = '<STR_LIT:U+0020>'.join('<STR_LIT>'.format(col_width)<EOL>for col_width in col_widths)<EOL>header_border = '<STR_LIT:U+0020>'.join('<STR_LIT:=>' * col_width for col_width in col_widths)<EOL>print(header_border)<EOL>print(format_str.format(*col_tuple))<EOL>print(header_border)<EOL>for row_tuple in row_tuples:<EOL><INDENT>print(format_str.format(*row_tuple))<EOL><DEDENT>print(header_border)<EOL>print()<EOL>

Print column headers and rows as a reStructuredText table.

    Args:
        col_tuple: Tuple of column name strings.
        row_tuples: List of tuples containing row data.


def generate_enum_doc(enum_descriptor, locations, path, name_prefix='<STR_LIT>'):

print(make_subsection(name_prefix + enum_descriptor.name))<EOL>location = locations[path]<EOL>if location.HasField('<STR_LIT>'):<EOL><INDENT>print(textwrap.dedent(location.leading_comments))<EOL><DEDENT>row_tuples = []<EOL>for value_index, value in enumerate(enum_descriptor.value):<EOL><INDENT>field_location = locations[path + (<NUM_LIT:2>, value_index)]<EOL>row_tuples.append((<EOL>make_code(value.name),<EOL>value.number,<EOL>textwrap.fill(get_comment_from_location(field_location), INFINITY),<EOL>))<EOL><DEDENT>print_table(('<STR_LIT:Name>', '<STR_LIT>', '<STR_LIT>'), row_tuples)<EOL>

Generate doc for an enum.

    Args:
        enum_descriptor: descriptor_pb2.EnumDescriptorProto instance for enum
            to generate docs for.
        locations: Dictionary of location paths tuples to
            descriptor_pb2.SourceCodeInfo.Location instances.
        path: Path tuple to the enum definition.
        name_prefix: Optional prefix for this enum's name.


return '<STR_LIT>'.format(text)<EOL>

Format text as reStructuredText code.

    Args:
        text: Text string to format.

    Returns:
        Formatted text string.


def generate_message_doc(message_descriptor, locations, path, name_prefix='<STR_LIT>'):

<EOL>prefixed_name = name_prefix + message_descriptor.name<EOL>print(make_subsection(prefixed_name))<EOL>location = locations[path]<EOL>if location.HasField('<STR_LIT>'):<EOL><INDENT>print(textwrap.dedent(location.leading_comments))<EOL><DEDENT>row_tuples = []<EOL>for field_index, field in enumerate(message_descriptor.field):<EOL><INDENT>field_location = locations[path + (<NUM_LIT:2>, field_index)]<EOL>if field.type not in [<NUM_LIT:11>, <NUM_LIT>]:<EOL><INDENT>type_str = TYPE_TO_STR[field.type]<EOL><DEDENT>else:<EOL><INDENT>type_str = make_link(field.type_name.lstrip('<STR_LIT:.>'))<EOL><DEDENT>row_tuples.append((<EOL>make_code(field.name),<EOL>field.number,<EOL>type_str,<EOL>LABEL_TO_STR[field.label],<EOL>textwrap.fill(get_comment_from_location(field_location), INFINITY),<EOL>))<EOL><DEDENT>print_table(('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'),<EOL>row_tuples)<EOL>nested_types = enumerate(message_descriptor.nested_type)<EOL>for index, nested_message_desc in nested_types:<EOL><INDENT>generate_message_doc(nested_message_desc, locations,<EOL>path + (<NUM_LIT:3>, index),<EOL>name_prefix=prefixed_name + '<STR_LIT:.>')<EOL><DEDENT>for index, nested_enum_desc in enumerate(message_descriptor.enum_type):<EOL><INDENT>generate_enum_doc(nested_enum_desc, locations, path + (<NUM_LIT:4>, index),<EOL>name_prefix=prefixed_name + '<STR_LIT:.>')<EOL><DEDENT>

Generate docs for message and nested messages and enums.

    Args:
        message_descriptor: descriptor_pb2.DescriptorProto instance for message
            to generate docs for.
        locations: Dictionary of location paths tuples to
            descriptor_pb2.SourceCodeInfo.Location instances.
        path: Path tuple to the message definition.
        name_prefix: Optional prefix for this message's name.


out_file = tempfile.mkstemp()[<NUM_LIT:1>]<EOL>try:<EOL><INDENT>subprocess.check_output(['<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', out_file,<EOL>proto_file_path])<EOL><DEDENT>except subprocess.CalledProcessError as e:<EOL><INDENT>sys.exit('<STR_LIT>'.format(e.returncode))<EOL><DEDENT>return out_file<EOL>

Compile proto file to descriptor set.

    Args:
        proto_file_path: Path to proto file to compile.

    Returns:
        Path to file containing compiled descriptor set.

    Raises:
        SystemExit if the compilation fails.


@fixture<EOL>def post_token_dropbox(dropbox_container, config, post_token):

from briefkasten import parse_post_token<EOL>return dropbox_container.get_dropbox(<EOL>parse_post_token(<EOL>post_token,<EOL>secret=config.registry.settings['<STR_LIT>']<EOL>)<EOL>)<EOL>

returns a dropbox instance matching the given post_token


from briefkasten import generate_post_token<EOL>token = generate_post_token(secret=request.registry.settings['<STR_LIT>'])<EOL>return dict(<EOL>action=request.route_url('<STR_LIT>', token=token),<EOL>fileupload_url=request.route_url('<STR_LIT>', token=token),<EOL>**defaults(request))<EOL>

generates a dropbox uid and renders the submission form with a signed version of that id


try:<EOL><INDENT>data = dropbox_schema.deserialize(request.POST)<EOL><DEDENT>except Exception:<EOL><INDENT>return HTTPFound(location=request.route_url('<STR_LIT>'))<EOL><DEDENT>dropbox.message = data.get('<STR_LIT:message>')<EOL>if '<STR_LIT>' in dropbox.settings:<EOL><INDENT>dropbox.from_watchdog = is_equal(<EOL>unicode(dropbox.settings['<STR_LIT>']),<EOL>data.pop('<STR_LIT>', u'<STR_LIT>'))<EOL><DEDENT>if data.get('<STR_LIT>') is not None:<EOL><INDENT>dropbox.add_attachment(data['<STR_LIT>'])<EOL><DEDENT>dropbox.submit()<EOL>drop_url = request.route_url('<STR_LIT>', drop_id=dropbox.drop_id)<EOL>print("<STR_LIT>" % drop_url)<EOL>return HTTPFound(location=drop_url)<EOL>

handles the form submission, redirects to the dropbox's status page.


attachment = request.POST['<STR_LIT>']<EOL>attached = dropbox.add_attachment(attachment)<EOL>return dict(<EOL>files=[dict(<EOL>name=attached,<EOL>type=attachment.type,<EOL>)]<EOL>)<EOL>

accepts a single file upload and adds it to the dropbox as attachment


self.status = '<STR_LIT>'<EOL>return self._create_encrypted_zip(source='<STR_LIT>', fs_target_dir=self.container.fs_archive_cleansed)<EOL>

creates an encrypted archive of the dropbox outside of the drop directory.


if exists(self.fs_cleansed_attachment_container):<EOL><INDENT>return [join(self.fs_cleansed_attachment_container, attachment)<EOL>for attachment in listdir(self.fs_cleansed_attachment_container)]<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT>

returns a list of absolute paths to the cleansed attachements


<EOL>token = generate_drop_id()<EOL>name, extension = splitext(filename)<EOL>if extension:<EOL><INDENT>return '<STR_LIT>' % (token, extension)<EOL><DEDENT>else:<EOL><INDENT>return token<EOL><DEDENT>

preserve the file ending, but replace the name with a random token


if exists(self.fs_attachment_container):<EOL><INDENT>return [join(self.fs_attachment_container, attachment)<EOL>for attachment in listdir(self.fs_attachment_container)]<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT>

returns a list of absolute paths to the attachements


try:<EOL><INDENT>with open(join(self.fs_path, '<STR_LIT:message>')) as message_file:<EOL><INDENT>return '<STR_LIT>'.join([line.decode('<STR_LIT:utf-8>') for line in message_file.readlines()])<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>


return Dropbox(self, drop_id=drop_id)<EOL>

returns the dropbox with the given id, if it does not exist an empty dropbox
        will be created and returned


try:<EOL><INDENT>remove(join(self.fs_path, '<STR_LIT:message>'))<EOL>remove(join(self.fs_path, '<STR_LIT>'))<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT>shutil.rmtree(join(self.fs_path, '<STR_LIT>'), ignore_errors=True)<EOL>shutil.rmtree(join(self.fs_path, '<STR_LIT>'), ignore_errors=True)<EOL>

ensures that no data leaks from drop after processing by
        removing all data except the status file


if exists(self.fs_attachment_container):<EOL><INDENT>return len(listdir(self.fs_attachment_container))<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>

returns the current number of uploaded attachments in the filesystem


self._write_message(self.fs_path, '<STR_LIT:message>', newtext)<EOL>

overwrite the message text. this also updates the corresponding file.


def sendMultiPart(smtp, gpg_context, sender, recipients, subject, text, attachments):

sent = <NUM_LIT:0><EOL>for to in recipients:<EOL><INDENT>if not to.startswith('<STR_LIT:<>'):<EOL><INDENT>uid = '<STR_LIT>' % to<EOL><DEDENT>else:<EOL><INDENT>uid = to<EOL><DEDENT>if not checkRecipient(gpg_context, uid):<EOL><INDENT>continue<EOL><DEDENT>msg = MIMEMultipart()<EOL>msg['<STR_LIT>'] = sender<EOL>msg['<STR_LIT>'] = to<EOL>msg['<STR_LIT>'] = subject<EOL>msg["<STR_LIT>"] = formatdate(localtime=True)<EOL>msg.preamble = u'<STR_LIT>'<EOL>attach = MIMEText(str(gpg_context.encrypt(text.encode('<STR_LIT:utf-8>'), uid, always_trust=True)))<EOL>attach.set_charset('<STR_LIT>')<EOL>msg.attach(attach)<EOL>for attachment in attachments:<EOL><INDENT>with open(attachment, '<STR_LIT:rb>') as fp:<EOL><INDENT>attach = MIMEBase('<STR_LIT>', '<STR_LIT>')<EOL>attach.set_payload(str(gpg_context.encrypt_file(fp, uid, always_trust=True)))<EOL><DEDENT>attach.add_header('<STR_LIT>', '<STR_LIT>', filename=basename('<STR_LIT>' % attachment))<EOL>msg.attach(attach)<EOL><DEDENT>smtp.begin()<EOL>smtp.sendmail(sender, to, msg.as_string())<EOL>smtp.quit()<EOL>sent += <NUM_LIT:1><EOL><DEDENT>return sent<EOL>

a helper method that composes and sends an email with attachments
    requires a pre-configured smtplib.SMTP instance


try:<EOL><INDENT>return request.registry.settings['<STR_LIT>'].get_dropbox(request.matchdict['<STR_LIT>'])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise HTTPNotFound('<STR_LIT>')<EOL><DEDENT>

expects the id of an existing dropbox and returns its instance


return '<STR_LIT>'<EOL>

a 'negotiator' that always returns german


return URLSafeTimedSerializer(secret, salt=u'<STR_LIT>').dumps(generate_drop_id())<EOL>

returns a URL safe, signed token that contains a UUID


return configure(global_config, **settings).make_wsgi_app()<EOL>

Configure and create the main application.


upload_distfiles()<EOL>upload_packages()<EOL>

upload local ports tree, distfiles, and packages from poudriere


get_vars()<EOL>use_devpi(index=index)<EOL>with fab.lcd('<STR_LIT>'):<EOL><INDENT>fab.local('<STR_LIT>')<EOL><DEDENT>

Build the backend and upload it to the remote server at the given index


get_vars()<EOL>upload_target = '<STR_LIT>'<EOL>with fab.settings(fab.hide('<STR_LIT>')):<EOL><INDENT>fab.run('<STR_LIT>' % upload_target)<EOL>fab.run('<STR_LIT>' % upload_target)<EOL>local_key_path = path.join(fab.env['<STR_LIT>'], fab.env.instance.config['<STR_LIT>'])<EOL>remote_key_path = '<STR_LIT>'.format(**AV)<EOL>rsync('<STR_LIT>', local_key_path, '<STR_LIT>' % upload_target)<EOL>fab.run('<STR_LIT>' % (AV['<STR_LIT>'], remote_key_path))<EOL>fab.run('<STR_LIT>' % remote_key_path)<EOL>with fab.shell_env(GNUPGHOME=remote_key_path):<EOL><INDENT>fab.sudo('''<STR_LIT>''' % upload_target,<EOL>user=AV['<STR_LIT>'], shell_escape=False)<EOL><DEDENT>fab.run('<STR_LIT>' % upload_target)<EOL><DEDENT>

upload and/or update the PGP keys for editors, import them into PGP


if self.extra_args:<EOL><INDENT>sys.exit('<STR_LIT>'.format(self.name))<EOL><DEDENT>else:<EOL><INDENT>if self._toggle_value:<EOL><INDENT>nbextensions.install_nbextension_python(<EOL>_pkg_name, overwrite=True, symlink=False,<EOL>user=self.user, sys_prefix=self.sys_prefix, prefix=None,<EOL>nbextensions_dir=None, logger=None)<EOL><DEDENT>else:<EOL><INDENT>nbextensions.uninstall_nbextension_python(<EOL>_pkg_name, user=self.user, sys_prefix=self.sys_prefix,<EOL>prefix=None, nbextensions_dir=None, logger=None)<EOL><DEDENT>self.toggle_nbextension_python(_pkg_name)<EOL>self.toggle_server_extension_python(_pkg_name)<EOL><DEDENT>

Perform the App's actions as configured.


conflicting_flags = set(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>if len(conflicting_flags.intersection(set(argv))) > <NUM_LIT:1>:<EOL><INDENT>raise serverextensions.ArgumentConflict(<EOL>'<STR_LIT>')<EOL><DEDENT>return super(ToggleJupyterTensorboardApp,<EOL>self).parse_command_line(argv)<EOL>

Overriden to check for conflicting flags
Since notebook version doesn't do it well (or, indeed, at all)


try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', category_id)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Category(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Return a PYBOSSA Category for the category_id.

    :param category_id: PYBOSSA Category ID
    :type category_id: integer
    :rtype: PYBOSSA Category
    :returns: A PYBOSSA Category object


try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', project_id)<EOL>if type(res).__name__ == '<STR_LIT:bool>':<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Delete a Project with id = project_id.

    :param project_id: PYBOSSA Project ID
    :type project_id: integer
    :returns: True -- the response status code


try:<EOL><INDENT>helpingmaterial_id = helpingmaterial.id<EOL>helpingmaterial = _forbidden_attributes(helpingmaterial)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>',<EOL>helpingmaterial_id, payload=helpingmaterial.data)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return HelpingMaterial(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Update a helping material for a given helping material ID.

    :param helpingmaterial: PYBOSSA helping material


data = self.__dict__['<STR_LIT:data>']<EOL>if name == '<STR_LIT:data>':<EOL><INDENT>self.__dict__['<STR_LIT:data>'] = value<EOL>return True<EOL><DEDENT>if name in data:<EOL><INDENT>data[name] = value<EOL>return True<EOL><DEDENT>raise AttributeError('<STR_LIT>' + name)<EOL>


try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', params=kwargs)<EOL>if type(res).__name__ == '<STR_LIT:list>':<EOL><INDENT>return [Category(category) for category in res]<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Return a list with matching Category arguments.

    :param kwargs: PYBOSSA Category members
    :rtype: list
    :returns: A list of project that match the kwargs


try:<EOL><INDENT>category = dict(name=name, short_name=name.lower().replace("<STR_LIT:U+0020>", "<STR_LIT>"),<EOL>description=description)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', payload=category)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Category(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Create a Category.

    :param name: PYBOSSA Category Name
    :type name: string
    :param description: PYBOSSA Category description
    :type decription: string
    :returns: True -- the response status code


global _opts<EOL>_opts[key] = val<EOL>


<EOL>try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', task_id)<EOL>if type(res).__name__ == '<STR_LIT:bool>':<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Delete a task for a given task ID.

    :param task: PYBOSSA task


try:<EOL><INDENT>result_id = result.id<EOL>result = _forbidden_attributes(result)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT:result>', result_id, payload=result.data)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Result(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Update a result for a given result ID.

    :param result: PYBOSSA result


try:<EOL><INDENT>task_id = task.id<EOL>task = _forbidden_attributes(task)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', task_id, payload=task.data)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Task(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Update a task for a given task ID.

    :param task: PYBOSSA task


try:<EOL><INDENT>project_id = project.id<EOL>project = _forbidden_attributes(project)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', project_id, payload=project.data)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Project(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Update a project instance.

    :param project: PYBOSSA project
    :type project: PYBOSSA Project
    :returns: True -- the response status code


try:<EOL><INDENT>kwargs['<STR_LIT>'] = project_id<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', params=kwargs)<EOL>if type(res).__name__ == '<STR_LIT:list>':<EOL><INDENT>return [HelpingMaterial(helping) for helping in res]<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Return a list of matched helping materials for a given project ID.

    :param project_id: PYBOSSA Project ID
    :type project_id: integer
    :param kwargs: PYBOSSA HelpingMaterial members
    :type info: dict
    :rtype: list
    :returns: A list of helping materials that match the kwargs


return '<STR_LIT>' + str(self.id) + '<STR_LIT:)>'<EOL>


try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', project_id)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Project(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Return a PYBOSSA Project for the project_id.

    :param project_id: PYBOSSA Project ID
    :type project_id: integer
    :rtype: PYBOSSA Project
    :returns: A PYBOSSA Project object


def create_project(name, short_name, description):

try:<EOL><INDENT>project = dict(name=name, short_name=short_name,<EOL>description=description)<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', payload=project)<EOL>if res.get('<STR_LIT:id>'):<EOL><INDENT>return Project(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Create a project.

    :param name: PYBOSSA Project Name
    :type name: string
    :param short_name: PYBOSSA Project short name or slug
    :type short_name: string
    :param description: PYBOSSA Project description
    :type decription: string
    :returns: True -- the response status code


def create_helpingmaterial(project_id, info, media_url=None, file_path=None):

try:<EOL><INDENT>helping = dict(<EOL>project_id=project_id,<EOL>info=info,<EOL>media_url=None,<EOL>)<EOL>if file_path:<EOL><INDENT>files = {'<STR_LIT:file>': open(file_path, '<STR_LIT:rb>')}<EOL>payload = {'<STR_LIT>': project_id}<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT>',<EOL>payload=payload, files=files)<EOL><DEDENT>else:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>', payload=helping)<EOL><DEDENT>if res.get('<STR_LIT:id>'):<EOL><INDENT>return HelpingMaterial(res)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Create a helping material for a given project ID.

    :param project_id: PYBOSSA Project ID
    :type project_id: integer
    :param info: PYBOSSA Helping Material info JSON field
    :type info: dict
    :param media_url: URL for a media file (image, video or audio)
    :type media_url: string
    :param file_path: File path to the local image, video or sound to upload.
    :type file_path: string
    :returns: True -- the response status code


try:<EOL><INDENT>kwargs['<STR_LIT>'] = project_id<EOL>res = _pybossa_req('<STR_LIT>', '<STR_LIT:result>', params=kwargs)<EOL>if type(res).__name__ == '<STR_LIT:list>':<EOL><INDENT>return [Result(result) for result in res]<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT><DEDENT>except:  <EOL><INDENT>raise<EOL><DEDENT>

Return a list of matched results for a given project ID.

    :param project_id: PYBOSSA Project ID
    :type project_id: integer
    :param kwargs: PYBOSSA Results members
    :type info: dict
    :rtype: list
    :returns: A list of results that match the kwargs


def _pybossa_req(method, domain, id=None, payload=None, params={},<EOL>headers={'<STR_LIT>': '<STR_LIT:application/json>'},<EOL>files=None):

url = _opts['<STR_LIT>'] + '<STR_LIT>' + domain<EOL>if id is not None:<EOL><INDENT>url += '<STR_LIT:/>' + str(id)<EOL><DEDENT>if '<STR_LIT>' in _opts:<EOL><INDENT>params['<STR_LIT>'] = _opts['<STR_LIT>']<EOL><DEDENT>if method == '<STR_LIT>':<EOL><INDENT>r = requests.get(url, params=params)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>if files is None and headers['<STR_LIT>'] == '<STR_LIT:application/json>':<EOL><INDENT>r = requests.post(url, params=params, headers=headers,<EOL>data=json.dumps(payload))<EOL><DEDENT>else:<EOL><INDENT>r = requests.post(url, params=params, files=files, data=payload)<EOL><DEDENT><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>r = requests.put(url, params=params, headers=headers,<EOL>data=json.dumps(payload))<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>r = requests.delete(url, params=params, headers=headers,<EOL>data=json.dumps(payload))<EOL><DEDENT>if r.status_code // <NUM_LIT:100> == <NUM_LIT:2>:<EOL><INDENT>if r.text and r.text != '<STR_LIT>':<EOL><INDENT>return json.loads(r.text)<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return json.loads(r.text)<EOL><DEDENT>

Send a JSON request.

Returns True if everything went well, otherwise it returns the status
code of the response.


def get_categories(limit=<NUM_LIT:20>, offset=<NUM_LIT:0>, last_id=None):

if last_id is not None:<EOL><INDENT>params = dict(limit=limit, last_id=last_id)<EOL><DEDENT>else:<EOL><INDENT>params = dict(limit=limit, offset=offset)<EOL>print(OFFSET_WARNING)<EOL><DEDENT>try:<EOL><INDENT>res = _pybossa_req('<STR_LIT>', '<STR_LIT>',<EOL>params=params)<EOL>if type(res).__name__ == '<STR_LIT:list>':<EOL><INDENT>return [Category(category) for category in res]<EOL><DEDENT>else:<EOL><INDENT>raise TypeError<EOL><DEDENT><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>

Return a list of registered categories.

    :param limit: Number of returned items, default 20
    :type limit: integer
    :param offset: Offset for the query, default 0
    :type offset: integer
    :param last_id: id of the last category, used for pagination. If provided, offset is ignored
    :type last_id: integer
    :rtype: list
    :returns: A list of PYBOSSA Categories


if pieces["<STR_LIT>"]:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL>if pieces["<STR_LIT>"] or pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>" % pieces["<STR_LIT>"]<EOL>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT>rendered += plus_or_dot(pieces)<EOL>rendered += "<STR_LIT>" % pieces["<STR_LIT>"]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>rendered = "<STR_LIT>" % pieces["<STR_LIT>"]<EOL>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT>rendered += "<STR_LIT>" % pieces["<STR_LIT>"]<EOL><DEDENT>return rendered<EOL>

TAG[.postDISTANCE[.dev0]+gHEX] .

    The ".dev0" means dirty. Note that .dev0 sorts backwards
    (a dirty tree will appear "older" than the corresponding clean one),
    but you shouldn't be releasing software with -dirty anyways.

    Exceptions:
    1: no tags. 0.postDISTANCE[.dev0]


if pieces["<STR_LIT>"]:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>" % (pieces["<STR_LIT>"], pieces["<STR_LIT>"])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL><DEDENT>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT>return rendered<EOL>

TAG[-DISTANCE-gHEX][-dirty].

    Like 'git describe --tags --dirty --always'.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)


def versions_from_parentdir(parentdir_prefix, root, verbose):

dirname = os.path.basename(root)<EOL>if not dirname.startswith(parentdir_prefix):<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>" % (root, dirname, parentdir_prefix))<EOL><DEDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>return {"<STR_LIT:version>": dirname[len(parentdir_prefix):],<EOL>"<STR_LIT>": None,<EOL>"<STR_LIT>": False, "<STR_LIT:error>": None}<EOL>

Try to determine the version from the parent directory name.

    Source tarballs conventionally unpack into a directory that includes
    both the project name and a version string.


if pieces["<STR_LIT>"]:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL>if pieces["<STR_LIT>"] or pieces["<STR_LIT>"]:<EOL><INDENT>rendered += plus_or_dot(pieces)<EOL>rendered += "<STR_LIT>" % (pieces["<STR_LIT>"], pieces["<STR_LIT>"])<EOL>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>rendered = "<STR_LIT>" % (pieces["<STR_LIT>"],<EOL>pieces["<STR_LIT>"])<EOL>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT><DEDENT>return rendered<EOL>

Build up version string, with post-release "local version identifier".

    Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
    get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty

    Exceptions:
    1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty]


if "<STR_LIT:+>" in pieces.get("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>return "<STR_LIT:.>"<EOL><DEDENT>return "<STR_LIT:+>"<EOL>

Return a + if we don't already have one, else return a .


def decorate(f):<EOL><INDENT>"""<STR_LIT>"""<EOL>if vcs not in HANDLERS:<EOL><INDENT>HANDLERS[vcs] = {}<EOL><DEDENT>HANDLERS[vcs][method] = f<EOL>return f<EOL><DEDENT>return decorate<EOL>

Decorator to mark a method as the handler for a particular VCS.


<EOL>keywords = {}<EOL>try:<EOL><INDENT>f = open(versionfile_abs, "<STR_LIT:r>")<EOL>for line in f.readlines():<EOL><INDENT>if line.strip().startswith("<STR_LIT>"):<EOL><INDENT>mo = re.search(r'<STR_LIT>', line)<EOL>if mo:<EOL><INDENT>keywords["<STR_LIT>"] = mo.group(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>if line.strip().startswith("<STR_LIT>"):<EOL><INDENT>mo = re.search(r'<STR_LIT>', line)<EOL>if mo:<EOL><INDENT>keywords["<STR_LIT>"] = mo.group(<NUM_LIT:1>)<EOL><DEDENT><DEDENT><DEDENT>f.close()<EOL><DEDENT>except EnvironmentError:<EOL><INDENT>pass<EOL><DEDENT>return keywords<EOL>

Extract version information from the given file.


<EOL>cfg = VersioneerConfig()<EOL>cfg.VCS = "<STR_LIT>"<EOL>cfg.style = "<STR_LIT>"<EOL>cfg.tag_prefix = "<STR_LIT:v>"<EOL>cfg.parentdir_prefix = "<STR_LIT>"<EOL>cfg.versionfile_source = "<STR_LIT>"<EOL>cfg.verbose = False<EOL>return cfg<EOL>

Create, populate and return the VersioneerConfig() object.


def read_stream_tuples(schema, stream, *, buffer_size=io.DEFAULT_BUFFER_SIZE):

reader = _lancaster.Reader(schema, _get_datetime_flags(schema))<EOL>buf = stream.read(buffer_size)<EOL>remainder = b'<STR_LIT>'<EOL>while len(buf) > <NUM_LIT:0>:<EOL><INDENT>values, n = reader.read_seq_tuples(buf)<EOL>yield from values<EOL>remainder = buf[n:]<EOL>buf = stream.read(buffer_size)<EOL>if len(buf) > <NUM_LIT:0> and len(remainder) > <NUM_LIT:0>:<EOL><INDENT>ba = bytearray()<EOL>ba.extend(remainder)<EOL>ba.extend(buf)<EOL>buf = memoryview(ba).tobytes()<EOL><DEDENT><DEDENT>if len(remainder) > <NUM_LIT:0>:<EOL><INDENT>raise EOFError('<STR_LIT>'<EOL>'<STR_LIT>'.format(len(remainder)))<EOL><DEDENT>

Using a schema, deserialize a stream of consecutive Avro values
    into tuples.

    This assumes the input is avro records of simple values (numbers,
    strings, etc.).

    :param str schema: json string representing the Avro schema, field
        names may include 'is_datetime' boolean fields to force
        decoding long values of epoch nanoseconds into datetime
        objects
    :param file-like stream: a buffered stream of binary input
    :param int buffer_size: size of bytes to read from the stream each time
    :return: yields a sequence of python tuples deserialized from the stream


if pieces["<STR_LIT:error>"]:<EOL><INDENT>return {"<STR_LIT:version>": "<STR_LIT>",<EOL>"<STR_LIT>": pieces.get("<STR_LIT>"),<EOL>"<STR_LIT>": None,<EOL>"<STR_LIT:error>": pieces["<STR_LIT:error>"]}<EOL><DEDENT>if not style or style == "<STR_LIT:default>":<EOL><INDENT>style = "<STR_LIT>"  <EOL><DEDENT>if style == "<STR_LIT>":<EOL><INDENT>rendered = render_pep440(pieces)<EOL><DEDENT>elif style == "<STR_LIT>":<EOL><INDENT>rendered = render_pep440_pre(pieces)<EOL><DEDENT>elif style == "<STR_LIT>":<EOL><INDENT>rendered = render_pep440_post(pieces)<EOL><DEDENT>elif style == "<STR_LIT>":<EOL><INDENT>rendered = render_pep440_old(pieces)<EOL><DEDENT>elif style == "<STR_LIT>":<EOL><INDENT>rendered = render_git_describe(pieces)<EOL><DEDENT>elif style == "<STR_LIT>":<EOL><INDENT>rendered = render_git_describe_long(pieces)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % style)<EOL><DEDENT>return {"<STR_LIT:version>": rendered, "<STR_LIT>": pieces["<STR_LIT>"],<EOL>"<STR_LIT>": pieces["<STR_LIT>"], "<STR_LIT:error>": None}<EOL>

Render the given version pieces into the requested style.


@register_vcs_handler("<STR_LIT>", "<STR_LIT>")<EOL>def git_versions_from_keywords(keywords, tag_prefix, verbose):

if not keywords:<EOL><INDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>refnames = keywords["<STR_LIT>"].strip()<EOL>if refnames.startswith("<STR_LIT>"):<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>refs = set([r.strip() for r in refnames.strip("<STR_LIT>").split("<STR_LIT:U+002C>")])<EOL>TAG = "<STR_LIT>"<EOL>tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])<EOL>if not tags:<EOL><INDENT>tags = set([r for r in refs if re.search(r'<STR_LIT>', r)])<EOL>if verbose:<EOL><INDENT>print("<STR_LIT>" % "<STR_LIT:U+002C>".join(refs-tags))<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % "<STR_LIT:U+002C>".join(sorted(tags)))<EOL><DEDENT>for ref in sorted(tags):<EOL><INDENT>if ref.startswith(tag_prefix):<EOL><INDENT>r = ref[len(tag_prefix):]<EOL>if verbose:<EOL><INDENT>print("<STR_LIT>" % r)<EOL><DEDENT>return {"<STR_LIT:version>": r,<EOL>"<STR_LIT>": keywords["<STR_LIT>"].strip(),<EOL>"<STR_LIT>": False, "<STR_LIT:error>": None<EOL>}<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>return {"<STR_LIT:version>": "<STR_LIT>",<EOL>"<STR_LIT>": keywords["<STR_LIT>"].strip(),<EOL>"<STR_LIT>": False, "<STR_LIT:error>": "<STR_LIT>"}<EOL>

Get version information from git keywords.


if pieces["<STR_LIT>"]:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL>rendered += "<STR_LIT>" % (pieces["<STR_LIT>"], pieces["<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>rendered = pieces["<STR_LIT>"]<EOL><DEDENT>if pieces["<STR_LIT>"]:<EOL><INDENT>rendered += "<STR_LIT>"<EOL><DEDENT>return rendered<EOL>

TAG-DISTANCE-gHEX[-dirty].

    Like 'git describe --tags --dirty --always -long'.
    The distance/hash is unconditional.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)


found = set()<EOL>setters = False<EOL>errors = <NUM_LIT:0><EOL>with open("<STR_LIT>", "<STR_LIT:r>") as f:<EOL><INDENT>for line in f.readlines():<EOL><INDENT>if "<STR_LIT>" in line:<EOL><INDENT>found.add("<STR_LIT>")<EOL><DEDENT>if "<STR_LIT>" in line:<EOL><INDENT>found.add("<STR_LIT>")<EOL><DEDENT>if "<STR_LIT>" in line:<EOL><INDENT>found.add("<STR_LIT>")<EOL><DEDENT>if "<STR_LIT>" in line:<EOL><INDENT>setters = True<EOL><DEDENT>if "<STR_LIT>" in line:<EOL><INDENT>setters = True<EOL><DEDENT><DEDENT><DEDENT>if len(found) != <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>errors += <NUM_LIT:1><EOL><DEDENT>if setters:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>errors += <NUM_LIT:1><EOL><DEDENT>return errors<EOL>

Validate the contents of setup.py against Versioneer's expectations.


<EOL>setup_cfg = os.path.join(root, "<STR_LIT>")<EOL>parser = configparser.SafeConfigParser()<EOL>with open(setup_cfg, "<STR_LIT:r>") as f:<EOL><INDENT>parser.readfp(f)<EOL><DEDENT>VCS = parser.get("<STR_LIT>", "<STR_LIT>")  <EOL>def get(parser, name):<EOL><INDENT>if parser.has_option("<STR_LIT>", name):<EOL><INDENT>return parser.get("<STR_LIT>", name)<EOL><DEDENT>return None<EOL><DEDENT>cfg = VersioneerConfig()<EOL>cfg.VCS = VCS<EOL>cfg.style = get(parser, "<STR_LIT>") or "<STR_LIT>"<EOL>cfg.versionfile_source = get(parser, "<STR_LIT>")<EOL>cfg.versionfile_build = get(parser, "<STR_LIT>")<EOL>cfg.tag_prefix = get(parser, "<STR_LIT>")<EOL>if cfg.tag_prefix in ("<STR_LIT>", '<STR_LIT>'):<EOL><INDENT>cfg.tag_prefix = "<STR_LIT>"<EOL><DEDENT>cfg.parentdir_prefix = get(parser, "<STR_LIT>")<EOL>cfg.verbose = get(parser, "<STR_LIT>")<EOL>return cfg<EOL>

Read the project setup.cfg file to determine Versioneer config.


@register_vcs_handler("<STR_LIT>", "<STR_LIT>")<EOL>def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):

if not os.path.exists(os.path.join(root, "<STR_LIT>")):<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % root)<EOL><DEDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>GITS = ["<STR_LIT>"]<EOL>if sys.platform == "<STR_LIT:win32>":<EOL><INDENT>GITS = ["<STR_LIT>", "<STR_LIT>"]<EOL><DEDENT>describe_out = run_command(GITS, ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>" % tag_prefix],<EOL>cwd=root)<EOL>if describe_out is None:<EOL><INDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>describe_out = describe_out.strip()<EOL>full_out = run_command(GITS, ["<STR_LIT>", "<STR_LIT>"], cwd=root)<EOL>if full_out is None:<EOL><INDENT>raise NotThisMethod("<STR_LIT>")<EOL><DEDENT>full_out = full_out.strip()<EOL>pieces = {}<EOL>pieces["<STR_LIT>"] = full_out<EOL>pieces["<STR_LIT>"] = full_out[:<NUM_LIT:7>]  <EOL>pieces["<STR_LIT:error>"] = None<EOL>git_describe = describe_out<EOL>dirty = git_describe.endswith("<STR_LIT>")<EOL>pieces["<STR_LIT>"] = dirty<EOL>if dirty:<EOL><INDENT>git_describe = git_describe[:git_describe.rindex("<STR_LIT>")]<EOL><DEDENT>if "<STR_LIT:->" in git_describe:<EOL><INDENT>mo = re.search(r'<STR_LIT>', git_describe)<EOL>if not mo:<EOL><INDENT>pieces["<STR_LIT:error>"] = ("<STR_LIT>"<EOL>% describe_out)<EOL>return pieces<EOL><DEDENT>full_tag = mo.group(<NUM_LIT:1>)<EOL>if not full_tag.startswith(tag_prefix):<EOL><INDENT>if verbose:<EOL><INDENT>fmt = "<STR_LIT>"<EOL>print(fmt % (full_tag, tag_prefix))<EOL><DEDENT>pieces["<STR_LIT:error>"] = ("<STR_LIT>"<EOL>% (full_tag, tag_prefix))<EOL>return pieces<EOL><DEDENT>pieces["<STR_LIT>"] = full_tag[len(tag_prefix):]<EOL>pieces["<STR_LIT>"] = int(mo.group(<NUM_LIT:2>))<EOL>pieces["<STR_LIT>"] = mo.group(<NUM_LIT:3>)<EOL><DEDENT>else:<EOL><INDENT>pieces["<STR_LIT>"] = None<EOL>count_out = run_command(GITS, ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"],<EOL>cwd=root)<EOL>pieces["<STR_LIT>"] = int(count_out)  <EOL><DEDENT>return pieces<EOL>

Get version from 'git describe' in the root of the source tree.

    This only gets called if the git-archive 'subst' keywords were *not*
    expanded, and _version.py hasn't already been rewritten with a short
    version string, meaning we're inside a checked out source tree.


if "<STR_LIT>" in sys.modules:<EOL><INDENT>del sys.modules["<STR_LIT>"]<EOL><DEDENT>cmds = {}<EOL>from distutils.core import Command<EOL>class cmd_version(Command):<EOL><INDENT>description = "<STR_LIT>"<EOL>user_options = []<EOL>boolean_options = []<EOL>def initialize_options(self):<EOL><INDENT>pass<EOL><DEDENT>def finalize_options(self):<EOL><INDENT>pass<EOL><DEDENT>def run(self):<EOL><INDENT>vers = get_versions(verbose=True)<EOL>print("<STR_LIT>" % vers["<STR_LIT:version>"])<EOL>print("<STR_LIT>" % vers.get("<STR_LIT>"))<EOL>print("<STR_LIT>" % vers.get("<STR_LIT>"))<EOL>if vers["<STR_LIT:error>"]:<EOL><INDENT>print("<STR_LIT>" % vers["<STR_LIT:error>"])<EOL><DEDENT><DEDENT><DEDENT>cmds["<STR_LIT:version>"] = cmd_version<EOL>if "<STR_LIT>" in sys.modules:<EOL><INDENT>from setuptools.command.build_py import build_py as _build_py<EOL><DEDENT>else:<EOL><INDENT>from distutils.command.build_py import build_py as _build_py<EOL><DEDENT>class cmd_build_py(_build_py):<EOL><INDENT>def run(self):<EOL><INDENT>root = get_root()<EOL>cfg = get_config_from_root(root)<EOL>versions = get_versions()<EOL>_build_py.run(self)<EOL>if cfg.versionfile_build:<EOL><INDENT>target_versionfile = os.path.join(self.build_lib,<EOL>cfg.versionfile_build)<EOL>print("<STR_LIT>" % target_versionfile)<EOL>write_to_version_file(target_versionfile, versions)<EOL><DEDENT><DEDENT><DEDENT>cmds["<STR_LIT>"] = cmd_build_py<EOL>if "<STR_LIT>" in sys.modules:  <EOL><INDENT>from cx_Freeze.dist import build_exe as _build_exe<EOL>class cmd_build_exe(_build_exe):<EOL><INDENT>def run(self):<EOL><INDENT>root = get_root()<EOL>cfg = get_config_from_root(root)<EOL>versions = get_versions()<EOL>target_versionfile = cfg.versionfile_source<EOL>print("<STR_LIT>" % target_versionfile)<EOL>write_to_version_file(target_versionfile, versions)<EOL>_build_exe.run(self)<EOL>os.unlink(target_versionfile)<EOL>with open(cfg.versionfile_source, "<STR_LIT:w>") as f:<EOL><INDENT>LONG = LONG_VERSION_PY[cfg.VCS]<EOL>f.write(LONG %<EOL>{"<STR_LIT>": "<STR_LIT:$>",<EOL>"<STR_LIT>": cfg.style,<EOL>"<STR_LIT>": cfg.tag_prefix,<EOL>"<STR_LIT>": cfg.parentdir_prefix,<EOL>"<STR_LIT>": cfg.versionfile_source,<EOL>})<EOL><DEDENT><DEDENT><DEDENT>cmds["<STR_LIT>"] = cmd_build_exe<EOL>del cmds["<STR_LIT>"]<EOL><DEDENT>if "<STR_LIT>" in sys.modules:<EOL><INDENT>from setuptools.command.sdist import sdist as _sdist<EOL><DEDENT>else:<EOL><INDENT>from distutils.command.sdist import sdist as _sdist<EOL><DEDENT>class cmd_sdist(_sdist):<EOL><INDENT>def run(self):<EOL><INDENT>versions = get_versions()<EOL>self._versioneer_generated_versions = versions<EOL>self.distribution.metadata.version = versions["<STR_LIT:version>"]<EOL>return _sdist.run(self)<EOL><DEDENT>def make_release_tree(self, base_dir, files):<EOL><INDENT>root = get_root()<EOL>cfg = get_config_from_root(root)<EOL>_sdist.make_release_tree(self, base_dir, files)<EOL>target_versionfile = os.path.join(base_dir, cfg.versionfile_source)<EOL>print("<STR_LIT>" % target_versionfile)<EOL>write_to_version_file(target_versionfile,<EOL>self._versioneer_generated_versions)<EOL><DEDENT><DEDENT>cmds["<STR_LIT>"] = cmd_sdist<EOL>return cmds<EOL>

Get the custom setuptools/distutils subclasses used by Versioneer.


def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False):

assert isinstance(commands, list)<EOL>p = None<EOL>for c in commands:<EOL><INDENT>try:<EOL><INDENT>dispcmd = str([c] + args)<EOL>p = subprocess.Popen([c] + args, cwd=cwd, stdout=subprocess.PIPE,<EOL>stderr=(subprocess.PIPE if hide_stderr<EOL>else None))<EOL>break<EOL><DEDENT>except EnvironmentError:<EOL><INDENT>e = sys.exc_info()[<NUM_LIT:1>]<EOL>if e.errno == errno.ENOENT:<EOL><INDENT>continue<EOL><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % dispcmd)<EOL>print(e)<EOL><DEDENT>return None<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % (commands,))<EOL><DEDENT>return None<EOL><DEDENT>stdout = p.communicate()[<NUM_LIT:0>].strip()<EOL>if sys.version_info[<NUM_LIT:0>] >= <NUM_LIT:3>:<EOL><INDENT>stdout = stdout.decode()<EOL><DEDENT>if p.returncode != <NUM_LIT:0>:<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % dispcmd)<EOL><DEDENT>return None<EOL><DEDENT>return stdout<EOL>


return parser.parse(text).parsed<EOL>

Simple convenience function to unwrap the array of parameters.


<EOL>response, _ = self.client.options(self.right_path)<EOL>assert response.status == http.client.OK<EOL>assert header not in response<EOL>response, _ = self.client.options(<EOL>self.right_path,<EOL>headers={<EOL>'<STR_LIT>': self.right_origin,<EOL>'<STR_LIT>': self.default_method})<EOL>assert response.status == http.client.OK<EOL>assert header not in response<EOL>response, _ = self.client.options(<EOL>self.right_path, headers={'<STR_LIT>': '<STR_LIT:*>'})<EOL>assert response.status == http.client.OK<EOL>assert header not in response<EOL>response, _ = self.client.options(self.left_path)<EOL>assert response.status == http.client.OK<EOL>assert header not in response<EOL>response, _ = self.client.options(<EOL>self.left_path,<EOL>headers={<EOL>'<STR_LIT>': self.left_origin,<EOL>'<STR_LIT>': self.default_method})<EOL>assert response.status == http.client.OK<EOL>assert header not in response<EOL>

Test each Access-Control-Allow header here, since they
all do the same thing.


o = StringIO()<EOL>o.write('<STR_LIT:(>')<EOL>if self.negated:<EOL><INDENT>o.write('<STR_LIT>')<EOL><DEDENT>o.write('<STR_LIT:.>'.join(self.path))<EOL>if self.values:<EOL><INDENT>o.write('<STR_LIT>' % REVERSED_OPERATOR_SUFFIX_MAP[self.operator])<EOL><DEDENT>o.write('<STR_LIT>'.join(map(lambda x: "<STR_LIT>".format(str(x)), self.values)))<EOL>o.write('<STR_LIT:)>')<EOL>return o.getvalue()<EOL>

Format this query segment in a human-readable representation
intended for debugging.


buf = StringIO()<EOL>segments = []<EOL>combinators = []<EOL>last_group = False<EOL>iterator = iter(text)<EOL>last_negation = False<EOL>for character in iterator:<EOL><INDENT>if character in COMBINATORS:<EOL><INDENT>if last_negation:<EOL><INDENT>buf.write(constants.OPERATOR_NEGATION)<EOL><DEDENT>val = buf.getvalue()<EOL>reset_stringio(buf)<EOL>if not last_group and not len(val):<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>if len(val):<EOL><INDENT>segments.append(parse_segment(val))<EOL><DEDENT>combinators.append(COMBINATORS[character])<EOL><DEDENT>elif character == constants.GROUP_BEGIN:<EOL><INDENT>if buf.tell():<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>seg = split_segments(iterator, True)<EOL>if last_negation:<EOL><INDENT>seg = UnarySegmentCombinator(seg)<EOL><DEDENT>segments.append(seg)<EOL>last_group = True<EOL>continue<EOL><DEDENT>elif character == constants.GROUP_END:<EOL><INDENT>val = buf.getvalue()<EOL>if not buf.tell() or not closing_paren:<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>segments.append(parse_segment(val))<EOL>return combine(segments, combinators)<EOL><DEDENT>elif character == constants.OPERATOR_NEGATION and not buf.tell():<EOL><INDENT>last_negation = True<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>if last_negation:<EOL><INDENT>buf.write(constants.OPERATOR_NEGATION)<EOL><DEDENT>if last_group:<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>buf.write(character)<EOL><DEDENT>last_negation = False<EOL>last_group = False<EOL><DEDENT>else:<EOL><INDENT>if closing_paren:<EOL><INDENT>raise ValueError('<STR_LIT>' % constants.GROUP_END)<EOL><DEDENT>if not last_group:<EOL><INDENT>segments.append(parse_segment(buf.getvalue()))<EOL><DEDENT><DEDENT>return combine(segments, combinators)<EOL>


Initialize authentication protocol; establish parameters.


raise http.exceptions.Forbidden()<EOL>

Callback that is invoked when after a user is determined to
be unauthenticated.


return None<EOL>

Gets the a user if they are authenticated; else None.

        @retval False    Unable to authenticate.
        @retval None     Able to authenticate but failed.
        @retval <user>   User object representing the current user.


try:<EOL><INDENT>self.serialize(data)<EOL>return True<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>

Tests this serializer to see if it can serialize.


def is_accessible(self, user, method, resource):

return True<EOL>

Determines the accessibility to a resource endpoint for a particular
method. An inaccessible resource is indistinguishable from a
non-existant resource.

@param[in] user
    The user in question that is being checked.

@param[in] method
    The method in question that is being performed (eg. 'GET').

@param[in] resource
    The resource instance that is being authorized.

@returns
    Returns true if the user can access the resource for
    the passed operation; otherwise, false.


def filter(self, user, operation, resource, iterable):

return iterable<EOL>

Filters an iterable to contain only the items for which the user
is authorized to perform the operation on.

@param[in] user
    The user in question that is being checked.

@param[in] operation
    The operation in question that is being performed (eg. 'read').

@param[in] resource
    The resource instance that is being authorized.

@param[in] iterable
    The iterable of objects to be checked. This method is called
    from the model connector so the actual value of this parameter
    depends on the model connector (eg. it may be a django queryset).

@returns
    Returns an iterable containing the remaining objects.


raise http.exceptions.Forbidden()<EOL>

Informs the client that it is not authrozied for the resource.


raise http.exceptions.Forbidden()<EOL>

Informs the client that the resource is inaccessible.


return self.headers.get('<STR_LIT>') or '<STR_LIT:127.0.0.1>'<EOL>

Retrieves the hostname, normally from the `Host` header.


return tuple(sequence._headers.get(name, '<STR_LIT>').split('<STR_LIT:U+002C>'))<EOL>

Returns the passed header as a tuple.

        Implements a facade so that the response headers can override
        this to provide a mutable sequence header.


return self.headers.getlist(name)<EOL>

Retrieves a the multi-valued list of the header with
the passed name.


return self._sequence[name]<EOL>

Retrieves the passed header as a tuple of its values.


def read(self, deserialize=False, format=None):

if deserialize:<EOL><INDENT>data, _ = self.deserialize(format=format)<EOL>return data<EOL><DEDENT>content = self._read()<EOL>if not content:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if type(content) is six.binary_type:<EOL><INDENT>content = content.decode(self.encoding)<EOL><DEDENT>return content<EOL>

Read and return the request data.

        @param[in] deserialize
            True to deserialize the resultant text using a determiend format
            or the passed format.

        @param[in] format
            A specific format to deserialize in; if provided, no detection is
            done. If not provided, the content-type header is looked at to
            determine an appropriate deserializer.


raise NotImplementedError()<EOL>

Returns the complete URI of the request.


return self.headers[name]<EOL>

Retrieves a header with the passed name.


return None<EOL>

Read and return the request data.

        @note Connectors should override this method.


return self.headers.values()<EOL>


return self.headers.reverse(name)<EOL>

Reverse the elements of the list, in place.


Removes a header with the passed name.

        @param[in] name
            The case-insensitive name of the header to remove
            from the response.


return self.headers.getlist(name)<EOL>

Retrieves the passed header as a sequence of its values.


return self._sequence[name].remove(value)<EOL>

Remove the first item with the passed value from the
list for the named header.


return self.headers.index(name, value)<EOL>

Return the index in the list of the first item whose value is x in
the values of the named header.


raise NotImplementedError()<EOL>


self.require_not_closed()<EOL>if self.streaming:<EOL><INDENT>raise exceptions.InvalidOperation('<STR_LIT>')<EOL><DEDENT>

Raises an exception if the response is not open.


def write(self, chunk, serialize=False, format=None):

<EOL>self.require_not_closed()<EOL>if chunk is None:<EOL><INDENT>return<EOL><DEDENT>if serialize or format is not None:<EOL><INDENT>self.serialize(chunk, format=format)<EOL>return  <EOL><DEDENT>if type(chunk) is six.binary_type:<EOL><INDENT>self._length += len(chunk)<EOL>self._stream.write(chunk)<EOL><DEDENT>elif isinstance(chunk, six.string_types):<EOL><INDENT>encoding = self.encoding<EOL>if encoding is not None:<EOL><INDENT>chunk = chunk.encode(encoding)<EOL><DEDENT>else:<EOL><INDENT>raise exceptions.InvalidOperation(<EOL>'<STR_LIT>')<EOL><DEDENT>self._length += len(chunk)<EOL>self._stream.write(chunk)<EOL><DEDENT>elif isinstance(chunk, collections.Iterable):<EOL><INDENT>for section in chunk:<EOL><INDENT>self.write(section)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise exceptions.InvalidOperation(<EOL>'<STR_LIT>')<EOL><DEDENT>

Writes the given chunk to the output buffer.

        @param[in] chunk
            Either a byte array, a unicode string, or a generator. If `chunk`
            is a generator then calling `self.write(<generator>)` is
            equivalent to:

            @code
                for x in <generator>:
                    self.write(x)
                    self.flush()
            @endcode

        @param[in] serialize
            True to serialize the lines in a determined serializer.

        @param[in] format
            A specific format to serialize in; if provided, no detection is
            done. If not provided, the accept header (as well as the URL
            extension) is looked at to determine an appropriate serializer.


del self.headers<EOL>


<EOL>self.require_not_closed()<EOL>if not self.streaming or self.asynchronous:<EOL><INDENT>if '<STR_LIT>' not in self.headers:<EOL><INDENT>self.headers['<STR_LIT>'] = self.tell()<EOL><DEDENT><DEDENT>self.flush()<EOL>self._closed = True<EOL>

Flush and close the stream.

        This is called automatically by the base resource on resources
        unless the resource is operating asynchronously; in that case,
        this method MUST be called in order to signal the end of the request.
        If not the request will simply hang as it is waiting for some
        thread to tell it to return to the client.


return self._length<EOL>


return self._resource.serialize(data, response=self, format=format)<EOL>

Serializes the data into this response using a serializer.

        @param[in] data
            The data to be serialized.

        @param[in] format
            A specific format to serialize in; if provided, no detection is
            done. If not provided, the accept header (as well as the URL
            extension) is looked at to determine an appropriate serializer.

        @returns
            A tuple of the serialized text and an instance of the
            serializer used.


self.headers[name] = value<EOL>


return not self._closed<EOL>


self.write(*args, **kwargs)<EOL>self.flush()<EOL>

Writes the passed chunk and flushes it to the client.


self._body = value<EOL>

Sets the response body to the passed value.

        @note
            During asynchronous or streaming responses, remember that
            the `body` property refers to the portion of the response *not*
            sent to the client.


value = value.strip()<EOL>value = re.sub(r'<STR_LIT>', r'<STR_LIT>', value)<EOL>value = re.sub(r'<STR_LIT>', r'<STR_LIT:->', value)<EOL>value = re.sub(r'<STR_LIT>', r'<STR_LIT>', value)<EOL>value = value.lower()<EOL>return value<EOL>


try:<EOL><INDENT>return importlib.import_module(name)<EOL><DEDENT>except ImportError:<EOL><INDENT>return None<EOL><DEDENT>

Attempt to import a module; returns None if unsuccessful.


if isinstance(value, collections.Mapping):<EOL><INDENT>if collection is None:<EOL><INDENT>collection = {}<EOL><DEDENT>collection.update(**value)<EOL><DEDENT>elif isinstance(value, six.string_types):<EOL><INDENT>if collection is None:<EOL><INDENT>collection = []<EOL><DEDENT>collection.append(value)<EOL><DEDENT>elif isinstance(value, collections.Iterable):<EOL><INDENT>if collection is None:<EOL><INDENT>collection = []<EOL><DEDENT>collection.extend(value)<EOL><DEDENT>else:<EOL><INDENT>if collection is None:<EOL><INDENT>collection = []<EOL><DEDENT>collection.append(value)<EOL><DEDENT>return collection<EOL>


specifiers = "<STR_LIT>".join(specifiers.split())<EOL>for specifier in specifiers.split('<STR_LIT:U+002C>'):<EOL><INDENT>if len(specifier) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>count = specifier.count('<STR_LIT:->')<EOL>if (count and specifier[<NUM_LIT:0>] == '<STR_LIT:->') or not count:<EOL><INDENT>yield int(specifier), int(specifier)<EOL>continue<EOL><DEDENT>specifier = list(map(int, specifier.split('<STR_LIT:->')))<EOL>if len(specifier) == <NUM_LIT:2>:<EOL><INDENT>if specifier[<NUM_LIT:0>] < <NUM_LIT:0> or specifier[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if specifier[<NUM_LIT:1>] < specifier[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>")<EOL><DEDENT>yield tuple(specifier)<EOL>continue<EOL><DEDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

Consumes set specifiers as text and forms a generator to retrieve
the requested ranges.

@param[in] specifiers
    Expected syntax is from the byte-range-specifier ABNF found in the
    [RFC 2616]; eg. 15-17,151,-16,26-278,15

@returns
    Consecutive tuples that describe the requested range; eg. (1, 72) or
    (1, 1) [read as 1 to 72 or 1 to 1].


if not self._set:<EOL><INDENT>return<EOL><DEDENT>if self.path is None:<EOL><INDENT>self.set = lambda *a: None<EOL>return None<EOL><DEDENT>if self._segments[target.__class__]:<EOL><INDENT>self.get(target)<EOL><DEDENT>if self._segments[target.__class__]:<EOL><INDENT>return<EOL><DEDENT>parent_getter = compose(*self._getters[target.__class__][:-<NUM_LIT:1>])<EOL>target = parent_getter(target)<EOL>func = self._make_setter(self.path.split('<STR_LIT:.>')[-<NUM_LIT:1>], target.__class__)<EOL>func(target, value)<EOL>def setter(target, value):<EOL><INDENT>func(parent_getter(target), value)<EOL><DEDENT>self.set = setter<EOL>

Set the value of this attribute for the passed object.


<EOL>return value<EOL>

Prepare the value for serialization and presentation to the client.


def inner(function):<EOL><INDENT>name = kwargs.pop('<STR_LIT:name>', None)<EOL>if name is None:<EOL><INDENT>name = utils.dasherize(function.__name__)<EOL><DEDENT>methods = kwargs.pop('<STR_LIT>', None)<EOL>if isinstance(methods, six.string_types):<EOL><INDENT>methods = methods,<EOL><DEDENT>handler = (function, methods)<EOL>if name not in _resources:<EOL><INDENT>_handlers[name] = []<EOL>from armet import resources<EOL>kwargs['<STR_LIT:name>'] = name<EOL>class LightweightResource(resources.Resource):<EOL><INDENT>Meta = type(str('<STR_LIT:Meta>'), (), kwargs)<EOL>def route(self, request, response):<EOL><INDENT>for handler, methods in _handlers[name]:<EOL><INDENT>if methods is None or request.method in methods:<EOL><INDENT>return handler(request, response)<EOL><DEDENT><DEDENT>resources.Resource.route(self)<EOL><DEDENT><DEDENT>_resources[name] = LightweightResource<EOL><DEDENT>_handlers[name].append(handler)<EOL>return _resources[name]<EOL><DEDENT>return inner<EOL>

Wraps the decorated function in a lightweight resource.


if self.slug is not None:<EOL><INDENT>return self.meta.detail_allowed_operations<EOL><DEDENT>return self.meta.list_allowed_operations<EOL>

Retrieves the allowed operations for this request.


<EOL>self.assert_operations('<STR_LIT>')<EOL>items = self.read()<EOL>if not items:<EOL><INDENT>raise http.exceptions.NotFound()<EOL><DEDENT>if (isinstance(items, Iterable)<EOL>and not isinstance(items, six.string_types)) and items:<EOL><INDENT>items = pagination.paginate(self.request, self.response, items)<EOL><DEDENT>self.make_response(items)<EOL>


<EOL>for resource, pattern in cls.meta.patterns:<EOL><INDENT>match = re.match(pattern, path)<EOL>if match is not None:<EOL><INDENT>return resource, match.groupdict(), match.string[match.end():]<EOL><DEDENT><DEDENT>return None if not cls.meta.patterns else False<EOL>

Parses out parameters and separates them out of the path.

        This uses one of the many defined patterns on the options class. But,
        it defaults to a no-op if there are no defined patterns.


@classmethod<EOL><INDENT>def traverse(cls, request, params=None):<DEDENT>

<EOL>result = cls.parse(request.path)<EOL>if result is None:<EOL><INDENT>return cls, {}<EOL><DEDENT>elif not result:<EOL><INDENT>raise http.exceptions.NotFound()<EOL><DEDENT>resource, data, rest = result<EOL>if params:<EOL><INDENT>data.update(params)<EOL><DEDENT>if resource is None:<EOL><INDENT>return cls, data<EOL><DEDENT>if data.get('<STR_LIT:path>') is not None:<EOL><INDENT>request.path = data.pop('<STR_LIT:path>')<EOL><DEDENT>elif rest is not None:<EOL><INDENT>request.path = rest<EOL><DEDENT>result = resource.traverse(request, params=data)<EOL>return result<EOL>

Traverses down the path and determines the accessed resource.

        This makes use of the patterns array to implement simple traversal.
        This defaults to a no-op if there are no defined patterns.


@classmethod<EOL><INDENT>def redirect(cls, request, response):<DEDENT>

if cls.meta.legacy_redirect:<EOL><INDENT>if request.method in ('<STR_LIT:GET>', '<STR_LIT>',):<EOL><INDENT>response.status = http.client.MOVED_PERMANENTLY<EOL><DEDENT>else:<EOL><INDENT>response.status = http.client.TEMPORARY_REDIRECT<EOL><DEDENT><DEDENT>else:<EOL><INDENT>response.status = http.client.PERMANENT_REDIRECT<EOL><DEDENT>response.close()<EOL>

Redirect to the canonical URI for this resource.


<EOL>response['<STR_LIT>'] = '<STR_LIT:U+002CU+0020>'.join(self.meta.http_allowed_methods)<EOL>response.status = http.client.OK<EOL>

Process an `OPTIONS` request.

        Used to initiate a cross-origin request. All handling specific to
        CORS requests is done on every request however this method also
        returns a list of available methods.


str_list = [random.choice(string.digits + string.ascii_letters) for i in range(length)]<EOL>return '<STR_LIT>'.join(str_list)<EOL>

Generate random string with parameter length.
Example:

    >>> from eggit.egg_string import random_string
    >>> random_string(8)
    'q4f2eaT4'
    >>>


try:<EOL><INDENT>dft = DTFormat()<EOL>return datetime.strptime(datetime_str, dft.datetime_format)<EOL><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT>

Get datetime object from datetime string

example:
    DateTimeUtils.get_datetime_object('2018-01-01 00:00:00')

:param str string: datetime string
:return: datetime object
:rtype: datetime


if isinstance(timestamp, (int, float, str)):<EOL><INDENT>try:<EOL><INDENT>timestamp = float(timestamp)<EOL>if timestamp.is_integer():<EOL><INDENT>timestamp = int(timestamp)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT>temp = str(timestamp).split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>if len(temp) == <NUM_LIT>:<EOL><INDENT>timestamp = timestamp / <NUM_LIT><EOL><DEDENT>if len(temp) < <NUM_LIT:10>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>return datetime.fromtimestamp(timestamp)<EOL>

1514736000 --> datetime object

:param int timestamp: unix timestamp (int)
:return: datetime object or None
:rtype: datetime or None


try:<EOL><INDENT>dtf = DTFormat()<EOL>struct_time = time.strptime(datetime_str, dtf.datetime_format)<EOL>return time.mktime(struct_time)<EOL><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT>

'2018-01-01 00:00:00' (str) --> 1514736000

:param str datetime_str: datetime string
:return: unix timestamp (int) or None
:rtype: int or None


return DateTimeUtils.get_datetime_string(DateTimeUtils.timestamp_to_datetime(timestamp))<EOL>

1514736000 --> '2018-01-01 00:00:00' (str)

:param int timestamp: unix timestamp
:return: datetime str
:rtype: str


if isinstance(datetime_obj, datetime):<EOL><INDENT>dft = DTFormat()<EOL>return datetime_obj.strftime(dft.datetime_format)<EOL><DEDENT>return None<EOL>

Get datetime string from datetime object

:param datetime datetime_obj: datetime object
:return: datetime string
:rtype: str


def __init__(self, current_page, total_page_count, items, total_item_count, page_size=<NUM_LIT:10>):

self.current_page = current_page<EOL>self.total_page_count = total_page_count<EOL>self.items = items<EOL>self.total_item_count = total_item_count<EOL>self.page_size = page_size<EOL>

:param int current_page: Current page number
:param int total_page_count: Total page count
:param object items: Paging data
:param int total_item_count: Total item count
:param int page_size: How many items per page


with open(path, '<STR_LIT:r>') as f:<EOL><INDENT>_cookies = {}<EOL>for row in f.read().split('<STR_LIT:;>'):<EOL><INDENT>k, v = row.strip().split('<STR_LIT:=>', <NUM_LIT:1>)<EOL>_cookies[k] = v<EOL><DEDENT>return _cookies<EOL><DEDENT>

将 cookie 字符串转化为字典

:param path: cookies 文件路径
:return: cookies 字典


if os.path.exists(directory):<EOL><INDENT>if os.path.isdir(directory):<EOL><INDENT>for d in os.listdir(directory):<EOL><INDENT>path = os.path.join(directory, d)<EOL>if os.path.isdir(path):<EOL><INDENT>delete_empty_dir(path)<EOL><DEDENT><DEDENT><DEDENT>if not os.listdir(directory):<EOL><INDENT>os.rmdir(directory)<EOL>print("<STR_LIT>" + directory)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>


@contextmanager<EOL>def memoryit_block(group_by='<STR_LIT>', limit=<NUM_LIT:10>, label='<STR_LIT>'):

tracemalloc.start()<EOL>_start = tracemalloc.take_snapshot()<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>_end = tracemalloc.take_snapshot()<EOL>stats = _end.compare_to(_start, group_by)<EOL>_print(stats, limit, label)<EOL><DEDENT>

追踪代码块内存消耗情况

:param group_by: 统计分组，有 'filename', 'lineno', 'traceback' 可选
:param limit: 限制输出行数
:param label: 代码块标签


def wrapper(func):<EOL><INDENT>@wraps(func)<EOL>def inner(*args, **kwargs):<EOL><INDENT>start = time.time()<EOL>_result = func(*args, **kwargs)<EOL>_format(unit, time.time() - start, func.__name__ + '<STR_LIT>')<EOL>return _result<EOL><DEDENT>return inner<EOL><DEDENT>return wrapper<EOL>

测试函数耗时

:param unit: 时间单位，有 's','m','h' 可选（seconds，minutes，hours）


return '<STR_LIT>'.format(string)<EOL>


start = time.time()<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>_format(unit, time.time() - start, label)<EOL><DEDENT>

测试代码块耗时

:param unit: 时间单位，有 's','m','h' 可选（seconds，minutes，hours）
:param label: 代码块标签


def loads(s, separator=DEFAULT, index_separator=DEFAULT, cls=dict, list_cls=list):

if isinstance(s, six.text_type):<EOL><INDENT>io = StringIO(s)<EOL><DEDENT>else:<EOL><INDENT>io = BytesIO(s)<EOL><DEDENT>return load(<EOL>fp=io,<EOL>separator=separator,<EOL>index_separator=index_separator,<EOL>cls=cls,<EOL>list_cls=list_cls,<EOL>)<EOL>

Loads an object from a string.

    :param s: An object to parse
    :type s: bytes or str
    :param separator: The separator between key and value.  Defaults to u'|' or b'|', depending on the types.
    :param index_separator: The separator between key and index.  Defaults to u'_' or b'_', depending on the types.
    :param cls: A callable that returns a Mapping that is filled with pairs.  The most common alternate option would be OrderedDict.
    :param list_cls: A callable that takes an iterable and returns a sequence.


def load(fp, separator=DEFAULT, index_separator=DEFAULT, cls=dict, list_cls=list):

converter = None<EOL>output = cls()<EOL>arraykeys = set()<EOL>for line in fp:<EOL><INDENT>if converter is None:<EOL><INDENT>if isinstance(line, six.text_type):<EOL><INDENT>converter = six.u<EOL><DEDENT>else:<EOL><INDENT>converter = six.b<EOL><DEDENT>default_separator = converter('<STR_LIT:|>')<EOL>default_index_separator = converter('<STR_LIT:_>')<EOL>newline = converter('<STR_LIT:\n>')<EOL>if separator is DEFAULT:<EOL><INDENT>separator = default_separator<EOL><DEDENT>if index_separator is DEFAULT:<EOL><INDENT>index_separator = default_index_separator<EOL><DEDENT><DEDENT>key, value = line.strip().split(separator, <NUM_LIT:1>)<EOL>keyparts = key.split(index_separator)<EOL>try:<EOL><INDENT>index = int(keyparts[-<NUM_LIT:1>])<EOL>endwithint = True<EOL><DEDENT>except ValueError:<EOL><INDENT>endwithint = False<EOL><DEDENT>if len(keyparts) > <NUM_LIT:1> and endwithint:<EOL><INDENT>basekey = key.rsplit(index_separator, <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>if basekey not in arraykeys:<EOL><INDENT>arraykeys.add(basekey)<EOL><DEDENT>if basekey in output:<EOL><INDENT>if not isinstance(output[basekey], dict):<EOL><INDENT>output[basekey] = {-<NUM_LIT:1>: output[basekey]}<EOL><DEDENT><DEDENT>else:<EOL><INDENT>output[basekey] = {}<EOL><DEDENT>output[basekey][index] = value<EOL><DEDENT>else:<EOL><INDENT>if key in output and isinstance(output[key], dict):<EOL><INDENT>output[key][-<NUM_LIT:1>] = value<EOL><DEDENT>else:<EOL><INDENT>output[key] = value<EOL><DEDENT><DEDENT><DEDENT>for key in arraykeys:<EOL><INDENT>output[key] = list_cls(pair[<NUM_LIT:1>] for pair in sorted(six.iteritems(output[key])))<EOL><DEDENT>return output<EOL>

Load an object from the file pointer.

    :param fp: A readable filehandle.
    :param separator: The separator between key and value.  Defaults to u'|' or b'|', depending on the types.
    :param index_separator: The separator between key and index.  Defaults to u'_' or b'_', depending on the types.
    :param cls: A callable that returns a Mapping that is filled with pairs.  The most common alternate option would be OrderedDict.
    :param list_cls: A callable that takes an iterable and returns a sequence.


def dump(obj, fp, startindex=<NUM_LIT:1>, separator=DEFAULT, index_separator=DEFAULT):

if startindex < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(startindex))<EOL><DEDENT>try:<EOL><INDENT>firstkey = next(iter(obj.keys()))<EOL><DEDENT>except StopIteration:<EOL><INDENT>return<EOL><DEDENT>if isinstance(firstkey, six.text_type):<EOL><INDENT>converter = six.u<EOL><DEDENT>else:<EOL><INDENT>converter = six.b<EOL><DEDENT>default_separator = converter('<STR_LIT:|>')<EOL>default_index_separator = converter('<STR_LIT:_>')<EOL>newline = converter('<STR_LIT:\n>')<EOL>if separator is DEFAULT:<EOL><INDENT>separator = default_separator<EOL><DEDENT>if index_separator is DEFAULT:<EOL><INDENT>index_separator = default_index_separator<EOL><DEDENT>for key, value in six.iteritems(obj):<EOL><INDENT>if isinstance(value, (list, tuple, set)):<EOL><INDENT>for index, item in enumerate(value, start=startindex):<EOL><INDENT>fp.write(key)<EOL>fp.write(index_separator)<EOL>fp.write(converter(str(index)))<EOL>fp.write(separator)<EOL>fp.write(item)<EOL>fp.write(newline)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fp.write(key)<EOL>fp.write(separator)<EOL>fp.write(value)<EOL>fp.write(newline)<EOL><DEDENT><DEDENT>

Dump an object in req format to the fp given.

    :param Mapping obj: The object to serialize.  Must have a keys method.
    :param fp: A writable that can accept all the types given.
    :param separator: The separator between key and value.  Defaults to u'|' or b'|', depending on the types.
    :param index_separator: The separator between key and index.  Defaults to u'_' or b'_', depending on the types.


encoded = urlsafe_b64encode(data)<EOL>return encoded.replace(b'<STR_LIT:=>', b'<STR_LIT>')<EOL>

:param data: Data the encode.
:type data: bytes
:return: Base 64 encoded data with padding removed.
:rtype: bytes


def compare_token(expected: Union[str, bytes],<EOL>actual: Union[str, bytes]) -> bool:

expected = util.to_bytes(expected)<EOL>actual = util.to_bytes(actual)<EOL>_, expected_sig_seg = expected.rsplit(b'<STR_LIT:.>', <NUM_LIT:1>)<EOL>_, actual_sig_seg = actual.rsplit(b'<STR_LIT:.>', <NUM_LIT:1>)<EOL>expected_sig = util.b64_decode(expected_sig_seg)<EOL>actual_sig = util.b64_decode(actual_sig_seg)<EOL>return compare_signature(expected_sig, actual_sig)<EOL>

Compares the given tokens.

:param expected: The expected token.
:type expected: Union[str, bytes]
:param actual: The actual token.
:type actual: Union[str, bytes]
:return: Do the tokens match?
:rtype: bool


def get_algorithm(alg: str) -> Callable:

if alg not in algorithms:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(alg))<EOL><DEDENT>return algorithms[alg]<EOL>

:param alg: The name of the requested `JSON Web Algorithm <https://tools.ietf.org/html/rfc7519#ref-JWA>`_. `RFC7518 <https://tools.ietf.org/html/rfc7518#section-3.2>`_ is related.
:type alg: str
:return: The requested algorithm.
:rtype: Callable
:raises: ValueError


@property<EOL><INDENT>def valid_to(self) -> Union[int, None]:<DEDENT>

return self.registered_claims.get('<STR_LIT>')<EOL>

:return: Expires (`exp`) claim from the token.
:rtype: Union[int, None]


@property<EOL><INDENT>def issued_at(self) -> Union[int, None]:<DEDENT>

:return: Issued at (`iat`) claim from the token.
:rtype: Union[int, None]


claims_in_payload = [k for k in self.payload.keys() if<EOL>k in registered_claims.values()]<EOL>for name in claims_in_payload:<EOL><INDENT>self.registered_claims[name] = self.payload.pop(name)<EOL><DEDENT>

Check for registered claims in the payload and move them to the
registered_claims property, overwriting any extant claims.


def compare(self, jwt: '<STR_LIT>', compare_dates: bool = False) -> bool:

if self.secret != jwt.secret:<EOL><INDENT>return False<EOL><DEDENT>if self.payload != jwt.payload:<EOL><INDENT>return False<EOL><DEDENT>if self.alg != jwt.alg:<EOL><INDENT>return False<EOL><DEDENT>if self.header != jwt.header:<EOL><INDENT>return False<EOL><DEDENT>expected_claims = self.registered_claims<EOL>actual_claims = jwt.registered_claims<EOL>if not compare_dates:<EOL><INDENT>strip = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>expected_claims = {k: {v if k not in strip else None} for k, v in<EOL>expected_claims.items()}<EOL>actual_claims = {k: {v if k not in strip else None} for k, v in<EOL>actual_claims.items()}<EOL><DEDENT>if expected_claims != actual_claims:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Compare against another `Jwt`.

:param jwt: The token to compare against.
:type jwt: Jwt
:param compare_dates: Should the comparision take dates into account?
:type compare_dates: bool
:return: Are the two Jwt's the same?
:rtype: bool


@property<EOL><INDENT>def subject(self) -> Union[str, None]:<DEDENT>

:return: Subject (`sub`) claim from the token.
:rtype: Union[str, None]


def valid(self, time: int = None) -> bool:

if time is None:<EOL><INDENT>epoch = datetime(<NUM_LIT>, <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>)<EOL>now = datetime.utcnow()<EOL>time = int((now - epoch).total_seconds())<EOL><DEDENT>if isinstance(self.valid_from, int) and time < self.valid_from:<EOL><INDENT>return False<EOL><DEDENT>if isinstance(self.valid_to, int) and time > self.valid_to:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Is the token valid? This method only checks the timestamps within the
token and compares them against the current time if none is provided.

:param time: The timestamp to validate against
:type time: Union[int, None]
:return: The validity of the token.
:rtype: bool


@issuer.setter<EOL><INDENT>def issuer(self, issuer: str):<DEDENT>

self.registered_claims['<STR_LIT>'] = issuer<EOL>

Sets the issuer (`iss`) claim in the token.

:param issuer: New value.
:type issuer: str


def encode(secret: Union[str, bytes], payload: dict = None,<EOL>alg: str = default_alg, header: dict = None) -> str:

secret = util.to_bytes(secret)<EOL>payload = payload or {}<EOL>header = header or {}<EOL>header_json = util.to_bytes(json.dumps(header))<EOL>header_b64 = util.b64_encode(header_json)<EOL>payload_json = util.to_bytes(json.dumps(payload))<EOL>payload_b64 = util.b64_encode(payload_json)<EOL>pre_signature = util.join(header_b64, payload_b64)<EOL>signature = _hash(secret, pre_signature, alg)<EOL>signature_b64 = util.b64_encode(signature)<EOL>token = util.join(pre_signature, signature_b64)<EOL>return util.from_bytes(token)<EOL>

:param secret: The secret used to encode the token.
:type secret: Union[str, bytes]
:param payload: The payload to be encoded in the token.
:type payload: dict
:param alg: The algorithm used to hash the token.
:type alg: str
:param header: The header to be encoded in the token.
:type header: dict
:return: A new token
:rtype: str


@header.setter<EOL><INDENT>def header(self, header: dict):<DEDENT>

self._header = header<EOL>

Sets the token header.

:param header: New header
:type header: dict


def compare_signature(expected: Union[str, bytes],<EOL>actual: Union[str, bytes]) -> bool:

expected = util.to_bytes(expected)<EOL>actual = util.to_bytes(actual)<EOL>return hmac.compare_digest(expected, actual)<EOL>

Compares the given signatures.

:param expected: The expected signature.
:type expected: Union[str, bytes]
:param actual: The actual signature.
:type actual: Union[str, bytes]
:return: Do the signatures match?
:rtype: bool


def __common_triplet(input_string, consonants, vowels):

output = consonants<EOL>while len(output) < <NUM_LIT:3>:<EOL><INDENT>try:<EOL><INDENT>output += vowels.pop(<NUM_LIT:0>)<EOL><DEDENT>except IndexError:<EOL><INDENT>output += '<STR_LIT:X>'<EOL><DEDENT><DEDENT>return output[:<NUM_LIT:3>]<EOL>

__common_triplet(input_string, consonants, vowels) -> string


consonants, vowels = __consonants_and_vowels(input_string)<EOL>return __common_triplet(input_string, consonants, vowels)<EOL>


def build(surname, name, birthday, sex, municipality):

<EOL>output = __surname_triplet(surname) + __name_triplet(name)<EOL>output += str(birthday.year)[<NUM_LIT:2>:]<EOL>output += MONTHSCODE[birthday.month - <NUM_LIT:1>]<EOL>output += "<STR_LIT>" % (sex.upper() == '<STR_LIT:M>' and birthday.day or <NUM_LIT> + birthday.day)<EOL>output += municipality<EOL>output += control_code(output)<EOL>assert isvalid(output)<EOL>return output<EOL>

``build(surname, name, birthday, sex, municipality) -> string``

    Computes the fiscal code for the given person data.

    eg: build('Rocca', 'Emanuele', datetime.datetime(1983, 11, 18), 'M', 'D969')
        -> RCCMNL83S18D969H


assert isvalid(code)<EOL>return int(code[<NUM_LIT:9>:<NUM_LIT:11>]) < <NUM_LIT:32> and '<STR_LIT:M>' or '<STR_LIT:F>'<EOL>

``get_sex(code) -> string``

    The sex of the person whose fiscal code is 'code'.

    eg: sex('RCCMNL83S18D969H') -> 'M'
        sex('CNTCHR83T41D969D') -> 'F'


x = X<EOL>if not isinstance(X, list):<EOL><INDENT>x = [X]<EOL><DEDENT>y = self.estimator.predict(x)<EOL>y = [item[<NUM_LIT:0>] for item in y]<EOL>y = [self._remove_prefix(label) for label in y]<EOL>if not isinstance(X, list):<EOL><INDENT>y = y[<NUM_LIT:0>]<EOL><DEDENT>return y<EOL>

In order to obtain the most likely label for a list of text

        Parameters
        ----------
        X : list of string
            Raw texts

        Returns
        -------
        C : list of string
            List labels


def fit(self, X, y, model_filename=None):

train_file = "<STR_LIT>"<EOL>X = [x.replace("<STR_LIT:\n>", "<STR_LIT:U+0020>") for x in X]<EOL>y = [_.replace("<STR_LIT:U+0020>", "<STR_LIT:->") for _ in y]<EOL>lines = ["<STR_LIT>".format(self.prefix, j, i) for i, j in zip(X, y)]<EOL>content = "<STR_LIT:\n>".join(lines)<EOL>write(train_file, Text(content))<EOL>if model_filename:<EOL><INDENT>self.estimator = ft.supervised(train_file, model_filename)<EOL><DEDENT>else:<EOL><INDENT>self.estimator = ft.supervised(train_file, '<STR_LIT>')<EOL>os.remove('<STR_LIT>')<EOL><DEDENT>os.remove(train_file)<EOL>

Fit FastText according to X, y

        Parameters
        ----------
        X : list of string
            each item is a raw text
        y : list of string
            each item is a label


<EOL>word_vector_transformer = WordVectorTransformer(padding='<STR_LIT>')<EOL>X = word_vector_transformer.fit_transform(X)<EOL>X = LongTensor(X)<EOL>self.word_vector_transformer = word_vector_transformer<EOL>y_transformer = LabelEncoder()<EOL>y = y_transformer.fit_transform(y)<EOL>y = torch.from_numpy(y)<EOL>self.y_transformer = y_transformer<EOL>dataset = CategorizedDataset(X, y)<EOL>dataloader = DataLoader(dataset,<EOL>batch_size=self.batch_size,<EOL>shuffle=True,<EOL>num_workers=<NUM_LIT:4>)<EOL>KERNEL_SIZES = self.kernel_sizes<EOL>NUM_KERNEL = self.num_kernel<EOL>EMBEDDING_DIM = self.embedding_dim<EOL>model = TextCNN(<EOL>vocab_size=word_vector_transformer.get_vocab_size(),<EOL>embedding_dim=EMBEDDING_DIM,<EOL>output_size=len(self.y_transformer.classes_),<EOL>kernel_sizes=KERNEL_SIZES,<EOL>num_kernel=NUM_KERNEL)<EOL>if USE_CUDA:<EOL><INDENT>model = model.cuda()<EOL><DEDENT>EPOCH = self.epoch<EOL>LR = self.lr<EOL>loss_function = nn.CrossEntropyLoss()<EOL>optimizer = optim.Adam(model.parameters(), lr=LR)<EOL>for epoch in range(EPOCH):<EOL><INDENT>losses = []<EOL>for i, data in enumerate(dataloader):<EOL><INDENT>X, y = data<EOL>X, y = Variable(X), Variable(y)<EOL>optimizer.zero_grad()<EOL>model.train()<EOL>output = model(X)<EOL>loss = loss_function(output, y)<EOL>losses.append(loss.data.tolist()[<NUM_LIT:0>])<EOL>loss.backward()<EOL>optimizer.step()<EOL>if i % <NUM_LIT:100> == <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (<EOL>epoch, EPOCH, np.mean(losses)))<EOL>losses = []<EOL><DEDENT><DEDENT><DEDENT>self.model = model<EOL>

Fit KimCNNClassifier according to X, y

        Parameters
        ----------
        X : list of string
            each item is a raw text
        y : list of string
            each item is a label


if isinstance(X[<NUM_LIT:0>], list):<EOL><INDENT>return [self.estimator.tag(x) for x in X]<EOL><DEDENT>return self.estimator.tag(X)<EOL>

Predict class labels for samples in X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Samples.


file = join(model_folder, binary_file)<EOL>vectorizer = joblib.load(file)<EOL>output = []<EOL>for token in vectorizer.vocabulary_:<EOL><INDENT>index = vectorizer.vocabulary_[token]<EOL>ngram = len(token.split("<STR_LIT:U+0020>"))<EOL>output.append({<EOL>"<STR_LIT>": token,<EOL>"<STR_LIT>": ngram,<EOL>"<STR_LIT>": vectorizer.idf_[index],<EOL>"<STR_LIT>": vectorizer.period_[index].item(),<EOL>"<STR_LIT>": vectorizer.df_[index],<EOL>})<EOL><DEDENT>output = sorted(output, key=lambda item: item["<STR_LIT>"])<EOL>content = json.dumps(output, ensure_ascii=False)<EOL>write(join(log_folder, "<STR_LIT>"), content)<EOL>

Parameters
----------
model_folder : string
    folder contains binaries file of model
binary_file : string
    file path to tfidf binary file
log_folder : string
    log folder


def print_cm(cm, labels, hide_zeroes=False, hide_diagonal=False, hide_threshold=None):

columnwidth = max([len(x) for x in labels] + [<NUM_LIT:5>])  <EOL>empty_cell = "<STR_LIT:U+0020>" * columnwidth<EOL>print("<STR_LIT:U+0020>" + empty_cell, end="<STR_LIT:U+0020>")<EOL>for label in labels:<EOL><INDENT>print("<STR_LIT>".format(columnwidth) % label, end="<STR_LIT:U+0020>")<EOL><DEDENT>print()<EOL>for i, label1 in enumerate(labels):<EOL><INDENT>print("<STR_LIT>".format(columnwidth) % label1, end="<STR_LIT:U+0020>")<EOL>for j in range(len(labels)):<EOL><INDENT>cell = "<STR_LIT>".format(columnwidth) % cm[i, j]<EOL>if hide_zeroes:<EOL><INDENT>cell = cell if float(cm[i, j]) != <NUM_LIT:0> else empty_cell<EOL><DEDENT>if hide_diagonal:<EOL><INDENT>cell = cell if i != j else empty_cell<EOL><DEDENT>if hide_threshold:<EOL><INDENT>cell = cell if cm[i, j] > hide_threshold else empty_cell<EOL><DEDENT>print(cell, end="<STR_LIT:U+0020>")<EOL><DEDENT>print()<EOL><DEDENT>


self.transformers.append(transformer)<EOL>from languageflow.transformer.tagged import TaggedTransformer<EOL>if isinstance(transformer, TaggedTransformer):<EOL><INDENT>self.X, self.y = transformer.transform(self.sentences)<EOL><DEDENT>if isinstance(transformer, TfidfVectorizer):<EOL><INDENT>self.X = transformer.fit_transform(self.X)<EOL><DEDENT>if isinstance(transformer, CountVectorizer):<EOL><INDENT>self.X = transformer.fit_transform(self.X)<EOL><DEDENT>if isinstance(transformer, NumberRemover):<EOL><INDENT>self.X = transformer.transform(self.X)<EOL><DEDENT>if isinstance(transformer, MultiLabelBinarizer):<EOL><INDENT>self.y = transformer.fit_transform(self.y)<EOL><DEDENT>

Add transformer to flow and apply transformer to data in flow

Parameters
----------
transformer : Transformer
    a transformer to transform data


self.models.append(model)<EOL>


data_folder = join(dirname(dirname(__file__)), "<STR_LIT:data>")<EOL>data_file = join(data_folder, filepath)<EOL>self.data_file = data_file<EOL>self.words_data = None<EOL>

load words from Ho Ngoc Duc's dictionary

        :param str filepath: filename of dictionary data
        :type filepath: str