1 """
2 Application-class that implements pyFoamAddCaseDataToDatabase.py
3 """
4 from optparse import OptionGroup
5
6 from .PyFoamApplication import PyFoamApplication
7 from PyFoam.Basics.RunDatabase import RunDatabase
8
9 from os import path
10
11 from PyFoam.ThirdParty.six.moves import cPickle as pickle
12 from PyFoam.ThirdParty.six import print_
13
14 import sys
15
17 - def __init__(self,
18 args=None,
19 **kwargs):
20 description="""\
21 Adds the content of a number of pickledData-files to a sqlite database
22 """
23 PyFoamApplication.__init__(self,
24 args=args,
25 description=description,
26 usage="%prog <database.db> <pickleData1> ... <pickleData2>",
27 interspersed=True,
28 changeVersion=False,
29 nr=2,
30 exactNr=False,
31 **kwargs)
32
34 how=OptionGroup(self.parser,
35 "Behavior",
36 "How the application should behave")
37 self.parser.add_option_group(how)
38
39 how.add_option("--create",
40 action="store_true",
41 dest="create",
42 default=False,
43 help="Create a new database file. Fail if it already exists")
44 how.add_option("--verbose",
45 action="store_true",
46 dest="verbose",
47 default=False,
48 help="Tell about the data added")
49
50 how.add_option("--skip-missing",
51 action="store_true",
52 dest="skipMissing",
53 default=False,
54 help="Skip files that are missing or unreadable")
55
56
58 dest=self.parser.getArgs()[0]
59 if path.exists(dest) and self.opts.create:
60 self.error("database-file",dest,"exists already.")
61 sources=self.parser.getArgs()[1:]
62
63 db=RunDatabase(dest,
64 create=self.opts.create,
65 verbose=self.opts.verbose)
66
67 for s in sources:
68 if self.opts.verbose:
69 print_("\nProcessing file",s)
70 try:
71 data=pickle.Unpickler(open(s)).load()
72 except (IOError,pickle.UnpicklingError):
73 e = sys.exc_info()[1]
74 if self.opts.skipMissing:
75 self.warning("File",s,"missing")
76 continue
77 else:
78 self.error("There was a problem reading file",s,
79 ":",e)
80 db.add(data)
81