1#!/usr/bin/env python
2#############################################################################
3# Copyright (c) 2015-2018 Balabit
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 as published
7# by the Free Software Foundation, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17#
18# As an additional exemption you are allowed to compile & link against the
19# OpenSSL libraries as published by the OpenSSL project. See the file
20# COPYING for details.
21#
22#############################################################################
23from src.common.file import File
24
25
26class FileIO():
27    def __init__(self, file_path):
28        self.__readable_file = File(file_path)
29        self.__writeable_file = File(file_path)
30
31    def read_number_of_lines(self, counter):
32        if not self.__readable_file.is_opened():
33            if not self.__readable_file.wait_for_creation():
34                raise Exception("{} was not created in time.".format(self.__readable_file.path))
35            self.__readable_file.open("r")
36
37        return self.__readable_file.wait_for_number_of_lines(counter)
38
39    def read_until_lines(self, lines):
40        if not self.__readable_file.is_opened():
41            if not self.__readable_file.wait_for_creation():
42                raise Exception("{} was not created in time.".format(self.__readable_file.path))
43            self.__readable_file.open("r")
44
45        return self.__readable_file.wait_for_lines(lines)
46
47    def write(self, content):
48        if not self.__writeable_file.is_opened():
49            self.__writeable_file.open("a+")
50
51        self.__writeable_file.write(content)
52