1.1 --- a/imiptools/stores/file.py Thu May 12 17:51:10 2016 +0200
1.2 +++ b/imiptools/stores/file.py Thu May 12 22:58:57 2016 +0200
1.3 @@ -29,7 +29,7 @@
1.4 from imiptools.period import FreeBusyPeriod, FreeBusyGroupPeriod, \
1.5 FreeBusyOfferPeriod, FreeBusyCollection, \
1.6 FreeBusyGroupCollection, FreeBusyOffersCollection
1.7 -from imiptools.text import parse_line
1.8 +from imiptools.text import get_table, set_defaults
1.9 from os.path import isdir, isfile, join
1.10 from os import listdir, remove, rmdir
1.11 import codecs
1.12 @@ -47,18 +47,13 @@
1.13 # Utility methods.
1.14
1.15 def _set_defaults(self, t, empty_defaults):
1.16 - for i, default in empty_defaults:
1.17 - if i >= len(t):
1.18 - t += [None] * (i - len(t) + 1)
1.19 - if not t[i]:
1.20 - t[i] = default
1.21 - return t
1.22 + return set_defaults(t, empty_defaults)
1.23
1.24 - def _get_table(self, user, filename, empty_defaults=None, tab_separated=True):
1.25 + def _get_table(self, filename, empty_defaults=None, tab_separated=True):
1.26
1.27 """
1.28 - From the file for the given 'user' having the given 'filename', return
1.29 - a list of tuples representing the file's contents.
1.30 + From the file having the given 'filename', return a list of tuples
1.31 + representing the file's contents.
1.32
1.33 The 'empty_defaults' is a list of (index, value) tuples indicating the
1.34 default value where a column either does not exist or provides an empty
1.35 @@ -69,21 +64,7 @@
1.36 splitting each line of the file using tab characters as separators.
1.37 """
1.38
1.39 - f = codecs.open(filename, "rb", encoding="utf-8")
1.40 - try:
1.41 - l = []
1.42 - for line in f.readlines():
1.43 - line = line.strip(" \r\n")
1.44 - if tab_separated:
1.45 - t = line.split("\t")
1.46 - else:
1.47 - t = parse_line(line)
1.48 - if empty_defaults:
1.49 - t = self._set_defaults(t, empty_defaults)
1.50 - l.append(tuple(t))
1.51 - return l
1.52 - finally:
1.53 - f.close()
1.54 + return get_table(filename, empty_defaults, tab_separated)
1.55
1.56 def _get_table_atomic(self, user, filename, empty_defaults=None, tab_separated=True):
1.57
1.58 @@ -102,15 +83,14 @@
1.59
1.60 self.acquire_lock(user)
1.61 try:
1.62 - return self._get_table(user, filename, empty_defaults, tab_separated)
1.63 + return self._get_table(filename, empty_defaults, tab_separated)
1.64 finally:
1.65 self.release_lock(user)
1.66
1.67 - def _set_table(self, user, filename, items, empty_defaults=None):
1.68 + def _set_table(self, filename, items, empty_defaults=None):
1.69
1.70 """
1.71 - For the given 'user', write to the file having the given 'filename' the
1.72 - 'items'.
1.73 + Write to the file having the given 'filename' the 'items'.
1.74
1.75 The 'empty_defaults' is a list of (index, value) tuples indicating the
1.76 default value where a column either does not exist or provides an empty
1.77 @@ -146,7 +126,7 @@
1.78
1.79 self.acquire_lock(user)
1.80 try:
1.81 - self._set_table(user, filename, items, empty_defaults)
1.82 + self._set_table(filename, items, empty_defaults)
1.83 finally:
1.84 self.release_lock(user)
1.85