File: globdef.py

package info (click to toggle)
songwrite 0.14-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,440 kB
  • ctags: 993
  • sloc: python: 7,059; xml: 850; makefile: 10
file content (122 lines) | stat: -rw-r--r-- 3,732 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# SongWrite
# Copyright (C) 2001-2004 Jean-Baptiste LAMY -- jibalamy@free.fr
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# GlobDef : global stuff (cannot be called global nor glob so...).

import os, os.path, gettext, sys, editobj.cancel as cancel

APPDIR = os.path.dirname(__file__)

DATADIR = os.path.join(APPDIR, "data")
if not os.path.exists(DATADIR):
  import warnings
  warnings.warn("Songwrite's data directory cannot be found !")
  
LOCALEDIR = os.path.join(APPDIR, "locale")
if not os.path.exists(LOCALEDIR):
  LOCALEDIR = os.path.join(APPDIR, "..", "locale")
  if not os.path.exists(LOCALEDIR):
    LOCALEDIR = os.path.join("/", "usr", "share", "locale")
    
try:
  translator = gettext.translation("songwrite", LOCALEDIR)
except IOError: # Non-supported language, defaults to english
  translator = gettext.translation("songwrite", LOCALEDIR, ("en",))
translator.install(1)

CONFIGFILE = os.path.expanduser(os.path.join("~", ".songwrite"))
NO_CONFIG  = 0

class Config:
  def __init__(self):
    # Default config
    self.MIDI_COMMAND       = "timidity -id -"
    self.MIDI_USE_TEMP_FILE = 0
    self.PLAY_LOOP          = 0
    self.PREVIEW_COMMAND    = "gv"
    self.NUMBER_OF_CANCEL   = 20
    self.PREVIOUS_FILES     = []
    self.PAGE_FORMAT        = "a4paper"
    self.DISPLAY_PLAY_BAR   = 1
    
    global NO_CONFIG
    if os.path.exists(CONFIGFILE):
      try:
        execfile(CONFIGFILE, self.__dict__)
        
        cancel.NUMBER_OF_CANCEL = self.NUMBER_OF_CANCEL + 1
        
        NO_CONFIG = 0
        return
      except:
        sys.excepthook(*sys.exc_info())
        print "Error in config file ~/.songwrite ! Please reconfigure Songwrite !"
        
    NO_CONFIG = 1
    
  def add_previous_file(self, file):
    try: self.PREVIOUS_FILES.remove(file) # No dupplicated item.
    except: pass
    self.PREVIOUS_FILES.insert(0, file)
    if len(self.PREVIOUS_FILES) > 12: del self.PREVIOUS_FILES[-1]
    
  def __str__(self):
    return """
# Songwrite config file. Use a Python syntax.

# 1 to use a temporary MIDI file (call this file %%s in the command line).
# 0 to use standard input.
MIDI_USE_TEMP_FILE = %s

# Command line to play a midi file.
MIDI_COMMAND = "%s"

# 1 to play in loop. 0 to play once.
PLAY_LOOP = %s

# 1 for enabling playbar
DISPLAY_PLAY_BAR = %s

# Page format (LaTeX)
PAGE_FORMAT = "%s"

# Command line to preview/print postscript.
PREVIEW_COMMAND = "%s"

# Size of cancel stack.
NUMBER_OF_CANCEL = %s

# Previous opened files.
PREVIOUS_FILES = %s
""" % (self.MIDI_USE_TEMP_FILE, self.MIDI_COMMAND, self.PLAY_LOOP, self.DISPLAY_PLAY_BAR, self.PAGE_FORMAT, self.PREVIEW_COMMAND, self.NUMBER_OF_CANCEL, `self.PREVIOUS_FILES`)

  def save(self):
    open(CONFIGFILE, "w").write(str(self))

  def edit(self):
    import init_editobj
    
    def on_ok():
      self.MIDI_USE_TEMP_FILE = self.MIDI_COMMAND.find("%s") != -1
      cancel.NUMBER_OF_CANCEL = self.NUMBER_OF_CANCEL + 1
      self.save()
      
    init_editobj.edit(self, command = on_ok, preamble = _("__config_preamble__"))
    
    
config = Config()