auftragsordner.py, zurück zur Projektbeschreibung
   1#!/usr/bin/python
   2# -*- coding: utf-8 -*-
   3
   4# Copyright (C) 2006, 2011 Zacharias Korsalka <mail@sacharja.at>
   5#
   6# Permission is hereby granted, free of charge, to any person obtaining a copy
   7# of this software and associated documentation files (the "Software"), to deal
   8# in the Software without restriction, including without limitation the rights
   9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10# copies of the Software, and to permit persons to whom the Software is
  11# furnished to do so, subject to the following conditions:
  12#
  13# The above copyright notice and this permission notice shall be included in
  14# all copies or substantial portions of the Software.
  15#
  16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22# THE SOFTWARE.
  23#
  24
  25import pygtk
  26pygtk.require('2.0')
  27import gtk
  28import gobject
  29
  30import datetime
  31import os
  32import sys
  33import time
  34import platform
  35
  36#
  37##
  38### STAMMKUNDEN-DATEN
  39
  40Customers = [
  41  "Anton Berger", 
  42  "Clara Dorfer", 
  43  "Emil Fritsch", 
  44  u"Gustav H\xfcbner", 
  45  "unbenannt"]
  46
  47#
  48##
  49### EINSTELLUNGEN
  50
  51Searchdirs_windows = ["K:\\", "E:\\", "F:\\"]
  52Searchdirs_linux = ["/tmp", "/home/zacharias", "/isnot", "/usr/local/share"]
  53
  54Searchdirs = Searchdirs_windows
  55
  56# ACHTUNG: Ordnerzeichen am Schluss NICHT weglassen!
  57#   der Grund dafür liegt im Directoryhandling von Windows
  58Jobrootdir_linux = "/tmp/"
  59Jobrootdir_windows = "K:\\"
  60
  61Jobrootdir = Jobrootdir_windows
  62
  63Jobtypes = [              # '\t' wird hier für das aktuelle Datum missbraucht
  64  { "name":"normal", 
  65    "pattern":"\t", 
  66    "entry":"normaler Auftrag" }, 
  67  
  68  { "name":"tmp", 
  69    "pattern":"tmp \t", 
  70    "entry":u"tempor\xe4rer Auftrag" }, 
  71  
  72  { "name":"scan", 
  73    "pattern":"\tScan", 
  74    "entry":"Scanjob" }
  75  ]
  76
  77# Voreinstellung für die Auftrags-Notiz:
  78Jobnote_preset = "Notizen zum Auftrag:\n\n"
  79
  80#
  81##
  82### KONSTANTEN
  83
  84Copyrighttext = u"© 2006, 2011 Zacharias Korsalka."
  85
  86# Diese Zeichen werden aus Dateinamen ausgefiltert
  87Illegalchars = "\\/:;()[]{}?*!#\t\n<>|\"\'"
  88
  89#
  90##
  91### KLASSEN/FUNKTIONEN
  92
  93def filterchars(string):
  94  """
  95    alle "Illegalchars" in werden durch _ ersetzt, das Ergebnis returned
  96  """
  97  result = ""
  98  for c in string:
  99    for x in Illegalchars:
 100      if c == x:
 101        c = '_'
 102        break
 103    result +=  c
 104  return result
 105
 106
 107### Message
 108
 109class Message:
 110  text = ""
 111  
 112  def set_text(self, text):
 113    self.text = text
 114  
 115  def close(self, widget, parent):
 116    parent.destroy()
 117  
 118  def show(self, timeout = 0):
 119    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 120    
 121    window.set_border_width(10)
 122    window.set_size_request(600, 60)
 123    
 124    box = gtk.HBox()
 125    box.set_border_width(2)
 126    
 127    label = gtk.Label(self.text)
 128    box.pack_start(label, False)
 129    
 130    button = gtk.Button(" OK ")
 131    box.pack_end(button, False)
 132    
 133    button.connect("clicked", self.close, window)
 134    
 135    window.add(box)
 136    window.present()
 137    window.show_all()
 138    
 139    # this one is buggy as hell!
 140    if timeout>0:
 141      print timeout
 142      time.sleep(timeout)
 143      window.destroy()
 144      print "...done!"
 145  
 146  def __init__(self, text):
 147    self.text = text
 148
 149class File:
 150  """common file operations"""
 151  path = ""
 152  
 153  def copy(self, directory):
 154    """copy self.path to dir"""
 155    msg = Message("Konnte "+self.path+" nicht kopieren nach "+directory)
 156    try:
 157      src = open(self.path, "r")
 158    except:
 159      msg.show()
 160      return 1
 161    
 162    resultpath = os.path.join(directory, os.path.basename(self.path))
 163    try:
 164      # TODO: WHAT IF DESTINATION FILE ALREADY EXISTS?
 165      dest = open(resultpath, "w")
 166    except:
 167      msg.show()
 168      return 1
 169    
 170    dest.write(src.read())
 171    dest.close()
 172    src.close()
 173      
 174  def __init__(self, path):
 175    # TODO: What if file does not exist?
 176    self.path = path
 177
 178### Newjob
 179
 180class Newjob:
 181  
 182  def neuerauftrag(self, widget, parent, customernamewidget):
 183    verzeichnis = customernamewidget.get_text()
 184    if verzeichnis ==  "Kundenname":
 185      verzeichnis = "unbenannt"
 186    
 187    resultat = self.prefix+verzeichnis
 188    
 189    # Ausfiltern unerlaubter Zeichen:
 190    resultat = filterchars(resultat)
 191    resultat = resultat.encode("latin-1")
 192    
 193    # Der Algorithmus zum Vermeiden doppelter Verzeichnisnamen ist zwar
 194    #   unschön, aber er funktioniert bestens
 195    resultpath = os.path.join(self.ordner, resultat)
 196    while os.path.exists(resultpath):
 197      try:
 198        if resultat[-2] ==  '-':
 199          index = int(resultat[-1])
 200          resultat = resultat[:-2]
 201        else:
 202          index = 1
 203        
 204        index +=  1
 205        
 206        resultat = resultat + "-" + str(index)
 207        
 208      except:
 209        resultat = resultat+"-1"
 210        break
 211      resultpath = os.path.join(self.ordner, resultat)
 212    
 213    # Anlegen des Verzeichnisses
 214    try:
 215      os.makedirs(resultpath)
 216      parent.destroy()
 217      if platform.system() ==  'Windows':
 218        os.system("\"C:\\Programme\\Internet Explorer\\iexplore.exe\" "+resultpath)
 219
 220    except OSError:
 221      mssg = Message("Konnte Ordner   "+self.ordner+resultat+"   nicht anlegen!")
 222      mssg.show()
 223    
 224    # Anlegen der Auftragsnotiz
 225    if self.jobnotespeichern:
 226      mypath = os.path.join(self.ordner+resultat, resultat+".text")
 227      try:
 228        myfile = open(mypath, "w")
 229        myfile.write(self.jobnote)
 230        myfile.close()
 231      except:
 232        message = Message("Konnte Datei   "+mypath+"   nicht anlegen!")
 233        message.show()
 234    
 235  
 236  def jobtype(self, widget, pattern):
 237    if widget.get_active():
 238      myprefix = ""
 239      for c in pattern:
 240        if c ==  '\t':
 241          myprefix +=  str(datetime.date.today())+" "
 242        else:
 243          myprefix = myprefix+c
 244      self.label.set_text(myprefix)
 245      self.prefix = myprefix
 246  
 247  def customernamefilter(self, widget, input):
 248    vorschlag = filterchars(input.get_text())
 249    input.set_text(vorschlag)
 250  
 251  def customernamevorschlag(self, widget, target, string):
 252    target.set_text(string)
 253  
 254  def textupdate(self, widget, checkbox):
 255    begin = widget.get_start_iter()
 256    end = widget.get_end_iter()
 257    self.jobnote = widget.get_slice(begin, end, False)
 258    self.jobnotespeichern = True
 259    
 260    checkbox.set_active(True)
 261  
 262  def toggle_jobnote(self, widget):
 263    if widget.get_active():
 264      self.jobnotespeichern = True
 265    else:
 266      self.jobnotespeichern = False
 267  
 268
 269  def __init__(self):
 270    self.ordner = Jobrootdir
 271    self.prefix = str(datetime.date.today())+" "
 272    self.jobnotespeichern = False
 273    
 274    input = "Kundenname"
 275    vorgeschlagen = "unbenannt"
 276    
 277    # Fenster anlegen
 278    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 279    window.set_title("Neuen Auftragsordner anlegen")
 280    
 281    window.set_border_width(20)
 282    window.set_size_request(600, 600)
 283    
 284    box = gtk.VBox()
 285    box.set_border_width(2)
 286    
 287    hbox = gtk.HBox()
 288    
 289    # Mainjobdirname
 290    self.label = gtk.Label(self.prefix+" ")
 291    hbox.pack_start(self.label, False)
 292    self.label.show()
 293    
 294    customername = gtk.Entry()
 295    customername.set_text(input)
 296    
 297    hbox.pack_start(customername, True, True, 4)
 298    customername.show()
 299    box.pack_start(hbox, False)
 300    hbox.show()
 301    
 302    # Auswahlmenü for vordefinierte Ordnernamen
 303    menu = gtk.Menu()
 304    hbox = gtk.HBox()
 305    
 306    Customers.sort()
 307    for n in Customers:
 308      menueintrag = gtk.MenuItem(n)
 309      menu.append(menueintrag)
 310      menueintrag.connect("activate", self.customernamevorschlag, customername, n)
 311      menueintrag.show()
 312    
 313    basemenu = gtk.MenuItem("Namensvorschlag aus Liste")
 314    basemenu.show()
 315    basemenu.set_submenu(menu)
 316    
 317    menubar = gtk.MenuBar()
 318    hbox.pack_start(menubar, False)
 319    menubar.show()
 320    
 321    menubar.append(basemenu)
 322    
 323    separator = gtk.HSeparator()
 324    box.pack_start(separator, False, False, 4)
 325    separator.show()
 326    
 327    hbox.show()
 328    box.pack_start(hbox, False, False, 4)
 329    
 330    # Auftragsart auswählen
 331    dingi = None
 332    for item in Jobtypes:
 333      typ = gtk.RadioButton(dingi, item["entry"])
 334      typ.connect("toggled", self.jobtype, item["pattern"])
 335      if dingi == None:
 336        typ.set_active(True)
 337      box.pack_start(typ, False)
 338      typ.show()
 339      
 340      dingi = typ
 341    
 342    # Notiztextinput
 343    
 344    frame = gtk.Frame()
 345    frame.show()
 346    
 347    textbox = gtk.VBox()
 348    
 349    usetext = gtk.CheckButton("Auftragsnotiz speichern")
 350    textbox.pack_start(usetext, False, False, 4)
 351    usetext.show()
 352    usetext.connect("toggled", self.toggle_jobnote)
 353    
 354    separator = gtk.HSeparator()
 355    textbox.pack_start(separator, False, False, 0)
 356    separator.show()
 357    
 358    textbuf = gtk.TextBuffer()
 359    textbuf.set_text(Jobnote_preset)
 360    
 361    textview = gtk.TextView(textbuf)
 362    textview.set_wrap_mode(gtk.WRAP_WORD)
 363    textview.show()
 364    
 365    textbox.pack_start(textview, True, True, 0)
 366    frame.add(textbox)
 367    box.pack_start(frame, True, True, 4)
 368    textbox.show()
 369    
 370    # Enterbutton
 371    enterbutton = gtk.Button("Auftragsordner anlegen")
 372    enterbutton.set_flags(gtk.CAN_DEFAULT)
 373    # TODO: funzt net, sollte es aber lt docu: ->   enterbutton.grab_default()
 374    
 375    box.pack_end(enterbutton, False)
 376    enterbutton.show()
 377    
 378    textbuf.connect("changed", self.textupdate, usetext)
 379    customername.connect("changed", self.customernamefilter, customername)
 380    enterbutton.connect("clicked", self.neuerauftrag, window, customername)
 381    window.connect("destroy", gtk.main_quit)
 382    
 383    window.add(box)
 384    box.show()
 385    window.show()
 386
 387  def main(self):
 388    gtk.main()
 389
 390    
 391#
 392##
 393### HAUPTPROGRAMM
 394
 395if __name__ ==  "__main__":
 396  if platform.system() ==  'Linux':
 397    Searchdirs = Searchdirs_linux
 398    Jobrootdir = Jobrootdir_linux
 399  else:
 400    Jobrootdir = Jobrootdir_windows
 401    print """DIESES PROGRAMMFENSTER BITTE NICHT SCHLIESZEN!
 402  
 403    Wenn es aus dem Weg sein soll, dann bitte minimieren, 
 404    aber nicht schlieszen!"""
 405  
 406  print Searchdirs
 407  runme = Newjob()
 408  runme.main()
 409
 410# end of file
 411
 412