#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, os, re, shutil from plistlib import * usage = """ usage: emot_port.py emoticons.AdiumEmoticonSet [emoticons.colloquyEmoticons] """ adiumBundlePath = "sa.AdiumEmoticonSet" colloquyBundlePath = "sa.colloquyEmoticons" emoticonCSSCommonFrag = """ .emoticon:after { vertical-align: -40%; } """ emoticonCSSFragTmpl = """ .emoticon.%(safeName)s:after { content: url("%(safeImage)s"); } .emoticon.%(safeName)s samp { display: none; } """ def mkCSSSafeName(existingSafeNames, imgFile): first = '_'.join(re.findall(r"[A-Za-z0-9]+", imgFile)).lower() if first == '': first = 'anon' elif re.match(r"^\d+", first): first = 'num_' if first in existingSafeNames: first += "_dup_" i = 1 while (first + str(i) in existingSafeNames): i += 1 first += str(i) existingSafeNames.add(first) return first def main(): if len(sys.argv) < 2 or len(sys.argv) > 3: print usage sys.exit(1) adiumBundlePath = sys.argv[1] if len(sys.argv) > 2: colloquyBundlePath = sys.argv[2] else: colloquyBundlePath = os.path.splitext(adiumBundlePath)[0] + '.colloquyEmoticons' os.mkdir(colloquyBundlePath) os.mkdir(os.path.join(colloquyBundlePath, 'Contents')) os.mkdir(os.path.join(colloquyBundlePath, 'Contents', 'Resources')) bundleName = os.path.splitext(os.path.split(colloquyBundlePath)[1])[0] colloquyInfoPlist = { 'CFBundlePackageType': 'coEm', 'CFBundleName': bundleName, 'CFBundleIdentifier': 'us.bat-country.emot-port.%s.AdiumEmoticonSet' % bundleName, } writePlist(colloquyInfoPlist, os.path.join(colloquyBundlePath, 'Contents', 'Info.plist')) emoticonCSSF = open(os.path.join(colloquyBundlePath, 'Contents', 'Resources', 'emoticons.css'), 'w') emoticonCSSF.write(emoticonCSSCommonFrag) adiumEmots = readPlist(os.path.join(adiumBundlePath, 'Emoticons.plist'))['Emoticons'] colloquyMenuPlist = [] colloquyEmoticonsPlist = {} safeNames = set() for image, props in adiumEmots.items(): basename, ext = os.path.splitext(image) safeName = mkCSSSafeName(safeNames, basename) safeImage = safeName + ext emoticonCSSF.write(emoticonCSSFragTmpl % vars()) colloquyMenuPlist.append({ 'image': safeImage, 'name': props['Name'], 'insert': props['Equivalents'][0], }) colloquyEmoticonsPlist[safeName] = props['Equivalents'] shutil.copyfile( os.path.join(adiumBundlePath, image), os.path.join(colloquyBundlePath, 'Contents', 'Resources', safeImage)) emoticonCSSF.close() writePlist(colloquyMenuPlist, os.path.join(colloquyBundlePath, 'Contents', 'Resources', 'menu.plist')) writePlist(colloquyEmoticonsPlist, os.path.join(colloquyBundlePath, 'Contents', 'Resources', 'emoticons.plist')) if __name__ == '__main__': main()