[PyKDE] pylupdate4 doesn't work .ui files

Andreas Pakulat apaku at gmx.de
Fri Jun 30 12:58:23 BST 2006


On 30.06.06 13:52:58, Andreas Pakulat wrote:
> On 27.06.06 15:42:25, Andreas Pakulat wrote:
> > Hi,
> > 
> > a pylupdate4 ui/somewidget.ui -ts de.ts doesn't produce any output into
> > the .ts file.
> 
> I created a patch to make this work, it is attached.
> 
> Notes to make:
> 
> The code is copied from lupdate of Qt/X11 open source edition but should
> work on the other versions too. It uses a SAX2 handler that extracts the
> strings.
> 
> I needed to remove the support for inlined functions in the .ui-file,
> but I think that't not supported by pyuic4 anyway.
> 
> I also hardcoded the Ui_-prefix that will be generated by pyuic4 so the
> context is correct (else it would use the classname only as context).
> 
> Hope this makes it into the next PyQt4 along with the fix for pyuic4 to
> generate proper self.tr functions.

Forgot to actually attach the patch.

Andreas

-- 
You have a deep interest in all that is artistic.
-------------- next part --------------
diff -ur PyQt-x11-gpl-4.0/pylupdate/fetchtr.cpp PyQt-x11-gpl-4.0.pylup/pylupdate/fetchtr.cpp
--- PyQt-x11-gpl-4.0/pylupdate/fetchtr.cpp	2006-06-10 14:43:21.000000000 +0200
+++ PyQt-x11-gpl-4.0.pylup/pylupdate/fetchtr.cpp	2006-06-30 13:45:52.000000000 +0200
@@ -46,6 +46,7 @@
 #include <string.h>
 
 #include <QTextCodec>
+#include <QtXml>
 
 static const char MagicComment[] = "TRANSLATOR ";
 
@@ -555,3 +556,137 @@
     parse( tor, 0, defaultContext );
     fclose( yyInFile );
 }
+
+class UiHandler : public QXmlDefaultHandler
+{
+public:
+    UiHandler( MetaTranslator *translator, const char *fileName )
+        : tor( translator ), fname( fileName ), comment( "" ) { }
+
+    virtual bool startElement( const QString& namespaceURI,
+                               const QString& localName, const QString& qName,
+                               const QXmlAttributes& atts );
+    virtual bool endElement( const QString& namespaceURI,
+                             const QString& localName, const QString& qName );
+    virtual bool characters( const QString& ch );
+    virtual bool fatalError( const QXmlParseException& exception );
+
+private:
+    void flush();
+
+    MetaTranslator *tor;
+    QByteArray fname;
+    QString context;
+    QString source;
+    QString comment;
+
+    QString accum;
+
+    bool trString;
+};
+
+bool UiHandler::startElement( const QString& /* namespaceURI */,
+                              const QString& /* localName */,
+                              const QString& qName,
+                              const QXmlAttributes& atts )
+{
+    if ( qName == QString("item") ) {
+        flush();
+        if ( !atts.value(QString("text")).isEmpty() )
+            source = atts.value( QString("text") );
+    } else if ( qName == QString("string") ) {
+        flush();
+        if (atts.value(QString("notr")).isEmpty() ||
+            atts.value(QString("notr")) != QString("true")) {
+            trString = true;
+            comment = atts.value(QString("comment"));
+        } else {
+            trString = false;
+        }
+    }
+    accum.truncate( 0 );
+    return true;
+}
+
+bool UiHandler::endElement( const QString& /* namespaceURI */,
+                            const QString& /* localName */,
+                            const QString& qName )
+{
+    accum.replace( QRegExp(QString("\r\n")), "\n" );
+
+    if ( qName == QString("class") ) {
+        if ( context.isEmpty() )
+            context = QString("Ui_"+accum);
+    } else if ( qName == QString("string") && trString ) {
+        source = accum;
+    } else if ( qName == QString("comment") ) {
+        comment = accum;
+        flush();
+    } else {
+        flush();
+    }
+    return true;
+}
+
+bool UiHandler::characters( const QString& ch )
+{
+    accum += ch;
+    return true;
+}
+
+bool UiHandler::fatalError( const QXmlParseException& exception )
+{
+    QString msg;
+    msg.sprintf( "Parse error at line %d, column %d (%s).",
+                 exception.lineNumber(), exception.columnNumber(),
+                 exception.message().toLatin1().data() );
+    fprintf( stderr, "XML error: %s\n", msg.toLatin1().data() );
+    return false;
+}
+
+void UiHandler::flush()
+{
+    if ( !context.isEmpty() && !source.isEmpty() )
+        tor->insert( MetaTranslatorMessage(context.toUtf8(), source.toUtf8(),
+                                           comment.toUtf8(), QString(),
+                                           true) );
+    source.truncate( 0 );
+    comment.truncate( 0 );
+}
+
+void fetchtr_ui( const char *fileName, MetaTranslator *tor,
+                 const char * /* defaultContext */, bool mustExist )
+{
+    QFile f( fileName );
+    if ( !f.open(QIODevice::ReadOnly) ) {
+		if ( mustExist ) {
+#if defined(_MSC_VER) && _MSC_VER >= 1400
+			char buf[100];
+			strerror_s(buf, sizeof(buf), errno);
+			fprintf( stderr, "lupdate error: cannot open UI file '%s': %s\n",
+                     fileName, buf );
+#else
+            fprintf( stderr, "lupdate error: cannot open UI file '%s': %s\n",
+                     fileName, strerror(errno) );
+#endif
+		}
+        return;
+    }
+
+    QXmlInputSource in( &f );
+    QXmlSimpleReader reader;
+    reader.setFeature( "http://xml.org/sax/features/namespaces", false );
+    reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", true );
+    reader.setFeature( "http://trolltech.com/xml/features/report-whitespace"
+                       "-only-CharData", false );
+    QXmlDefaultHandler *hand = new UiHandler( tor, fileName );
+    reader.setContentHandler( hand );
+    reader.setErrorHandler( hand );
+
+    if ( !reader.parse(in) )
+        fprintf( stderr, "%s: Parse error in UI file\n", fileName );
+    reader.setContentHandler( 0 );
+    reader.setErrorHandler( 0 );
+    delete hand;
+    f.close();
+}
diff -ur PyQt-x11-gpl-4.0/pylupdate/main.cpp PyQt-x11-gpl-4.0.pylup/pylupdate/main.cpp
--- PyQt-x11-gpl-4.0/pylupdate/main.cpp	2006-06-10 14:43:21.000000000 +0200
+++ PyQt-x11-gpl-4.0.pylup/pylupdate/main.cpp	2006-06-30 13:43:15.000000000 +0200
@@ -48,6 +48,9 @@
 // defined in fetchtr.cpp
 extern void fetchtr_py( const char *fileName, MetaTranslator *tor,
                          const char *defaultContext, bool mustExist, const QByteArray &codecForSource );
+extern void fetchtr_ui( const char *fileName, MetaTranslator *tor,
+                         const char *defaultContext, bool mustExist );
+
 
 
 // defined in merge.cpp
@@ -109,6 +112,7 @@
     QByteArray codecForTr;
     QByteArray codecForSource;
     QStringList tsFileNames;
+    QStringList uiFileNames;
 
     bool verbose = false;
     bool noObsolete = false;
@@ -174,6 +178,7 @@
             codecForTr.clear();
             codecForSource.clear();
             tsFileNames.clear();
+            uiFileNames.clear();
 
             QMap<QString, QString> tagMap = proFileTagMap( fullText );
             QMap<QString, QString>::Iterator it;
@@ -195,7 +200,9 @@
                         codecForTr = (*t).toLatin1();
                     } else if ( it.key() == "CODECFORSRC" ) {
                         codecForSource = (*t).toLatin1();
-                    }
+                    } else if ( it.key() == "FORMS" ) {
+		        fetchtr_ui( (*t).toAscii(), &fetchedTor, defaultContext.toAscii(), true);
+		    }
                 }
             }
 
@@ -224,14 +231,18 @@
                                  " save '%s'\n",
                                  argv[i] );
                     }
-                } else {
+		} else {
                     fprintf( stderr,
                              "pylupdate4 error: File '%s' lacks .ts extension\n",
                              argv[i] );
                 }
             } else {
                 QFileInfo fi(argv[i]);
-                fetchtr_py( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true, codecForSource );
+		if ( fi.suffix() == "py" ) {
+		    fetchtr_py( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true, codecForSource );
+		} else {
+		    fetchtr_ui( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true);
+		}
             }
         }
         QDir::setCurrent( oldDir );


More information about the PyQt mailing list