From 69e69017b75d1cdaa1fd2aef2818de5111b29735 Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 14 Jul 2016 11:57:12 +0000 Subject: Update code and fix parsers --- src/Config.hs | 106 ---------------------------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 src/Config.hs (limited to 'src/Config.hs') diff --git a/src/Config.hs b/src/Config.hs deleted file mode 100644 index 0a80183..0000000 --- a/src/Config.hs +++ /dev/null @@ -1,106 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module Config - ( configUsage - , Config(..) - , getConfig - ) where - -import Data.Maybe (catMaybes, isJust, fromMaybe) -import Data.Map (Map) -import qualified Data.Map as M -import Data.Text (Text) -import qualified Data.Text as T -import qualified Data.Text.IO as T -import Data.Text.Read (decimal) - -import Control.Monad (guard) - -import System.Directory (doesFileExist) - -import Model.URL -import Model.Config - -import Utils.Text - -configUsage :: Text -configUsage = - T.intercalate "\n" - [ "" - , T.concat - [ " Some information is required in the file `" - , T.pack configPath - , "`:" - ] - , "" - , " - url (required)" - , " - mailTo (optional)" - , " - properties (optional)" - , "" - , " Example:" - , "" - , " # Lines beginning with '#' are ignored" - , "" - , " # The url field is required" - , " url = http://www.leboncoin.fr/locations/offres/ile_de_france/?f=a&th=1" - , "" - , " # The mailTo field is an optional list" - , " # mailTo = jean.dupont@mail.fr, john.smith@mail.com" - , "" - , " # The properties field is an optional list" - , " # properties = cp, city, surface, ges" - ] - -configPath :: FilePath -configPath = "conf" - -getConfig :: IO (Maybe Config) -getConfig = do - exists <- doesFileExist configPath - if exists - then - configFromFile <$> T.readFile configPath - else - return Nothing - -configFromFile :: Text -> Maybe Config -configFromFile = - configFromMap - . M.fromList - . catMaybes - . map lineConfig - . filter (not . T.null) - . filter (not . startsWith "#") - . map T.strip - . T.lines - -configFromMap :: Map Text Text -> Maybe Config -configFromMap map = do - url <- M.lookup "url" map - let config = - Config - { url = url - , mailTo = fieldValues "mailTo" map - , properties = fieldValues "properties" map - } - return config - -fieldValues :: Text -> Map Text Text -> [Text] -fieldValues field map = fromMaybe [] $ fmap T.strip . T.splitOn "," <$> M.lookup field map - -lineConfig :: Text -> Maybe (Text, Text) -lineConfig line = do - (key, value) <- keyValue line - guard (T.length key > 0) - return (key, value) - -keyValue :: Text -> Maybe (Text, Text) -keyValue line = - let sep = '=' - in if isJust (T.find (== sep) line) - then - let key = T.takeWhile (/= sep) line - value = T.drop 1 . T.dropWhile (/= sep) $ line - in Just (T.strip key, T.strip value) - else - Nothing -- cgit v1.2.3