<commit_before>using System;
using System.Reflection;
using System.Collections.Generic;

using log4net;


namespace Command
{

    /// Define an attribute which will be used to tag methods that can be
    /// invoked from a script or command file.
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    class CommandAttribute : Attribute
    {
    }



    /// Define an attribute which will be used to specify default values for
    /// optional parameters on a Command.
    /// A DefaultValue attribute is used instead of default values on the
    /// actual method, since a) default values are only available from v4 of
    /// .Net, and b) they have restrictions on where they can appear (e.g. only
    /// at the end of the list of parameters).
    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
    class DefaultValueAttribute : Attribute
    {
        public object Value;

        public DefaultValueAttribute(object val)
        {
            this.Value = val;
        }
    }



    /// Records details of a parameter to a Command.
    public class CommandParameter
    {
        public string Name;
        public Type ParameterType;
        public object DefaultValue;

        public CommandParameter(ParameterInfo pi)
        {
            this.Name = pi.Name;
            this.ParameterType = pi.ParameterType;
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.ParameterType.Name);
        }
    }



    /// Represents a method that can be invoked by some external means.
    public class Command
    {
        // Reference to class logger
        protected static readonly ILog _log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        protected Type _type;
        protected MethodInfo _mi;

        public string Name;
        public List<CommandParameter> Parameters = new List<CommandParameter>();


        // Constructor
        public Command(Type t, MethodInfo mi)
        {
            _type = t;
            _mi = mi;
            this.Name = mi.Name;

            _log.DebugFormat("Found command {0}", this.Name);

            foreach(var pi in mi.GetParameters()) {
                var param = new CommandParameter(pi);
                _log.DebugFormat("Found parameter {0}", param);
                foreach(var attr in pi.GetCustomAttributes(typeof(DefaultValueAttribute), false)) {
                    param.DefaultValue = (attr as DefaultValueAttribute).Value;
                }
                this.Parameters.Add(param);
            }
        }

    }



    /// Provides a registry of discovered Commands, as well as methods for
    /// discovering them.
    public class Registry
    {
        // Dictionary of command instances keyed by command name
        private IDictionary<string, Command> _commands = new Dictionary<string, Command>();


        /// Registers commands (i.e. methods tagged with the Command attribute)
        /// in the current assembly.
        public static Registry FindCommands(string ns)
        {
            return FindCommands(Assembly.GetExecutingAssembly(), ns, null);
        }


        /// Registers commands from the supplied assembly. Commands methods must
        /// be tagged with the attribute Command to be locatable.
        public static Registry FindCommands(Assembly asm, string ns, Registry registry)
        {
            if(registry == null) {
                registry  = new Registry();
            }

            foreach(var t in asm.GetExportedTypes()) {
                if(t.IsClass && t.Namespace == ns) {
                    foreach(var mi in t.GetMethods()) {
                        foreach(var attr in mi.GetCustomAttributes(typeof(CommandAttribute), false)) {
                            Command cmd = new Command(t, mi);
                            registry.Add(cmd);
                        }
                    }
                }
            }

            return registry;
        }


        public Registry()
        {
        }


        public void Add(Command cmd)
        {
            _commands.Add(cmd.Name, cmd);
        }
    }



    /// Records the current context within which Commands are executed. A
    /// Context is like a session object; it holds the current context within
    /// which a Command will be executed, and the method to which a Command
    /// relates will be executed on the object instance of the Command's Type
    /// which is currently in the Context.
    public class Context
    {
        // Reference to class logger
        protected static readonly ILog _log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        // Registry of known Commands
        protected Registry _registry;

        // A map holding object instances keyed by Type, representing the current
        // context. When a command is invoked, it is invoked on the object that
        // is in the map for the Type on which the Command was registered.
        protected Dictionary<Type, object> _context;


        // Constructor
        public Context(Registry registry)
        {
            _registry = registry;
        }


        /// Invoke an instance of the named command, using the supplied arguments
        /// Dictionary to obtain parameter values.
        public object Invoke(string command, Dictionary<string, object> args)
        {
            // TODO: If the method returns an object, set it in the context

            return null;
        }
    }

}
<commit_msg>Add method for querying if a command exists<commit_after>using System;
using System.Reflection;
using System.Collections.Generic;

using log4net;


namespace Command
{

    /// Define an attribute which will be used to tag methods that can be
    /// invoked from a script or command file.
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    class CommandAttribute : Attribute
    {
    }



    /// Define an attribute which will be used to specify default values for
    /// optional parameters on a Command.
    /// A DefaultValue attribute is used instead of default values on the
    /// actual method, since a) default values are only available from v4 of
    /// .Net, and b) they have restrictions on where they can appear (e.g. only
    /// at the end of the list of parameters).
    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
    class DefaultValueAttribute : Attribute
    {
        public object Value;

        public DefaultValueAttribute(object val)
        {
            this.Value = val;
        }
    }



    /// Records details of a parameter to a Command.
    public class CommandParameter
    {
        public string Name;
        public Type ParameterType;
        public object DefaultValue;

        public CommandParameter(ParameterInfo pi)
        {
            this.Name = pi.Name;
            this.ParameterType = pi.ParameterType;
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.ParameterType.Name);
        }
    }



    /// Represents a method that can be invoked by some external means.
    public class Command
    {
        // Reference to class logger
        protected static readonly ILog _log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected Type _type;
        protected MethodInfo _mi;

        public string Name;
        public List<CommandParameter> Parameters = new List<CommandParameter>();


        // Constructor
        public Command(Type t, MethodInfo mi)
        {
            _type = t;
            _mi = mi;
            this.Name = mi.Name;

            _log.DebugFormat("Found command {0}", this.Name);

            foreach(var pi in mi.GetParameters()) {
                var param = new CommandParameter(pi);
                _log.DebugFormat("Found parameter {0}", param);
                foreach(var attr in pi.GetCustomAttributes(typeof(DefaultValueAttribute), false)) {
                    param.DefaultValue = (attr as DefaultValueAttribute).Value;
                }
                this.Parameters.Add(param);
            }
        }

    }



    /// Provides a registry of discovered Commands, as well as methods for
    /// discovering them.
    public class Registry
    {
        // Reference to class logger
        protected static readonly ILog _log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        // Dictionary of command instances keyed by command name
        private IDictionary<string, Command> _commands;


        /// Registers commands (i.e. methods tagged with the Command attribute)
        /// in the current assembly.
        public static Registry FindCommands(string ns)
        {
            return FindCommands(Assembly.GetExecutingAssembly(), ns, null);
        }


        /// Registers commands from the supplied assembly. Commands methods must
        /// be tagged with the attribute Command to be locatable.
        public static Registry FindCommands(Assembly asm, string ns, Registry registry)
        {
            if(registry == null) {
                registry  = new Registry();
            }

            _log.DebugFormat("Searching for commands under namespace '{0}'...", ns);
            foreach(var t in asm.GetExportedTypes()) {
                if(t.IsClass && t.Namespace == ns) {
                    foreach(var mi in t.GetMethods()) {
                        foreach(var attr in mi.GetCustomAttributes(typeof(CommandAttribute), false)) {
                            Command cmd = new Command(t, mi);
                            registry.Add(cmd);
                        }
                    }
                }
            }

            return registry;
        }


        public Registry()
        {
            _commands = new Dictionary<string, Command>(StringComparer.OrdinalIgnoreCase);
        }


        public void Add(Command cmd)
        {
            _commands.Add(cmd.Name, cmd);
        }

        public bool Contains(string key)
        {
            return _commands.ContainsKey(key);
        }
    }



    /// Records the current context within which Commands are executed. A
    /// Context is like a session object; it holds the current context within
    /// which a Command will be executed, and the method to which a Command
    /// relates will be executed on the object instance of the Command's Type
    /// which is currently in the Context.
    public class Context
    {
        // Reference to class logger
        protected static readonly ILog _log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        // Registry of known Commands
        protected Registry _registry;

        // A map holding object instances keyed by Type, representing the current
        // context. When a command is invoked, it is invoked on the object that
        // is in the map for the Type on which the Command was registered.
        protected Dictionary<Type, object> _context;


        // Constructor
        public Context(Registry registry)
        {
            _registry = registry;
        }


        /// Invoke an instance of the named command, using the supplied arguments
        /// Dictionary to obtain parameter values.
        public object Invoke(string command, Dictionary<string, object> args)
        {
            // TODO: If the method returns an object, set it in the context

            return null;
        }
    }

}

{'fraction_non_alphanumeric': 0.0752991452991453, 'fraction_numerical': 0.0011111111111111111, 'mean_word_length': 1.8671894143592256, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7609296', 'n_tokens_mistral': 2808, 'n_tokens_neox': 2670, 'n_words': 1030}