Code - what could be done better?

Florian Wollenschein Florian.Wollenschein at FernUni-Hagen.de
Sat May 9 05:41:25 EDT 2009


Hi all,

here's the main code of thc, my txt to html converter. Since I'm a 
beginner it is far, far, faaaaaaar away from perfect or even good :-)
What could be done better?

====================================================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# thc
# Copyright (C) 2007 - 2009 Listick Lorch
#
# <info at listick.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
02110-1301 USA

import sys
import webbrowser
import texts_en as texts # you have to change only the countrycode like 
texts_de ...
from PyQt4 import QtGui, QtCore
from mainframe_ui import Ui_MainWindow as Frm
import abtDialogFrame

class ThcMainframe(QtGui.QMainWindow, Frm):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.setupUi(self)

         # ---MEMBERS---
         self.file_to_convert     = ''
         self.file_to_write       = ''
         self.txt_file_to_convert = '' # stores the text of the .txt file
         self.bgColor_R = hex(255)[2:].zfill(2)
         self.bgColor_G = hex(255)[2:].zfill(2)
         self.bgColor_B = hex(255)[2:].zfill(2)
         self.font_size   = 0

         # ---SLOTS---
         # browse file to convert
         self.connect(self.btnBrowseFileConv,
                 QtCore.SIGNAL("clicked()"), self.onBrowseFileConv)
         # browse file to write
         self.connect(self.btnBrowseFileWrite,
                 QtCore.SIGNAL("clicked()"), self.onBrowseFileWrite)
         # convert
         self.connect(self.btnConvert,
                 QtCore.SIGNAL("clicked()"), self.onConvert)
         # show result
         self.connect(self.btnShowResult,
                 QtCore.SIGNAL("clicked()"), self.onShowResult)
         # select color
         self.connect(self.btnColorSelect,
                 QtCore.SIGNAL("clicked()"), self.SelectColor)
         # font size
         self.connect(self.horizontalSlider,
                 QtCore.SIGNAL("changed()"), self.onFontSizeChanged)
         # help menu
         self.connect(self.actionGo_to_listick_org,
                 QtCore.SIGNAL("triggered()"), self.GoToListickOrg)

         self.connect(self.actionAbout,
                 QtCore.SIGNAL("triggered()"), self.OpenAbout)
         # quit
         self.connect(self.btnQuit,
                 QtCore.SIGNAL("clicked()"), self.onQuit)

         self.connect(self.actionExit,
                 QtCore.SIGNAL("triggered()"), self.onQuit)

     def onBrowseFileConv(self):
         self.file_to_convert = QtGui.QFileDialog.getOpenFileName(self, 
texts.FileMenuOpen, '/home', '*.txt')
         self.inpFileConv.setText(self.file_to_convert)

     def onBrowseFileWrite(self):
         self.file_to_write = QtGui.QFileDialog.getSaveFileName(self, 
texts.FileMenuSaveAs, '/home', '*.htm*')
         self.inpFileWrite.setText(self.file_to_write)

     def onConvert(self):
         # read the title
         html_file_title = str(self.inpTitle.displayText())
         # read the txt
         file_open = open(self.file_to_convert)
         print texts.FileOpened
         self.txtOutput.insertPlainText(texts.FileOpened)
         self.txt_file_to_convert = file_open.read()
         file_open.close()
         print texts.FileClosed
         self.txtOutput.insertPlainText(texts.FileClosed)

         # write html
         file_open = open(self.file_to_write, "w")
         print texts.WritingFile
         self.txtOutput.insertPlainText(texts.WritingFile)
         strict_or_transitional = {True: 'Transitional', False: 'Strict'}
         spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()]
         doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 
%s//EN">\n' % spec
         file_open.write(doctype)
         file_open.write('<HTML>\n')
         # HTML head stuff
         file_open.write('<HEAD>\n')
         file_open.write('<META HTTP-EQUIV="Content-Type" 
CONTENT="text/html; charset=UTF-8">\n')
         file_open.write('<TITLE>%s</TITLE>\n' % html_file_title)
         file_open.write('<META NAME="Generator" CONTENT="thc - 
Txt-to-Html-Converter %s">\n' %  texts.versionInfo)
         file_open.write('</HEAD>\n')
         #head end

         #HTML body
         file_open.write('<BODY BGCOLOR="#%s%s%s">\n' % (self.bgColor_R, 
self.bgColor_G, self.bgColor_B))
         file_open.write(self.txt_file_to_convert)
         file_open.write('<BR>')

         # Created-with-thc notice
         file_open.write(texts.createdWithNote)
         file_open.write('</BODY>\n')
         #body end
         file_open.write('</HTML>')

         file_open.close()
         print texts.FileWritten
         self.txtOutput.insertPlainText(texts.FileWritten)
         self.txtOutput.insertPlainText(texts.Success)

     def onShowResult(self):
         webbrowser.open_new(self.file_to_write)

     def SelectColor(self):
         color = QtGui.QColorDialog.getColor()
         self.bgColor_R  = hex(color.red())[2:].zfill(2)
         self.bgColor_G  = hex(color.green())[2:].zfill(2)
         self.bgColor_B  = hex(color.blue())[2:].zfill(2)
         self.txtOutput.insertPlainText("\nSelected color (RGB Hex): 
%s%s%s" % (self.bgColor_R,  self.bgColor_G,  self.bgColor_B))

     def onFontSizeChanged(self):
         self.font_size = horizontalSlider.value()
         self.txtOutput.insertPlainText("font size: %s" % self.font_size)

     def OpenAbout(self):
         abtDialog = abtDialogFrame.ThcAboutDialog()
         abtDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
         abtDialog.exec_()

     def GoToListickOrg(self):
         webbrowser.open_new('http://www.listick.org')

     def onQuit(self):
         print texts.Quit
         self.close()

app = QtGui.QApplication(sys.argv)
mainframe = ThcMainframe()
mainframe.show()
sys.exit(app.exec_())
========================================================================================

Thanks in advance.

Listick
http://www.listick.org



More information about the Python-list mailing list