]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/OptionalConfiguration.hs
Allow "TBA" laps in TSN.XML.AutoRacingSchedule.
[dead/htsn-import.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5
6 -- | An OptionalConfiguration is just like a 'Configuration', except
7 -- all of its fields are optional. The user can set options in two
8 -- places: the command-line, and a configuration file. Obviously if
9 -- a parameter is set in one place, it doesn't need to be set in the
10 -- other. Thus, the latter needs to be optional.
11 --
12 module OptionalConfiguration (
13 OptionalConfiguration(..),
14 from_rc,
15 merge_maybes )
16 where
17
18 -- System imports.
19 import qualified Data.Configurator as DC (
20 Worth(Optional),
21 load,
22 lookup )
23 import qualified Data.Configurator.Types as DCT (
24 Configured,
25 Value( String ),
26 convert )
27 import Data.Data ( Data )
28 import Data.Monoid ( Monoid(..) )
29 import Data.Typeable ( Typeable )
30 import Paths_htsn_import ( getSysconfDir )
31 import System.Directory ( getHomeDirectory )
32 import System.FilePath ( (</>) )
33 import System.IO.Error ( catchIOError )
34 import System.Log ( Priority(..) )
35 import Text.Read ( readMaybe )
36
37 -- Local imports.
38 import Backend ( Backend(..) )
39 import ConnectionString ( ConnectionString )
40 import Network.Services.TSN.Report ( report_error )
41
42
43 -- Derive standalone instances of Data and Typeable for Priority. This
44 -- is necessary for OptionalConfiguration (which contains a Maybe
45 -- Priority) to derive Data and Typeable.
46 deriving instance Data Priority
47 deriving instance Typeable Priority
48
49 -- | The same as 'Configuration', except everything is optional. It's
50 -- easy to merge two of these by simply dropping the 'Nothing's in
51 -- favor of the 'Just's. The 'xml_files' are left un-maybed so that
52 -- cmdargs can parse more than one of them.
53 --
54 data OptionalConfiguration =
55 OptionalConfiguration {
56 backend :: Maybe Backend,
57 connection_string :: Maybe ConnectionString,
58 log_file :: Maybe FilePath,
59 log_level :: Maybe Priority,
60 remove :: Maybe Bool,
61 syslog :: Maybe Bool,
62 xml_files :: [FilePath] }
63 deriving (Show, Data, Typeable)
64
65
66 -- | Combine two Maybes into one, essentially mashing them
67 -- together. We give precedence to the second argument when both are
68 -- Justs.
69 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
70 merge_maybes Nothing Nothing = Nothing
71 merge_maybes (Just x) Nothing = Just x
72 merge_maybes Nothing (Just x) = Just x
73 merge_maybes (Just _) (Just y) = Just y
74
75
76 -- | The Monoid instance for these lets us \"combine\" two
77 -- OptionalConfigurations. The \"combine\" operation that we'd like to
78 -- perform is, essentially, to mash them together. So if we have two
79 -- OptionalConfigurations, each half full, we could combine them
80 -- into one big one.
81 --
82 -- This is used to merge command-line and config-file settings.
83 --
84 instance Monoid OptionalConfiguration where
85 -- | An empty OptionalConfiguration.
86 mempty = OptionalConfiguration
87 Nothing
88 Nothing
89 Nothing
90 Nothing
91 Nothing
92 Nothing
93 []
94
95
96 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
97 -- XML files can only be specified on the command-line, so we
98 -- just join them together here.
99 cfg1 `mappend` cfg2 =
100 OptionalConfiguration
101 (merge_maybes (backend cfg1) (backend cfg2))
102 (merge_maybes (connection_string cfg1) (connection_string cfg2))
103 (merge_maybes (log_file cfg1) (log_file cfg2))
104 (merge_maybes (log_level cfg1) (log_level cfg2))
105 (merge_maybes (remove cfg1) (remove cfg2))
106 (merge_maybes (syslog cfg1) (syslog cfg2))
107 ((xml_files cfg1) ++ (xml_files cfg2))
108
109 instance DCT.Configured Priority where
110 -- | This allows us to read a Priority level out of a Configurator
111 -- config file. By default Configurator wouldn't know what to do,
112 -- so we have to tell it that we expect one of the valid Priority
113 -- constructors.
114 convert (DCT.String "INFO") = Just INFO
115 convert (DCT.String "WARNING") = Just WARNING
116 convert (DCT.String "ERROR") = Just ERROR
117 convert _ = Nothing
118
119
120 -- | Obtain an OptionalConfiguration from htsn-importrc in either the global
121 -- configuration directory or the user's home directory. The one in
122 -- $HOME is prefixed by a dot so that it is hidden.
123 --
124 -- We make an attempt at cross-platform compatibility; we will try
125 -- to find the correct directory even on Windows. But if the calls
126 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
127 -- fall back to using the Unix-specific /etc and $HOME.
128 --
129 from_rc :: IO OptionalConfiguration
130 from_rc = do
131 etc <- catchIOError getSysconfDir (\e -> do
132 report_error (show e)
133 return "/etc")
134 home <- catchIOError getHomeDirectory (\e -> do
135 report_error (show e)
136 return "$(HOME)")
137 let global_config_path = etc </> "htsn-importrc"
138 let user_config_path = home </> ".htsn-importrc"
139 cfg <- DC.load [ DC.Optional global_config_path,
140 DC.Optional user_config_path ]
141 cfg_backend <- DC.lookup cfg "backend"
142 cfg_connection_string <- DC.lookup cfg "connection_string"
143 cfg_log_file <- DC.lookup cfg "log_file"
144 cfg_log_level <- DC.lookup cfg "log_level"
145 cfg_remove <- DC.lookup cfg "remove"
146 cfg_syslog <- DC.lookup cfg "syslog"
147 let cfg_xml_files = [] -- This won't be in the config file.
148 return $ OptionalConfiguration
149 (case cfg_backend of -- Try to convert a String to a Backend.
150 Nothing -> Nothing
151 Just s -> readMaybe s)
152 cfg_connection_string
153 cfg_log_file
154 cfg_log_level
155 cfg_remove
156 cfg_syslog
157 cfg_xml_files