def read(self, request, key_value_id=None):
        #if keystore get var is set return the whole keystore
        if 'keystore' in request.GET:
            #if key get var is set return the keystore based on the existance of this key
            if 'key' in request.GET:
                base = KeyValue.objects.filter(key=request.GET['keystore']).filter(keyvalue_set__contains=request.GET['key'])
                tmp_list = []
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.obj.hostname, r.key)
                    tmp_list[key_name] = r.value
            if 'key' not in request.GET:
                tree = KeyValueTree(request.GET['keystore']).final
                return tree
        elif 'key_type' in request.GET:
            key_type = request.GET['key_type']
            tmp_list = []
            if key_type == 'dhcp_scopes':
                #Get keystores from truth that have dhcp.is_scope = True
                base = TruthKeyValue.objects.filter(key='dhcp.is_scope',value='True')
                #Iterate through the list and get all of the key/value pairs
                for row in base:
                    keyvalue = TruthKeyValue.objects.filter(truth=row.truth)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_list.append(tmp_dict)
                return tmp_list

            if key_type == 'system_by_reverse_dns_zone':

                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='reverse_dns_zone',value=request.GET['zone']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.obj)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.obj.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.obj.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'system_by_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                keyvalue_pairs = KeyValue.objects.filter(key__contains='dhcp_scope',value=request.GET['scope']).filter(key__startswith='nic.')
                #Iterate through the list and get all of the key/value pairs
                tmp_list = []
                for row in keyvalue_pairs:
                    keyvalue = KeyValue.objects.filter(obj=row.obj)
                    tmp_dict = {}
                    for kv in keyvalue:
                        tmp_dict[kv.key] = kv.value
                    tmp_dict['hostname'] = row.obj.hostname
                    appendable = True
                    for the_items in tmp_list:
                        if 'hostname' not in the_items:
                            appendable = True
                        elif the_items['hostname'] == row.obj.hostname:
                            appendable = False
                    if appendable is True:
                        tmp_list.append(tmp_dict)
                    #tmp_list = list(set(tmp_list))
                return tmp_list
            if key_type == 'adapters_by_system':
                #Get keystores from truth that have dhcp.is_scope = True
                system = None
                try:
                    system = System.objects.get(hostname=request.GET['system'])
                except:
                    system = None
                if not system:
                    try:
                        system = System.objects.get(id=request.GET['system'])
                    except:
                        system = None
                if not system:
                    resp = rc.NOT_FOUND
                    resp.write('json = {"error_message":"Unable to find system"}')
                    return resp

                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        if matches.group(1) not in adapter_ids:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_scope = ''
                    dhcp_filename = ''
                    ipv4_address = ''
                    dhcp_domain_name_servers = ''
                    option_hostname = ""
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.option_hostname.0' % a in tmp_dict:
                        option_hostname = tmp_dict['nic.%s.option_hostname.0' % a]
                    if 'nic.%s.dhcp_scope.0' % a in tmp_dict:
                        dhcp_scope = tmp_dict['nic.%s.dhcp_scope.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name_servers.0' % a in tmp_dict:
                        dhcp_domain_name_servers = tmp_dict['nic.%s.dhcp_domain_name_servers.0' % a]
                    try:
                        final_list.append({
                                'system_hostname':system.hostname,
                                'ipv4_address':ipv4_address,
                                'adapter_name':adapter_name,
                                'mac_address':mac_address,
                                'option_hostname':option_hostname,
                                'dhcp_scope':dhcp_scope,
                                'dhcp_filename':dhcp_filename,
                                'dhcp_domain_name_servers':dhcp_domain_name_servers,
                            }
                            )
                    except Exception, e:
                        pass
                #tmp_list.append(tmp_dict)
                return final_list
            if key_type == 'adapters_by_system_and_zone':
                #Get keystores from truth that have dhcp.is_scope = True
                zone = request.GET['zone']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.reverse_dns_zone.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == zone:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address})
                #tmp_list.append(tmp_dict)
                return final_list
            if 'key_type' in request.GET and request.GET['key_type'] == 'key_by_system':
                try:
                    hostname = request.GET.get('hostname')
                    key = request.GET.get('key')
                    system = System.objects.get(hostname=hostname)
                    objects = KeyValue.objects.filter(key=key, obj=system)
                    tmp = []
                    for obj in objects:
                        tmp.append({'key': obj.key, 'value': obj.value})
                    resp = rc.ALL_OK
                    resp.write("json = {'data': %s}" % json.dumps(tmp))
                except:
                    resp = rc.NOT_FOUND
                    resp.write('json = {"error_message":"Unable to find Key or system"}')

                return resp
            if key_type == 'adapters_by_system_and_scope':
                #Get keystores from truth that have dhcp.is_scope = True
                dhcp_scope = request.GET['dhcp_scope']
                system = System.objects.get(hostname=request.GET['system'])
                keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(obj=system).order_by('key')
                #Iterate through the list and get all of the key/value pairs
                tmp_dict = {}
                adapter_ids = []
                final_list = []
                for kv in keyvalue_pairs:
                    tmp_dict[kv.key] = kv.value
                for k in tmp_dict.iterkeys():
                    matches = re.match('nic\.(\d+).*',k)
                    if matches.group is not None:
                        dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group(1)
                        ip_address_match = 'nic.%s.ipv4_address.0' % matches.group(1)
                        if matches.group(1) not in adapter_ids and ip_address_match in tmp_dict and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == dhcp_scope:
                        #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope:
                            adapter_ids.append(matches.group(1))
                adapter_ids.sort()
                for a in adapter_ids:
                    adapter_name = ''
                    mac_address = ''
                    dhcp_hostname = ''
                    dhcp_filename = ''
                    dhcp_domain_name = ''
                    ipv4_address = ''
                    dhcp_domain_name_servers = ''
                    if 'nic.%s.ipv4_address.0' % a in tmp_dict:
                        ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a]
                    if 'nic.%s.name.0' % a in tmp_dict:
                        adapter_name = tmp_dict['nic.%s.name.0' % a]
                    if 'nic.%s.mac_address.0' % a in tmp_dict:
                        mac_address = tmp_dict['nic.%s.mac_address.0' % a]
                    if 'nic.%s.dhcp_hostname.0' % a in tmp_dict and 'nic.%s.option_hostname.0' % a not in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a]
                    if  'nic.%s.option_hostname.0' % a in tmp_dict:
                        dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a]
                    if 'nic.%s.dhcp_filename.0' % a in tmp_dict:
                        dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a]
                    if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict:
                        dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a]
                    if 'nic.%s.dhcp_domain_name_servers.0' % a in tmp_dict:
                        dhcp_domain_name_servers = tmp_dict['nic.%s.dhcp_domain_name_servers.0' % a]
                    final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address,  'adapter_name':adapter_name, 'mac_address':mac_address, 'option_hostname': dhcp_hostname, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name, 'dhcp_domain_name_servers':dhcp_domain_name_servers})
                #tmp_list.append(tmp_dict)
                return final_list
        elif 'key' in request.GET and request.GET['key'] > '':
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.obj.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(key=request.GET['key'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list
        elif 'value' in request.GET:
            tmp_list = {}
            try:
                base = KeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'host:%s:%s' % (r.obj.hostname, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass
            try:
                base = TruthKeyValue.objects.filter(value=request.GET['value'])
                for row in base:
                    matches = re.match("\$\{(.*)\}", row.value)
                    if matches is not None:
                        m = MacroExpansion(matches.group(1))
                        row.value = m.output()
                for r in base:
                    key_name = 'truth:%s:%s' % (r.truth.name, r.key)
                    tmp_list[key_name] = r.value
            except Exception, e:
                pass

            return tmp_list
dataset/ETHPy150Open mozilla/inventory/api_v2/keyvalue_handler.py/KeyValueHandler.read/original