コード例 #1
0
 def test_to_unicode_exception(self):
     unicode_s = u"❤ ☀ ☆ ☂"
     e = Exception(unicode_s)
     self.assertEqual(to_unicode(e), unicode_s)
     unicode_s = u"éà"
     e = Exception(unicode_s.encode('latin-1'))
     self.assertEqual(to_unicode(e), unicode_s)
コード例 #2
0
 def test_to_unicode_exception(self):
     unicode_s = u"❤ ☀ ☆ ☂"
     e = Exception(unicode_s)
     self.assertEqual(to_unicode(e), unicode_s)
     unicode_s = u"éà"
     e = Exception(unicode_s.encode('latin-1'))
     self.assertEqual(to_unicode(e), unicode_s)
コード例 #3
0
def handle_board_cards(me, board_id, push_api, token, prev_result, logger):
    """ Function template to generate a trello board's cards fetch from its
    ID. The docido_sdk compliant task should be created with functools.partial
    with a trello obtained board id.

    :param str board_id: the boards' to fetch members IDs
    :param push_api: The IndexAPI to use
    :param token: an OauthToken object
    :param prev_results: Previous tasks results
    :param logger: A logging.logger instance
    """
    # prev result is not used but needed to work with docido SDK
    # pylint: disable=unused-argument
    logger.info("fetching cards for board: {}".format(board_id))
    current_gen = get_last_gen(push_api) + 1
    trello = create_trello_client(token)
    docido_cards = []
    params = dict(
        actions="createCard,commentCard,copyCard,convertToCardFromCheckItem",
        attachments="true",
        attachment_fields="all",
        members="true",
        member_fields="all",
        checklists="all",
    )
    url_attachment_label = u"View {kind} {name} on Trello"

    board_lists = {l["id"]: l["name"] for l in trello.list_board_lists(board_id, fields="name")}

    trello_cards = trello.list_board_cards(board_id, **params)
    for card in trello_cards:
        actions = {}
        for action in card["actions"]:
            actions.setdefault(action["type"], []).append(action)
        if not any(actions.get(CREATE_CARD_ACTION, [])):
            # no card creation event, author cannot get inferred
            continue
        create_card_a = actions[CREATE_CARD_ACTION][0]
        full_text = StringIO()
        with closing(full_text):
            writer = codecs.StreamReaderWriter(full_text, UTF8_CODEC.streamreader, UTF8_CODEC.streamwriter)
            writer.write(card["desc"])
            for checklist in card.get("checklists", []):
                writer.write("\n\n### ")
                writer.write(checklist["name"])
                writer.write("\n")
                for checkItem in checklist.get("checkItems", []):
                    if checkItem["state"] == "complete":
                        writer.write("\n* [x] ")
                    else:
                        writer.write("\n* [ ] ")
                    writer.write(checkItem["name"])
            description = to_unicode(full_text.getvalue())

        author = create_card_a["memberCreator"]
        author_id = create_card_a["idMemberCreator"]
        author_thumbnail = thumbnail_from_avatar_hash(author.get("avatarHash"))
        labels = [l["name"] for l in card["labels"]]
        labels = filter(lambda l: any(l), labels)
        try:
            embed = markdown.markdown(description, extensions=["markdown_checklist.extension"])
        except:
            embed = None
        docido_card = {
            "attachments": [
                {"type": u"link", "url": card["shortUrl"], "_analysis": False, "title": "View card on Trello"}
            ],
            "id": card["id"],
            "title": card["name"],
            "private": dict(sync_id=current_gen),
            "description": description,
            "embed": embed,
            "date": date_to_timestamp(card["dateLastActivity"]),
            "favorited": card["subscribed"],
            "created_at": date_to_timestamp(create_card_a["date"]),
            "author": {"name": author["fullName"], "username": author["username"], "thumbnail": author_thumbnail},
            "labels": labels,
            "group_name": board_lists[card["idList"]],
            "flags": "closed" if card.get("closed", False) else "open",
            "kind": u"note",
        }
        if author_id == me["id"]:
            docido_card["private"]["twitter_id"] = 1
        else:
            is_member = False
            for member in card.get("members", []):
                if member["id"] == me["id"]:
                    is_member = True
                    break
            if is_member:
                docido_card["private"]["twitter_id"] = 0

        for kind, link in create_card_a.get("data", {}).iteritems():
            if kind != "card" and "shortLink" in link:
                docido_card["attachments"].append(
                    dict(
                        type=u"link",
                        _analysis=False,
                        url="https://trello.com/{kind}/{url}".format(kind=kind, url=link["shortLink"]),
                        title=url_attachment_label.format(kind=kind, name=link["name"]),
                    )
                )
                if kind == "board":
                    docido_card["attachments"].append(dict(type=u"notebook", name=link["name"]))

        docido_card["attachments"].extend(
            [
                {
                    "type": u"file",
                    "origin_id": a["id"],
                    "title": a["name"],
                    "url": a["url"],
                    "date": date_to_timestamp(a["date"]),
                    "size": a["bytes"],
                    "preview": pick_preview(a["previews"]),
                    "mime_type": pick_mime_type(a),
                    "filetype": pick_filetype(a),
                }
                for a in card["attachments"]
            ]
        )
        docido_card["attachments"].extend([dict(type=u"tag", name=label) for label in labels])
        docido_card["to"] = [
            {"name": m["fullName"], "username": m["username"], "thumbnail": thumbnail_from_avatar_hash(m["avatarHash"])}
            for m in card["members"]
        ]
        for comment in reversed(actions.get(COMMENT_CARD_ACTION, [])):
            creator = comment.get("memberCreator", {})
            thumbnail = thumbnail_from_avatar_hash(creator.get("avatarHash"))
            text = comment.get("data", {}).get("text")
            if text is not None:
                try:
                    html_text = markdown.markdown(text, extensions=["markdown_checklist.extension"])
                except:
                    html_text = None
            else:
                html_text = text
            docido_card.setdefault("comments", []).append(
                dict(
                    text=text,
                    embed=html_text,
                    date=timestamp_ms.feeling_lucky(comment["date"]),
                    author=dict(name=creator.get("fullName"), username=creator.get("username"), thumbnail=thumbnail),
                )
            )
        docido_card["comments_count"] = len(docido_card.get("comments", []))
        docido_cards.append(docido_card)
    logger.info("indexing {} cards for board: {}".format(len(docido_cards), board_id))
    push_api.push_cards(docido_cards)
コード例 #4
0
 def _validate_unicode_conversion(self, unicode_s, charset):
     charset_s = unicode_s.encode(charset)
     self.assertEqual(unicode_s, to_unicode(charset_s, charset))
     self.assertEqual(unicode_s, to_unicode(charset_s))
     self.assertEqual(unicode_s, to_unicode(Exception(charset_s)))
コード例 #5
0
 def test_to_unicode_invalid_charset(self):
     unicode_s = u"❤ ☀ ☆ ☂"
     utf_8_s = unicode_s.encode('utf-8')
     with self.assertRaises(LookupError):
         to_unicode(utf_8_s, 'latin-42')
コード例 #6
0
 def _validate_unicode_conversion(self, unicode_s, charset):
     charset_s = unicode_s.encode(charset)
     self.assertEqual(unicode_s, to_unicode(charset_s, charset))
     self.assertEqual(unicode_s, to_unicode(charset_s))
     self.assertEqual(unicode_s, to_unicode(Exception(charset_s)))
コード例 #7
0
 def test_to_unicode_invalid_charset(self):
     unicode_s = u"❤ ☀ ☆ ☂"
     utf_8_s = unicode_s.encode('utf-8')
     with self.assertRaises(LookupError):
         to_unicode(utf_8_s, 'latin-42')