4 import Control.Arrow ( (&&&), arr, returnA )
5 import Control.Monad ( when )
6 import Control.Monad.IO.Class ( liftIO )
7 import Database.Groundhog.Core ( PersistEntity )
8 import Database.Groundhog.Sqlite (
9 defaultMigrationLogger,
15 import Data.Maybe ( isNothing )
16 import Data.Monoid ( (<>) )
17 import System.Console.CmdArgs ( def )
18 import System.Exit ( exitWith, ExitCode (ExitFailure) )
19 import System.IO.Error ( catchIOError )
20 import Text.XML.HXT.Core (
42 import CommandLine ( get_args )
43 import Configuration ( Configuration(..), merge_optional )
45 exit_no_connection_string,
47 import Network.Services.TSN.Logging ( init_logging )
48 import qualified OptionalConfiguration as OC (
49 OptionalConfiguration ( connection_string, xml_files ),
51 import Network.Services.TSN.Report (
54 import qualified TSN.Injuries as Injuries (
56 Message ( listings ) )
57 import qualified TSN.InjuriesDetail as InjuriesDetail (
58 Listing ( player_listings ),
64 -- | A list of options passed to 'readDocument' when we parse an XML
65 -- document. We don't validate because the DTDs from TSN are
66 -- wrong. As a result, we don't want to keep useless DTDs
67 -- areound. Thus we disable 'withSubstDTDEntities' which, when
68 -- combined with "withValidate no", prevents HXT from trying to read
71 parse_opts :: SysConfigList
73 [ withPreserveComment no,
75 withSubstDTDEntities no,
79 -- | We put the 'XmlTree' argument last so that it's easy to eta
80 -- reduce all of the import_foo functions that call this.
82 import_generic :: (XmlPickler a, PersistEntity b)
83 => b -- ^ Dummy Listing instance needed for 'migrate'
84 -> (a -> [b]) -- ^ listings getter
86 -> IO (Maybe Int) -- ^ Return the number of records inserted.
87 import_generic dummy g xml =
88 withSqliteConn "foo.sqlite3" $ runDbConn $ do
89 runMigration defaultMigrationLogger $ do
91 let root_element = unpickleDoc xpickle xml
94 let msg = "Could not unpickle document in import_generic."
95 liftIO $ report_error msg
98 ids <- mapM (\l -> insert l) (g elt)
99 return $ Just (length ids)
101 -- | Import TSN.Injuries from an 'XmlTree'.
102 import_injuries :: XmlTree -> IO (Maybe Int)
105 (undefined :: Injuries.Listing)
108 -- | Import TSN.InjuriesDetail from an 'XmlTree'.
109 import_injuries_detail :: XmlTree -> IO (Maybe Int)
110 import_injuries_detail =
112 (undefined :: InjuriesDetail.PlayerListing)
113 ( (concatMap InjuriesDetail.player_listings) . InjuriesDetail.listings)
115 import_file :: FilePath -> IO ()
116 import_file path = do
117 results <- catchIOError
120 report_error (show e)
121 report_error $ "Failed to import file " ++ path ++ "."
122 -- Return a nonempty list so we don't claim incorrectly that
123 -- we couldn't parse the DTD.
127 -- If results' is empty, one of the arrows return "nothing."
128 [] -> report_error $ "Unable to determine DTD for file " ++ path ++ "."
132 Just cnt -> report_info $ "Successfully imported " ++
134 " records from " ++ path ++ "."
136 -- | An arrow that reads a document into an 'XmlTree'.
137 readA :: IOStateArrow s a XmlTree
138 readA = readDocument parse_opts path
140 -- | An arrow which parses the doctype "SYSTEM" of an 'XmlTree'.
141 -- We use these to determine the parser to use.
142 doctypeA :: ArrowXml a => a XmlTree String
143 doctypeA = getAttrl >>> hasName "doctype-SYSTEM" /> getText
145 -- | Combine the arrows above as well as the function below
146 -- (arrowized with 'arr') into an IO action that does everything
147 -- (parses and then runs the import on what was parsed).
149 -- The result of runX has type IO [IO (Maybe Int)]. We thus use
150 -- bind (>>=) and sequence to combine all of the IOs into one
151 -- big one outside of the list.
152 parse_and_import :: IO [Maybe Int]
154 (runX $ readA >>> (doctypeA &&& returnA) >>> (arr import_with_dtd))
158 -- | Takes a 'Doctype', 'XmlTree' pair and uses the 'Doctype' to
159 -- determine which function to call on the 'XmlTree'.
160 import_with_dtd :: (String, XmlTree) -> IO (Maybe Int)
161 import_with_dtd (dtd,xml)
162 | dtd == "injuriesxml.dtd" = import_injuries xml
163 | dtd == "Injuries_Detail_XML.dtd" = import_injuries_detail xml
165 report_info $ "Unrecognized DTD in " ++ path ++ ": " ++ dtd ++ "."
173 -- Merge the config file options with the command-line ones,
174 -- prefering the command-line ones.
175 let opt_config = rc_cfg <> cmd_cfg
177 -- Update a default config with any options that have been set in
178 -- either the config file or on the command-line. We initialize
179 -- logging before the missing parameter checks below so that we can
181 let cfg = (def :: Configuration) `merge_optional` opt_config
182 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
184 -- Check the optional config for missing required options.
185 when (null $ OC.xml_files opt_config) $ do
186 report_error "No XML files given."
187 exitWith (ExitFailure exit_no_xml_files)
189 -- There's a default connection string, namely the empty string, but
190 -- it's not much use to us. So we make sure that we were given
191 -- something explicitly.
192 when (isNothing (OC.connection_string opt_config)) $ do
193 report_error "No connection string supplied."
194 exitWith (ExitFailure exit_no_connection_string)
196 -- We don't do this in parallel (for now?) to keep the error
197 -- messages nice and linear.
198 mapM_ import_file (OC.xml_files opt_config)