<commit_before>#!/usr/bin/env python
# encoding: utf-8
from yard.exceptions import VersionException
from yard.resources import Resource
import re

class ResourceVersions(object):

    def __init__(self, api, routes):
        for name,version in self.__class__.__dict__.iteritems():
            if callable(version) and issubclass(version, Resource):
                setattr(self, name, version(api, routes))

    def __call__(self, request, **kwargs):
        version = re.findall(r'.*version=(.*)', request.META.get('HTTP_ACCEPT', ''))
        requested_version = version[0] if version else request.GET.get('version')
        if version or hasattr(self, requested_version):
            return getattr(self, requested_version)(request, **kwargs)
        if hasattr(self, 'default'):
            return getattr(self, 'default')(request, **kwargs)
        if hasattr(self, 'latest'):
            return getattr(self, 'latest')(request, **kwargs)
        raise VersionException()
    
    @classmethod    
    def get_resource_attribute(self, attr):
        resource = getattr(self, 'latest', getattr(self, 'default'))
        return getattr(resource, attr, None) 
                <commit_msg>bugfix resource versioning<commit_after>#!/usr/bin/env python
# encoding: utf-8
from yard.exceptions import VersionException
from yard.resources import Resource
import re

class ResourceVersions(object):

    def __init__(self, api, routes):
        for name,version in self.__class__.__dict__.iteritems():
            if callable(version) and issubclass(version, Resource):
                setattr(self, name, version(api, routes))

    def __call__(self, request, **kwargs):
        version = re.findall(r'.*version=(.*)', request.META.get('HTTP_ACCEPT', ''))
        requested_version = version[0] if version else request.GET.get('version')
        if requested_version and hasattr(self, requested_version):
            return getattr(self, requested_version)(request, **kwargs)
        if hasattr(self, 'default'):
            return getattr(self, 'default')(request, **kwargs)
        if hasattr(self, 'latest'):
            return getattr(self, 'latest')(request, **kwargs)
        raise VersionException()
    
    @classmethod    
    def get_resource_attribute(self, attr):
        resource = getattr(self, 'latest', getattr(self, 'default'))
        return getattr(resource, attr, None) 
                
{'fraction_non_alphanumeric': 0.12229617304492513, 'fraction_numerical': 0.0016638935108153079, 'mean_word_length': 3.1826086956521737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4850817', 'n_tokens_mistral': 668, 'n_tokens_neox': 658, 'n_words': 170}