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