Created
March 31, 2017 12:21
-
-
Save jarnaldich/517b77d755da186a4f9091d62838d4d2 to your computer and use it in GitHub Desktop.
Add fields from a directory tree with XML files using lenses, Directory Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- | |
stack | |
--resolver lts-4.2 | |
--install-ghc runghc | |
--package xml-lens | |
--package directory-tree | |
-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import qualified System.Directory.Tree as DT | |
import System.FilePath ( takeExtension ) | |
import System.Environment (getArgs) | |
import Text.XML | |
import Text.XML.Lens | |
import Text.Printf (printf) | |
import Data.Text as T | |
import Data.Text.Read (decimal) | |
main = do | |
[diskRoot] <- getArgs -- Gross... | |
tree <- DT.readDirectoryWith readTiles diskRoot | |
printf "Total tiles: %d\n" (sum $ DT.dirTree tree) | |
where | |
readTiles :: FilePath -> IO Int | |
readTiles f | |
| takeExtension f `elem` [".xml", ".XML"] = do | |
doc <- Text.XML.readFile def f | |
let lens = root . el "isd" ./ el "TIL" ./ el "NUMTILES" . text | |
return $ either (const 0) fst $ decimal (doc ^. lens) | |
| otherwise = return 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment