<commit_before>from django.core.urlresolvers import reverse
from django.core.urlresolvers import NoReverseMatch


class MenuBase(object):
    """
    Base class that generates menu list.
    """
    def __init__(self):
        self.path = ''
        self.is_staff = False
        self.is_superuser = False
        self.is_authenticated = False

    def save_user_state(self, request):
        """
        Given a request object, store the current user attributes
        """
        self.path = request.path
        self.is_staff = request.user.is_staff
        self.is_superuser = request.user.is_superuser
        self.is_authenticated = request.user.is_authenticated()

    def is_true(self, item_dict, key):
        """
        Given a menu item dictionary, and a key, it returns True if key is set to True
        else returns False
        """
        yep = item_dict.get(key, False)
        return yep

    def show_to_all(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be shown
        for both authenticated and unauthenticated users. (e.g. a `contact` menu item)
        """
        show = self.is_true(item_dict, 'render_for_unauthenticated') and \
            self.is_true(item_dict, 'render_for_authenticated')
        return show

    def show_to_authenticated(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to authenticated users. (e.g. a `logout` menu item)
        """
        show = self.is_true(item_dict, 'render_for_authenticated') and self.is_authenticated
        return show

    def show_to_unauthenticated(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to unauthenticated users. (e.g. a `login` menu item)
        """
        show = self.is_true(item_dict, 'render_for_unauthenticated') and not self.is_authenticated
        return show

    def is_superuser_safe(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to super users. (e.g. a `admin` menu item)
        """
        yep = True
        if self.is_true(item_dict, 'render_for_superuser') and not self.is_superuser:
            yep = False
        return yep

    def is_staff_safe(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to staff users. (e.g. a `limited admin` menu item)
        """
        yep = True
        if self.is_true(item_dict, 'render_for_staff') and not self.is_staff:
            yep = False
        return yep

    def has_name(self, item_dict):
        """
        Given a menu item dictionary, it returns true if attribute `name` is set.
        """
        yep = self.is_true(item_dict, 'name')
        if yep:
            if not item_dict.get('name'):
                yep = False
        return yep

    def get_url(self, item_dict):
        """
        Given a menu item dictionary, it returns the URL or an empty string.
        """
        final_url = ''
        url = item_dict.get('url', '')
        try:
            final_url = reverse(url)
        except NoReverseMatch:
            final_url = url
        return final_url

    def has_url(self, item_dict):
        """
        Given a menu item dictionary, it returns true if attribute `url` is set.
        """
        if not self.get_url(item_dict):
            return False
        return True

    def process_url(self, item_dict, best_match_url):
        """
        Given a menu item dictionary, it returns a consumable `url` and
        the `best_match_url`.
        """
        url = self.get_url(item_dict)

        # record the based matched url on the requested path
        if len(url) > 1 and url in self.path:
            if len(best_match_url) < len(url):
                best_match_url = url

        return url, best_match_url

    def process_breadcrums(self, menu_list, best_match_url):
        """
        Given a menu list, it marks the best match url as selected, which
        can be used as breadcrumbs
        """
        matched_index = -1
        for item in menu_list:
            if best_match_url == item['url']:
                matched_index = menu_list.index(item)
                break
        if matched_index > -1:
            menu_list[matched_index]['selected'] = True

    def get_submenu_list(self, parent_dict):
        """
        Given a menu item dictionary, it returns a submenu if one exist, or
        returns None
        """
        submenu = parent_dict.get('submenu', None)
        if submenu is None:
            return submenu

        for item in submenu:
            item['render_for_staff'] = self.is_true(parent_dict, 'render_for_staff')
            item['render_for_superuser'] = self.is_true(parent_dict, 'render_for_superuser')
            item['render_for_authenticated'] = self.is_true(parent_dict, 'render_for_authenticated')
            item['render_for_unauthenticated'] = self.is_true(parent_dict, 'render_for_unauthenticated')

        submenu = self.generate_menu(submenu)
        if not submenu:
            return None
        return submenu
<commit_msg>add inheritable attributes<commit_after>from django.core.urlresolvers import reverse
from django.core.urlresolvers import NoReverseMatch


class MenuBase(object):
    """
    Base class that generates menu list.
    """
    def __init__(self):
        self.path = ''
        self.is_staff = False
        self.is_superuser = False
        self.is_authenticated = False
        self.inheritable_attributes = [
            'render_for_staff',
            'render_for_superuser',
            'render_for_authenticated',
            'render_for_unauthenticated',
        ]

    def save_user_state(self, request):
        """
        Given a request object, store the current user attributes
        """
        self.path = request.path
        self.is_staff = request.user.is_staff
        self.is_superuser = request.user.is_superuser
        self.is_authenticated = request.user.is_authenticated()

    def is_true(self, item_dict, key):
        """
        Given a menu item dictionary, and a key, it returns True if key is set to True
        else returns False
        """
        yep = item_dict.get(key, False)
        return yep

    def show_to_all(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be shown
        for both authenticated and unauthenticated users. (e.g. a `contact` menu item)
        """
        show = self.is_true(item_dict, 'render_for_unauthenticated') and \
            self.is_true(item_dict, 'render_for_authenticated')
        return show

    def show_to_authenticated(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to authenticated users. (e.g. a `logout` menu item)
        """
        show = self.is_true(item_dict, 'render_for_authenticated') and self.is_authenticated
        return show

    def show_to_unauthenticated(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to unauthenticated users. (e.g. a `login` menu item)
        """
        show = self.is_true(item_dict, 'render_for_unauthenticated') and not self.is_authenticated
        return show

    def is_superuser_safe(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to super users. (e.g. a `admin` menu item)
        """
        yep = True
        if self.is_true(item_dict, 'render_for_superuser') and not self.is_superuser:
            yep = False
        return yep

    def is_staff_safe(self, item_dict):
        """
        Given a menu item dictionary, it returns true if menu item should be only shown
        to staff users. (e.g. a `limited admin` menu item)
        """
        yep = True
        if self.is_true(item_dict, 'render_for_staff') and not self.is_staff:
            yep = False
        return yep

    def has_name(self, item_dict):
        """
        Given a menu item dictionary, it returns true if attribute `name` is set.
        """
        yep = self.is_true(item_dict, 'name')
        if yep:
            if not item_dict.get('name'):
                yep = False
        return yep

    def get_url(self, item_dict):
        """
        Given a menu item dictionary, it returns the URL or an empty string.
        """
        final_url = ''
        url = item_dict.get('url', '')
        try:
            final_url = reverse(url)
        except NoReverseMatch:
            final_url = url
        return final_url

    def has_url(self, item_dict):
        """
        Given a menu item dictionary, it returns true if attribute `url` is set.
        """
        if not self.get_url(item_dict):
            return False
        return True

    def process_url(self, item_dict, best_match_url):
        """
        Given a menu item dictionary, it returns a consumable `url` and
        the `best_match_url`.
        """
        url = self.get_url(item_dict)

        # record the based matched url on the requested path
        if len(url) > 1 and url in self.path:
            if len(best_match_url) < len(url):
                best_match_url = url

        return url, best_match_url

    def process_breadcrums(self, menu_list, best_match_url):
        """
        Given a menu list, it marks the best match url as selected, which
        can be used as breadcrumbs
        """
        matched_index = -1
        for item in menu_list:
            if best_match_url == item['url']:
                matched_index = menu_list.index(item)
                break
        if matched_index > -1:
            menu_list[matched_index]['selected'] = True

    def copy_attributes(self, parent_dict, child_dict, attrs):
        """
        Given a list of attribute, it copies the same attributes from the parent
        dict to the child dict.
        """
        for attr in attrs:
            if self.is_true(parent_dict, attr):
                child_dict[attr] = True

    def get_submenu_list(self, parent_dict):
        """
        Given a menu item dictionary, it returns a submenu if one exist, or
        returns None
        """
        submenu = parent_dict.get('submenu', None)
        if submenu is None:
            return submenu

        for child_dict in submenu:
            self.copy_attributes(parent_dict, child_dict, self.inheritable_attributes)

        submenu = self.generate_menu(submenu)
        if not submenu:
            return None
        return submenu

{'fraction_non_alphanumeric': 0.08093291209812302, 'fraction_numerical': 0.0005575171901133618, 'mean_word_length': 2.0620199146514935, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4178661', 'n_tokens_mistral': 3107, 'n_tokens_neox': 3026, 'n_words': 964}