Mercurial > projects > dil
comparison src/translator/project.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/project.py@1401e38d1e2e |
children |
comparison
equal
deleted
inserted
replaced
805:a3fab8b74a7d | 806:bcb74c9b895c |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # Author: Aziz Köksal | |
3 # License: GPL2 | |
4 import os | |
5 from errors import LoadingError | |
6 import langfile | |
7 import datetime | |
8 import yaml | |
9 | |
10 def newProjectData(projectName): | |
11 return { | |
12 "Name":projectName, | |
13 "LangFiles":[], | |
14 "SourceLangFile":'', | |
15 "MsgIDs":[], | |
16 "CreationDate":str(datetime.datetime.utcnow()), | |
17 "BuildScript":'' | |
18 } | |
19 | |
20 class Project: | |
21 # Members: | |
22 # name | |
23 # source | |
24 # langFilePaths | |
25 # langFile | |
26 # msgids | |
27 # creationDate | |
28 def __init__(self, projectPath): | |
29 self.projectPath = projectPath | |
30 # Load project file and check data integrity. | |
31 try: | |
32 self.doc = yaml.load(open(projectPath, "r")) | |
33 except yaml.YAMLError, e: | |
34 raise LoadingError(str(e)) | |
35 self.verify() | |
36 | |
37 def verify(self): | |
38 doc = self.doc | |
39 self.checkType(doc, dict) | |
40 try: | |
41 self.name = unicode(doc["Name"]) | |
42 self.srcLangFilePath = str(doc["SourceLangFile"]) | |
43 self.langFilePaths = list(doc["LangFiles"]) | |
44 self.msgIDs = list(doc["MsgIDs"]) | |
45 self.creationDate = str(doc["CreationDate"]) | |
46 self.buildScript = str(doc["BuildScript"]) | |
47 except KeyError, e: | |
48 raise LoadingError("Missing member '%s' in '%s'" % (e.message, projectPath)) | |
49 | |
50 for path in self.langFilePaths: | |
51 self.checkType(path, str) | |
52 | |
53 msgIDs = [] | |
54 for msg in self.msgIDs: | |
55 self.checkType(msg, dict) | |
56 try: | |
57 msg["ID"] = int(msg["ID"]) | |
58 msg["Name"] = unicode(msg["Name"]) | |
59 msg["Order"] = int(msg["Order"]) | |
60 except KeyError, e: | |
61 raise LoadingError("Project: a message is missing the '%s' key." % str(e)) | |
62 msgIDs += [msg] | |
63 self.msgIDs = msgIDs | |
64 | |
65 # Load language files. | |
66 self.langFiles = [] | |
67 IDList = [msg["ID"] for msgID in self.msgIDs] | |
68 for filePath in self.langFilePaths: | |
69 langFile = self.loadLangFile(filePath) | |
70 langFile.createMissingMessages(IDList) | |
71 self.langFiles += [langFile] | |
72 self.srcLangFile = self.loadLangFile(self.srcLangFilePath) | |
73 self.srcLangFile.createMissingMessages(IDList) | |
74 self.srcLangFile.isSource = True | |
75 self.langFiles += [self.srcLangFile] | |
76 | |
77 for langFile in self.langFiles: | |
78 langFile.setSource(self.srcLangFile) | |
79 | |
80 def checkType(self, var, type_): | |
81 if not isinstance(var, type_): | |
82 raise LoadingException("%s is not of type %s" % (str(var), str(type_))) | |
83 | |
84 def loadLangFile(self, filePath): | |
85 if not os.path.exists(filePath): | |
86 # Look in project directory. | |
87 projectDir = os.path.dirname(self.projectPath) | |
88 filePath2 = os.path.join(projectDir, filePath) | |
89 if not os.path.exists(filePath2): | |
90 raise LoadingError("Project: Language file '%s' doesn't exist"%filePath) | |
91 filePath = filePath2 | |
92 return langfile.LangFile(filePath) | |
93 | |
94 def setName(self, name): | |
95 self.name = name | |
96 | |
97 def setBuildScript(self, filePath): | |
98 self.buildScript = filePath | |
99 | |
100 def setCreationDate(self, date): | |
101 self.creationDate = date | |
102 | |
103 def save(self): | |
104 self.doc["Name"] = self.name | |
105 self.doc["BuildScript"] = self.buildScript | |
106 self.doc["CreationDate"] = self.creationDate | |
107 file_ = open(self.projectPath, "w") | |
108 yaml.dump(self.doc, stream=file_, allow_unicode=True) | |
109 file_.close() |