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
18 description="""\
19 Adds the content of a number of pickledData-files to a sqlite database
20 """
21 PyFoamApplication.__init__(self,
22 args=args,
23 description=description,
24 usage="%prog <database.db> <pickleData1> ... <pickleData2>",
25 interspersed=True,
26 changeVersion=False,
27 nr=2,
28 exactNr=False)
29
31 how=OptionGroup(self.parser,
32 "Behavior",
33 "How the application should behave")
34 self.parser.add_option_group(how)
35
36 how.add_option("--create",
37 action="store_true",
38 dest="create",
39 default=False,
40 help="Create a new database file. Fail if it already exists")
41 how.add_option("--verbose",
42 action="store_true",
43 dest="verbose",
44 default=False,
45 help="Tell about the data added")
46
47 how.add_option("--skip-missing",
48 action="store_true",
49 dest="skipMissing",
50 default=False,
51 help="Skip files that are missing or unreadable")
52
53
55 dest=self.parser.getArgs()[0]
56 if path.exists(dest) and self.opts.create:
57 self.error("database-file",dest,"exists already.")
58 sources=self.parser.getArgs()[1:]
59
60 db=RunDatabase(dest,
61 create=self.opts.create,
62 verbose=self.opts.verbose)
63
64 for s in sources:
65 if self.opts.verbose:
66 print_("\nProcessing file",s)
67 try:
68 data=pickle.Unpickler(open(s)).load()
69 except (IOError,pickle.UnpicklingError):
70 e = sys.exc_info()[1]
71 if self.opts.skipMissing:
72 self.warning("File",s,"missing")
73 continue
74 else:
75 self.error("There was a problem reading file",s,
76 ":",e)
77 db.add(data)
78