1 | #!/usr/bin/python |
---|
2 | # |
---|
3 | # Import custom smileys into Pidgin from Emesene 1.0. |
---|
4 | # Requires Python 2.5 or later. |
---|
5 | # |
---|
6 | # Copyright (C) 2009, Danilo Piazzalunga <danilopiazza@gmail.com> |
---|
7 | # This file is distributed under the same license as the Pidgin package. |
---|
8 | # |
---|
9 | # Usage: |
---|
10 | # pidgin-import-emesene-smileys.py EMESENE_EMOTICON_MAP [PURPLE_USER_DIR] |
---|
11 | # |
---|
12 | # EMESENE_EMOTICON_MAP: the custom_emoticons/map file in your emesene |
---|
13 | # account directory, to be used as the input file |
---|
14 | # For example, $HOME/.config/emesene1.0/*/custom_emoticons/map. |
---|
15 | # |
---|
16 | # PURPLE_USER_DIR: the libpurple user directory, to be used as output |
---|
17 | # directory. Defaults to $HOME/.purple. |
---|
18 | # |
---|
19 | |
---|
20 | from __future__ import with_statement |
---|
21 | |
---|
22 | import hashlib |
---|
23 | import os.path |
---|
24 | import shutil |
---|
25 | import sys |
---|
26 | import xml.dom.minidom |
---|
27 | |
---|
28 | def usage(): |
---|
29 | return ("usage: %s EMESENE_EMOTICON_MAP [PURPLE_USER_DIR]" % sys.argv[0]) |
---|
30 | |
---|
31 | def purple_user_dir(): |
---|
32 | for dir in [ "$PURPLEHOME", "%APPDATA%", "$HOME", "~" ]: |
---|
33 | purple_dir = os.path.join( |
---|
34 | os.path.expandvars(os.path.expanduser(dir)), |
---|
35 | ".purple") |
---|
36 | if os.path.isdir(purple_dir): |
---|
37 | return purple_dir |
---|
38 | |
---|
39 | def open_cmdline_arg(filename, input=True): |
---|
40 | if filename == "-": |
---|
41 | if input: return sys.stdin |
---|
42 | else: return sys.stdout |
---|
43 | else: |
---|
44 | if input: return open(filename, "U") |
---|
45 | else: return open(filename, "w") |
---|
46 | |
---|
47 | def parse_emesene_map(map): |
---|
48 | smileys = {} |
---|
49 | |
---|
50 | while True: |
---|
51 | shortcut = map.readline().rstrip("\n") |
---|
52 | if not shortcut: break |
---|
53 | |
---|
54 | picture_name = map.readline().rstrip("\n") |
---|
55 | smileys[shortcut] = picture_name |
---|
56 | |
---|
57 | return smileys |
---|
58 | |
---|
59 | def sha1sum(filename): |
---|
60 | with open(filename, "rb") as file: |
---|
61 | data = file.read() |
---|
62 | |
---|
63 | sha1sum = hashlib.sha1() |
---|
64 | sha1sum.update(data) |
---|
65 | return sha1sum.hexdigest() |
---|
66 | |
---|
67 | def import_smileys(smileys, purple_dir): |
---|
68 | purple_custom_smiley_dir = os.path.join(purple_dir, "custom_smiley") |
---|
69 | purple_smileys_xml = os.path.join(purple_dir, "smileys.xml") |
---|
70 | |
---|
71 | if not os.path.isdir(purple_custom_smiley_dir): |
---|
72 | os.mkdir(purple_custom_smiley_dir) |
---|
73 | |
---|
74 | smileys_purple = [] |
---|
75 | |
---|
76 | for shortcut, picture_name in smileys.iteritems(): |
---|
77 | checksum = sha1sum(picture_name) |
---|
78 | ext = os.path.splitext(picture_name)[1] |
---|
79 | filename = "".join([checksum, ext]) |
---|
80 | |
---|
81 | shutil.copyfile(picture_name, |
---|
82 | os.path.join(purple_custom_smiley_dir, filename)) |
---|
83 | |
---|
84 | smileys_purple.append({"shortcut": shortcut, |
---|
85 | "checksum": checksum, |
---|
86 | "filename": filename }) |
---|
87 | |
---|
88 | write_purple_smileys_xml(smileys_purple, purple_smileys_xml) |
---|
89 | |
---|
90 | def write_purple_smileys_xml(smileys_purple, purple_smileys_xml): |
---|
91 | dom = xml.dom.minidom.getDOMImplementation() |
---|
92 | doc = dom.createDocument(None, "smileys", None) |
---|
93 | |
---|
94 | try: |
---|
95 | top_element = doc.documentElement |
---|
96 | top_element.setAttribute("version", "1.0") |
---|
97 | |
---|
98 | profile_element = doc.createElement("profile") |
---|
99 | profile_element.setAttribute("name", "Default") |
---|
100 | top_element.appendChild(profile_element) |
---|
101 | |
---|
102 | smiley_set_element = doc.createElement("smiley_set") |
---|
103 | profile_element.appendChild(smiley_set_element) |
---|
104 | |
---|
105 | for s in smileys_purple: |
---|
106 | smiley_element = doc.createElement("smiley") |
---|
107 | smiley_element.setAttribute("shortcut", s["shortcut"]) |
---|
108 | smiley_element.setAttribute("checksum", s["checksum"]) |
---|
109 | smiley_element.setAttribute("filename", s["filename"]) |
---|
110 | smiley_set_element.appendChild(smiley_element) |
---|
111 | |
---|
112 | with open(purple_smileys_xml, "w") as writer: |
---|
113 | doc.writexml(writer, addindent="\t", newl="\n") |
---|
114 | finally: |
---|
115 | doc.unlink() |
---|
116 | |
---|
117 | if __name__ == "__main__": |
---|
118 | if len(sys.argv) < 2 or len(sys.argv) > 3: |
---|
119 | sys.exit(usage()) |
---|
120 | |
---|
121 | input_filename = sys.argv[1] |
---|
122 | purple_dir = sys.argv[2] if len(sys.argv) > 2 else purple_user_dir() |
---|
123 | |
---|
124 | with open_cmdline_arg(input_filename) as map: |
---|
125 | smileys = parse_emesene_map(map) |
---|
126 | |
---|
127 | import_smileys(smileys, purple_dir) |
---|
128 | |
---|