<commit_before>"""
rave file system provider module.

This module provides a source for rave's virtual file system that sources from a real existing file system.
"""
import os
from os import path
import builtins
import errno
from functools import wraps
import rave.filesystem as fs

__author__ = 'Shiz'
__version__ = '0.1.0'
__version_info__ = (0, 1, 0)
__license__ = '2-clause BSD'



## Internal module stuff.

def _is_error(self, exception, *errors):
    """ Determine if an exception belongs to one of the given named errno module errors. """
    errors = [ getattr(errno, error, None) for error in errors ]
    return exception.errno in errors

def _translate_errors(f):
    """ Translate native errors to rave file system errors. """
    @wraps(f)
    def translate(self, *args, **kwargs):
        filename = None
        # Kind of dirty.
        if isinstance(self, FileSystemFile):
            filename = self.filename
        elif isinstance(self, FileSystemProvider):
            filename = self.basepath

        try:
            return f(self, *args, **kwargs)
        except fs.FileSystemError:
            raise
        except FileNotFoundError as e:
            raise fs.FileNotFound(filename) from e
        except PermissionError as e:
            raise fs.AccessDenied(filename) from e
        except IsADirectoryError as e:
            raise fs.NotAFile(filename) from e
        except NotADirectoryError as e:
            raise fs.NotADirectory(filename) from e
        except IOError as e:
            # Translate errno errors.
            if _is_error(e, 'EPERM', 'EACCES', 'EFAULT', 'EBUSY'):
                raise fs.AccessDenied(filename) from e
            if _is_error(e, 'ENOENT', 'ENXIO', 'ENODEV'):
                raise fs.FileNotFound(filename) from e
            if _is_error(e, 'ENOTDIR'):
                raise fs.NotADirectory(filename) from e
            if _is_error(e, 'EISDIR'):
                raise fs.NotAFile(filename) from e
            if _is_error(e, 'EROFS'):
                raise fs.FileNotWritable(filename) from e
            # We have no clue, raise a native error.
            raise fs.NativeError(filename, e) from e

    return translate

def _ensure_opened(f):
    """ Check if file is open before doing anything. """
    @wraps(f)
    def check(self, *args, **kwargs):
        if not self.opened():
            raise fs.FileClosed(self.filename)
        return f(self, *args, **kwargs)

    return check


class FileSystemProvider:
    """ A provider that sources from an actual underlying file system. """

    def __init__(self, filesystem, basepath):
        self.filesystem = filesystem
        self.basepath = basepath
        self._file_list = None

        if not path.exists(self.basepath):
            raise fs.FileNotFound(self.basepath)
        elif not path.isdir(self.basepath):
            raise fs.NotADirectory(self.basepath)

    def __repr__(self):
        return '<{cls}[{base}]>'.format(cls=self.__class__.__name__, base=self.basepath)

    def _to_native_path(self, filename):
        """ Translate virtual file system path to native file system path. """
        filename = filename.lstrip(self.filesystem.PATH_SEPARATOR).replace(self.filesystem.PATH_SEPARATOR, os.sep)
        return path.join(self.basepath, filename)

    def _from_native_path(self, filename):
        """ Translate native file system path to virtual file system path. """
        filename = path.relpath(filename, self.basepath)
        return filename.replace(os.sep, self.filesystem.PATH_SEPARATOR)<commit_msg>modules/filesystemprovider: Take absolute path before doing anything.<commit_after>"""
rave file system provider module.

This module provides a source for rave's virtual file system that sources from a real existing file system.
"""
import os
from os import path
import builtins
import errno
from functools import wraps
import rave.filesystem as fs

__author__ = 'Shiz'
__version__ = '0.1.0'
__version_info__ = (0, 1, 0)
__license__ = '2-clause BSD'



## Internal module stuff.

def _is_error(self, exception, *errors):
    """ Determine if an exception belongs to one of the given named errno module errors. """
    errors = [ getattr(errno, error, None) for error in errors ]
    return exception.errno in errors

def _translate_errors(f):
    """ Translate native errors to rave file system errors. """
    @wraps(f)
    def translate(self, *args, **kwargs):
        filename = None
        # Kind of dirty.
        if isinstance(self, FileSystemFile):
            filename = self.filename
        elif isinstance(self, FileSystemProvider):
            filename = self.basepath

        try:
            return f(self, *args, **kwargs)
        except fs.FileSystemError:
            raise
        except FileNotFoundError as e:
            raise fs.FileNotFound(filename) from e
        except PermissionError as e:
            raise fs.AccessDenied(filename) from e
        except IsADirectoryError as e:
            raise fs.NotAFile(filename) from e
        except NotADirectoryError as e:
            raise fs.NotADirectory(filename) from e
        except IOError as e:
            # Translate errno errors.
            if _is_error(e, 'EPERM', 'EACCES', 'EFAULT', 'EBUSY'):
                raise fs.AccessDenied(filename) from e
            if _is_error(e, 'ENOENT', 'ENXIO', 'ENODEV'):
                raise fs.FileNotFound(filename) from e
            if _is_error(e, 'ENOTDIR'):
                raise fs.NotADirectory(filename) from e
            if _is_error(e, 'EISDIR'):
                raise fs.NotAFile(filename) from e
            if _is_error(e, 'EROFS'):
                raise fs.FileNotWritable(filename) from e
            # We have no clue, raise a native error.
            raise fs.NativeError(filename, e) from e

    return translate

def _ensure_opened(f):
    """ Check if file is open before doing anything. """
    @wraps(f)
    def check(self, *args, **kwargs):
        if not self.opened():
            raise fs.FileClosed(self.filename)
        return f(self, *args, **kwargs)

    return check


class FileSystemProvider:
    """ A provider that sources from an actual underlying file system. """

    def __init__(self, filesystem, basepath):
        self.filesystem = filesystem
        self.basepath = path.abspath(basepath)
        self._file_list = None

        if not path.exists(self.basepath):
            raise fs.FileNotFound(self.basepath)
        elif not path.isdir(self.basepath):
            raise fs.NotADirectory(self.basepath)

    def __repr__(self):
        return '<{cls}[{base}]>'.format(cls=self.__class__.__name__, base=self.basepath)

    def _to_native_path(self, filename):
        """ Translate virtual file system path to native file system path. """
        filename = filename.lstrip(self.filesystem.PATH_SEPARATOR).replace(self.filesystem.PATH_SEPARATOR, os.sep)
        return path.join(self.basepath, filename)

    def _from_native_path(self, filename):
        """ Translate native file system path to virtual file system path. """
        filename = path.relpath(filename, self.basepath)
        return filename.replace(os.sep, self.filesystem.PATH_SEPARATOR)
{'fraction_non_alphanumeric': 0.09366314619232907, 'fraction_numerical': 0.0019455252918287938, 'mean_word_length': 2.5699404761904763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2949074', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2005, 'n_words': 643}