annotate Generic/durusdatabase.py @ 22:7bd4ca56607d

bug fix: Mutable default arguments
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Tue, 30 Sep 2014 17:16:50 +0200
parents 0359329a1c26
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
1 #!/usr/bin/python
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
3 ## File durus.py
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
4 ## This file is part of pyArq-Presupuestos.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
5 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
6 ## Copyright (C) 2010 Miguel Ángel Bárcena Rodríguez
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
7 ## <miguelangel@obraencurso.es>
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
8 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
10 ## it under the terms of the GNU General Public License as published by
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
12 ## (at your option) any later version.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
13 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
14 ## pyArq-Presupuestos is distributed in the hope that it will be useful,
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
17 ## GNU General Public License for more details.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
18 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
20 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
21
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
22 # Modules
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
23 import os.path
5
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
24 import time
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
25 # Durus Modules
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
26 from durus.file_storage import FileStorage
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
27 from durus.connection import Connection
5
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
28 # pyArq Presupuestos Modules
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
29 from Generic import utils
7
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
30 from Generic import globalVars
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
31 class DurusFile(object):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
32 def __init__(self, file, new):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
33 self.__file = file
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
34 if new:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
35 if os.path.exists(self.__file):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
36 os.remove(self.__file)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
37 self.__connection = Connection(FileStorage(self.__file))
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
38 self.__root = self.__connection.get_root()
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
39
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
40 def close(self):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
41 self.__connection.get_storage().close()
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
42
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
43 def getBudget(self):
7
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
44 if self.__root.has_key("baseversion") and \
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
45 globalVars.baseversion == self.__root["baseversion"]:
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
46 return self.__root["budget"]
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
47 else:
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
48 print _("Incorrent Base version")
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
49 return None
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
50
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
51 def setBudget(self, budget):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
52 self.__root["budget"] = budget
7
0359329a1c26 Navigation buttons. Version of the data structure.
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 5
diff changeset
53 self.__root["baseversion"] = globalVars.baseversion
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
54 self.__connection.commit()
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
55
5
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
56 class Read(object):
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
57 def __init__(self, filename=None, budget=None):
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
58 self.__budget = budget
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
59 self.__filename = filename
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
60 self.__cancel = False
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
61
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
62 def cancel(self):
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
63 """def cancel(self)
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
64
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
65 It do nothing
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
66 """
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
67 # TODO: Cancel reading Durus database.
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
68 self.__cancel = True
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
69
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
70 def readFile(self, budget=None, filename=None, interface=None):
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
71 if not filename is None:
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
72 self.__filename = filename
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
73 if self.__filename is None or self.__cancel == True:
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
74 return None
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
75 if not os.path.exists(self.__filename):
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
76 return None
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
77 print utils.mapping(_("Loading file: $1:"), (self.__filename,))
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
78 _time = time.time()
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
79 _durus_file = DurusFile(self.__filename, False)
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
80 self.__budget = _durus_file.getBudget()
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
81 _durus_file.close()
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
82 print utils.mapping(_("Loadig time: $1 seconds"),
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
83 (("%.2f" %(time.time()-_time)),))
6502bfdaa84d update import durus database
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
84 return self.__budget