def __connect_to_shotgun(self):
        """
        Try to connect to shotgun, alerting on failure.
        If connected, sync up interface with shotgun instance settings.
        """
        # try to load up the shotgun api
        try:
            if os.path.isdir(self.prefs.shotgun_api):
                sys.path.append(self.prefs.shotgun_api)
            else:
                sys.path.append(os.path.dirname(self.prefs.shotgun_api))
            import shotgun_api3 as sg
        except ImportError:
            sys.path.pop()
            QtGui.QMessageBox.critical(self, self.tr("uploader"),
                self.tr("shotgun_api3_preview module not found.  Update your Preferences."),
                QtGui.QMessageBox.Ok)
            return None

        # try to connect to shotgun
        try:
            conn = sg.Shotgun(self.prefs.shotgun_url, self.prefs.shotgun_script, self.prefs.shotgun_key)
            # verify with a simple fine
            conn.find_one('Project', [])
        except Exception, e:
            QtGui.QMessageBox.critical(self, self.tr("uploader"),
                self.tr("Could not connect to shotgun:\n\n%s.\n\nUpdate your Preferences." - e.message),
                QtGui.QMessageBox.Ok)
            return None

        # validate connection by seeing if Attachments are accessible
        try:
            # use path_field to validate that is set right, if it is set
            conn.schema_field_read('Attachment', self.prefs.path_field or 'project')
        except socket.gaierror:
            # raised if connection straight out failed
            QtGui.QMessageBox.critical(self, self.tr("uploader"),
                self.tr("cannot connect to shotgun host.  Update your Preferences."),
                QtGui.QMessageBox.Ok)
            return None
        except sg.Fault, e:
            QtGui.QMessageBox.critical(self, self.tr("uploader"),
                self.tr("Cannot get path field:\n%s.\n\nUpdate your Preferences." % e.message),
                QtGui.QMessageBox.Ok)

        if conn:
            # got a conn.  get sync'ed up with it
            self.__conn = conn
            # load up defaults for entity selection fields
            settings = QtCore.QSettings('ShotgunSharing', 'uploader')
            default_project = str(settings.value("main/project", '').toString())
            default_link_type = str(settings.value("main/link_type", '').toString())
            # load up projects, populate the completer and combo box
            projects = [(p['name'], p['id']) for p in conn.find('Project', [], ['name']) if p['name'] != 'Template Project']
            completer = QtGui.QCompleter([p[0] for p in projects], self)
            completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
            self.gui.project.clear()
            for p in projects:
                self.gui.project.addItem(p[0], p[1])
                if p[0] == default_project:
                    # name matched default project, select it
                    self.gui.project.setCurrentIndex(self.gui.project.count()-1)
            self.gui.project.setCompleter(completer)
            self.gui.project.setValidator(QtGui.QRegExpValidator(QtCore.QRegExp('|'.join([p[0] for p in projects]), QtCore.Qt.CaseInsensitive), self))
            # setup link type, populate the completer and combo box
            # TODO: should make sure these are active in the instance
            link_types = ['', 'Asset', 'Scene', 'Sequence', 'Shot', 'Task', 'Project', 'Tool', 'Ticket']
            completer = QtGui.QCompleter(link_types, self)
            completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
            self.gui.link_type.clear()
            for l in link_types:
                self.gui.link_type.addItem(l)
                if l == default_link_type:
                    # name matches default, update selection
                    self.gui.link_type.setCurrentIndex(self.gui.link_type.count()-1)
            self.gui.link_type.setCompleter(completer)
            self.gui.link_type.setValidator(QtGui.QRegExpValidator(QtCore.QRegExp('|'.join(link_types), QtCore.Qt.CaseInsensitive), self))
            # default used to update reference to
            self.default_link = conn.find_one('HumanUser', [['login', 'is', getpass.getuser()]], ['name'])
            return conn
        return None
dataset/ETHPy150Open shotgunsoftware/pyqt-uploader/uploader.py/Uploader.__connect_to_shotgun/operator `%` -> `-`