changeset 438:2c177053cd15

Translator: added modules langfile and project.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Fri, 12 Oct 2007 22:16:21 +0200
parents 7ac9f94ca7ff
children cdbb2bf6dd07
files trunk/src/translator/langfile.py trunk/src/translator/project.py trunk/src/translator/translator.py trunk/src/translator/translator.ui trunk/src/translator/ui_translator.py
diffstat 5 files changed, 136 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/translator/langfile.py	Fri Oct 12 22:16:21 2007 +0200
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+# Author: Aziz Köksal
+# License: GPL2
+import exceptions
+
+class LangFile:
+  class LoadingError(exceptions.Exception):
+    def __init__(self, msg):
+      self.msg = msg
+      return
+    def __str__(self):
+      return self.msg
+
+  def __init__(self, filePath):
+    # Load language file and check data integrity.
+    doc = yaml.load(open(filePath, "r"))
+    self.doc = doc
+    self.checkType(doc, dict)
+    try:
+      self.langCode = doc["LangCode"]
+      self.authors = doc["Authors"]
+      self.license = doc["License"]
+      self.messages = doc["Messages"]
+    except KeyError, e:
+      raise LoadingError("Missing member '%s' in '%s'" % (e.message, filePath))
+
+    self.checkType(self.langCode, dict)
+    self.checkType(self.authors, list)
+    for author in self.authors:
+      self.checkType(author, list)
+      if len(author) != 2:
+        raise LoadingError("")
+      self.checkType(author[0], str)
+      self.checkType(author[1], str)
+    self.checkType(self.license, str)
+    self.checkType(self.messages, list)
+    for msg in self.messages:
+      self.checkType(msg, dict)
+      if not msg.has_key("ID") or \
+         not msg.has_key("Text") or \
+         not msg.has_key("Annotation") or \
+         not msg.has_key("LastEdited"):
+        raise LoadingError("")
+
+  def checkType(var, type_, msg=""):
+    if not isinstance(var, type_):
+      raise LoadingError(msg)
+
+  def newLangFile(langCode, authors, license):
+    return {
+      "LangCode":langCode,
+      "Authors":authors,
+      "License":license,
+      "Messages":[]
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/translator/project.py	Fri Oct 12 22:16:21 2007 +0200
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+# Author: Aziz Köksal
+# License: GPL2
+import langfile
+import datetime
+import exceptions
+
+class Project:
+  class LoadingError(exceptions.Exception):
+    def __init__(self, msg):
+      self.msg = msg
+      return
+    def __str__(self):
+      return self.msg
+  # Members:
+  # name
+  # source
+  # langFilePaths
+  # langFile
+  # msgids
+  # creationDate
+  def __init__(self, projectPath):
+    # Load project file and check data integrity.
+    doc = yaml.load(open(projectPath, "r"))
+    self.doc = doc
+    self.checkType(doc, dict)
+    try:
+      self.name = doc["Name"]
+      self.source = doc["SourceLangFile"]
+      self.langFilePaths = doc["LangFiles"]
+      self.msgids = doc["MsgIDs"]
+      self.creationDate = doc["CreationDate"]
+    except KeyError, e:
+      raise LoadingError("Missing member '%s' in '%s'" % (e.message, filePath))
+
+    self.checkType(self.name, str)
+    self.checkType(self.source, str)
+    self.checkType(self.langFilePaths, list)
+    for path in self.langFilesPaths:
+      self.checkType(path, str)
+    self.checkType(self.msgIDs, list)
+    for msg in self.msgIDs:
+      if not isinstance(msg, dict) or \
+         not msg.has_key("ID")     or \
+         not msg.has_key("Name")   or \
+         not msg.has_key("Order"):
+        raise LoadingError("")
+    self.checkType(self.creationDate, str)
+
+    # Load language files.
+    self.langFiles = []
+    for filePath in self.langFilePaths:
+      self.langFiles += LangFile(filePath)
+
+  def checkType(var, type_):
+    if not isinstance(var, type_):
+      raise LoadingException("%s is not of type %s" % (str(var), str(type_)))
+
+  def newProjectData(projectName):
+    return {
+      "Name":projectName,
+      "LangFiles":[],
+      "SourceLangFile":'',
+      "MsgIDs":[],
+      "CreationDate":str(datetime.datetime.utcnow())
+    }
--- a/trunk/src/translator/translator.py	Fri Oct 12 11:12:10 2007 +0200
+++ b/trunk/src/translator/translator.py	Fri Oct 12 22:16:21 2007 +0200
@@ -4,12 +4,15 @@
 # License: GPL2
 import sys, os
 import yaml
+
 from PyQt4 import QtCore, QtGui
 # User interface modules
 from ui_translator import Ui_MainWindow
 from ui_about import Ui_AboutDialog
 from ui_new_project import Ui_NewProjectDialog
 
+import project
+
 g_scriptDir = sys.path[0]
 g_CWD = os.getcwd()
 g_settingsFile = os.path.join(g_scriptDir, "settings.yaml")
@@ -46,16 +49,15 @@
   def readSettings(self):
     # Set default size
     self.resize(QtCore.QSize(500, 400))
-
+    doc = {}
     try:
       doc = yaml.load(open(g_settingsFile, "r"))
     except:
       self.moveToCenterOfDesktop()
       return
 
-    if isinstance(doc, type({})):
-      g_settings = doc
-    else:
+    g_settings = doc
+    if not isinstance(doc, dict):
       g_settings = {}
 
     try:
@@ -76,6 +78,12 @@
     yaml.dump(g_settings, open(g_settingsFile, "w")) #default_flow_style=False
 
 
+class ProjectTree(QtGui.QTreeWidget)
+  QtGui.QTreeWidget.__init__(self)
+  def __init__(self):
+    pass
+
+
 class NewProjectDialog(QtGui.QDialog, Ui_NewProjectDialog):
   def __init__(self):
     QtGui.QDialog.__init__(self)
@@ -101,12 +109,7 @@
       QtGui.QMessageBox.warning(self, "Warning", "Please, choose or enter a path for the project file.")
       return
 
-    projectData = {
-      "Name":projectName,
-      "LangFiles":[],
-      "SourceLangFile":'',
-      "MsgIDs":[]
-    }
+    projectData = Project.newProjectData(projectName)
 
     if os.path.splitext(filePath)[1] != ".tproj":
       filePath += ".tproj"
--- a/trunk/src/translator/translator.ui	Fri Oct 12 11:12:10 2007 +0200
+++ b/trunk/src/translator/translator.ui	Fri Oct 12 22:16:21 2007 +0200
@@ -13,59 +13,7 @@
    <string>Translator</string>
   </property>
   <widget class="QWidget" name="centralwidget" >
-   <layout class="QVBoxLayout" >
-    <item>
-     <layout class="QVBoxLayout" >
-      <item>
-       <widget class="QSplitter" name="splitter_2" >
-        <property name="orientation" >
-         <enum>Qt::Horizontal</enum>
-        </property>
-        <widget class="QListView" name="listView" />
-        <widget class="QSplitter" name="splitter" >
-         <property name="orientation" >
-          <enum>Qt::Vertical</enum>
-         </property>
-         <widget class="QTextEdit" name="textEdit" >
-          <property name="tabChangesFocus" >
-           <bool>true</bool>
-          </property>
-         </widget>
-         <widget class="QTextEdit" name="textEdit_2" >
-          <property name="tabChangesFocus" >
-           <bool>true</bool>
-          </property>
-         </widget>
-        </widget>
-       </widget>
-      </item>
-      <item>
-       <layout class="QHBoxLayout" >
-        <item>
-         <spacer>
-          <property name="orientation" >
-           <enum>Qt::Horizontal</enum>
-          </property>
-          <property name="sizeHint" >
-           <size>
-            <width>40</width>
-            <height>20</height>
-           </size>
-          </property>
-         </spacer>
-        </item>
-        <item>
-         <widget class="QPushButton" name="pushButton" >
-          <property name="text" >
-           <string>Exit</string>
-          </property>
-         </widget>
-        </item>
-       </layout>
-      </item>
-     </layout>
-    </item>
-   </layout>
+   <layout class="QVBoxLayout" />
   </widget>
   <widget class="QMenuBar" name="menubar" >
    <property name="geometry" >
@@ -141,31 +89,9 @@
    </property>
   </action>
  </widget>
- <tabstops>
-  <tabstop>listView</tabstop>
-  <tabstop>textEdit</tabstop>
-  <tabstop>textEdit_2</tabstop>
-  <tabstop>pushButton</tabstop>
- </tabstops>
  <resources/>
  <connections>
   <connection>
-   <sender>pushButton</sender>
-   <signal>clicked()</signal>
-   <receiver>MainWindow</receiver>
-   <slot>close()</slot>
-   <hints>
-    <hint type="sourcelabel" >
-     <x>596</x>
-     <y>430</y>
-    </hint>
-    <hint type="destinationlabel" >
-     <x>266</x>
-     <y>171</y>
-    </hint>
-   </hints>
-  </connection>
-  <connection>
    <sender>action_Quit</sender>
    <signal>triggered()</signal>
    <receiver>MainWindow</receiver>
--- a/trunk/src/translator/ui_translator.py	Fri Oct 12 11:12:10 2007 +0200
+++ b/trunk/src/translator/ui_translator.py	Fri Oct 12 22:16:21 2007 +0200
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'translator.ui'
 #
-# Created: Fri Oct 12 10:58:49 2007
+# Created: Fri Oct 12 22:15:30 2007
 #      by: PyQt4 UI code generator 4.1
 #
 # WARNING! All changes made in this file will be lost!
@@ -20,41 +20,6 @@
 
         self.vboxlayout = QtGui.QVBoxLayout(self.centralwidget)
         self.vboxlayout.setObjectName("vboxlayout")
-
-        self.vboxlayout1 = QtGui.QVBoxLayout()
-        self.vboxlayout1.setObjectName("vboxlayout1")
-
-        self.splitter_2 = QtGui.QSplitter(self.centralwidget)
-        self.splitter_2.setOrientation(QtCore.Qt.Horizontal)
-        self.splitter_2.setObjectName("splitter_2")
-
-        self.listView = QtGui.QListView(self.splitter_2)
-        self.listView.setObjectName("listView")
-
-        self.splitter = QtGui.QSplitter(self.splitter_2)
-        self.splitter.setOrientation(QtCore.Qt.Vertical)
-        self.splitter.setObjectName("splitter")
-
-        self.textEdit = QtGui.QTextEdit(self.splitter)
-        self.textEdit.setTabChangesFocus(True)
-        self.textEdit.setObjectName("textEdit")
-
-        self.textEdit_2 = QtGui.QTextEdit(self.splitter)
-        self.textEdit_2.setTabChangesFocus(True)
-        self.textEdit_2.setObjectName("textEdit_2")
-        self.vboxlayout1.addWidget(self.splitter_2)
-
-        self.hboxlayout = QtGui.QHBoxLayout()
-        self.hboxlayout.setObjectName("hboxlayout")
-
-        spacerItem = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
-        self.hboxlayout.addItem(spacerItem)
-
-        self.pushButton = QtGui.QPushButton(self.centralwidget)
-        self.pushButton.setObjectName("pushButton")
-        self.hboxlayout.addWidget(self.pushButton)
-        self.vboxlayout1.addLayout(self.hboxlayout)
-        self.vboxlayout.addLayout(self.vboxlayout1)
         MainWindow.setCentralWidget(self.centralwidget)
 
         self.menubar = QtGui.QMenuBar(MainWindow)
@@ -105,16 +70,11 @@
         self.menubar.addAction(self.menu_Help.menuAction())
 
         self.retranslateUi(MainWindow)
-        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),MainWindow.close)
         QtCore.QObject.connect(self.action_Quit,QtCore.SIGNAL("triggered()"),MainWindow.close)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)
-        MainWindow.setTabOrder(self.listView,self.textEdit)
-        MainWindow.setTabOrder(self.textEdit,self.textEdit_2)
-        MainWindow.setTabOrder(self.textEdit_2,self.pushButton)
 
     def retranslateUi(self, MainWindow):
         MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Translator", None, QtGui.QApplication.UnicodeUTF8))
-        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
         self.menu_File.setTitle(QtGui.QApplication.translate("MainWindow", "&File", None, QtGui.QApplication.UnicodeUTF8))
         self.menu_Help.setTitle(QtGui.QApplication.translate("MainWindow", "&Help", None, QtGui.QApplication.UnicodeUTF8))
         self.menu_Project.setTitle(QtGui.QApplication.translate("MainWindow", "&Project", None, QtGui.QApplication.UnicodeUTF8))