Question

I am writing a script and in my script I have this function:

def insert_image(cursor, object_id, sku):
    product_obj = core.Object.get(object_id)
    string_sku = str(sku)
    folder = string_sku[0] + string_sku[1] + string_sku[2]
    found_url = False
    # KLUDGE This is ugly and redundant, however putting this in an if elif elif else throws error when url not found
    # try this url first
    try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False
    # If that one didn't work try this one
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku), "%sPK-PT,PM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # If still nothing, one last attempt
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku), "%sCC-PT,IM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # We failed to find an image for this product, it will have to be done manually
    if found_url == False:
        log.info("Could not find the image on notions")
        return False

    # Hey we found something! Open the image....
    send_image = open('%sPK-PT,PM.jpg' % sku, 'r')
    # ...and send it for processing
    if product_obj.set_image(send_image, 5, 1) == False:
        return False
    else:
        log.debug("Inserted Image")
        return True

This worked fine until I added the try catches. I did have the if, elif, the function ran just fine. Here is my call and the peice of code that runs right before it:

   if rollback == False:
        # Nah -- it's all good SAVE IT!
        count += 1
        log.debug("INSERT %s" % count)
        conn.commit()
    else:
        # Yeah something went wrong, errors reported why, roll it back
        conn.rollback()
        log.debug("skipped %s" % skip_count)

  # Insert images
        if rollback == False:
            sku = row[0]
            if insert_image(cursor, object_id, sku) == False:
                log.error("Could not get the image inserted for product: %s" % object_id)
                conn.rollback()
            else:
                conn.commit()

My error is:

16:33:46,153 DEBUG [pylons-admin] Inserted Description
16:33:46,164 DEBUG [pylons-admin] Inserted Attributes
16:33:46,164 DEBUG [pylons-admin] INSERT 1
Traceback (most recent call last):
File "<console>", line 47, in <module>
NameError: name 'insert_image' is not defined

I don't know what line 47 means because the call is on line 2101, again before I added the trys, it found the function just fine. I also switched the first commit to be before the insert_image call when I added the trys like you see now, before the commit was after we called insert_image. I checked indents, and spaces, and tabs w/ no avail.

I use TextMate, when I run the script from TextMate, I get a syntax error here:

 try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):

It points to the ( on (folder... But I don't see where I have a syntax error. Please help. I have been working on this script for a couple of weeks now, this was supposed to be the last run to test and call it finished :(

Was it helpful?

Solution

You have syntax errors in your function:

try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False

It should be:

try:
    urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
    urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
    found_url = True
except:
    found_url = False

You also have some broad catches, that catch those SyntaxErrors and hide errors, but insert_image is not defined this way. Never use alone except:, always put name of the Exception, that you want to catch. Otherwise you will also catch things like SyntaxError and this is very dangerous.

OTHER TIPS

This sounds like you have a whitespace issue before the method. The incorrect whitespace has moved it outside of the normal code path and if it is in a class it may not appear to be in the class any more

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top