public
Created

Update node materials - Python

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.