示例#1
0
 def test_gmail(self):
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER_FIXED_TZ),
                      KIOTO_TIMESTAMP_MS)
     # timezone information will be deleted
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER_SPURIOUS_TZ),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER_GMT),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER_QUOTED_GMT),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.from_str(KIOTO_IMAP_HEADER_QUOTED_0000),
                      KIOTO_TIMESTAMP_MS)
     # tz is such a mess it is not removed, so dateutil raises
     with self.assertRaises(ValueError):
         timestamp_ms.feeling_lucky(KIOTO_IMAP_HEADER_SPURIOUS2_TZ)
示例#2
0
 def test_bengali_timezone(self):
     self.assertEqual(
         timestamp_ms.feeling_lucky(NEW_YEAR_BENGALI),
         NEW_YEAR_BENGALI_TS
     )
     self.assertEqual(
         timestamp_ms.feeling_lucky(NEW_YEAR_BENGALI_DASH),
         NEW_YEAR_BENGALI_TS
     )
     self.assertEqual(
         timestamp_ms.feeling_lucky(NEW_YEAR_BENGALI_DOT),
         NEW_YEAR_BENGALI_TS
     )
     self.assertEqual(
         timestamp_ms.feeling_lucky(NEW_YEAR_BENGALI_EXTRA_PARENT),
         NEW_YEAR_BENGALI_TS
     )
 def test_feeling_lucky(self):
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_ISO_8601),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_ISO_8601_TZ),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_RFC_2822),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_RFC_3339),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_DATETIME),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_IMAP_HEADER),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(
         timestamp_ms.feeling_lucky(KIOTO_IMAP_HEADER_FIXED_TZ),
         KIOTO_TIMESTAMP_MS
     )
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_TIMESTAMP_SEC),
                      KIOTO_TIMESTAMP_MS)
     with self.assertRaises(Exception):
         timestamp_ms.feeling_lucky(Exception('invalid obj'))
示例#4
0
 def test_feeling_lucky(self):
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_ISO_8601),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_ISO_8601_TZ),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_RFC_2822),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_RFC_3339),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_DATETIME),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_IMAP_HEADER),
                      KIOTO_TIMESTAMP_MS)
     self.assertEqual(
         timestamp_ms.feeling_lucky(KIOTO_IMAP_HEADER_FIXED_TZ),
         KIOTO_TIMESTAMP_MS
     )
     self.assertEqual(timestamp_ms.feeling_lucky(KIOTO_TIMESTAMP_SEC),
                      KIOTO_TIMESTAMP_MS)
     with self.assertRaises(Exception):
         timestamp_ms.feeling_lucky(Exception('invalid obj'))
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)