]> git.phdru.name Git - m_lib.git/blobdiff - m_lib/flad/flad.py
Fix exceptions for Py3 compatibility
[m_lib.git] / m_lib / flad / flad.py
index b2e91c87e96d9f7006cc0f6cb0669d3daf75bede..8ca1093607a4b3d1f7b5fa5ca77ed07d5d93b637 100644 (file)
@@ -4,18 +4,15 @@
 """
 
 
-import string
-from UserList import UserList
-
-
 # Flad restriction error
-checking_error = "flad.checking_error"
+class checking_error(Exception):
+    pass
 
 # Default key/value separator
 def_keysep = ": "
 
 
-class Flad(UserList):
+class Flad(list):
    """
       Class to represent memory database.
       FLAD database is a list of records,
@@ -27,7 +24,7 @@ class Flad(UserList):
    # field_sep = rec_sep = '\n'
 
    def __init__(self, check_record_func = None, key_sep = def_keysep):
-      UserList.__init__(self)
+      list.__init__(self)
       self.check_record_func = check_record_func
       self.key_sep = key_sep
 
@@ -40,7 +37,7 @@ class Flad(UserList):
          if callable(self.check_record_func):
             return self.check_record_func(self, record)
          else:
-            raise TypeError, "non-callable restriction function"
+            raise TypeError("non-callable restriction function")
       else:
          return 1
 
@@ -53,42 +50,42 @@ class Flad(UserList):
       if not self.check_record(item):
          self.checking_error()
       else:
-         UserList.__setitem__(self, i, item)
+         list.__setitem__(self, i, item)
 
 
-   def __setslice__(self, i, j, list):
-      if list:
-         copy_list = list[:]
-         for item in list:
+   def __setslice__(self, i, j, v_list):
+      if v_list:
+         copy_list = v_list[:]
+         for item in v_list:
             if not self.check_record(item):
                self.checking_error()
                del copy_list[copy_list.index(item)]
-         UserList.__setslice__(self, i, j, copy_list)
+         list.__setslice__(self, i, j, copy_list)
 
 
    def append(self, item):
       if not self.check_record(item):
          self.checking_error()
       else:
-         UserList.append(self, item)
+         list.append(self, item)
 
 
    def insert(self, i, item):
       if not self.check_record(item):
          self.checking_error()
       else:
-         UserList.insert(self, i, item)
+         list.insert(self, i, item)
 
 
    def split_key(self, line):
       """
          Split input line to key/value pair and add the pair to dictionary
       """
-      ###line = string.lstrip(line) # Do not rstrip - if empty value, this will remove space from key
+      ###line = line.lstrip() # Do not rstrip - if empty value, this will remove space from key
       if line[-1] == '\n':
          line = line[:-1] # Chop
 
-      l = string.split(line, self.key_sep, 1) # Just split to key and reminder
+      l = line.split(self.key_sep, 1) # Just split to key and reminder
       return tuple(l)
 
 
@@ -105,7 +102,7 @@ class Flad(UserList):
                                   # so it is not ready to be checked :(
                                   # And, of course, two keys with the same name
                                   # cannot be added to dictionary
-            raise KeyError, "field key \"" + key + "\" already in record"
+            raise KeyError("field key \"" + key + "\" already in record")
 
          record[key] = value
 
@@ -119,12 +116,12 @@ class Flad(UserList):
    def feed(self, record, line): # Method can be overriden in subclasses
       if line:
          if self.wait_comment:
-            if string.strip(line) == '':
+            if line.strip() == '':
                self.comment = self.comment + '\n'
                self.wait_string = 0
                return 0
 
-            elif string.lstrip(line)[0] == '#':
+            elif line.lstrip()[0] == '#':
                self.comment = self.comment + line
                return 0
 
@@ -228,7 +225,7 @@ class Flad(UserList):
             flush_record = 1    # Set flag for all but 1st record
 
          if copy_rec:
-            for key in copy_rec.keys():
+            for key in list(copy_rec.keys()):
                outfile.write(key + self.key_sep + copy_rec[key] + '\n')
                del copy_rec[key]