<commit_before>from __future__ import absolute_import

from datafs.core import data_file
from datafs.core.versions import BumpableVersion
from datafs._compat import string_types
from contextlib import contextmanager
import fs1.utils, fs1.path
from fs1.osfs import OSFS
import os

def _process_version(self, version):
    if not self.versioned and version is None:
        return None

    elif not self.versioned and version is not None:
        raise ValueError('Cannot specify version on an unversioned archive.')

    elif version is None:
        return self.get_default_version()

    elif isinstance(version, BumpableVersion):
        return version

    elif isinstance(version, string_types) and version == 'latest':
        return self.get_latest_version()

    elif isinstance(version, string_types):
        return BumpableVersion(version)


class DataArchive(object):

    def __init__(
        self, 
        api, 
        archive_name, 
        authority_name, 
        archive_path, 
        versioned=True,
        default_version=None):

        self.api = api
        self.archive_name = archive_name

        self._authority_name = authority_name
        self._archive_path = archive_path

        self._versioned = versioned
        self._default_version = default_version

    def __repr__(self):
        return "<{} {}://{}>".format(self.__class__.__name__,
                                     self.authority_name, self.archive_name)

    @property
    def versioned(self):
        return self._versioned

    
    def get_latest_version(self):

        versions = self.get_versions()
    
        if len(versions) == 0:
            return None
    
        else:
            return max(versions)


    def get_versions(self):

        versions = self.get_history()
    
        if len(versions) == 0:
            return []

        elif not self.versioned:
            return [None]
    
        else:
            return sorted(map(BumpableVersion, set([v['version'] for v in versions]))) 


    
    def get_default_version(self):

        if not self.versioned:
            return None

        versions = self.get_versions()

        if self._default_version is None or self._default_version == 'latest':
            if len(versions) == 0:
                return None

            return max(versions)

        matches = filter(lambda v: v==self._default_version, self.get_versions())

        if len(matches) > 0:
            return max(matches)

        raise ValueError('Archive "{}" version {} not found'.format(
            self.archive_name, self._default_version))


    def get_version_path(self, version=None):
        '''
        Returns a storage path for the archive and version

        If the archive is versioned, the version number is used as the file path 
        and the archive path is the directory. If not, the archive path is used 
        as the file path.

        Parameters
        ----------
        version : str or object
            Version number to use as file name on versioned archives (default 
            latest unless ``default_version`` set)

        Examples
        --------

        .. code-block:: python

            >>> arch = DataArchive(None, 'arch', None, 'arch1', versioned=False)
            >>> print(arch.get_version_path())
            arch1
            >>>
            >>> ver = DataArchive(None, 'ver', None, 'arch2', versioned=True)
            >>> print(ver.get_version_path('0.0.0'))
            arch2/0.0
            >>>
            >>> print(ver.get_version_path('0.0.1a1'))
            arch2/0.0.1a1
            >>> 
            >>> print(ver.get_version_path('latest')) # doctest: +ELLIPSIS
            Traceback (most recent call last):
            ...
            AttributeError: 'NoneType' object has no attribute 'manager'


        '''

        version = _process_version(self, version)

        if self.versioned:
            return fs.path.join(self.archive_path, str(version))

        else:
            return self.archive_path


    @property
    def authority_name(self):<commit_msg>fs.path --> fs1.path<commit_after>from __future__ import absolute_import

from datafs.core import data_file
from datafs.core.versions import BumpableVersion
from datafs._compat import string_types
from contextlib import contextmanager
import fs1.utils, fs1.path
from fs1.osfs import OSFS
import os

def _process_version(self, version):
    if not self.versioned and version is None:
        return None

    elif not self.versioned and version is not None:
        raise ValueError('Cannot specify version on an unversioned archive.')

    elif version is None:
        return self.get_default_version()

    elif isinstance(version, BumpableVersion):
        return version

    elif isinstance(version, string_types) and version == 'latest':
        return self.get_latest_version()

    elif isinstance(version, string_types):
        return BumpableVersion(version)


class DataArchive(object):

    def __init__(
        self, 
        api, 
        archive_name, 
        authority_name, 
        archive_path, 
        versioned=True,
        default_version=None):

        self.api = api
        self.archive_name = archive_name

        self._authority_name = authority_name
        self._archive_path = archive_path

        self._versioned = versioned
        self._default_version = default_version

    def __repr__(self):
        return "<{} {}://{}>".format(self.__class__.__name__,
                                     self.authority_name, self.archive_name)

    @property
    def versioned(self):
        return self._versioned

    
    def get_latest_version(self):

        versions = self.get_versions()
    
        if len(versions) == 0:
            return None
    
        else:
            return max(versions)


    def get_versions(self):

        versions = self.get_history()
    
        if len(versions) == 0:
            return []

        elif not self.versioned:
            return [None]
    
        else:
            return sorted(map(BumpableVersion, set([v['version'] for v in versions]))) 


    
    def get_default_version(self):

        if not self.versioned:
            return None

        versions = self.get_versions()

        if self._default_version is None or self._default_version == 'latest':
            if len(versions) == 0:
                return None

            return max(versions)

        matches = filter(lambda v: v==self._default_version, self.get_versions())

        if len(matches) > 0:
            return max(matches)

        raise ValueError('Archive "{}" version {} not found'.format(
            self.archive_name, self._default_version))


    def get_version_path(self, version=None):
        '''
        Returns a storage path for the archive and version

        If the archive is versioned, the version number is used as the file path 
        and the archive path is the directory. If not, the archive path is used 
        as the file path.

        Parameters
        ----------
        version : str or object
            Version number to use as file name on versioned archives (default 
            latest unless ``default_version`` set)

        Examples
        --------

        .. code-block:: python

            >>> arch = DataArchive(None, 'arch', None, 'arch1', versioned=False)
            >>> print(arch.get_version_path())
            arch1
            >>>
            >>> ver = DataArchive(None, 'ver', None, 'arch2', versioned=True)
            >>> print(ver.get_version_path('0.0.0'))
            arch2/0.0
            >>>
            >>> print(ver.get_version_path('0.0.1a1'))
            arch2/0.0.1a1
            >>> 
            >>> print(ver.get_version_path('latest')) # doctest: +ELLIPSIS
            Traceback (most recent call last):
            ...
            AttributeError: 'NoneType' object has no attribute 'manager'


        '''

        version = _process_version(self, version)

        if self.versioned:
            return fs1.path.join(self.archive_path, str(version))

        else:
            return self.archive_path


    @property
    def authority_name(self):
{'fraction_non_alphanumeric': 0.09185420743639922, 'fraction_numerical': 0.006360078277886497, 'mean_word_length': 1.9897623400365632, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6408644', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2136, 'n_words': 604}