Esempio n. 1
0
	def getMakesModels(self):
	
		mms = {}
		returnList = []
		
		for k, v in self.conn.getPPDs().items():

			if mms.get(v['ppd-make'], None) is None:
				mms[v['ppd-make']] = []

			v = camelCase(v)
			v['uuid'] = k
			
			mms[v['PpdMake']].append(v)
			
		for k,v in mms.items():
			v.sort(lambda a,b: cups.modelSort(a['PpdMakeAndModel'], b['PpdMakeAndModel']))
			returnList.append({
				'make': k,
				'models': v
			})
		
		returnList.sort(lambda a,b: cups.modelSort(a['make'],b['make'] ))
		
		print json.dumps(returnList)
Esempio n. 2
0
    def _findBestMatchPPDs (self, mdls, mdl):
        """
        Find the best-matching PPDs based on the MDL Device ID.
        This function could be made a lot smarter.
        """

        _debugprint ("Trying best match")
        mdll = mdl.lower ()
        if mdll.endswith (" series"):
            # Strip " series" from the end of the MDL field.
            mdll = mdll[:-7]
            mdl = mdl[:-7]
        best_mdl = None
        best_matchlen = 0
        mdlnames = list(mdls.keys ())

        # Perform a case-insensitive model sort on the names.
        mdlnamesl = [(x, x.lower()) for x in mdlnames]
        mdlnamesl.append ((mdl, mdll))
        mdlnamesl.sort (key=functools.cmp_to_key(lambda x, y: cups.modelSort(x[1], y[1])))
        i = mdlnamesl.index ((mdl, mdll))
        candidates = [mdlnamesl[i - 1]]
        if i + 1 < len (mdlnamesl):
            candidates.append (mdlnamesl[i + 1])
            _debugprint (candidates[0][0] + " <= " + mdl + " <= " +
                        candidates[1][0])
        else:
            _debugprint (candidates[0][0] + " <= " + mdl)

        # Look at the models immediately before and after ours in the
        # sorted list, and pick the one with the longest initial match.
        for (candidate, candidatel) in candidates:
            prefix = os.path.commonprefix ([candidatel, mdll])
            if len (prefix) > best_matchlen:
                best_mdl = list(mdls[candidate].keys ())
                best_matchlen = len (prefix)
                _debugprint ("%s: match length %d" % (candidate, best_matchlen))

        # Did we match more than half of the model name?
        if best_mdl and best_matchlen > (len (mdll) / 2):
            ppdnamelist = best_mdl
            if best_matchlen == len (mdll):
                fit = self.FIT_EXACT
            else:
                fit = self.FIT_CLOSE
        else:
            fit = self.FIT_NONE
            ppdnamelist = None

            # Last resort.  Find the "most important" word in the MDL
            # field and look for a match based solely on that.  If
            # there are digits, try lowering the number of
            # significant figures.
            mdlnames.sort (key=functools.cmp_to_key(cups.modelSort))
            mdlitems = [(x.lower (), mdls[x]) for x in mdlnames]
            modelid = None
            for word in mdll.split (' '):
                if modelid == None:
                    modelid = word

                have_digits = False
                for i in range (len (word)):
                    if word[i].isdigit ():
                        have_digits = True
                        break

                if have_digits:
                    modelid = word
                    break

            digits = 0
            digits_start = -1
            digits_end = -1
            for i in range (len (modelid)):
                if modelid[i].isdigit ():
                    if digits_start == -1:
                        digits_start = i
                    digits_end = i
                    digits += 1
                elif digits_start != -1:
                    break
            digits_end += 1
            modelnumber = 0
            if digits > 0:
                modelnumber = int (modelid[digits_start:digits_end])
                modelpattern = (modelid[:digits_start] + "%d" +
                                modelid[digits_end:])
                _debugprint ("Searching for model ID '%s', '%s' %% %d" %
                             (modelid, modelpattern, modelnumber))
                ignore_digits = 0
                best_mdl = None
                found = False
                while ignore_digits < digits:
                    div = pow (10, ignore_digits)
                    modelid = modelpattern % ((modelnumber / div) * div)
                    _debugprint ("Ignoring %d of %d digits, trying %s" %
                                 (ignore_digits, digits, modelid))

                    for (name, ppds) in mdlitems:
                        for word in name.split (' '):
                            if word.lower () == modelid:
                                found = True
                                break

                        if found:
                            best_mdl = list(ppds.keys ())
                            break

                    if found:
                        break

                    ignore_digits += 1
                    if digits < 2:
                        break

                if found:
                    ppdnamelist = best_mdl
                    fit = self.FIT_CLOSE

        return (fit, ppdnamelist)
Esempio n. 3
0
 def compare_models (a,b):
     first = normalize (a)
     second = normalize (b)
     return cups.modelSort(first, second)
Esempio n. 4
0
    def _findBestMatchPPDs(self, mdls, mdl):
        """
        Find the best-matching PPDs based on the MDL Device ID.
        This function could be made a lot smarter.
        """

        _debugprint("Trying best match")
        mdll = mdl.lower()
        if mdll.endswith(" series"):
            # Strip " series" from the end of the MDL field.
            mdll = mdll[:-7]
            mdl = mdl[:-7]
        best_mdl = None
        best_matchlen = 0
        mdlnames = list(mdls.keys())

        # Perform a case-insensitive model sort on the names.
        mdlnamesl = [(x, x.lower()) for x in mdlnames]
        mdlnamesl.append((mdl, mdll))
        mdlnamesl.sort(
            key=functools.cmp_to_key(lambda x, y: cups.modelSort(x[1], y[1])))
        i = mdlnamesl.index((mdl, mdll))
        candidates = [mdlnamesl[i - 1]]
        if i + 1 < len(mdlnamesl):
            candidates.append(mdlnamesl[i + 1])
            _debugprint(candidates[0][0] + " <= " + mdl + " <= " +
                        candidates[1][0])
        else:
            _debugprint(candidates[0][0] + " <= " + mdl)

        # Look at the models immediately before and after ours in the
        # sorted list, and pick the one with the longest initial match.
        for (candidate, candidatel) in candidates:
            prefix = os.path.commonprefix([candidatel, mdll])
            if len(prefix) > best_matchlen:
                best_mdl = list(mdls[candidate].keys())
                best_matchlen = len(prefix)
                _debugprint("%s: match length %d" % (candidate, best_matchlen))

        # Did we match more than half of the model name?
        if best_mdl and best_matchlen > (len(mdll) / 2):
            ppdnamelist = best_mdl
            if best_matchlen == len(mdll):
                fit = self.FIT_EXACT
            else:
                fit = self.FIT_CLOSE
        else:
            fit = self.FIT_NONE
            ppdnamelist = None

            # Last resort.  Find the "most important" word in the MDL
            # field and look for a match based solely on that.  If
            # there are digits, try lowering the number of
            # significant figures.
            mdlnames.sort(key=functools.cmp_to_key(cups.modelSort))
            mdlitems = [(x.lower(), mdls[x]) for x in mdlnames]
            modelid = None
            for word in mdll.split(' '):
                if modelid is None:
                    modelid = word

                have_digits = False
                for i in range(len(word)):
                    if word[i].isdigit():
                        have_digits = True
                        break

                if have_digits:
                    modelid = word
                    break

            digits = 0
            digits_start = -1
            digits_end = -1
            for i in range(len(modelid)):
                if modelid[i].isdigit():
                    if digits_start == -1:
                        digits_start = i
                    digits_end = i
                    digits += 1
                elif digits_start != -1:
                    break
            digits_end += 1
            modelnumber = 0
            if digits > 0:
                modelnumber = int(modelid[digits_start:digits_end])
                modelpattern = (modelid[:digits_start] + "%d" +
                                modelid[digits_end:])
                _debugprint("Searching for model ID '%s', '%s' %% %d" %
                            (modelid, modelpattern, modelnumber))
                ignore_digits = 0
                best_mdl = None
                found = False
                while ignore_digits < digits:
                    div = pow(10, ignore_digits)
                    modelid = modelpattern % ((modelnumber / div) * div)
                    _debugprint("Ignoring %d of %d digits, trying %s" %
                                (ignore_digits, digits, modelid))

                    for (name, ppds) in mdlitems:
                        for word in name.split(' '):
                            if word.lower() == modelid:
                                found = True
                                break

                        if found:
                            best_mdl = list(ppds.keys())
                            break

                    if found:
                        break

                    ignore_digits += 1
                    if digits < 2:
                        break

                if found:
                    ppdnamelist = best_mdl
                    fit = self.FIT_CLOSE

        return (fit, ppdnamelist)
Esempio n. 5
0
 def compare_models(a, b):
     first = normalize(a)
     second = normalize(b)
     return cups.modelSort(first, second)