<s> """<STR_LIT>""" <EOL> import collections <EOL> import faker <EOL> import json <EOL> import hashlib <EOL> from hashlib import sha1 <EOL> import ipaddress <EOL> import pytest <EOL> import random <EOL> import socket <EOL> import struct <EOL> import time <EOL> fake = faker . Factory . create ( ) <EOL> ATTRIBUTE_DATA = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:foo>' : [ '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> Attribute = collections . namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def rando ( ) : <EOL> return random . choice ( ( True , False ) ) <EOL> def generate_words ( num_items = <NUM_LIT:100> , title = False , add_suffix = False ) : <EOL> stuff = set ( ) <EOL> for _ in range ( num_items + <NUM_LIT:1> ) : <EOL> things = ( fake . word ( ) , fake . first_name ( ) , fake . last_name ( ) ) <EOL> suffix = str ( random . randint ( <NUM_LIT:1> , <NUM_LIT:32> ) ) if add_suffix else '<STR_LIT>' <EOL> word = random . choice ( things ) + suffix <EOL> if rando ( ) : <EOL> word = word . title ( ) <EOL> if rando ( ) : <EOL> word = word [ : : - <NUM_LIT:1> ] <EOL> else : <EOL> word = word . lower ( ) <EOL> yield word <EOL> def generate_hostnames ( num_items = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:1> , num_items + <NUM_LIT:1> ) : <EOL> yield '<STR_LIT>' % i <EOL> def generate_ipv4 ( ) : <EOL> """<STR_LIT>""" <EOL> return socket . inet_ntoa ( struct . pack ( '<STR_LIT>' , random . randint ( <NUM_LIT:1> , <NUM_LIT> ) ) ) <EOL> def generate_ipv4list ( num_items = <NUM_LIT:100> , include_hosts = False ) : <EOL> """<STR_LIT>""" <EOL> ipset = set ( ) <EOL> while len ( ipset ) < num_items : <EOL> ip = generate_ipv4 ( ) <EOL> if ip . startswith ( '<STR_LIT:0>' ) : <EOL> continue <EOL> if ip . endswith ( '<STR_LIT>' ) : <EOL> prefix = '<STR_LIT>' <EOL> elif ip . endswith ( '<STR_LIT>' ) : <EOL> prefix = '<STR_LIT>' <EOL> elif ip . endswith ( '<STR_LIT>' ) : <EOL> prefix = '<STR_LIT>' <EOL> elif include_hosts : <EOL> prefix = '<STR_LIT>' <EOL> else : <EOL> continue <EOL> ip += prefix <EOL> ipset . add ( ip ) <EOL> return sorted ( ipset ) <EOL> def enumerate_attributes ( resource_name , attributes = None ) : <EOL> if attributes is None : <EOL> attributes = ATTRIBUTE_DATA <EOL> for name in attributes : <EOL> yield { '<STR_LIT:name>' : name , '<STR_LIT>' : resource_name } <EOL> def generate_attributes ( attributes = None , as_dict = True ) : <EOL> """<STR_LIT>""" <EOL> if attributes is None : <EOL> attributes = ATTRIBUTE_DATA <EOL> attrs = [ ] <EOL> for attr_name , attr_values in attributes . iteritems ( ) : <EOL> if random . choice ( ( True , False ) ) : <EOL> attr_value = random . choice ( attr_values ) <EOL> attrs . append ( Attribute ( attr_name , attr_value ) ) <EOL> if as_dict : <EOL> attrs = dict ( attrs ) <EOL> return attrs <EOL> def generate_devices ( num_items = <NUM_LIT:100> , with_attributes = True ) : <EOL> """<STR_LIT>""" <EOL> hostnames = generate_hostnames ( num_items ) <EOL> devices = [ ] <EOL> for hostname in hostnames : <EOL> item = { '<STR_LIT>' : hostname } <EOL> if with_attributes : <EOL> attributes = generate_attributes ( ) <EOL> item [ '<STR_LIT>' ] = attributes <EOL> yield item <EOL> def generate_networks ( num_items = <NUM_LIT:100> , with_attributes = True , include_hosts = False ) : <EOL> """<STR_LIT>""" <EOL> ipv4list = generate_ipv4list ( num_items , include_hosts = include_hosts ) <EOL> networks = [ ] <EOL> for cidr in ipv4list : <EOL> attributes = generate_attributes ( ) <EOL> item = { '<STR_LIT>' : cidr } <EOL> if with_attributes : <EOL> attributes = generate_attributes ( ) <EOL> item [ '<STR_LIT>' ] = attributes <EOL> networks . append ( item ) <EOL> return networks <EOL> def rando_set_action ( ) : <EOL> return random . choice ( [ '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT>' ] ) <EOL> def rando_set_query ( ) : <EOL> action = rando_set_action ( ) <EOL> return '<STR_LIT:U+0020>' . join ( <EOL> action + '<STR_LIT>' % ( k , v ) for k , v in generate_attributes ( ) . iteritems ( ) <EOL> ) </s>
