1 #! /usr/local/bin/python -O
3 Test FLAD database for: duplicate URLs, too big indent, incorrect record
6 Written by BroytMann, Jun 1997 - Feb 2000. Copyright (C) 1997-2000 PhiloSoft Design
10 from getopt import getopt
11 from copy import _copy_dict
17 global errors_found, report_stats
22 errors_found = errors_found + 1
23 sys.stderr.write("%s\n" % err_str)
26 logfile.write("%s\n" % err_str)
29 def check_key(record_no, record, key, allow_empty=1):
30 if not record.has_key(key):
31 error("Expected `%s' in record %d -- %s" % (key, record_no, str(record)))
34 if not allow_empty and not record[key]:
35 error("Empty key `%s' in record %d -- %s" % (key, record_no, str(record)))
39 def check_date(record_no, record, key):
40 if not record.has_key(key):
41 error("Expected `%s' in record %d -- %s" % (key, record_no, str(record)))
44 _date = string.atoi(record[key])
45 except string.atoi_error:
46 error("Bad `%s' format in record %d -- %s" % (key, record_no, str(record)))
50 def check_empty(record_no, record):
52 error("Spare keys in record %d -- %s" % (record_no, str(record)))
54 def check_url(record_no, record):
55 # I am not testing here check_url("Level") because it is impossible
56 # to come here without "Level" key - fladm.check_record has to reject
57 # entire database if there is record without this "must key".
58 # If someone adds record without "Level" manually - it is serious error
59 # and the following line raise exception.
62 check_key(record_no, record, "Title")
63 check_key(record_no, record, "URL")
64 check_key(record_no, record, "Comment")
66 check_date(record_no, record, "AddDate")
67 check_date(record_no, record, "LastVisit")
68 check_date(record_no, record, "LastModified")
70 check_empty(record_no, record)
72 def check_folder(record_no, record):
73 # Read comment above - in the beginning of check_url()
76 check_key(record_no, record, "Folder")
77 check_key(record_no, record, "Comment")
79 check_date(record_no, record, "AddDate")
80 check_empty(record_no, record)
82 def check_ruler(record_no, record):
83 # Read comment above - in the beginning of check_url()
86 if not record.has_key("Ruler"):
87 error("No `Ruler' in record %d -- %s" % (record_no, str(record)))
89 if record["Ruler"] <> "YES": # Impossible: ruler saying it is not ruler
90 error("Ruler saying it is not ruler in record %d -- %s" % (record_no, str(record)))
93 check_empty(record_no, record)
97 optlist, args = getopt(sys.argv[1:], "l:s")
99 global errors_found, report_stats, logfile
105 for _opt, _arg in optlist:
116 sys.stderr.write("check_db: too many arguments\n")
121 logfile = open(logfname, 'w')
124 sys.stdout.write("Loading: ")
127 bookmarks_db = fladm.load_from_file("bookmarks.db", fladm.check_record, ["Level"])
131 sys.stdout.write("Testing: ")
136 got_folder = 1 # Start as if we already have one folder
139 URL_d = {} # Create hash table full of URLs
141 for record in bookmarks_db:
142 record_no = record_no + 1
143 level = string.atoi(record["Level"])
145 if record.has_key("URL"):
146 if URL_d.has_key(record["URL"]):
147 error("Duplicate URL (rec. %d, 1st at rec. %d): %s" % (record_no, URL_d[record["URL"]], str(record["URL"])))
149 URL_d[record["URL"]] = record_no
151 check_url(record_no, _copy_dict(record))
153 elif record.has_key("Folder"):
154 check_folder(record_no, _copy_dict(record))
156 elif record.has_key("Ruler"):
157 check_ruler(record_no, _copy_dict(record))
160 raise KeyError, "neither \"URL\" nor \"Folder\" nor \"Ruler\" in record " + str(record)
163 if (level > save_level + 1):
164 error("Indent %d too big (want %d at rec. %d), record: %s" % (level, save_level, record_no, str(record)))
166 if (level > save_level):
167 error("Indent %d without folder (rec. %d), record: %s" % (level, record_no, str(record)))
170 got_folder = record.has_key("Folder") # Test here to save got_folder for next loop
178 print record_no, "records tested"
179 if errors_found == 0:
180 print "Ok (no errors found)"
182 print "%d errors found" % errors_found
185 if __name__ == '__main__':