Mercurial > projects > dil
comparison src/translator/langfile.py @ 806:bcb74c9b895c
Moved out files in the trunk folder to the root.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 09 Mar 2008 00:12:19 +0100 |
parents | trunk/src/translator/langfile.py@312da78ab301 |
children |
comparison
equal
deleted
inserted
replaced
805:a3fab8b74a7d | 806:bcb74c9b895c |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # Author: Aziz Köksal | |
3 # License: GPL2 | |
4 import yaml | |
5 from errors import LoadingError | |
6 | |
7 # Avoid that all unicode strings are tagged with "!!python/unicode". | |
8 def unicode_representer(dumper, data): | |
9 return dumper.represent_scalar(u'tag:yaml.org,2002:str', data) | |
10 yaml.add_representer(unicode, unicode_representer) | |
11 | |
12 def newLangFile(langCode, authors, license): | |
13 return { | |
14 "LangCode":langCode, | |
15 "Authors":authors, | |
16 "License":license, | |
17 "Messages":[] | |
18 } | |
19 | |
20 class LangFile: | |
21 def __init__(self, filePath): | |
22 from os import path | |
23 self.filePath = path.abspath(filePath) | |
24 self.isSource = False | |
25 self.source = None | |
26 # Load language file and check data integrity. | |
27 try: | |
28 self.doc = yaml.load(open(filePath, "r")) | |
29 except yaml.YAMLError, e: | |
30 raise LoadingError(str(e)) | |
31 self.verify() | |
32 | |
33 def verify(self): | |
34 doc = self.doc | |
35 self.checkType(doc, dict) | |
36 try: | |
37 self.langCode = str(doc["LangCode"]) | |
38 self.authors = list(doc["Authors"]) | |
39 self.license = unicode(doc["License"]) | |
40 self.messages = list(doc["Messages"]) | |
41 except KeyError, e: | |
42 raise LoadingError("Missing member '%s' in '%s'" % (e.message, filePath)) | |
43 | |
44 authors = [] | |
45 for author in self.authors: | |
46 self.checkType(author, dict, "LangFile: author must be of type dict.") | |
47 try: | |
48 author["Name"] = unicode(author["Name"]) | |
49 author["EMail"] = str(author["EMail"]) | |
50 authors += [author] | |
51 except KeyError, e: | |
52 raise LoadingError("Author is missing '%s' in '%s'" % (e.message, filePath)) | |
53 self.authors = authors | |
54 | |
55 self.msgDict = {} # {ID : msg, ...} | |
56 for msg in self.messages: | |
57 self.checkType(msg, dict, "LangFile: messages must be of type dict.") | |
58 try: | |
59 msg["ID"] = int(msg["ID"]) | |
60 msg["Text"] = unicode(msg["Text"]) | |
61 msg["Annot"] = unicode(msg["Annot"]) | |
62 msg["LastEd"] = int(msg["LastEd"]) | |
63 except KeyError, e: | |
64 raise LoadingError("LangFile: a message is missing the '%s' key." % str(e)) | |
65 self.msgDict[msg["ID"]] = msg | |
66 | |
67 def checkType(self, var, type_, msg=""): | |
68 if not isinstance(var, type_): | |
69 raise LoadingError(msg) | |
70 | |
71 def setSource(self, sourceLangFile): | |
72 self.source = sourceLangFile | |
73 | |
74 def getMsg(self, ID): | |
75 for msg in self.messages: | |
76 if msg["ID"] == ID: | |
77 return msg | |
78 return None | |
79 | |
80 def createMissingMessages(self, IDs): | |
81 for ID in IDs: | |
82 if not self.msgDict.has_key(ID): | |
83 msg = self.createEmptyMsg(ID) | |
84 self.msgDict[ID] = msg | |
85 self.messages += [msg] | |
86 | |
87 def createEmptyMsg(self, ID): | |
88 return {"ID":ID,"Text":"","Annot":"","LastEd":""} | |
89 | |
90 def save(self): | |
91 self.doc["LangCode"] = self.langCode | |
92 self.doc["License"] = self.license | |
93 self.doc["Authors"] = self.authors | |
94 langFile = open(self.filePath, "w") | |
95 yaml.dump(self.doc, stream=langFile, allow_unicode=True) | |
96 langFile.close() | |
97 | |
98 def setLangCode(self, langCode): | |
99 self.langCode = langCode | |
100 | |
101 def setLicense(self, license): | |
102 self.license = license | |
103 | |
104 def setAuthors(self, authors): | |
105 self.authors = authors | |
106 | |
107 def getFileName(self): | |
108 from os import path | |
109 return path.basename(self.filePath) | |
110 | |
111 def getFilePath(self): | |
112 return self.filePath |