~nottrobin/charms/trusty/wsgi-app/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/fstab.py

  • Committer: Robin Winslow
  • Date: 2014-12-02 22:54:40 UTC
  • Revision ID: robin@robinwinslow.co.uk-20141202225440-ruuctvfe7pdh1dd8
Try reverting most of charmhelpers to the old version

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
5
5
 
6
 
import io
7
6
import os
8
7
 
9
8
 
10
 
class Fstab(io.FileIO):
 
9
class Fstab(file):
11
10
    """This class extends file in order to implement a file reader/writer
12
11
    for file `/etc/fstab`
13
12
    """
25
24
                options = "defaults"
26
25
 
27
26
            self.options = options
28
 
            self.d = int(d)
29
 
            self.p = int(p)
 
27
            self.d = d
 
28
            self.p = p
30
29
 
31
30
        def __eq__(self, o):
32
31
            return str(self) == str(o)
46
45
            self._path = path
47
46
        else:
48
47
            self._path = self.DEFAULT_PATH
49
 
        super(Fstab, self).__init__(self._path, 'rb+')
 
48
        file.__init__(self, self._path, 'r+')
50
49
 
51
50
    def _hydrate_entry(self, line):
52
 
        # NOTE: use split with no arguments to split on any
53
 
        #       whitespace including tabs
54
51
        return Fstab.Entry(*filter(
55
52
            lambda x: x not in ('', None),
56
 
            line.strip("\n").split()))
 
53
            line.strip("\n").split(" ")))
57
54
 
58
55
    @property
59
56
    def entries(self):
60
57
        self.seek(0)
61
58
        for line in self.readlines():
62
 
            line = line.decode('us-ascii')
63
59
            try:
64
 
                if line.strip() and not line.startswith("#"):
 
60
                if not line.startswith("#"):
65
61
                    yield self._hydrate_entry(line)
66
62
            except ValueError:
67
63
                pass
77
73
        if self.get_entry_by_attr('device', entry.device):
78
74
            return False
79
75
 
80
 
        self.write((str(entry) + '\n').encode('us-ascii'))
 
76
        self.write(str(entry) + '\n')
81
77
        self.truncate()
82
78
        return entry
83
79
 
84
80
    def remove_entry(self, entry):
85
81
        self.seek(0)
86
82
 
87
 
        lines = [l.decode('us-ascii') for l in self.readlines()]
 
83
        lines = self.readlines()
88
84
 
89
85
        found = False
90
86
        for index, line in enumerate(lines):
99
95
        lines.remove(line)
100
96
 
101
97
        self.seek(0)
102
 
        self.write(''.join(lines).encode('us-ascii'))
 
98
        self.write(''.join(lines))
103
99
        self.truncate()
104
100
        return True
105
101