<commit_before>#!/usr/bin/env python
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import libkeepass
import os
import json
import lxml.etree as ET
from string import maketrans
import base64

def kdb_inventory():
  filename = os.environ["KDB_PATH"]
  credentials = { 'password' : <PASSWORD>["<PASSWORD>"] }
  vgroups = ['product', 'stage', 'tier', 'type']
  hosts = {}
  inventory = {}
  inventory_vars = {}
  with libkeepass.open(filename, **credentials) as kdb:
    xmldata = ET.fromstring(kdb.pretty_print())
    for history in xmldata.xpath(".//History"):
      history.getparent().remove(history)
    for group in xmldata.findall(".//Group"):
      group_name = group.find("./Name").text.lower()
      group_uuid = group.find("./UUID").text
      group_uuid = base64.b16encode(base64.b64decode(group_uuid))
      group_name_uuid = group_name + "_" + group_uuid
      inventory[group_name_uuid] = {}
      subgroups = []
      for subgroup in group.findall("./Group"):
        subgroup_name = subgroup.find("./Name").text.lower()
        subgroup_uuid = subgroup.find("./UUID").text
        subgroup_uuid = base64.b16encode(base64.b64decode(subgroup_uuid))
        subgroup_name_uuid = subgroup_name + "_" + subgroup_uuid
        subgroups.append(subgroup_name_uuid)
      if subgroups:
        inventory[group_name_uuid]["children"] = subgroups
      for entry in group.findall("./Entry"):
        hostvars = {}
        hostgroups = []
        hostname = None
        for string in entry.findall("./String"):
          key   = string.findtext("./Key").lower()
          value = string.findtext("./Value")
          if key == 'title' and ' ' not in value:
            hostname = value.lower()
          if value and key != "title":
            hostvars[key] = value
        if hostname and hostname != "group_vars" and ' ' not in hostname:
          hostgroups.append(group_name_uuid)
          groups = {
            group.find('Name').text.lower()<commit_msg>parse YAML code<commit_after>#!/usr/bin/env python
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import libkeepass
import os
import json
import lxml.etree as ET
from string import maketrans
import base64
import re
import yaml

def kdb_inventory():
  filename = os.environ["KDB_PATH"]
  credentials = { 'password' : <PASSWORD>["KDB_<PASSWORD>"] }
  vgroups = ['product', 'stage', 'tier', 'type']
  hosts = {}
  inventory = {}
  inventory_vars = {}
  with libkeepass.open(filename, **credentials) as kdb:
    xmldata = ET.fromstring(kdb.pretty_print())
    for history in xmldata.xpath(".//History"):
      history.getparent().remove(history)
    for group in xmldata.findall(".//Group"):
      group_name = group.find("./Name").text.lower()
      group_uuid = group.find("./UUID").text
      group_uuid = base64.b16encode(base64.b64decode(group_uuid))
      group_name_uuid = group_name + "_" + group_uuid
      inventory[group_name_uuid] = {}
      subgroups = []
      for subgroup in group.findall("./Group"):
        subgroup_name = subgroup.find("./Name").text.lower()
        subgroup_uuid = subgroup.find("./UUID").text
        subgroup_uuid = base64.b16encode(base64.b64decode(subgroup_uuid))
        subgroup_name_uuid = subgroup_name + "_" + subgroup_uuid
        subgroups.append(subgroup_name_uuid)
      if subgroups:
        inventory[group_name_uuid]["children"] = subgroups
      for entry in group.findall("./Entry"):
        hostvars = {}
        hostgroups = []
        hostname = None
        for string in entry.findall("./String"):
          key   = string.findtext("./Key").lower()
          value = string.findtext("./Value")
          if key == 'title' and ' ' not in value:
            hostname = value.lower()
          if value and key != "title":
            hostvars[key] = value
          if re.match('^---', value):
            hostvars[key] = yaml.load(value)
        if hostname and hostname != "group_vars" and ' ' not in hostname:
          hostgroups.append(group_name_uuid)
          groups = {
            group.find('Name').text.lower()
{'fraction_non_alphanumeric': 0.13982737361282369, 'fraction_numerical': 0.0093711467324291, 'mean_word_length': 2.8192090395480225, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2831891', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1272, 'n_words': 262}