-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,3 @@ cabal.project.local | |
cabal.project.local~ | ||
.HTF/ | ||
.ghc.environment.* | ||
examples/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
module Main where | ||
|
||
import Args | ||
import Options.Applicative (execParser) | ||
import Args | ||
import Itunes | ||
|
||
main :: IO () | ||
main = execParser app >>= run | ||
|
||
run :: Args -> IO () | ||
run = putStrLn . show | ||
|
||
escape :: Transform -> Action -> [Directory] -> IO () | ||
escape = undefined |
This file contains 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
Binary file not shown.
Binary file not shown.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,52 @@ | ||
module Itunes where | ||
|
||
import Control.Monad (filterM) | ||
import Data.List (foldl') | ||
import System.IO (FilePath) | ||
import System.FilePath ((</>), takeFileName) | ||
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory) | ||
import ID3.Simple (readTag, Tag) | ||
import Attributes (Attribute, getAttribute) | ||
|
||
type From = FilePath | ||
type To = FilePath | ||
type Directory = FilePath | ||
type PathSegment = String | ||
|
||
data Copy = Copy { from :: From | ||
, to :: To | ||
} | ||
|
||
type Action = Copy -> IO () | ||
type Transform = From -> IO To | ||
|
||
handleDirectory :: Directory -> Transform -> IO ([Directory], [Copy]) | ||
handleDirectory dir t = contents dir >>= | ||
(\(dirs, files) -> ((,) dirs) <$> (sequence $ mkTarget t <$> files)) | ||
|
||
contents :: Directory -> IO ([Directory], [FilePath]) | ||
contents d = do | ||
contents <- (fmap $ (</>) d) <$> listDirectory d | ||
dirs <- filterM doesDirectoryExist contents | ||
files <- filterM doesFileExist contents | ||
return (dirs, files) | ||
|
||
mkTarget :: (From -> IO To) -> From -> IO Copy | ||
mkTarget t f = Copy f <$> t f | ||
|
||
mkTransform :: Directory -> [Attribute] -> (From -> IO To) | ||
mkTransform target attrs = transform | ||
where transform f = defaultPath target f <$> | ||
(fmap $ joinPath target f) <$> | ||
(=<<) (readAttrs attrs) <$> | ||
(readTag f) | ||
|
||
readAttrs :: [Attribute] -> Tag -> Maybe [PathSegment] | ||
readAttrs attrs tag = sequence $ getAttribute <$> attrs <*> [tag] | ||
|
||
joinPath :: Directory -> From -> [PathSegment] -> To | ||
joinPath target f path = (foldl' (</>) target path) </> (takeFileName f) | ||
|
||
defaultPath :: Directory -> From -> Maybe To -> To | ||
defaultPath target _ (Just p) = p | ||
defaultPath target f Nothing = (target </> "<Nothing>") </> (takeFileName f) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module ItunesSpec where | ||
|
||
import Test.Hspec | ||
import Attributes | ||
import Itunes | ||
|
||
spec :: Spec | ||
spec = | ||
describe "transforming file path" $ do | ||
it "joins the path segments from the attributes" $ do | ||
joinPath "./target" "./src/artist/piece.mp3" ["composer", "album"] `shouldBe` | ||
"./target/composer/album/piece.mp3" | ||
|
||
it "anchors at target and interpolates the attribute values" $ do | ||
let file = "./examples/holst/mars.mp3" | ||
let target = "/home/stuart/music" | ||
let attrs = [Composer, Album] | ||
let transform = mkTransform target attrs | ||
dest <- transform file | ||
dest `shouldBe` "/home/stuart/music/Holst/Planets -- Atlanta Symphony/mars.mp3" | ||
it "uses <Nothing> as a placeholder when attrs weren't satisfied" $ do | ||
let file = "./examples/strauss/dinner.mp3" | ||
let target = "/home/stuart/music" | ||
let attrs = [Composer, Album] | ||
let transform = mkTransform target attrs | ||
dest <- transform file | ||
dest `shouldBe` "/home/stuart/music/<Nothing>/dinner.mp3" |