# Path: mdn/utils.py
# def is_new_id(_id):
#     """Detect if an ID signifies a new resource.
# 
#     New resource IDs are text strings prefixed with an underscore.
#     """
#     return isinstance(_id, text_type) and _id[0] == '_'
# 
# def normalize_name(name):
#     """Normalize a name for IDs, slugs."""
#     to_remove = ('<code>', '</code>', '&lt;', '&gt;')
#     normalized_name = name.lower()
#     for removal in to_remove:
#         normalized_name = normalized_name.replace(removal, '')
#     return normalized_name
# 
# def slugify(word, length=50, suffix=''):
#     """Create a slugged version of a word or phrase."""
#     raw = word.lower()
#     out = []
#     acceptable = string.ascii_lowercase + string.digits + '_-'
#     for c in raw:
#         if c in acceptable:
#             out.append(c)
#         else:
#             out.append('_')
#     slugged = ''.join(out)
#     while '__' in slugged:
#         slugged = slugged.replace('__', '_')
#     if slugged.endswith('_') and len(slugged) > 1:
#         slugged = slugged[:-1]
#     suffix = text_type(suffix) if suffix else ''
#     with_suffix = slugged[slice(length - len(suffix))] + suffix
#     return with_suffix


from collections import namedtuple
from webplatformcompat.models import (
    Browser, Feature, Section, Specification, Support, Version)
from .utils import is_new_id, normalize_name, slugify
                base_slug = parent_feature.slug + '_' + nname
                feature_slug = slugify(base_slug, suffix=attempt)
                if Feature.objects.filter(slug=feature_slug).exists():
                    attempt += 1
                    feature_slug = ''
            subfeatures[nname] = self.FeatureParams(
                None, feature_id, feature_slug)

        return self.subfeature_data[parent_feature.id][nname]

    def lookup_section_id(self, spec_id, subpath, locale='en'):
        """Retrieve a section ID given a Specification ID and subpath."""
        for section in Section.objects.filter(specification_id=spec_id):
            if section.subpath.get(locale) == subpath:
                return section.id
        return None

    def lookup_specification(self, mdn_key):
        """Retrieve a Specification by key."""
        if mdn_key not in self.specifications:
            try:
                spec = Specification.objects.get(mdn_key=mdn_key)
            except Specification.DoesNotExist:
                spec = None
            self.specifications[mdn_key] = spec
        return self.specifications[mdn_key]

    def lookup_support_id(self, version_id, feature_id):
        """Lookup or create a support ID for a version and feature."""
        support = None

# Path: mdn/utils.py
# def is_new_id(_id):
#     """Detect if an ID signifies a new resource.
# 
#     New resource IDs are text strings prefixed with an underscore.
#     """
#     return isinstance(_id, text_type) and _id[0] == '_'
# 
# def normalize_name(name):
#     """Normalize a name for IDs, slugs."""
#     to_remove = ('<code>', '</code>', '&lt;', '&gt;')
#     normalized_name = name.lower()
#     for removal in to_remove:
#         normalized_name = normalized_name.replace(removal, '')
#     return normalized_name
# 
# def slugify(word, length=50, suffix=''):
#     """Create a slugged version of a word or phrase."""
#     raw = word.lower()
#     out = []
#     acceptable = string.ascii_lowercase + string.digits + '_-'
#     for c in raw:
#         if c in acceptable:
#             out.append(c)
#         else:
#             out.append('_')
#     slugged = ''.join(out)
#     while '__' in slugged:
#         slugged = slugged.replace('__', '_')
#     if slugged.endswith('_') and len(slugged) > 1:
#         slugged = slugged[:-1]
#     suffix = text_type(suffix) if suffix else ''
#     with_suffix = slugged[slice(length - len(suffix))] + suffix
#     return with_suffix


# Path: mdn/data.py
from collections import namedtuple
from webplatformcompat.models import (
    Browser, Feature, Section, Specification, Support, Version)
from .utils import is_new_id, normalize_name, slugify

                base_slug = parent_feature.slug + '_' + nname
                feature_slug = slugify(base_slug, suffix=attempt)
                if Feature.objects.filter(slug=feature_slug).exists():
                    attempt += 1
                    feature_slug = ''
            subfeatures[nname] = self.FeatureParams(
                None, feature_id, feature_slug)

        return self.subfeature_data[parent_feature.id][nname]

    def lookup_section_id(self, spec_id, subpath, locale='en'):
        """Retrieve a section ID given a Specification ID and subpath."""
        for section in Section.objects.filter(specification_id=spec_id):
            if section.subpath.get(locale) == subpath:
                return section.id
        return None

    def lookup_specification(self, mdn_key):
        """Retrieve a Specification by key."""
        if mdn_key not in self.specifications:
            try:
                spec = Specification.objects.get(mdn_key=mdn_key)
            except Specification.DoesNotExist:
                spec = None
            self.specifications[mdn_key] = spec
        return self.specifications[mdn_key]

    def lookup_support_id(self, version_id, feature_id):
        """Lookup or create a support ID for a version and feature."""
        support = None

        real_version = not is_new_id(version_id)