<commit_before>import sys
import re
import os

from django.conf import settings
from logging import getLogger
from django.db.utils import IntegrityError

from restclients.models.sws import Person
from restclients.models.canvas import CanvasUser
from restclients.canvas.users import Users as CanvasUsers
from restclients.canvas.accounts import Accounts as CanvasAccounts
from restclients.sws.campus import get_all_campuses
from restclients.sws.college import get_all_colleges
from restclients.sws.department import get_departments_by_college
from restclients.exceptions import DataFailureException

from suds.client import Client
from suds.transport.http import HttpTransport
from suds import WebFault

from astra.models import Admin, Account
from sis_provisioner.models import User
from sis_provisioner.loader import load_user
from sis_provisioner.policy import UserPolicy

import urllib2
import socket
import ssl
import httplib


class ASTRAException(Exception):
    pass


class HTTPSTransportV3(HttpTransport):
    def __init__(self, *args, **kwargs):
        HttpTransport.__init__(self, *args, **kwargs)

    def u2open(self, u2request):
        tm = self.options.timeout
        url = urllib2.build_opener(HTTPSClientAuthHandler())
        if self.u2ver() < 2.6:
            socket.setdefaulttimeout(tm)
            return url.open(u2request)
        else:
            return url.open(u2request, timeout=tm)


class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def __init__(self):
        urllib2.HTTPSHandler.__init__(self)

    def https_open(self, req):
        return self.do_open(HTTPSConnectionClientCertV3, req)

    def getConnection(self, host, timeout=300):
        return HTTPSConnectionClientCertV3(host)


class HTTPSConnectionClientCertV3(httplib.HTTPSConnection):
    def __init__(self, *args, **kwargs):
        httplib.HTTPSConnection.__init__(self, *args, **kwargs)
        self.key_file = settings.ASTRA_KEY
        self.cert_file = settings.ASTRA_CERT

    def connect(self):
        sock = socket.create_connection((self.host, self.port), self.timeout)
        if self._tunnel_host:
            self.sock = sock
            self._tunnel()
        try:
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
                                        ssl_version=ssl.PROTOCOL_TLSv1)
        except ssl.SSLError, e:
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
                                        ssl_version=ssl.PROTOCOL_SSLv3)


class ASTRA():
    """Load admin table with ASTRA-defined administrators
    """
    def __init__(self, options={}):
        self._astra = Client(settings.ASTRA_WSDL,
                             transport=HTTPSTransportV3())
        # prepare to map spans of control to campus and college resource values
        self._user_policy = UserPolicy()
        self._campuses = get_all_campuses()
        self._colleges = get_all_colleges()
        self._pid = os.getpid()
        self._log = getLogger(__name__)
        self._re_non_academic_code = re.compile(r'^canvas_([0-9]+)$')
        self._canvas_ids = {}
        self._verbosity = int(options.get('verbosity', 0))

    def _request(self, methodName, params={}):
        port = 'AuthzProvider'
        try:
            result = self._astra.service[port][methodName](params)
            return result
        except WebFault, err:
            self._log.error(err)
        except:
            self._log.error('Other error: ' + str(sys.exc_info()[1]))

        return None

    def _getAuthz(self, authFilter):
        return self._request('GetAuthz', authFilter)

    def get_version(self):
        return self._request('GetVersion', {})

    def _add_admin(self, **kwargs):
        netid = kwargs['net_id']
        regid = kwargs['reg_id']
        self._log.info('ADD: %s is %s in %s' % (netid,
                                                kwargs['role'],
                                                kwargs['account_id']))

        try:
            User.objects.get(reg_id=regid)
        except User.DoesNotExist:
            try:
                person = self._user_policy.get_person_by_netid(netid)
                name = person.full_name if (
                    isinstance(person, Person)) else person.display_name

                self._log.info('Provisioning admin: %s, %s, %s)' % (
                    person.uwnetid, person.uwregid, name))

                canvas = CanvasUsers()
                try:
                    user = canvas.get_user_by_sis_id(person.uwregid)
                except DataFailureException:
                    user = canvas.create_user(CanvasUser(name=name,
                                              login_id=person.uwnetid,
                                              sis_user_id=person.uwregid))

                load_user(person)

            except Exception, err:
                self._log.info('Skipped admin: %s (%s)' % (netid, err))
                return

        try:
            admin = Admin.objects.get(net_id=netid,<commit_msg>use sis provisioner dao, adds email to create_user<commit_after>import sys
import re
import os

from django.conf import settings
from logging import getLogger
from django.db.utils import IntegrityError

from restclients.models.canvas import CanvasUser
from restclients.canvas.users import Users as CanvasUsers
from restclients.canvas.accounts import Accounts as CanvasAccounts
from restclients.sws.campus import get_all_campuses
from restclients.sws.college import get_all_colleges
from restclients.sws.department import get_departments_by_college
from restclients.exceptions import DataFailureException

from suds.client import Client
from suds.transport.http import HttpTransport
from suds import WebFault

from astra.models import Admin, Account
from sis_provisioner.models import User
from sis_provisioner.loader import load_user
from sis_provisioner.dao.user import get_person_by_netid, user_fullname,\
    user_email

import urllib2
import socket
import ssl
import httplib


class ASTRAException(Exception):
    pass


class HTTPSTransportV3(HttpTransport):
    def __init__(self, *args, **kwargs):
        HttpTransport.__init__(self, *args, **kwargs)

    def u2open(self, u2request):
        tm = self.options.timeout
        url = urllib2.build_opener(HTTPSClientAuthHandler())
        if self.u2ver() < 2.6:
            socket.setdefaulttimeout(tm)
            return url.open(u2request)
        else:
            return url.open(u2request, timeout=tm)


class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def __init__(self):
        urllib2.HTTPSHandler.__init__(self)

    def https_open(self, req):
        return self.do_open(HTTPSConnectionClientCertV3, req)

    def getConnection(self, host, timeout=300):
        return HTTPSConnectionClientCertV3(host)


class HTTPSConnectionClientCertV3(httplib.HTTPSConnection):
    def __init__(self, *args, **kwargs):
        httplib.HTTPSConnection.__init__(self, *args, **kwargs)
        self.key_file = settings.ASTRA_KEY
        self.cert_file = settings.ASTRA_CERT

    def connect(self):
        sock = socket.create_connection((self.host, self.port), self.timeout)
        if self._tunnel_host:
            self.sock = sock
            self._tunnel()
        try:
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
                                        ssl_version=ssl.PROTOCOL_TLSv1)
        except ssl.SSLError, e:
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
                                        ssl_version=ssl.PROTOCOL_SSLv3)


class ASTRA():
    """Load admin table with ASTRA-defined administrators
    """
    def __init__(self, options={}):
        self._astra = Client(settings.ASTRA_WSDL,
                             transport=HTTPSTransportV3())
        # prepare to map spans of control to campus and college resource values
        self._campuses = get_all_campuses()
        self._colleges = get_all_colleges()
        self._pid = os.getpid()
        self._log = getLogger(__name__)
        self._re_non_academic_code = re.compile(r'^canvas_([0-9]+)$')
        self._canvas_ids = {}
        self._verbosity = int(options.get('verbosity', 0))

    def _request(self, methodName, params={}):
        port = 'AuthzProvider'
        try:
            result = self._astra.service[port][methodName](params)
            return result
        except WebFault, err:
            self._log.error(err)
        except:
            self._log.error('Other error: ' + str(sys.exc_info()[1]))

        return None

    def _getAuthz(self, authFilter):
        return self._request('GetAuthz', authFilter)

    def get_version(self):
        return self._request('GetVersion', {})

    def _add_admin(self, **kwargs):
        netid = kwargs['net_id']
        regid = kwargs['reg_id']
        self._log.info('ADD: %s is %s in %s' % (netid,
                                                kwargs['role'],
                                                kwargs['account_id']))

        try:
            User.objects.get(reg_id=regid)
        except User.DoesNotExist:
            try:
                person = get_person_by_netid(netid)

                self._log.info('Provisioning admin: %s (%s)' % (
                    person.uwnetid, person.uwregid))

                canvas = CanvasUsers()
                try:
                    user = canvas.get_user_by_sis_id(person.uwregid)
                except DataFailureException:
                    user = canvas.create_user(
                        CanvasUser(name=user_fullname(person),
                                   login_id=person.uwnetid,
                                   sis_user_id=person.uwregid,
                                   email=user_email(person)))

                load_user(person)

            except Exception, err:
                self._log.info('Skipped admin: %s (%s)' % (netid, err))
                return

        try:
            admin = Admin.objects.get(net_id=netid,
{'fraction_non_alphanumeric': 0.09005778043434948, 'fraction_numerical': 0.004981071926678621, 'mean_word_length': 2.246765847347995, 'pattern_counts': {'":': 0, '<': 5, '<?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': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1404377', 'n_tokens_mistral': 2966, 'n_tokens_neox': 2867, 'n_words': 617}