Mercurial > projects > dil
annotate trunk/src/translator/langfile.py @ 480:f25985f6b222
Translator: catalogues can be saved to disk now.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Mon, 12 Nov 2007 20:40:41 +0100 |
parents | 3b5421d40f9b |
children | 312da78ab301 |
rev | line source |
---|---|
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
2 # Author: Aziz Köksal |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
3 # License: GPL2 |
439
cdbb2bf6dd07
Translator: applied some fixes and made some additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
438
diff
changeset
|
4 import yaml |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
5 from errors import LoadingError |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
6 |
480
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
7 # Avoid that all unicode strings are tagged with "!!python/unicode". |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
8 def unicode_representer(dumper, data): |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
9 return dumper.represent_scalar(u'tag:yaml.org,2002:str', data) |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
10 yaml.add_representer(unicode, unicode_representer) |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
11 |
460
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
12 def newLangFile(langCode, authors, license): |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
13 return { |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
14 "LangCode":langCode, |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
15 "Authors":authors, |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
16 "License":license, |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
17 "Messages":[] |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
18 } |
77c17bbe9d20
Translator: added new modules; applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
442
diff
changeset
|
19 |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
20 class LangFile: |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
21 def __init__(self, filePath): |
477
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
22 from os import path |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
23 self.filePath = path.abspath(filePath) |
472
30925590f392
Translator: implemented more functionalities of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
471
diff
changeset
|
24 self.isSource = False |
30925590f392
Translator: implemented more functionalities of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
471
diff
changeset
|
25 self.source = None |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
26 # Load language file and check data integrity. |
477
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
27 try: |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
28 self.doc = yaml.load(open(filePath, "r")) |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
29 except yaml.YAMLError, e: |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
30 raise LoadingError(str(e)) |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
31 self.verify() |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
32 |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
33 def verify(self): |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
34 doc = self.doc |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
35 self.checkType(doc, dict) |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
36 try: |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
37 self.langCode = str(doc["LangCode"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
38 self.authors = list(doc["Authors"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
39 self.license = unicode(doc["License"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
40 self.messages = list(doc["Messages"]) |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
41 except KeyError, e: |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
42 raise LoadingError("Missing member '%s' in '%s'" % (e.message, filePath)) |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
43 |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
44 authors = [] |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
45 for author in self.authors: |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
46 author = list(author) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
47 author_len = len(author) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
48 if author_len == 0: |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
49 pass |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
50 elif author_len == 1: |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
51 authors += [unicode(author[0]), ""] |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
52 else: |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
53 authors += [unicode(author[0]), unicode(author[1])] |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
54 self.authors = authors |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
55 |
471
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
56 self.msgDict = {} # {ID : msg, ...} |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
57 for msg in self.messages: |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
58 self.checkType(msg, dict, "LangFile: messages must be of type dict.") |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
59 try: |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
60 msg["ID"] = int(msg["ID"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
61 msg["Text"] = unicode(msg["Text"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
62 msg["Annot"] = unicode(msg["Annot"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
63 msg["LastEd"] = unicode(msg["LastEd"]) |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
64 except KeyError, e: |
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
65 raise LoadingError("LangFile: a message is missing the '%s' key." % str(e)) |
471
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
66 self.msgDict[msg["ID"]] = msg |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
67 |
439
cdbb2bf6dd07
Translator: applied some fixes and made some additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
438
diff
changeset
|
68 def checkType(self, var, type_, msg=""): |
438
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
69 if not isinstance(var, type_): |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
70 raise LoadingError(msg) |
2c177053cd15
Translator: added modules langfile and project.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
71 |
472
30925590f392
Translator: implemented more functionalities of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
471
diff
changeset
|
72 def setSource(self, sourceLangFile): |
30925590f392
Translator: implemented more functionalities of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
471
diff
changeset
|
73 self.source = sourceLangFile |
30925590f392
Translator: implemented more functionalities of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
471
diff
changeset
|
74 |
471
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
75 def getMsg(self, ID): |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
76 for msg in self.messages: |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
77 if msg["ID"] == ID: |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
78 return msg |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
79 return None |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
80 |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
81 def createMissingMessages(self, IDs): |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
82 for ID in IDs: |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
83 if not self.msgDict.has_key(ID): |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
84 msg = self.createEmptyMsg(ID) |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
85 self.msgDict[ID] = msg |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
86 self.messages += [msg] |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
87 |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
88 def createEmptyMsg(self, ID): |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
89 return {"ID":ID,"Text":"","Annot":"","LastEd":""} |
b6fa41e6f0b4
Translator: implemented some functionality of MsgForm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
460
diff
changeset
|
90 |
442
5968e273449b
Translator: added new modules; made fixes and many additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
439
diff
changeset
|
91 def save(self): |
480
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
92 langFile = open(self.filePath, "w") |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
93 yaml.dump(self.doc, stream=langFile, allow_unicode=True) |
f25985f6b222
Translator: catalogues can be saved to disk now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
477
diff
changeset
|
94 langFile.close() |
477
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
95 |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
96 def getFileName(self): |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
97 from os import path |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
98 return path.basename(self.filePath) |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
99 |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
100 def getFilePath(self): |
3b5421d40f9b
Translator: major changes and additions.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
472
diff
changeset
|
101 return self.filePath |