<commit_before># -*- coding: utf-8 -*-
import os
import logging
import httplib as http
from mako.template import Template

from framework import request
from framework.exceptions import HTTPError

from website.project.decorators import must_be_contributor_or_public
from website.project.decorators import must_have_addon
from website.util import rubeus

from website.addons.github.exceptions import ApiError
from website.addons.github.api import GitHub, build_github_urls, ref_to_params
from website.addons.github.views.util import _get_refs, _check_permissions
from website.addons.github.exceptions import NotFoundError, EmptyRepoError


logger = logging.getLogger(__name__)

logging.getLogger('github3').setLevel(logging.WARNING)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.WARNING)

# TODO: Probably should just take a node object instead of having to pass
# in both url and api_url
def to_hgrid(data, node_url, node_api_url=None, branch=None, sha=None,
             can_edit=True, parent=None, **kwargs):
    """
    :param list data: The return value of Github's `contents` endpoint.
    """
    grid = []
    folders = {}

    for datum in data:
        if data[datum].type in ['file', 'blob']:
            item = {
                rubeus.KIND: rubeus.FILE,
                'urls': build_github_urls(
                    data[datum], node_url, node_api_url, branch, sha
                )
            }
        elif data[datum].type in ['tree', 'dir']:
            item = {
                rubeus.KIND: rubeus.FOLDER,
                'children': [],
            }
        else:
            continue

        item.update({
            'addon': 'github',
            'permissions': {
                'view': True,
                'edit': can_edit,
            },
            'urls': build_github_urls(
                data[datum], node_url, node_api_url, branch, sha
            ),
            'accept': {
                'maxSize': kwargs.get('max_size', 128),
                'acceptedFiles': kwargs.get('accepted_files', None)
            }
        })

        head, item['name'] = os.path.split(data[datum].path)
        if parent:
            head = head.split(parent)[-1]
        if head:
            folders[head]['children'].append(item)
        else:
            grid.append(item)

        # Update cursor
        if item[rubeus.KIND] == rubeus.FOLDER:
            key = data[datum].path
            if parent:
                key = key.split(parent)[-1]
            folders[key] = item

    return grid


github_branch_template = Template('''
    % if len(branches) > 1:
            <select class="github-branch-select">
                % for each in branches:
                    <option value="${each}" ${"selected" if each == branch else ""}>${each}</option>
                % endfor
            </select>
    % else:
        <span>${branch}</span>
    % endif
    % if sha:
        <a href="https://github.com/${owner}/${repo}/commit/${sha}" class="github-sha text-muted">${sha[:10]}</a>
    % endif
''')

def github_branch_widget(branches, owner, repo, branch, sha):
    """Render branch selection widget for GitHub add-on. Displayed in the
    name field of HGrid file trees.

    """
    rendered = github_branch_template.render(
        branches=[each.name for each in branches],
        branch=branch,
        sha=sha,
        owner=owner,
        repo=repo
    )
    return rendered


def github_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    # Initialize repo here in the event that it is set in the privacy check
    # below. This potentially saves an API call in _check_permissions, below.
    repo = None

    # Quit if privacy mismatch and not contributor
    if node_settings.owner.is_public:
        repo = connection.repo(node_settings.user, node_settings.repo)
        if repo.private:
            return

    try:
        branch, sha, branches = _get_refs(
            node_settings,
            branch=kwargs.get('branch'),
            sha=kwargs.get('sha'),
            connection=connection,
        )
    except NotFoundError:
        # TODO: Show an alert or change GitHub configuration?
        logger.error('GitHub repo  not found')
        return

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = _check_permissions(
            node_settings, auth, connection, branch, sha, repo=repo,
        )
        name_append = github_branch_widget(branches, owner=node_settings.user,
            repo=node_settings.repo, branch=branch, sha=sha)
    else:

        ref = None
        can_edit = False
        name_append = None

    name_tpl = '{user}/{repo}'.format(
        user=node_settings.user, repo=node_settings.repo
    )

    permissions = {
        'edit': can_edit,
        'view': True
    }
    urls = {
        'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''),
        'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''),
        'branch': node_settings.owner.api_url + 'github/hgrid/root/',
    }
    return [rubeus.build_addon_root(
        node_settings,<commit_msg>handle error when github repo is inaccessible<commit_after># -*- coding: utf-8 -*-
import os
import logging
import httplib as http
from mako.template import Template

from framework import request
from framework.status import push_status_message
from framework.exceptions import HTTPError

from website.project.decorators import must_be_contributor_or_public
from website.project.decorators import must_have_addon
from website.util import rubeus

from website.addons.github.exceptions import ApiError
from website.addons.github.api import GitHub, build_github_urls, ref_to_params
from website.addons.github.views.util import _get_refs, _check_permissions
from website.addons.github.exceptions import NotFoundError, EmptyRepoError


logger = logging.getLogger(__name__)

logging.getLogger('github3').setLevel(logging.WARNING)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.WARNING)

# TODO: Probably should just take a node object instead of having to pass
# in both url and api_url
def to_hgrid(data, node_url, node_api_url=None, branch=None, sha=None,
             can_edit=True, parent=None, **kwargs):
    """
    :param list data: The return value of Github's `contents` endpoint.
    """
    grid = []
    folders = {}

    for datum in data:
        if data[datum].type in ['file', 'blob']:
            item = {
                rubeus.KIND: rubeus.FILE,
                'urls': build_github_urls(
                    data[datum], node_url, node_api_url, branch, sha
                )
            }
        elif data[datum].type in ['tree', 'dir']:
            item = {
                rubeus.KIND: rubeus.FOLDER,
                'children': [],
            }
        else:
            continue

        item.update({
            'addon': 'github',
            'permissions': {
                'view': True,
                'edit': can_edit,
            },
            'urls': build_github_urls(
                data[datum], node_url, node_api_url, branch, sha
            ),
            'accept': {
                'maxSize': kwargs.get('max_size', 128),
                'acceptedFiles': kwargs.get('accepted_files', None)
            }
        })

        head, item['name'] = os.path.split(data[datum].path)
        if parent:
            head = head.split(parent)[-1]
        if head:
            folders[head]['children'].append(item)
        else:
            grid.append(item)

        # Update cursor
        if item[rubeus.KIND] == rubeus.FOLDER:
            key = data[datum].path
            if parent:
                key = key.split(parent)[-1]
            folders[key] = item

    return grid


github_branch_template = Template('''
    % if len(branches) > 1:
            <select class="github-branch-select">
                % for each in branches:
                    <option value="${each}" ${"selected" if each == branch else ""}>${each}</option>
                % endfor
            </select>
    % else:
        <span>${branch}</span>
    % endif
    % if sha:
        <a href="https://github.com/${owner}/${repo}/commit/${sha}" class="github-sha text-muted">${sha[:10]}</a>
    % endif
''')

def github_branch_widget(branches, owner, repo, branch, sha):
    """Render branch selection widget for GitHub add-on. Displayed in the
    name field of HGrid file trees.

    """
    rendered = github_branch_template.render(
        branches=[each.name for each in branches],
        branch=branch,
        sha=sha,
        owner=owner,
        repo=repo
    )
    return rendered


def github_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    # Initialize repo here in the event that it is set in the privacy check
    # below. This potentially saves an API call in _check_permissions, below.
    repo = None

    # Quit if privacy mismatch and not contributor
    if node_settings.owner.is_public:
        try:
            repo = connection.repo(node_settings.user, node_settings.repo)
        except NotFoundError:
            # TODO: Test me @jmcarp
            # TODO: Add warning message
            logging.error('Could not access GitHub repo')
            return None
        if repo.private:
            return None

    try:
        branch, sha, branches = _get_refs(
            node_settings,
            branch=kwargs.get('branch'),
            sha=kwargs.get('sha'),
            connection=connection,
        )
    except NotFoundError:
        # TODO: Show an alert or change GitHub configuration?
        logger.error('GitHub repo not found')
        return

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = _check_permissions(
            node_settings, auth, connection, branch, sha, repo=repo,
        )
        name_append = github_branch_widget(branches, owner=node_settings.user,
            repo=node_settings.repo, branch=branch, sha=sha)
    else:

        ref = None
        can_edit = False
        name_append = None

    name_tpl = '{user}/{repo}'.format(
        user=node_settings.user, repo=node_settings.repo
    )

    permissions = {
        'edit': can_edit,
        'view': True
    }
    urls = {
        'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''),
        'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''),
        'branch': node_settings.owner.api_url + 'github/hgrid/root/',
    }
    return [rubeus.build_addon_root(
        node_settings,
{'fraction_non_alphanumeric': 0.10823030079350433, 'fraction_numerical': 0.002029894814541428, 'mean_word_length': 2.192636229749632, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 2, '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': '3207366', 'n_tokens_mistral': 3198, 'n_tokens_neox': 3117, 'n_words': 784}