
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


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:
            # the .. is appended as we are in spec_cleaner sub_folder
            _file = open('{0}/../data/{1}'.format(os.path.dirname(os.path.realpath(__file__)), name), 'r')
        except IOError:
            # try venv structure
            try:
                _file = open('{0}/../usr/share/spec-cleaner/{1}'.format(
                    os.path.dirname(os.path.realpath(__file__)), name), 'r')
            except IOError as error:
                # try system dir
                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

Check an extra possible data dir when installing in a venv
Check an extra possible data dir when installing in a venv

When installing spec-cleaner in a virtual env, the data files
(i.e. "excludes-bracketing.txt") are available in a different
directory. Also check this directory.

Fixes #128

  
  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:
+             # the .. is appended as we are in spec_cleaner sub_folder
              _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 venv structure
              try:
-                 _file = open('/usr/share/spec-cleaner/{0}'.format(name), 'r')
+                 _file = open('{0}/../usr/share/spec-cleaner/{1}'.format(
+                     os.path.dirname(os.path.realpath(__file__)), name), 'r')
              except IOError as error:
+                 # try system dir
+                 try:
+                     _file = open('/usr/share/spec-cleaner/{0}'.format(name), 'r')
+                 except IOError as error:
-                 raise RpmException(error.strerror)
+                     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
  
Check an extra possible data dir when installing in a venv
## Code Before:

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

## Instruction:
Check an extra possible data dir when installing in a venv
## Code After:

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:
            # the .. is appended as we are in spec_cleaner sub_folder
            _file = open('{0}/../data/{1}'.format(os.path.dirname(os.path.realpath(__file__)), name), 'r')
        except IOError:
            # try venv structure
            try:
                _file = open('{0}/../usr/share/spec-cleaner/{1}'.format(
                    os.path.dirname(os.path.realpath(__file__)), name), 'r')
            except IOError as error:
                # try system dir
                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
