<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using Kudu.Contracts.Settings;
using XmlSettings;

namespace Kudu.Core.Settings
{
    public class DeploymentSettingsManager : IDeploymentSettingsManager
    {
        private const string DeploymentSettingsSection = "deployment";
        private readonly ISettings _settings;

        public DeploymentSettingsManager(ISettings settings)
        {
            _settings = settings;
        }

        public void SetValue(string key, string value)
        {
            _settings.SetValue(DeploymentSettingsSection, key, value);
        }

        public IEnumerable<KeyValuePair<string, string>> GetValues()
        {
            var values = _settings.GetValues(DeploymentSettingsSection);

            if (values == null)
            {
                return Enumerable.Empty<KeyValuePair<string, string>>();
            }

            return values;
        }

        public string GetValue(string key)
        {
            return _settings.GetValue(DeploymentSettingsSection, key);
        }

        public void DeleteValue(string key)
        {
            _settings.DeleteValue(DeploymentSettingsSection, key);
        }
    }
}<commit_msg>Unify shared settings, per-site settings and environment settings<commit_after>﻿using Kudu.Contracts.Settings;
using System;
using System.Collections.Generic;
using XmlSettings;

namespace Kudu.Core.Settings
{
    public class DeploymentSettingsManager : IDeploymentSettingsManager
    {
        private const string DeploymentSettingsSection = "deployment";
        private const string EnvVariablePrefix = "KUDU_";
        private readonly ISettings _settings;

        // Ideally, these default settings would live in Kudu's web.config. However, we also need them in 
        // kudu.exe, so they actually need to be in a shaed config file. For now, it's easier to hard code
        // the defaults, since things like 'branch' will rarely want a different global default
        private static Dictionary<string, string> _defaultSettings = new Dictionary<string, string> {
            { "branch", "master" }
        };

        public DeploymentSettingsManager(ISettings settings)
        {
            _settings = settings;
        }

        public void SetValue(string key, string value)
        {
            // Note that this only applies to persisted per-site settings
            _settings.SetValue(DeploymentSettingsSection, key, value);
        }

        public IEnumerable<KeyValuePair<string, string>> GetValues()
        {
            var values = new Dictionary<string, string>();

            // Start with the default values, potentially overridden by environment variables
            foreach (var pair in _defaultSettings)
            {
                values[pair.Key] = GetEnvironmentVariableValueWithFallback(pair.Key);
            }

            // Add all the per-site settings, overriding AppSettings if needed
            var settings = _settings.GetValues(DeploymentSettingsSection);
            if (settings != null)
            {
                foreach (var entry in settings)
                {
                    values[entry.Key] = entry.Value;
                }
            }

            return values;
        }

        public string GetValue(string key)
        {
            // First try the per-site persisted settings
            string val = _settings.GetValue(DeploymentSettingsSection, key);
            if (!String.IsNullOrEmpty(val))
            {
                return val;
            }

            return GetEnvironmentVariableValueWithFallback(key);
        }

        public void DeleteValue(string key)
        {
            // Note that this only applies to persisted per-site settings
            _settings.DeleteValue(DeploymentSettingsSection, key);
        }

        private string GetEnvironmentVariableValueWithFallback(string key)
        {
            // Note that we only look for environment variables if they match a default setting
            if (!_defaultSettings.ContainsKey(key)) return null;

            string val = System.Environment.GetEnvironmentVariable(EnvVariablePrefix + key);
            if (String.IsNullOrEmpty(val))
            {
                // Fall back to the default
                val = _defaultSettings[key];
            }

            return val;
        }
    }
}
{'fraction_non_alphanumeric': 0.06935888660734657, 'fraction_numerical': 0.0, 'mean_word_length': 2.038115038115038, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '5892383', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1008, 'n_words': 302}