LeenO computo metrico con LibreOffice  3.22.0
Il software libero per la gestione di computi metrici e contabilità lavori.
LeenoConfig.py
Vai alla documentazione di questo file.
1 '''
2 Gestione delle impostazioni di LeenO
3 '''
4 import sys
5 import os
6 import configparser
7 
8 from os.path import expanduser
9 from datetime import date
10 
11 import LeenoUtils
12 import PersistUtils
13 
14 class Borg:
15  '''
16  Singleton/BorgSingleton.py
17  Alex Martelli's 'Borg'
18  '''
19 
20  _shared_state = {}
21 
22  def __init__(self):
23  self.__dict__ = self._shared_state
24 
25 
26 class Config(Borg):
27  '''
28  classe contenente la configurazione di LeenO
29  la classe è un singleton - anche creando vari
30  oggetti tutti contengono gli stessi dati
31  '''
32  def __init__(self):
33  Borg.__init__(self)
34 
35  if self._shared_state == {}:
36 
37  # the path of config file
38  if sys.platform == 'win32':
39  self._path = os.getenv("APPDATA") + '/.config/leeno/leeno.conf'
40  else:
41  self._path = os.getenv("HOME") + '/.config/leeno/leeno.conf'
42 
43  # just in case... crea la cartella radice dove piazzare
44  # il file di configurazione
45  try:
46  os.makedirs(os.path.dirname(self._path))
47  except FileExistsError:
48  pass
49 
50  #self._parser = configparser.ConfigParser()
51 
52  self._parser = configparser.RawConfigParser()
53  self._parser.optionxform = lambda option: option
54 
55  # load values from file, if exist
56  self._load()
57 
58  # fill with default values items that are missing
59  self._initDefaults()
60 
61  def _initDefaults(self):
62  '''
63  default configuration parameters, if not changed by user
64  '''
65  parametri = (
66  ('Zoom', 'fattore', '100'),
67  ('Zoom', 'fattore_ottimale', '81'),
68  ('Zoom', 'fullscreen', '0'),
69 
70  ('Generale', 'dialogo', '1'),
71  # ('Generale', 'visualizza', 'Menù Principale'),
72  ('Generale', 'altezza_celle', '1.25'),
73  ('Generale', 'pesca_auto', '1'),
74  ('Generale', 'movedirection', '1'),
75  ('Generale', 'descrizione_in_una_colonna', '0'),
76  ('Generale', 'toolbar_contestuali', '1'),
77  ('Generale', 'vedi_voce_breve', '50'),
78  ('Generale', 'dettaglio', '1'),
79  ('Generale', 'torna_a_ep', '1'),
80  ('Generale', 'copie_backup', '5'),
81  ('Generale', 'pausa_backup', '15'),
82  ('Generale', 'conta_usi', '0'),
83  ('Generale', 'ultimo_percorso', expanduser("~")),
84 
85  # ('Computo', 'riga_bianca_categorie', '1'),
86  # ('Computo', 'voci_senza_numerazione', '0'),
87  ('Computo', 'inizio_voci_abbreviate', '100'),
88  ('Computo', 'fine_voci_abbreviate', '120'),
89  ('Contabilita', 'cont_inizio_voci_abbreviate', '100'),
90  ('Contabilita', 'cont_fine_voci_abbreviate', '120'),
91  ('Contabilita', 'abilitaconfigparser', '0'),
92  ('Contabilita', 'idxsal', '20'),
93  ('Contabilita', 'ricicla_da', 'COMPUTO'),
94 
95  ('Importazione', 'ordina_computo', '1'),
96 
97  ('Lavoro', 'committente', '(str)'),
98  ('Lavoro', 'stazioneAppaltante', '(str)'),
99  ('Lavoro', 'progetto', '(str)'),
100  ('Lavoro', 'rup', '(str)'),
101  ('Lavoro', 'progettista', '(str)'),
102  ('Lavoro', 'data', '(date)' + LeenoUtils.date2String(date.today(), 1)),
103  ('Lavoro', 'revisione', '(str)'),
104  ('Lavoro', 'dataRevisione', '(date)' + LeenoUtils.date2String(date.today(), 1)),
105 
106  ('ImpostazioniStampa', 'fileCopertine', '(str)'),
107  ('ImpostazioniStampa', 'copertina', '(str)'),
108  ('ImpostazioniStampa', 'intSx', '(str)[COMMITTENTE]'),
109  ('ImpostazioniStampa', 'intCenter', '(str)'),
110  ('ImpostazioniStampa', 'intDx', '(str)[PROGETTO]'),
111  ('ImpostazioniStampa', 'ppSx', '(str)[OGGETTO]'),
112  ('ImpostazioniStampa', 'ppCenter', '(str)'),
113  ('ImpostazioniStampa', 'ppDx', '(str)Pagina [PAGINA] di [PAGINE]'),
114 
115  ('ImpostazioniExport', 'npElencoPrezzi', '(str)1'),
116  ('ImpostazioniExport', 'npComputoMetrico', '(str)2'),
117  ('ImpostazioniExport', 'npCostiManodopera', '(str)3'),
118  ('ImpostazioniExport', 'npQuadroEconomico', '(str)4'),
119  ('ImpostazioniExport', 'cbElencoPrezzi', '(bool)True'),
120  ('ImpostazioniExport', 'cbComputoMetrico', '(bool)True'),
121  ('ImpostazioniExport', 'cbCostiManodopera', '(bool)True'),
122  ('ImpostazioniExport', 'cbQuadroEconomico', '(bool)True'),
123 
124  )
125 
126  for param in parametri:
127  try:
128  self._parser.get(param[0], param[1])
129  except Exception:
130  if not self._parser.has_section(param[0]):
131  self._parser.add_section(param[0])
132  self._parser.set(param[0], param[1], param[2])
133 
134  def _load(self):
135  '''
136  load configuration data from disk
137  '''
138  try:
139  self._parser.read(self._path)
140  except Exception:
141  os.remove(self._path)
142 
143  def _store(self):
144  '''
145  store configuration data to disk
146  '''
147  fp = open(self._path, 'w')
148  self._parser.write(fp)
149  fp.close()
150 
151  def read(self, section, option, convert=False):
152  '''
153  read an option from config
154  if convert is True, do the string->value conversion
155  (for latter, the string must have the correct format)
156  '''
157  try:
158  return self._parser.get(section, option)
159  except Exception:
160  return None
161 
162  def readBlock(self, section, convert=False):
163  '''
164  read a block of options from config given the section name
165  if convert is True, do the string->value conversion
166  (for latter, the strings must have the correct format)
167  '''
168  options = self._parser.options(section)
169  res = {}
170  for option in options:
171  val = self._parser.get(section, option)
172  if val is None:
173  continue
174  if convert:
175  try:
176  val = PersistUtils.string2var(val)
177  if val is not None:
178  res[option] = val
179  except Exception:
180  pass
181  else:
182  res[option] = val
183  return res
184 
185  def write(self, section, option, val):
186  '''
187  write an option to config
188  '''
189  if not self._parser.has_section(section):
190  self._parser.add_section(section)
191  self._parser.set(section, option, val)
192 
193  # we must store on each write, because we can't know
194  # when LeenoConfig gets destroyed with python
195  self._store()
196 
197  def writeBlock(self, section, valDict, convert=False):
198  '''
199  write a block of options to config given the section name
200  if convert is True, do the value->string conversion
201  '''
202  if not self._parser.has_section(section):
203  self._parser.add_section(section)
204  for key, val in valDict.items():
205  if convert:
206  val = PersistUtils.var2string(val)
207  if val is None:
208  continue
209  self._parser.set(section, key, val)
210  self._store()
LeenoConfig.Config.__init__
def __init__(self)
Definition: LeenoConfig.py:32
LeenoConfig.Config._store
def _store(self)
Definition: LeenoConfig.py:143
PersistUtils.string2var
def string2var(s)
Definition: PersistUtils.py:24
LeenoConfig.Config._initDefaults
def _initDefaults(self)
Definition: LeenoConfig.py:61
PersistUtils.var2string
def var2string(var)
Definition: PersistUtils.py:41
LeenoConfig.Borg._shared_state
dictionary _shared_state
Definition: LeenoConfig.py:20
LeenoConfig.Config._parser
_parser
Definition: LeenoConfig.py:52
LeenoConfig.Borg.__dict__
__dict__
Definition: LeenoConfig.py:23
LeenoConfig.Config._path
_path
Definition: LeenoConfig.py:39
LeenoConfig.Borg.__init__
def __init__(self)
Definition: LeenoConfig.py:22
LeenoConfig.Config
Definition: LeenoConfig.py:26
LeenoUtils.date2String
def date2String(dat, fmt=0)
Definition: LeenoUtils.py:189
LeenoConfig.Config.readBlock
def readBlock(self, section, convert=False)
Definition: LeenoConfig.py:162
LeenoConfig.Borg
Definition: LeenoConfig.py:14
LeenoConfig.Config._load
def _load(self)
Definition: LeenoConfig.py:134
LeenoConfig.Config.write
def write(self, section, option, val)
Definition: LeenoConfig.py:185
LeenoConfig.Config.writeBlock
def writeBlock(self, section, valDict, convert=False)
Definition: LeenoConfig.py:197
LeenoConfig.Config.read
def read(self, section, option, convert=False)
Definition: LeenoConfig.py:151