def simplify(name, prettyprint=True): """ Simplifies all the measure files in pdxml format of the given measure, converting them using the simplify XSL stylesheet. Old simplifications will be overwritten. If the prettyprint optional argument is True, the result will be formatted using the xmllint tool. """ host_shared = settings.PATHS["shared-measures"][0] pattern = os.path.join(host_shared, name, "*", "*.xml") simplifier = xml.Transformation(stylesheet("simplify.xsl")) for source in glob.glob(pattern): if len(os.path.basename(source).split(".")) == 3: dest = source.replace(".xml", ".simple.xml") simplifier.parameters["loopback"] = str(int(source.endswith(".lo.xml"))) simplifier.transform(source, dest) if prettyprint: xml.prettyprint(dest)
def decode(name, measure_case, prettyprint=False): """ Decodes the simplified XML representation of the given measure by adding a "decoded" element to each packet containing a payload. The decoding is done using an XSL transformation coupled with an xslt python extension function which provides the "decoded" element given a payload text string. """ host_shared = settings.PATHS["shared-measures"][0] types = os.path.join(measure_case, "types.py") types_registry = registry.TypesRegistry() types_registry.load("pas.conf.basetypes") try: types_registry.parse(types) except IOError: pass proto = protocol.MappingProtocol(types_registry) trans = xml.Transformation(stylesheet("decode.xsl")) def _decode(context, payload): """ Decoding callback """ # Convert the ascii representation back to binary data bin_payload = binascii.a2b_hex("".join(payload)) # Create an xdr stream with the payload stream = xdrlib.Unpacker(bin_payload) # Read the full frame length, it is not needed here _ = stream.unpack_uint() try: # Decode the remaining data as a full frame... # ...hoping that tcp hasn't split the message in more frames message = proto.decode_full_frame(stream) # @TODO: Logging, output and error management except EOFError as e: print "-" * 80 print context, "Not enough data:", e print repr(stream.get_buffer()) print "-" * 80 return except errors.UnknownClass as e: print "-" * 80 print context.context_node.attrib["timestamp"], print "Error while decoding packet:", e print binascii.b2a_hex(stream.get_buffer()) print "-" * 80 return except errors.UnknownMethod as e: print "-" * 80 print context.context_node.attrib["timestamp"], print "Error while decoding packet:", e print binascii.b2a_hex(stream.get_buffer()) print "-" * 80 return except xdrlib.Error as e: print "-" * 80 print context.context_node.attrib["timestamp"], e print repr(e.message) rest = stream.get_buffer() rem = stream.get_position() print binascii.b2a_hex(rest[rem:]) print print repr(rest[rem:]) print print str(rem) + "/" + str(_) print "*" * 80 return # Convert the message to xml and send it back to the XSL template return message.toxml() trans.register_function("http://gridgroup.eia-fr.ch/popc", _decode, "decode") # Apply transformation to all simplified xml files pattern = os.path.join(host_shared, name, "*", "*.simple.xml") for source in glob.glob(pattern): dest = source.replace(".simple.xml", ".decoded.xml") trans.transform(source, dest) if prettyprint: xml.prettyprint(dest)