Created
January 18, 2012 00:46
-
-
Save dave5/1630064 to your computer and use it in GitHub Desktop.
Update node materials - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_node_materials(node, URL, keydata, verbose=True): | |
""" for a manufacturing node: | |
- retrieve the catalog in json format | |
- convert it to python dictionary and list structures | |
""" | |
location = node[u'name'] | |
date = node[u'materials_updated_at'] | |
key = node[u'key'] | |
date = date[:-6] # strip off seconds | |
recorded_materials = Node_materials[location] | |
ourdate = datetime.strptime(recorded_materials[0], "%Y/%m/%d %H:%M:%S") | |
newdate = datetime.strptime(date, "%Y/%m/%d %H:%M:%S") | |
if verbose: print "Checking: %s vs. %s" % (ourdate, newdate) | |
if ourdate <= newdate: | |
# need to update some or all materials | |
catalog = recorded_materials[1] | |
catalogURL = "%s%s%s" % (URL, nodesTAG, material_catalogTAG) | |
request = "%s%s?%s" %(catalogURL, key, keydata) | |
if verbose: print "Requesting Catalog updates for %s\n %s" % (location, request) | |
req = Request(request) | |
result = basic_request(req) | |
if not result[0]: | |
# fail | |
print result[1] | |
else: # Success | |
materials = json.loads(result[1][0]) | |
show_materials(materials, location) | |
# update the local store | |
#!!! | |
else: # no need to update | |
print "No change to materials for %s" % (location) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment