Skip to content

Instantly share code, notes, and snippets.

@WillSams
Created October 17, 2012 13:29
Show Gist options
  • Save WillSams/3905532 to your computer and use it in GitHub Desktop.
Save WillSams/3905532 to your computer and use it in GitHub Desktop.
Configuration Helper file to use with Iron Python apps. Just compile it from the command-line.
//ConfigProxy.cs
// Taken modified:
// http://tomestephens.com/2011/02/making-ironpython-work-overriding-the-configurationmanager/
// http://tomestephens.com/2011/02/extending-my-ironpython-toolset/
// command line compile: csc /target:library /out:ConfigProxy.dll ConfigProxy.cs
// In IronPython, to load up your config file:
// import clr
// clr.AddReference('System.Core')
// clr.AddReference('System.Xml')
// clr.AddReference('System.Configuration')
// clr.AddReferenceToFile('ConfigProxy.dll')
// import ConfigurationProxy
// proxy = ConfigurationProxy('activedirectory.config')
// proxy.InjectToConfigurationManager():
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Configuration.Internal;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.XmlConfiguration;
public sealed class ConfigurationProxy : IInternalConfigSystem
{
Configuration config;
Dictionary<string, IConfigurationSectionHandler> customSections;
public Configuration Configuration
{
get { return config; }
}
// this is called filename but really it's the path as needed...
// it defaults to checking the directory you're running in.
public ConfigurationProxy(string fileName)
{
customSections = new Dictionary<string,IConfigurationSectionHandler>();
if (!this.Load(fileName))
throw new ConfigurationErrorsException(string.Format(
"File: {0} could not be found or was not a valid cofiguration file.",
config.FilePath));
}
private bool Load(string file)
{
var map = new ExeConfigurationFileMap { ExeConfigFilename = file };
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var xml = new XmlDocument();
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read))
xml.Load(stream);
var cfgSections = xml.GetElementsByTagName("configSections");
if (cfgSections.Count > 0)
{
foreach (XmlNode node in cfgSections[0].ChildNodes)
{
var type = System.Activator.CreateInstance(
Type.GetType(node.Attributes["type"].Value))
as IConfigurationSectionHandler;
if (type == null) continue;
customSections.Add(node.Attributes["name"].Value, type);
}
}
return config.HasFile;
}
#region IInternalConfigSystem Members
public object GetSection(string configKey)
{
if (configKey == "appSettings")
return BuildAppSettings();
object sect = config.GetSection(configKey);
if (customSections.ContainsKey(configKey) && sect != null)
{
var xml = new XmlDocument();
xml.LoadXml(((ConfigurationSection)sect).SectionInformation.GetRawXml());
// I have no idea what I should normally be passing through in the first
// two params, but I never use them in my confighandlers so I opted not to
// worry about it and just pass through something...
sect = customSections[configKey].Create(config,
config.EvaluationContext,
xml.FirstChild);
}
return sect;
}
public void RefreshConfig(string sectionName)
{
// I suppose this will work. Reload the whole file?
this.Load(config.FilePath);
}
public bool SupportsUserConfig
{
get { return false; }
}
#endregion
private NameValueCollection BuildAppSettings()
{
var coll = new NameValueCollection();
foreach (var key in config.AppSettings.Settings.AllKeys)
coll.Add(key, config.AppSettings.Settings[key].Value);
return coll;
}
public bool InjectToConfigurationManager()
{
// inject self into ConfigurationManager
var configSystem = typeof(ConfigurationManager).GetField("s_configSystem",
BindingFlags.Static | BindingFlags.NonPublic);
configSystem.SetValue(null, this);
// lame check, but it's something
if (ConfigurationManager.AppSettings.Count == config.AppSettings.Settings.Count)
return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment