]> git.phdru.name Git - m_lib.git/blob - m_lib/flad/fladm.py
Raise Error(message) for Py3 compatibility
[m_lib.git] / m_lib / flad / fladm.py
1 """
2    Flat ASCII Database with "must" keys
3 """
4
5
6 from flad import Flad, def_keysep
7
8
9 class Flad_WithMustKeys(Flad):
10    """
11       Database with two lists of keys - keys that must be in every record,
12       and keys that allowed to be in some records.
13    """
14
15    def __init__(self, check_record_func = None, must_keys = None, other_keys = None):
16       Flad.__init__(self, check_record_func)
17       self.must_keys = must_keys   # Save keys lists to store...
18       self.other_keys = other_keys #... desired sequence of keys
19
20
21    def store_to_file(self, f):
22       if type(f) == type(''): # If f is string - use it as file's name
23          outfile = open(f, 'w')
24       else:
25          outfile = f          # else assume it is opened file (fileobject) or
26                               # "compatible" object (must has write() method)
27
28       flush_record = 0 # Do not close record on 1st loop
29
30       for record in self:
31          copy_rec = record.copy() # Make a copy to delete keys
32
33          if flush_record:
34             outfile.write('\n') # Close record
35          else:
36             flush_record = 1    # Set flag for all but 1st record
37
38          if self.must_keys:
39             for key in self.must_keys:
40                outfile.write(key + def_keysep + copy_rec[key] + '\n')
41                del copy_rec[key]
42
43          if self.other_keys:
44             for key in self.other_keys:
45                if copy_rec.has_key(key):
46                   outfile.write(key + def_keysep + copy_rec[key] + '\n')
47                   del copy_rec[key]
48
49          if copy_rec:
50             for key in copy_rec.keys():
51                outfile.write(key + def_keysep + copy_rec[key] + '\n')
52                del copy_rec[key]
53
54       if type(f) == type(''): # If f was open - close it
55          outfile.close()
56
57
58 def check_record(data, record):
59    """
60       Check record for consistency and append it to list of records
61    """
62    must_keys = data.must_keys
63    other_keys  = data.other_keys
64
65    if must_keys:
66       copy_must = must_keys[:] # Make a copy
67    else:
68       copy_must = None
69
70    for key in record.keys(): # Check every key
71       if must_keys and (key in must_keys):
72          del copy_must[copy_must.index(key)] # Remove the key from copied list
73       elif (must_keys and (key not in must_keys) and (other_keys and (key not in other_keys))) or (other_keys and (key not in other_keys)):
74          raise KeyError("field key \"" + key + "\" is not in list of allowed keys")
75
76    if copy_must: # If there is at least one key - it is an error:
77                       # not all "must" keys are in record
78       raise KeyError("not all \"must\" keys are in record; keys: " + str(copy_must))
79
80    return 1
81
82
83 def load_file(f, check_record_func = None, must_keys = None, other_keys = None):
84    """
85       Create a database object and load it from file
86    """
87
88    db = Flad_WithMustKeys(check_record_func, must_keys, other_keys)
89    db.load_file(f)
90
91    return db
92
93
94 def load_from_file(f, check_record_func = None, must_keys = None, other_keys = None):
95    """
96       Create a database object and load it from file
97    """
98
99    db = Flad_WithMustKeys(check_record_func, must_keys, other_keys)
100    db.load_from_file(f)
101
102    return db