+-- | Parse the command-line options, and display help text if
+-- necessary.
module CommandLine (
get_args )
where
typ,
typDir )
--- Get the version from Cabal.
+-- This let's us get the version from Cabal.
import Paths_htsn (version)
import Data.Version (showVersion)
import OptionalConfiguration (OptionalConfiguration(..))
+
+-- | The description of the program, displayed as part of the help.
description :: String
description = "Parse XML files from The Sports Network."
+-- | The name of this program.
program_name :: String
program_name = "htsn"
+-- | A summary string output as part of the help.
my_summary :: String
my_summary = program_name ++ "-" ++ (showVersion version)
+
+-- | A description of the "password" option.
password_help :: String
password_help =
"Password to use when connecting to the feed"
+-- | A description of the "output_directory" option.
output_directory_help :: String
output_directory_help =
"Directory in which to output the XML files; must be writable"
+-- | A description of the "username" option.
username_help :: String
username_help =
"Username to use when connecting to the feed"
+-- | A data structure representing the possible command-line
+-- options. The CmdArgs library is doing heavy magic beneath the
+-- hood here.
arg_spec :: OptionalConfiguration
arg_spec =
OptionalConfiguration {
&= details [description]
-
+-- | A convenience function; our only export. Meant to be used in
+-- 'main' to retrieve the command-line arguments.
get_args :: IO OptionalConfiguration
get_args = cmdArgs arg_spec
-- | This module defines the 'Configuration' type, which is just a
-- wrapper around all of the configuration options we accept on the
--- command line. We thread this throughout the rest of the program.
-
+-- command line.
+--
module Configuration (
Configuration(..),
merge_optional )
instance Default Configuration where
def = Configuration def def "." def
+
+-- | Merge a Configuration with an OptionalConfiguration. This is more
+-- or less the Monoid instance for OptionalConfiguration, but since
+-- the two types are different, we have to repeat ourselves.
merge_optional :: Configuration
-> OC.OptionalConfiguration
-> Configuration
import TSN.Xml (parse_xmlfid, xml_prologue)
+-- | Receive a single line of text from a Handle, echoing it to stdout
+-- in the process.
+--
recv_line :: Handle -> IO String
recv_line h = do
line <- hGetLine h
return line
+-- | Takes a Configuration, and an XML document (as a String). The XML
+-- document is written to the output directory, as specified by the
+-- Configuration.
+--
+-- This can fail, but we don't purposefully throw any exceptions. If
+-- something goes wrong, we would rather log it and keep going.
+--
save_document :: Configuration -> String -> IO ()
save_document cfg doc =
case maybe_path of
filename = fmap (++ ".xml") xmlfid
maybe_path = fmap ((output_directory cfg) </>) filename
+
-- | Loop forever, writing the buffer to file whenever a new XML
--- prologue is seen.
+-- prologue is seen. This is the low-level "loop forever" function
+-- that we stay in as long as we are connected to one feed.
+--
loop :: Configuration -> Handle -> [String] -> IO ()
loop !cfg !h !buffer = do
line <- recv_line h
save_document cfg document
loop cfg h [line] -- empty the buffer before looping again
else
- loop cfg h (line : buffer) -- append line to the head of the buffer and loop
+ -- append line to the head of the buffer and loop
+ loop cfg h (line : buffer)
log_in :: Configuration -> Handle -> IO ()
-- | A wrapper around threadDelay which takes seconds instead of
-- microseconds as its argument.
+--
thread_sleep :: Int -> IO ()
thread_sleep seconds = do
let microseconds = seconds * (10 ^ (6 :: Int))
threadDelay microseconds
+-- | The entry point of the program.
main :: IO ()
main = do
rc_cfg <- OC.from_rc
-- set in either the config file or on the command-line.
let cfg = (def :: Configuration) `merge_optional` opt_config
+ -- This may be superstition (and I believe stderr is unbuffered),
+ -- but it can't hurt.
hSetBuffering stderr NoBuffering
hSetBuffering stdout NoBuffering
+ -- Begin connecting to our feed hosts, starting with the first one.
round_robin cfg 0
where
+ -- | This is the top-level "loop forever" function. If an
+ -- exception is thrown, it will propagate up to this point, where
+ -- it will be logged and ignored in style.
+ --
+ -- Afterwards, we recurse (call ourself) again to loop more forevers.
+ --
round_robin :: Configuration -> Int -> IO ()
round_robin cfg feed_host_idx = do
let hosts = get_feed_hosts $ feed_hosts cfg
instance DCT.Configured FeedHosts where
+ -- | This allows us to read a FeedHosts object out of a Configurator
+ -- config file. By default Configurator wouldn't know what to do,
+ -- so we have to tell it that we expect a list, and if that list
+ -- has strings in it, we can apply the FeedHosts constructor to
+ -- it.
convert (DCT.List xs) =
+ -- mapM gives us a Maybe [String] here.
fmap FeedHosts (mapM convert_string xs)
where
convert_string :: DCT.Value -> Maybe String
convert_string = DCT.convert
+ -- If we read anything other than a list of values out of the file,
+ -- fail.
convert _ = Nothing
-
+-- | Minimal XML functionality needed to parse each document's
+-- XML_File_ID.
+--
module TSN.Xml (
parse_xmlfid,
xml_prologue )