<commit_before>/*
 * Desktop Agnostic Library: Configuration Schema.
 *
 * Copyright (C) 2008, 2009 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Author : <NAME> <<EMAIL>>
 */

using DesktopAgnostic.VFS;

namespace DesktopAgnostic.Config
{
  /**
   * Configuration schema error types.
   */
  public errordomain SchemaError
  {
    PARSE,
    INVALID_METADATA_OPTION,
    INVALID_METADATA_TYPE,
    INVALID_TYPE,
    INVALID_LIST_TYPE,
    TYPE_NAME_EXISTS,
    TYPE_GTYPE_EXISTS
  }
  /**
   * A representation of a configuration schema, comprised of one or more
   * configuration options.
   */
  public class Schema : Object
  {
    // static code
    private static HashTable<Type,SchemaType> type_registry =
      new HashTable<Type,SchemaType> ((HashFunc)gtype_hash,
                                      (EqualFunc)gtype_equal);
    private static HashTable<string,SchemaType> name_registry =
      new HashTable<string,SchemaType> (str_hash, str_equal);
    private static HashTable<string,Value?> common_metadata_keys =
      new HashTable<string,Value?> (str_hash, str_equal);
    static construct
    {
      // Loads the type modules via the module search paths + cwd.
      List<string> type_modules;
      string[] paths;
      SList<string> search_paths;

      type_modules = new List<string> ();
      // pre-load the ModuleLoader static constructor
      ModuleLoader.get_default ();
      // pre-load the default config backend module.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        // Do nothing.
      }

      // search for the config type modules
      paths = ModuleLoader.get_search_paths ();
      search_paths = new SList<string> ();
      foreach (unowned string path in paths)
      {
        if (path != null)
        {
          search_paths.append (path);
        }
      }
      search_paths.append (Environment.get_current_dir ());

      foreach (unowned string path in search_paths)
      {
        if (!FileUtils.test (path, FileTest.IS_DIR))
        {
          continue;
        }
        string module_glob = Path.build_filename (path, "libda-cfg-type-*");
        try
        {
          Glob found_modules;
          unowned string[] modules_paths;

          found_modules = Glob.execute (module_glob);
          modules_paths = found_modules.get_paths ();
          foreach (unowned string module in modules_paths)
          {
            if (type_modules.find (module) == null)
            {
              Type type;

              unowned ModuleLoader loader = ModuleLoader.get_default ();
              type = loader.load_from_path (Path.get_basename (module),
                                            module);
              if (type != Type.INVALID)
              {
                try
                {
                  Object obj = Object.new (type);

                  register_type ((SchemaType)((owned)obj));
                  type_modules.append (module);
                }
                catch (SchemaError err)
                {
                  warning ("Schema error: %s", err.message);
                }
              }
              else
              {
                warning ("Could not load the config type module: %s", module);
              }
            }
          }
        }
        catch (GlobError err)
        {
          if (!(err is GlobError.NOMATCH))
          {
            warning ("Glob-related error: %s", err.message);
          }
        }
      }
      // initialize the common metadata keys hashtable
      Value val = Value (typeof (bool));
      val.set_boolean (true);
      common_metadata_keys.insert ("single_instance", val);
      // force the config module to load before creating the schema.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        critical ("Config error: %s", err.message);
      }
    }
    // properties
    private string _filename;
    public string filename
    {
      /**
       * Sets the schema's file name and app name (assuming the file name
       * is valid).
       */
      construct
      {
        string? basename = null;

        if (!value.has_suffix (".schema-ini"))
        {
          throw new SchemaError.PARSE ("Schema files MUST have the extension '.schema-ini'.");
        }
        if (!FileUtils.test (value, FileTest.EXISTS))
        {
          throw new SchemaError.PARSE ("The file '%s' could not be found.",
                                       value);
        }
        this._filename = value;
        basename = Path.get_basename (value);<commit_msg>Config Schema: fix Vala compiler warnings.<commit_after>/*
 * Desktop Agnostic Library: Configuration Schema.
 *
 * Copyright (C) 2008, 2009 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Author : <NAME> <<EMAIL>>
 */

using DesktopAgnostic.VFS;

namespace DesktopAgnostic.Config
{
  /**
   * Configuration schema error types.
   */
  public errordomain SchemaError
  {
    PARSE,
    INVALID_METADATA_OPTION,
    INVALID_METADATA_TYPE,
    INVALID_TYPE,
    INVALID_LIST_TYPE,
    TYPE_NAME_EXISTS,
    TYPE_GTYPE_EXISTS
  }
  /**
   * A representation of a configuration schema, comprised of one or more
   * configuration options.
   */
  public class Schema : Object
  {
    // static code
    private static HashTable<Type,SchemaType> type_registry =
      new HashTable<Type,SchemaType> ((HashFunc)gtype_hash,
                                      (EqualFunc)gtype_equal);
    private static HashTable<string,SchemaType> name_registry =
      new HashTable<string,SchemaType> (str_hash, str_equal);
    private static HashTable<string,Value?> common_metadata_keys =
      new HashTable<string,Value?> (str_hash, str_equal);
    static construct
    {
      // Loads the type modules via the module search paths + cwd.
      List<string> type_modules;
      string[] paths;
      SList<string> search_paths;

      type_modules = new List<string> ();
      // pre-load the ModuleLoader static constructor
      ModuleLoader.get_default ();
      // pre-load the default config backend module.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        // Do nothing.
      }

      // search for the config type modules
      paths = ModuleLoader.get_search_paths ();
      search_paths = new SList<string> ();
      foreach (unowned string path in paths)
      {
        if (path != null)
        {
          search_paths.append (path);
        }
      }
      search_paths.append (Environment.get_current_dir ());

      foreach (unowned string path in search_paths)
      {
        if (!FileUtils.test (path, FileTest.IS_DIR))
        {
          continue;
        }
        string module_glob = Path.build_filename (path, "libda-cfg-type-*");
        try
        {
          Glob found_modules;
          unowned string[] modules_paths;

          found_modules = Glob.execute (module_glob);
          modules_paths = found_modules.get_paths ();
          foreach (unowned string module in modules_paths)
          {
            if (type_modules.find (module) == null)
            {
              Type type;

              unowned ModuleLoader loader = ModuleLoader.get_default ();
              type = loader.load_from_path (Path.get_basename (module),
                                            module);
              if (type != Type.INVALID)
              {
                try
                {
                  Object obj = Object.new (type);

                  register_type ((SchemaType)((owned)obj));
                  type_modules.append (module);
                }
                catch (SchemaError err)
                {
                  warning ("Schema error: %s", err.message);
                }
              }
              else
              {
                warning ("Could not load the config type module: %s", module);
              }
            }
          }
        }
        catch (GlobError err)
        {
          if (!(err is GlobError.NOMATCH))
          {
            warning ("Glob-related error: %s", err.message);
          }
        }
      }
      // initialize the common metadata keys hashtable
      Value val = Value (typeof (bool));
      val.set_boolean (true);
      common_metadata_keys.insert ("single_instance", val);
      // force the config module to load before creating the schema.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        critical ("Config error: %s", err.message);
      }
    }
    // properties
    private string _filename;
    public string filename
    {
      /**
       * Sets the schema's file name and app name (assuming the file name
       * is valid).
       */
      construct
      {
        string? basename = null;

        if (!value.has_suffix (".schema-ini"))
        {
          critical ("Schema files MUST have the extension '.schema-ini'.");
          return;
        }
        if (!FileUtils.test (value, FileTest.EXISTS))
        {
          critical ("The file '%s' could not be found.", value);
          return;
        }
        this._filename = value;
        basename = Path.get_basename (value);
{'fraction_non_alphanumeric': 0.08897667419112114, 'fraction_numerical': 0.0039503386004514675, 'mean_word_length': 1.8537305421363393, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4724529', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2768, 'n_words': 884}