Blob Blame History Raw
diff --git a/prnt/cups.py b/prnt/cups.py
index a9f410a..3783a60 100644
--- a/prnt/cups.py
+++ b/prnt/cups.py
@@ -489,78 +489,77 @@ def getPPDFile2(mq,model, ppds): # New PPD find
     #Check if common ppd name is already given in models.dat(This is needed because in case of devices having more than one derivatives
     #will have diffrent model name strings in device ID, because of which we don't get the common ppd name for search)
     family_check=isfamilydrv(ppds)
-    family_class=getFamilyClassName(model)
+
     model = models.normalizeModelName(model)
-    if family_check==0:
-       ppd_name = mq.get('ppd-name',0)
-    else:
-       ppd_name = mq.get('family-ppd',0)
 
-    if ppd_name == 0:
-        stripped_model = stripModel2(model)
-    else:
-        stripped_model = stripModel2(ppd_name)
+    ppd_name = mq.get('{}'.format('family-ppd' if family_check else 'ppd-name'), 0)
+
+    stripped_model = stripModel2(ppd_name) if ppd_name else stripModel2(model)
+
+    wanted_model = getFamilyClassName(model) if family_check else stripped_model
 
     log.debug("Matching PPD list to model  %s..." % stripped_model)
 
     matches = []
-    if family_check ==0 :
-        for f in ppds:
-            match = ppd_pat.match(f)
-            if match is not None:  
-                if match.group(1) == stripped_model:
-                    log.debug("Found match: %s" % f)
-                    try:
-                       pdls = match.group(2).split('-')
-                    except AttributeError:
-                         pdls = []
-                    if (prop.hpcups_build and 'hpijs' not in f) or \
-                        ((prop.hpijs_build and 'hpijs' in pdls) or (prop.hpcups_build and 'hpijs' not in pdls)) or \
-                         ('ps' in pdls) or ('pdf' in pdls):
-                          matches.append((f, [p for p in pdls if p and p != 'hpijs']))  
-    else:
-        for f in ppds:
-            match = ppd_pat1.match(f)
-            if match is not None: 
-                if match.group(1) == family_class:
-                    log.debug("Found match: %s" % f)
-                    try:
-                       pdls = match.group(2).split('-')
-                    except AttributeError:
-                         pdls = []
-                    if (prop.hpcups_build and 'hpijs' not in f) or \
-                        ((prop.hpijs_build and 'hpijs' in pdls) or (prop.hpcups_build and 'hpijs' not in pdls)) or \
-                         ('ps' in pdls) or ('pdf' in pdls):
-                          matches.append((f, [p for p in pdls if p and p != 'hpijs']))    
-    log.debug(matches)
-    num_matches = len(matches)
+    for f in ppds:
+        # ignore foomatic and gutenprint drivers
+        if 'foomatic' in f or 'gutenprint' in f:
+            continue
+
+        # see if driver type is in driver name
+        driver_types = []
+        if 'hpcups' in f:
+            driver_types.append('hpcups')
+        if 'hpijs' in f:
+            driver_types.append('hpijs')
+
+
+        ppd_filename = f.rsplit('/', 1)[1].split('.')[0].replace('hp-', '')
+
+        if not ppd_filename:
+            continue
+
+        # we need to sanitize the end of filename - there can be a driver type (-hpijs, -hpcups),
+        # pdl name (-zjstream, -pdf, -ps etc.) or the device can just have '-' in their name
+        # (HP Photosmart Premium C309g-m).
+        # So if we don't know the name after '-', take it as part of device name.
+        # If we know them either like driver type of PDL, remove the string from ppd name
+        # so we can compare it with stripped model
+        pdl_candidates = []
+        pdl_candidates = ppd_filename.split('-')[1:]
+
+        pdls = []
+        ppd_model = ppd_filename
+
+        for pdl in pdl_candidates:
+            if pdl in ['hpijs', 'hpcups']:
+                ppd_model=ppd_model.replace('-{}'.format(pdl), '')
+                continue
 
-    if num_matches == 0:
-        log.debug("No PPD found for model %s using new algorithm. Trying old algorithm..." % stripped_model)
-        #Using Old algo, ignores the series keyword in ppd searching.
-        matches2 = list(getPPDFile(stripModel(stripped_model), ppds).items())
-        log.debug(matches2)
-        num_matches2 = len(matches2)
-        if num_matches2:
-            for f, d in matches2:
-                match = ppd_pat.match(f)
-                if match is not None:
-                    log.debug("Found match: %s" % f)
-                    try:
-                        pdls = match.group(2).split('-')
-                    except AttributeError:
-                        pdls = []
+            if not models.PDL_TYPES.get(pdl):
+                log.debug('Unknown PDL named \'{}\' - can be a new PDL or '
+                          'just a part of device name. Assume it is '
+                          'a part of device name.'.format(pdl))
+            else:
+                pdls.append(pdl)
+                ppd_model=ppd_model.replace('-{}'.format(pdl), '')
+
+        if ppd_model != wanted_model and ppd_model != '{}_series'.format(wanted_model):
+            continue
+
+        log.debug("Found match: %s" % f)
 
-                    if (prop.hpcups_build and 'hpijs' not in f) or \
-                       ((prop.hpijs_build and 'hpijs' in pdls) or (prop.hpcups_build and 'hpijs' not in pdls)) or \
-                       ('ps' in pdls) or ('pdf' in pdls):
-                        matches.append((f, [p for p in pdls if p and p != 'hpijs']))
+        if (prop.hpcups_build and 'hpijs' not in f) or \
+            ((prop.hpijs_build and 'hpijs' in driver_types) or (prop.hpcups_build and 'hpijs' not in driver_types)) or \
+            ('ps' in pdls) or ('pdf' in pdls):
+            matches.append((f, pdls, [d for d in driver_types if d and d != 'hpijs']))
 
-        log.debug(matches)
-        num_matches = len(matches)
+
+    log.debug(matches)
+    num_matches = len(matches)
 
     if num_matches == 0:
-        log.error("No PPD found for model %s using old algorithm." % stripModel(stripped_model))
+        log.error("No PPD found for model %s." % stripModel(stripped_model))
         return None
 
     elif num_matches == 1:
@@ -570,7 +569,7 @@ def getPPDFile2(mq,model, ppds): # New PPD find
     # > 1
     log.debug("%d matches found. Searching based on PDL: Host > PS,PDF > PCL/Other" % num_matches)
     for p in [models.PDL_TYPE_HOST, models.PDL_TYPE_PS,models.PDL_TYPE_PDF, models.PDL_TYPE_PCL]:
-        for f, pdl_list in matches:
+        for f, pdl_list, driver_list in matches:
             for x in pdl_list:
                 # default to HOST-based PDLs, as newly supported PDLs will most likely be of this type
                 if models.PDL_TYPES.get(x, models.PDL_TYPE_HOST) == p:
@@ -579,8 +578,8 @@ def getPPDFile2(mq,model, ppds): # New PPD find
 
     log.debug("%d matches found. Searching based on Filters: HPCUPS > HPIJS" % num_matches)
     for p in ["hpcups","hpijs"]:
-        for f, pdl_list in matches:
-            if p in f:
+        for f, pdl_list, driver_list in matches:
+            if p in driver_list:
                 log.debug("Selecting PPD: %s" % (f))
                 return (f, '')