#http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing, software #distributed under the License is distributed on an "AS IS" BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #See the License for the specific language governing permissions and #limitations under the License. import cStringIO, md5 import simplejson import avro.schema as schema #The version implemented. VERSION = 1 _SYSTEM_ERROR = schema._StringSchema() _SYSTEM_ERRORS = schema._UnionSchema([_SYSTEM_ERROR]) class Protocol(object): """A set of messages forming an application protocol.""" def __init__(self, name=None, namespace=None): self.__types = schema._Names() self.__messages = dict() self.__name = name self.__namespace = namespace self.__md5 = None def getname(self): return self.__name
def nextdata(self, schm, d=0): if schm.gettype() == schema.BOOLEAN: return random.sample([True, False], 1)[0] elif schm.gettype() == schema.STRING: sample = random.sample('abcdefghijklmnopqrstuvwxyz1234590-=', random.randint(0,20)) string = cStringIO.StringIO() for s in sample: string.write(s) return unicode(string.getvalue()) elif schm.gettype() == schema.INT: return random.randint(io._INT_MIN_VALUE, io._INT_MAX_VALUE) elif schm.gettype() == schema.LONG: return random.randint(io._LONG_MIN_VALUE, io._LONG_MAX_VALUE) elif schm.gettype() == schema.FLOAT: return round(random.uniform(-1024, 1024)) elif schm.gettype() == schema.DOUBLE: return random.uniform(io._LONG_MIN_VALUE, io._LONG_MAX_VALUE) elif schm.gettype() == schema.BYTES: string = cStringIO.StringIO() len = random.randint(0, 20) for i in range(1, len): string.write(struct.pack('c',random.sample('12345abcd', 1)[0])) return string.getvalue() elif schm.gettype() == schema.NULL: return None elif schm.gettype() == schema.ARRAY: arr = list() len = random.randint(0, 4)+2-d if len < 0: len = 0 for i in range(1, len): arr.append(self.nextdata(schm.getelementtype(), d+1)) return arr elif schm.gettype() == schema.MAP: map = dict() len = random.randint(0, 4)+2-d if len < 0: len = 0 for i in range(1, len): map[self.nextdata(schema._StringSchema())] = self.nextdata( schm.getvaluetype(), d+1) return map elif schm.gettype() == schema.RECORD: m = dict() for field in schm.getfields().values(): m[field.getname()] = self.nextdata(field.getschema(), d+1) return m elif schm.gettype() == schema.UNION: datum = self.nextdata(random.choice(schm.getelementtypes()), d) return datum elif schm.gettype() == schema.ENUM: symbols = schm.getenumsymbols() len = symbols.__len__() if len == 0: return None return symbols[random.randint(0,len)-1] elif schm.gettype() == schema.FIXED: string = cStringIO.StringIO() for i in range(0, schm.getsize()): string.write(struct.pack('c',random.sample('12345abcd', 1)[0])) return string.getvalue()
def nextdata(self, schm, d=0): if schm.gettype() == schema.BOOLEAN: return random.sample([True, False], 1)[0] elif schm.gettype() == schema.STRING: sample = random.sample('abcdefghijklmnopqrstuvwxyz1234590-=', random.randint(0, 20)) string = cStringIO.StringIO() for s in sample: string.write(s) return unicode(string.getvalue()) elif schm.gettype() == schema.INT: return random.randint(io._INT_MIN_VALUE, io._INT_MAX_VALUE) elif schm.gettype() == schema.LONG: return random.randint(io._LONG_MIN_VALUE, io._LONG_MAX_VALUE) elif schm.gettype() == schema.FLOAT: return round(random.uniform(-1024, 1024)) elif schm.gettype() == schema.DOUBLE: return random.uniform(io._LONG_MIN_VALUE, io._LONG_MAX_VALUE) elif schm.gettype() == schema.BYTES: string = cStringIO.StringIO() len = random.randint(0, 20) for i in range(1, len): string.write(struct.pack('c', random.sample('12345abcd', 1)[0])) return string.getvalue() elif schm.gettype() == schema.NULL: return None elif schm.gettype() == schema.ARRAY: arr = list() len = random.randint(0, 4) + 2 - d if len < 0: len = 0 for i in range(1, len): arr.append(self.nextdata(schm.getelementtype(), d + 1)) return arr elif schm.gettype() == schema.MAP: map = dict() len = random.randint(0, 4) + 2 - d if len < 0: len = 0 for i in range(1, len): map[self.nextdata(schema._StringSchema())] = self.nextdata( schm.getvaluetype(), d + 1) return map elif schm.gettype() == schema.RECORD: m = dict() for field in schm.getfields().values(): m[field.getname()] = self.nextdata(field.getschema(), d + 1) return m elif schm.gettype() == schema.UNION: datum = self.nextdata(random.choice(schm.getelementtypes()), d) return datum elif schm.gettype() == schema.ENUM: symbols = schm.getenumsymbols() len = symbols.__len__() if len == 0: return None return symbols[random.randint(0, len) - 1] elif schm.gettype() == schema.FIXED: string = cStringIO.StringIO() for i in range(0, schm.getsize()): string.write(struct.pack('c', random.sample('12345abcd', 1)[0])) return string.getvalue()
#http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing, software #distributed under the License is distributed on an "AS IS" BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #See the License for the specific language governing permissions and #limitations under the License. import cStringIO, md5 import simplejson import avro.schema as schema #The version implemented. VERSION = 1 _SYSTEM_ERROR = schema._StringSchema() _SYSTEM_ERRORS = schema._UnionSchema([_SYSTEM_ERROR]) class Protocol(object): """A set of messages forming an application protocol.""" def __init__(self, name=None, namespace=None): self.__types = schema._Names() self.__messages = dict() self.__name = name self.__namespace = namespace self.__md5 = None def getname(self): return self.__name def getnamespace(self):