Skip to content

Instantly share code, notes, and snippets.

@xfguo
Created May 21, 2014 07:23
Show Gist options
  • Save xfguo/3395fe6c1b954a7efe26 to your computer and use it in GitHub Desktop.
Save xfguo/3395fe6c1b954a7efe26 to your computer and use it in GitHub Desktop.
Replace #N to Card URL for Trello
from trello import *
import re
from pprint import pprint
from difflib import *
client = TrelloClient(
'YOUR_KEY',
'YOUR_TOKEN',
)
boards = client.list_boards()
diff = Differ()
cards_short_urls = dict()
for board in boards:
if board.name == 'YOUR_BOARD_NAME':
all_cards = board.all_cards()
for card in all_cards:
card.fetch()
print "Card #%s feched" % card.short_id
cards_short_urls[card.short_id] = card.url
print "v" * 80
print "MAPPING TABLE"
pprint (cards_short_urls)
print "^" * 80
## replace shord_id in card items
for card in all_cards:
for checklist in card.checklists:
for checkitem in checklist.items:
item_name = checkitem['name']
new_name = item_name
ref_cards = re.findall(r"[^\n]#(\d+)", item_name)
ref_cards = list(set(ref_cards))
for ref_card in ref_cards:
if cards_short_urls.has_key(int(ref_card)):
print "*", ref_card, ">> ", cards_short_urls[int(ref_card)]
new_name = re.sub(r"[^\n]#%s" % ref_card, " %s " % cards_short_urls[int(ref_card)], new_name)
checklist.rename_checklist_item(item_name, new_name)
## replace shord_id in card items
for card in all_cards:
print card.name, "*" * 80
desc = card.description
orig_desc = desc
ref_cards = re.findall(r"[^\n]#(\d+)", desc)
ref_cards = list(set(ref_cards))
for ref_card in ref_cards:
if cards_short_urls.has_key(int(ref_card)):
print "*", ref_card, ">> ", cards_short_urls[int(ref_card)]
desc = re.sub(r"[^\n]#%s" % ref_card, " %s " % cards_short_urls[int(ref_card)], desc)
diff_result = \
filter(
lambda l:l.startswith('+') or l.startswith('-'),
list(diff.compare(desc.splitlines(1), orig_desc.splitlines(1))
)
)
print "-" * 80
pprint(diff_result)
card.set_description(desc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment