Example #1
0
 public Variable(string name, Type type, FilterCollection filters = null)
 {
     this.name = name;
     this.type = type;
     if (filters == null || filters.getSize() <= 0)
     {
         this.filters = null;
     }
     else
     {
         this.filters = filters;
     }
 }
        public static FilterCollection parseList(YamlSequenceNode list, Dictionary<string, Filter> filterMap)
        {
            FilterCollection collection = new FilterCollection();

            if (list != null)
            {
                foreach (YamlNode node in list.Children)
                {
                    parseFilterNode(collection, filterMap, node);
                }
            }

            return collection;
        }
        private static void parseFilterNode(FilterCollection filterCollection, Dictionary<string, Filter> filterMap, YamlNode node)
        {
            string name;
            string[] args;
            if (node is YamlScalarNode)
            {
                name = ((YamlScalarNode)node).Value.Trim().ToLower();
                args = new string[0];
            }
            else if (node is YamlSequenceNode)
            {
                IEnumerator<YamlNode> it = ((YamlSequenceNode)node).Children.GetEnumerator();
                if (!it.MoveNext())
                {
                    throw new InvalidConfigurationException("An empty list as a filter is not valid!");
                }
                node = it.Current;
                if (!(node is YamlScalarNode))
                {
                    throw new InvalidConfigurationException("Filter definitions as a list my only contain strings!");
                }
                name = ((YamlScalarNode)node).Value.Trim().ToLower();
                args = readFilterArgs(it);
            }
            else if (node is YamlMappingNode)
            {
                YamlMappingNode filterConfig = (YamlMappingNode)node;
                IDictionary<YamlNode, YamlNode> childNodes = filterConfig.Children;
                node = (childNodes.ContainsKey(Node.NAME) ? childNodes[Node.NAME] : null);
                if (!(node is YamlScalarNode))
                {
                    throw new InvalidConfigurationException("The filter name is missing or invalid!");
                }
                name = ((YamlScalarNode)node).Value.Trim().ToLower();

                node = (childNodes.ContainsKey(Node.ARGS) ? childNodes[Node.ARGS] : null);
                if (node is YamlSequenceNode)
                {
                    args = readFilterArgs(((YamlSequenceNode)node).Children.GetEnumerator());
                }
                else
                {
                    args = new string[0];
                }
            }
            else
            {
                throw new InvalidConfigurationException("Invalid filter configuration");
            }

            if (!filterMap.ContainsKey(name))
            {
                throw new InvalidConfigurationException("Unknown filter " + name);
            }

            filterCollection.Add(filterMap[name], args);
        }
        /// <summary>
        /// Loads a configuration from any TextReader
        /// </summary>
        /// <param name="configReader">the reader</param>
        /// <exception cref="InvalidConfigurationException">if an error occurs during configuration loading</exception>
        public void loadProvider(TextReader configReader)
        {
            YamlStream yaml = new YamlStream();
            try
            {
                yaml.Load(configReader);
            }
            catch (Exception e)
            {
                throw new InvalidConfigurationException(e.Message, e);
            }

            YamlNode node;
            IDictionary<YamlNode, YamlNode> rootNodes = ((YamlMappingNode)yaml.Documents[0].RootNode).Children;

            string loaderName;
            node = (rootNodes.ContainsKey(Node.LOADER) ? rootNodes[Node.LOADER] : null);
            if (node is YamlScalarNode)
            {
                loaderName = ((YamlScalarNode)node).Value.Trim().ToLower();
            }
            else
            {
                loaderName = "static";
            }
            if (!loaderFactories.ContainsKey(loaderName))
            {
                throw new InvalidConfigurationException("Unknown provider type " + loaderName + ", skipping");
            }
            LyricsLoaderFactory loaderFactory = loaderFactories[loaderName];

            node = (rootNodes.ContainsKey(Node.NAME) ? rootNodes[Node.NAME] : null);
            if (!(node is YamlScalarNode))
            {
                throw new InvalidConfigurationException("No provider name given!");
            }
            string name = ((YamlScalarNode)node).Value.Trim();

            ushort quality = 50;
            node = (rootNodes.ContainsKey(Node.QUALITY) ? rootNodes[Node.QUALITY] : null);
            if (node is YamlScalarNode)
            {
                try
                {
                    quality = Convert.ToUInt16(((YamlScalarNode)node).Value.Trim());
                }
                catch (FormatException e)
                {
                    throw new InvalidConfigurationException("Invalid quality value given, only positive numbers >= 0 are allowed!", e);
                }
            }

            RateLimit rateLimit = null;
            node = (rootNodes.ContainsKey(Node.RATE_LIMIT) ? rootNodes[Node.RATE_LIMIT] : null);
            if (node is YamlScalarNode)
            {
                rateLimit = RateLimit.parse(((YamlScalarNode) node).Value.Trim());
            }

            node = (rootNodes.ContainsKey(Node.VARIABLES) ? rootNodes[Node.VARIABLES] : null);
            Dictionary<string, Variable> variables = new Dictionary<string, Variable>();
            if (node is YamlMappingNode)
            {
                foreach (KeyValuePair<YamlNode, YamlNode> preparationEntry in ((YamlMappingNode)node).Children)
                {
                    node = preparationEntry.Key;
                    if (node is YamlScalarNode)
                    {
                        string variableName = ((YamlScalarNode)node).Value.ToLower();

                        if (variables.ContainsKey(variableName))
                        {
                            throw InvalidConfigurationException.fromFormat("{0}: Variable already defined!", variableName);
                        }

                        node = preparationEntry.Value;
                        // variable value without filters
                        if (node is YamlScalarNode)
                        {
                            string typeString = ((YamlScalarNode)node).Value.ToLower();
                            try
                            {
                                Variable.Type variableType = VARIABLE_TYPES[typeString];
                                variables.Add(variableName, new Variable(variableName, variableType));
                            }
                            catch
                            {
                                throw InvalidConfigurationException.fromFormat("{0}: Unknown variable type {1}!", variableName, typeString);
                            }
                        }
                        // value with filters expected
                        else if (node is YamlMappingNode)
                        {
                            YamlMappingNode variableConfig = (YamlMappingNode)node;

                            node = variableConfig.Children[Node.Variables.TYPE];
                            if (!(node is YamlScalarNode))
                            {
                                throw InvalidConfigurationException.fromFormat("{0}: Invalid variable type!", variableName);
                            }

                            Variable.Type type;
                            string typeString = ((YamlScalarNode)node).Value.ToLower();
                            try
                            {
                                type = VARIABLE_TYPES[typeString];
                            }
                            catch
                            {
                                throw InvalidConfigurationException.fromFormat("{0}: Unknown variable type {1}!", variableName, typeString);
                            }

                            FilterCollection filterCollection;

                            node = (variableConfig.Children.ContainsKey(Node.Variables.FILTERS) ? variableConfig.Children[Node.Variables.FILTERS] : null);
                            // variable reference
                            if (node is YamlScalarNode)
                            {
                                string referencedVar = ((YamlScalarNode)node).Value.ToLower();
                                try
                                {
                                    filterCollection = variables[referencedVar].getFilters();
                                }
                                catch
                                {
                                    throw InvalidConfigurationException.fromFormat("{0}: Unknown variable {1} referenced!", variableName, referencedVar);
                                }
                            }
                            // a list of filters
                            else if (node is YamlSequenceNode)
                            {
                                filterCollection = FilterCollection.parseList((YamlSequenceNode)node, filters);
                            }
                            else
                            {
                                throw new InvalidConfigurationException("Invalid filter option specified!");
                            }

                            variables.Add(variableName, new Variable(variableName, type, filterCollection));
                        }
                    }
                    else
                    {
                        throw new InvalidConfigurationException("Invalid configration, aborting the configuration.");
                    }
                }
            }

            foreach (KeyValuePair<string, Variable.Type> entry in VARIABLE_TYPES)
            {
                if (!variables.ContainsKey(entry.Key))
                {
                    variables.Add(entry.Key, new Variable(entry.Key, entry.Value));
                }
            }

            node = (rootNodes.ContainsKey(Node.POST_FILTERS) ? rootNodes[Node.POST_FILTERS] : null);
            FilterCollection postFilters;
            if (node is YamlSequenceNode)
            {
                postFilters = FilterCollection.parseList((YamlSequenceNode)node, filters);
            }
            else
            {
                postFilters = new FilterCollection();
            }

            node = (rootNodes.ContainsKey(Node.VALIDATIONS) ? rootNodes[Node.VALIDATIONS] : null);
            ValidationCollection validations;
            if (node is YamlSequenceNode)
            {
                validations = ValidationCollection.parseList((YamlSequenceNode)node, validators);
            }
            else
            {
                validations = new ValidationCollection();
            }

            IDictionary<String, String> headers = new Dictionary<String, String>();
            node = (rootNodes.ContainsKey(Node.HEADERS) ? rootNodes[Node.HEADERS] : null);
            if (node is YamlMappingNode)
            {
                foreach (KeyValuePair<YamlNode, YamlNode> entry in (YamlMappingNode)node)
                {
                    if (entry.Key is YamlScalarNode && entry.Value is YamlScalarNode)
                    {
                        headers.Add(((YamlScalarNode)entry.Key).Value.Trim(), ((YamlScalarNode)entry.Value).Value);
                    }
                }
            }

            YamlMappingNode configNode;
            node = (rootNodes.ContainsKey(Node.CONFIG) ? rootNodes[Node.CONFIG] : null);
            if (node is YamlMappingNode)
            {
                configNode = (YamlMappingNode)node;
            }
            else
            {
                configNode = new YamlMappingNode();
            }

            LyricsLoader loader = loaderFactory.newLoader(configNode);

            Provider provider = new Provider(lyricsReloaded, name, quality, variables, postFilters, validations, headers, loader, rateLimit);
            logger.info("Provider loaded: " + provider.getName());

            lock (providerLock)
            {
                if (providers.ContainsKey(provider.getName()))
                {
                    logger.info("The provider {0} does already exist and will be replaced.", provider.getName());
                    providers.Remove(provider.getName());
                }
                providers.Add(provider.getName(), provider);
            }
        }