<commit_before>from yaml.representer import Representer
import collections
import logging
import yaml
import os

CONFIG_TEMPLATE = """
# what source should be used as an inventory source
inventory:
  source: csv
  update_at_start: false

# if you are using a csv file
csv:
  name: example.csv
  # requires at least name and address
  fields: name, address, random
  delimiter: '|'

# all aws inventory configuration
aws:
  access_key_id: ACCESS_KEY
  secret_access_key: SECRET_KEY
  session_token: SESSION_TOKEN
  region: us-west-2

# all newrelic inventory configuration
newrelic:
  account_number: ACCOUNT_NUMBER
  insights_query_api_key: API_KEY

# define ssh behavior and preferences
ssh:
  user: ubuntu
  options: -C -o ServerAliveInterval=255
  command: sudo -i su - another_user -s /bin/bash

# if you need to connect to aws hosts via a bastion, then
# provide all connectivity information here
bastion:
  user: ubuntu
  address: zest
  options: -C -o ServerAliveInterval=255

# define tmux layouts and (optional) canned commands
tmux:
  layout:
    # bridgy ssh -l example host...
    example:
      - cmd: split-window -h
        run: echo "first split" && bash
      - cmd: split-window -h
        run: echo "second split" && bash
      - cmd: split-window -v
        run: echo "third split" && bash
"""


class Config(object):
    __path = "~/.bridgy/config.yml"
    __inventory = "~/.bridgy/inventory"
    __mount = "~/.bridgy/mounts"
    __conf = None
    # todo: remove this and make the inventory module the source of truth
    inventorySources = ['gcp', 'aws', 'csv', 'newrelic']

    def __init__(self, initialData=None):
        self.__conf = initialData

    def read(self):
        # ensure yaml uses a defaultdict(str)
        yaml.add_representer(collections.defaultdict,
                             Representer.represent_str)
        with open(os.path.expanduser(self.__path), 'r') as fh:
            self.__conf = yaml.load(fh)

    def create(self):
        configFile = os.path.expanduser(self.__path)
        if not os.path.exists(configFile):
            logging.getLogger().info("Creating %s" % self.__path)
            parentDir = os.path.dirname(configFile)
            if not os.path.exists(parentDir):
                os.mkdir(parentDir)
            with open(configFile, 'w') as fh:
                fh.write(CONFIG_TEMPLATE)

        inventoryCache = os.path.expanduser(self.__inventory)
        if not os.path.exists(inventoryCache):
            parentDir = os.path.dirname(inventoryCache)
            if not os.path.exists(parentDir):
                os.mkdir(parentDir)
            os.mkdir(inventoryCache)

        for source in self.inventorySources:
            sourcePath = os.path.join(inventoryCache, source)
            if not os.path.exists(sourcePath):
                os.mkdir(sourcePath)

        mountPath = os.path.expanduser(self.__mount)
        if not os.path.exists(mountPath):
            os.mkdir(mountPath)

    def inventoryDir(self, source):
        if source not in self.inventorySources:
            raise RuntimeError(
                "Unexpected inventory source: %s" % repr(source))
        return os.path.join(os.path.expanduser(self.__inventory),
                            source)

    @property
    def mount_root_dir(self):
        return os.path.expanduser(self.__mount)

    # TODO
    def verify(self): pass

    def __iter__(self):<commit_msg>var name refactors in config<commit_after>from yaml.representer import Representer
import collections
import logging
import yaml
import os

import inventory

CONFIG_TEMPLATE = """
# what source should be used as an inventory source
inventory:
  source: csv
  update_at_start: false

# if you are using a csv file
csv:
  name: example.csv
  # requires at least name and address
  fields: name, address, random
  delimiter: '|'

# all aws inventory configuration
aws:
  access_key_id: ACCESS_KEY
  secret_access_key: SECRET_KEY
  session_token: SESSION_TOKEN
  region: us-west-2

# all newrelic inventory configuration
newrelic:
  account_number: ACCOUNT_NUMBER
  insights_query_api_key: API_KEY

# define ssh behavior and preferences
ssh:
  user: ubuntu
  options: -C -o ServerAliveInterval=255
  command: sudo -i su - another_user -s /bin/bash

# if you need to connect to aws hosts via a bastion, then
# provide all connectivity information here
bastion:
  user: ubuntu
  address: zest
  options: -C -o ServerAliveInterval=255

# define tmux layouts and (optional) canned commands
tmux:
  layout:
    # bridgy ssh -l example host...
    example:
      - cmd: split-window -h
        run: echo "first split" && bash
      - cmd: split-window -h
        run: echo "second split" && bash
      - cmd: split-window -v
        run: echo "third split" && bash
"""


class Config(object):
    __path = "~/.bridgy/config.yml"
    __inventory = "~/.bridgy/inventory"
    __mount = "~/.bridgy/mounts"
    __conf = None

    def __init__(self, initial_data=None):
        self.__conf = initial_data

    def read(self):
        # ensure yaml uses a defaultdict(str)
        yaml.add_representer(collections.defaultdict,
                             Representer.represent_str)
        with open(os.path.expanduser(self.__path), 'r') as fh:
            self.__conf = yaml.load(fh)

    def create(self):
        config_file = os.path.expanduser(self.__path)
        if not os.path.exists(config_file):
            logging.getLogger().info("Creating %s" % self.__path)
            parent_dir = os.path.dirname(config_file)
            if not os.path.exists(parent_dir):
                os.mkdir(parent_dir)
            with open(config_file, 'w') as fh:
                fh.write(CONFIG_TEMPLATE)

        inventory_cache = os.path.expanduser(self.__inventory)
        if not os.path.exists(inventory_cache):
            parent_dir = os.path.dirname(inventory_cache)
            if not os.path.exists(parent_dir):
                os.mkdir(parent_dir)
            os.mkdir(inventory_cache)

        for source in inventory.SOURCES.keys():
            source_path = os.path.join(inventory_cache, source)
            if not os.path.exists(source_path):
                os.mkdir(source_path)

        mount_path = os.path.expanduser(self.__mount)
        if not os.path.exists(mount_path):
            os.mkdir(mount_path)

    def inventoryDir(self, source):
        if source not in inventory.SOURCES.keys():
            raise RuntimeError(
                "Unexpected inventory source: %s" % repr(source))
        return os.path.join(os.path.expanduser(self.__inventory),
                            source)

    @property
    def mount_root_dir(self):
        return os.path.expanduser(self.__mount)

    # TODO
    def verify(self): pass

    def __iter__(self):
{'fraction_non_alphanumeric': 0.09368996601152653, 'fraction_numerical': 0.0020688636027781883, 'mean_word_length': 2.5158441558441558, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1162612', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2001, 'n_words': 552}