Skip to content

Instantly share code, notes, and snippets.

@nurav
Created April 29, 2016 15:20
Show Gist options
  • Save nurav/ae830c443230f97788de404c63be80c6 to your computer and use it in GitHub Desktop.
Save nurav/ae830c443230f97788de404c63be80c6 to your computer and use it in GitHub Desktop.
import re
import xml.etree.ElementTree as ET
from auslib import AUS
from auslib.blobs.base import Blob
from auslib.errors import BadDataError
class SuperBlob(Blob):
jsonschema = "superblob.yml"
def __init__(self, **kwargs):
Blob.__init__(self, **kwargs)
if "schema_version" not in self:
self["schema_version"] = 1000
def _getConstituentBlobs(self, updateQuery):
constituentBlobs = []
for product in self["products"]:
updateQueryForProduct = updateQuery.copy()
updateQueryForProduct['product'] = product
blob, update_type = AUS.evaluateRules(updateQueryForProduct)
constituentBlobs.append((blob, updateQueryForProduct, update_type))
return constituentBlobs
def _findRootTag(self, blobXMLs):
# For now, we return the enclosing tag of the first blob.
# We could change this ensure that all the XMLs have similar enclosing tags.
blobXML = blobXMLs.get(0, None)
if blobXML is None:
raise BadDataError
tree = ET.fromstring(blobXML)
root = tree.getroot()
updates = root.find('update')
return list(updates)[0]
def _findChildTags(self, blobXMLs):
childTags = []
for blobXML in blobXMLs:
tree = ET.fromstring(blobXML)
root = tree.getroot()
for update in root.findall('updates'):
for child in update:
childTags.append(child.text)
return childTags
def shouldServeUpdate(self, updateQuery):
# Since a superblob update will always be returned.
return True
def createXML(self, updateQuery, update_type, whitelistedDomains, specialForceHosts):
blobs = self._getConstituentBlobs(updateQuery)
blobXMLs = []
for blob, updateQueryForProduct, productUpdateType in blobs:
blobXML = blob.createXML(updateQueryForProduct, productUpdateType, whitelistedDomains, specialForceHosts)
blobXMLs.append(blobXML)
rootTag = self._findRootTag(blobXMLs)
xml = ['<?xml version="1.0"?>']
xml.append('<updates>')
xml.append(' <%s>' % (rootTag))
xml.extend(self._findChildTags(blobXMLs))
xml.append(' </%s>' % (rootTag))
xml.append('</updates>')
# ensure valid xml by using the right entity for ampersand
return re.sub('&(?!amp;)', '&amp;', '\n'.join(xml))
---
title: Super Blob
type: object
required:
- name
- schema_version
- products
additionalProperties: false
properties:
name:
type: string
description: Human readable identifier
minLength: 1
maxLength: 100
schema_version:
type: number
description: Blob identifier number
enum:
- 1000
products:
type: array
items:
type: string
description: The name of the response product
minLength: 1
maxLength: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment