def __link_for_file(self, fname, warn=False):
        try:
            import shotgun_api3_preview as sg
            for line in [line for line in self.prefs.link_map.split('\n') if line]:
                try:
                    # translate the lines link_type: match into a useful regexp
                    (link_type, link_match) = [s.strip() for s in line.split(':', 1)]
                    re_match = link_match
                    # translate glob syntax to regexp
                    re_match = re_match.replace('?', '.{1}')
                    re_match = re_match.replace('*', '[^%s]+' % os.sep)
                    name_map = {}

                    def track_names(match):
                        """map temp group names to the pattern they are generated for"""
                        pat = '(?P<name%s>[^%s]+)' % (len(name_map), os.sep)
                        name_map['name%d' % len(name_map)] = match.group('name')
                        return pat
                    # find all variable replacements and replace them with regexp
                    # syntax that'll grab the text from the file paths for us
                    result_re = self.__PATTERN_RE.sub(track_names, re_match)
                    # now we've got a workable regexp, match against the file name
                    match = re.search(result_re, fname)
                    if match:
                        # we have a rule that matches, create a shotgun filter
                        # representing that match and see if it worked
                        filters = []
                        for (key, value) in match.groupdict().iteritems():
                            filters.append([name_map[key], 'is', value])
                        link = self.__conn.find_one(link_type, filters, ['name', 'code'])
                        return link
                except ValueError:
                    if warn:
                        QtGui.QMessageBox.warning(self, self.tr("uploader"),
                            self.tr("Couldn't parse link map line '%s'.  Fix your Preferences." % line),
                            QtGui.QMessageBox.Ok)
                except sg.Fault:
                    # error in shotgun, no link, keep on trying
                    pass
            # no matches
        except ImportError:
            # couldn't import shotgun, not configured correctly, just return
            pass
        return None
dataset/ETHPy150Open shotgunsoftware/pyqt-uploader/uploader.py/Uploader.__link_for_file/original