# -*- coding: ISO-8859-1 -*-
""" capellaScript -- (c) Paul Villiger
>>> braille-view-control

Skript zum Konfigurieren und starten von Braille-View.|
|
Optionen Speicherort der Braille-Dateien:|
- im Partiturverzeichnis|
- im Unterverzeichnis "Braille"|
- in einem beliebigen Zielverzeichnis|
|
Nach der Auswahl wird Braille-View gestartet, ausser bei der Auswahl
des Zielverzeichnisses.
Es besteht auch die Möglichkeit ein ganzes Verzeichnis zu übersetzen.

<<<

Rückmeldungen bitte an villpaul(a)bluewin.ch

History:    ????        Erstversion
            11.03.2014  Anpassung an capella 7
            
"""

# import win32traceutil

class ScriptOptions (object):
    def __init__(self):
        dir, file = os.path.split(sys.argv[0])
        fileName = 'braille-view.opt'
        self._optionsFile = os.path.join(dir, fileName)

    def get(self):
        opt = dict()
        try:
            f = file(self._optionsFile)
            l = f.readlines()
            f.close()
            for x in l:
                a = [s.strip() for s in x.split('=')] 
                if len(a) == 2:
                    opt[a[0]] = a[1]
        except: pass
        return opt

    def set(self, d):
        f = file(self._optionsFile, 'w')
        for i in d:
            f.write('%s = %s\n' % (i, str(d[i])))
        f.flush()            
        f.close()

options = ScriptOptions()
opt = options.get()     # opt = dict()

dir, fName = os.path.split(sys.argv[0])
handleDir = int(opt.setdefault('BrailleDirectoryHandling','0'))
ownDir = opt.setdefault('BrailleDirectory', dir)

options.set(opt)

selActions = Radio(['in Partiturverzeichnis',
                    'in Unterverzeichnis Braille',
                    'in Zielverzeichnis',
                    'Zielverzeichnis auswählen'], value = handleDir, padding = 8)
selDir     = Edit(ownDir, width=40, padding = 8)
selWholeDir = CheckBox('Ganzes Verzeichnis übersetzen', value = False, padding = 8)
    
dlg = Dialog('Braille-View Configuration',
             VBox([Label('Braille-Dateien ablegen:'),
                  Label(' '),
                  selActions,
                  Label(' '),
                  Label('Zielverzeichnis:'),
                  Label(' '),
                  selDir,
                  Label(' '),
                  selWholeDir,
                  Label(' ')]
                 )
            )


if dlg.run():
    if selActions.value() == 3:
        dlg = FileDialog()
        dlg.__init__(bOpen=False)
        dlg.setTitle('Bitte Zielverzeichnis auswählen')
        dlg.addFilter('capella-Dateien', '*.cap;*.capx')
        dlg.setStartFile('$$XX.cap')

        if dlg.run():
            capFile=dlg.filePath()
            capDir, capName = os.path.split(capFile)
            opt['BrailleDirectory'] = capDir
            options.set(opt)
    else:
        opt['BrailleDirectoryHandling'] = str(selActions.value())
        opt['BrailleDirectory'] = selDir.value()
        options.set(opt)


    if selActions.value() == 3:
        pass
    elif selWholeDir.value() == 0 :
        dir, fileName = os.path.split(sys.argv[0])
        progFile = os.path.join(dir, 'braille-view.py')
        if os.path.isfile(progFile):
            execfile(progFile)
        
    # ganzes Verzeichnis übersetzen
    elif selWholeDir.value() == 1 :
        dlg = FileDialog()
        dlg.__init__(bOpen=False)
        dlg.setTitle('Bitte Partiturordner auswählen')
        dlg.addFilter('capella-Dateien', '*.cap;*.capx')
        dlg.setStartFile('$$XX.cap')

        if dlg.run():
            capFile=dlg.filePath()
            capDir, capName = os.path.split(capFile)

            dirlist = os.listdir(capDir)
            dirlist.sort()

            for capFile in dirlist:
                head, tail = os.path.splitext(capFile)
                if tail.lower() <> '.cap':
                    continue
                
                if not os.path.isfile(os.path.join(capDir,capFile)):
                    continue

                openScore(os.path.join(capDir,capFile))
                if activeScore():

                    dir, fileName = os.path.split(sys.argv[0])
                    progFile = os.path.join(dir, 'braille-view.py')
                    if os.path.isfile(progFile):
                        execfile(progFile)
                
                closeActiveScore()



