# vim: set ts=4 sw=4 et: coding=UTF-8

import os

from .rpmexception import RpmException


class FileUtils(object):

    """
    Class working with file operations.
    Read/write..
    """

    # file variable
    f = None

    def open_datafile(self, name):
        """
        Function to open data files.
        Used all around so kept glob here for importing.
        """

        try:
            _file = open('{0}/../data/{1}'.format(os.path.dirname(os.path.realpath(__file__)), name), 'r')
        except IOError:
            # the .. is appended as we are in spec_cleaner sub_folder
            try:
                _file = open('/usr/share/spec-cleaner/{0}'.format(name), 'r')
            except IOError as error:
                raise RpmException(error.strerror)

        self.f = _file

    def open(self, name, mode):
        """
        Function to open regular files with exception handling.
        """

        try:
            _file = open(name, mode)
        except IOError as error:
            raise RpmException(error.strerror)

        self.f = _file

    def close(self):
        """
        Just wrapper for closing the file
        """

        if self.f:
            self.f.close()
            self.f = None

    def __del__(self):
        self.close()
        self.f = None

@@ -377,32 +377,102 @@
 %22%0A%0A        try:%0A
+            # the .. is appended as we are in spec_cleaner sub_folder%0A
             _fil
@@ -605,63 +605,267 @@
  # t
-he .. is appended as we are in 
+ry venv structure%0A            try:%0A                _file = open('%7B0%7D/../usr/share/
 spec
-_
+-
 cleaner
- sub_folder%0A
+/%7B1%7D'.format(%0A                    os.path.dirname(os.path.realpath(__file__)), name), 'r')%0A            except IOError as error:%0A                # try system dir%0A    
     
@@ -869,32 +869,36 @@
            try:%0A
+    
                 
@@ -963,32 +963,36 @@
 r')%0A            
+    
 except IOError a
@@ -992,32 +992,36 @@
 Error as error:%0A
+    
                 

Check an extra possible data dir when installing in a venv