<commit_before>{-
Homophone - Discover related artists
Copyright (C) 2019 <NAME> <<EMAIL>>

This program 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 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
-}


------------------------------------------------------------------------------
-- |
-- Module      : Homophone.Configuration
-- Description : Application configuration
-- Copyright   : (C) 2019 <NAME>
-- License     : LGPL-3
-- Maintainer  : <EMAIL>
--
-- Allows the Homophone application to retrieve its configuration options.
--
------------------------------------------------------------------------------

module Homophone.Configuration where

import System.Directory (XdgDirectory(..), getXdgDirectory)
import System.IO (FilePath(..))

import Data.Ini (lookupValue, readIniFile)
import System.FilePath ((</>))
import Data.Text (pack, unpack)

-- | Path to the directory where configuration files are stored.
configurationDirectory :: IO FilePath
configurationDirectory = getXdgDirectory XdgConfig "homophone"

-- | Path to the configuration file.
configurationFile :: IO FilePath
configurationFile = do
  base <- configurationDirectory
  return $ base </> "config.ini"

-- | Returns the value of a configuration option.
--
-- The value should be a period-separated path consisting of the section
-- and key name. For example, to get the Spotify application ID, pass
-- @spotify.client_id@ as the key parameter.
--
-- If an error occurs while retrieving the value, this function will return
-- a @Left@; otherwise, a @Right@ containing the value (as a string) will be
-- returned.
configurationValue :: String -> IO (Either String String)
configurationValue name = do
  let (section, key) = span (/= '.') name
  path <- configurationFile
  res <- readIniFile path<commit_msg>Re-organize imports<commit_after>{-
Homophone - Discover related artists
Copyright (C) 2019 <NAME> <<EMAIL>>

This program 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 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
-}


------------------------------------------------------------------------------
-- |
-- Module      : Homophone.Configuration
-- Description : Application configuration
-- Copyright   : (C) 2019 <NAME>
-- License     : LGPL-3
-- Maintainer  : <EMAIL>
--
-- Allows the Homophone application to retrieve its configuration options.
--
------------------------------------------------------------------------------

module Homophone.Configuration where

import Data.Ini (lookupValue, readIniFile)
import Data.Text (pack, unpack)
import System.Directory (XdgDirectory(..), getXdgDirectory)
import System.FilePath ((</>))
import System.IO (FilePath(..))

-- | Path to the directory where configuration files are stored.
configurationDirectory :: IO FilePath
configurationDirectory = getXdgDirectory XdgConfig "homophone"

-- | Path to the configuration file.
configurationFile :: IO FilePath
configurationFile = do
  base <- configurationDirectory
  return $ base </> "config.ini"

-- | Returns the value of a configuration option.
--
-- The value should be a period-separated path consisting of the section
-- and key name. For example, to get the Spotify application ID, pass
-- @spotify.client_id@ as the key parameter.
--
-- If an error occurs while retrieving the value, this function will return
-- a @Left@; otherwise, a @Right@ containing the value (as a string) will be
-- returned.
configurationValue :: String -> IO (Either String String)
configurationValue name = do
  let (section, key) = span (/= '.') name
  path <- configurationFile
  res <- readIniFile path
{'fraction_non_alphanumeric': 0.14979406026446998, 'fraction_numerical': 0.004335573379579449, 'mean_word_length': 2.9168081494057727, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2861753', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1099, 'n_words': 528}