-
Notifications
You must be signed in to change notification settings - Fork 1
/
LmodPackage.hs
110 lines (95 loc) · 3.21 KB
/
LmodPackage.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- (c) [email protected], 2013
-- | Parse Lmod JSON into more Haskell types.
--
--
-- The JSON can be generated from Lmod with:
--
-- @ $ \/path\/to\/spider -o softwarePage \/opt\/modulefiles > lmod.json@
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
module LmodPackage where
import Control.Applicative
import Control.Monad
import Data.Typeable
import Data.Data
import Data.List
import Data.Maybe
import Data.Aeson
import Data.Aeson.Types
import qualified Data.HashMap.Strict as HM
import qualified Data.ByteString.Lazy.Char8 as BS
import qualified Data.Text as T
import qualified Data.Vector as V
-- | Lmod package representation
data Package = Package
{ package :: T.Text
, displayName :: T.Text
, defaultVersionName :: T.Text
, description :: T.Text
, infoUrl :: T.Text
, license :: T.Text
, category :: T.Text
, moduleName :: T.Text
, versionPageUrl :: T.Text
, keywords :: [T.Text]
, versions :: HM.HashMap T.Text Version
, defaultVersion :: Version
} deriving (Eq, Show, Data, Typeable)
-- | Package version representation
data Version = Version
{ version :: T.Text
, fullName :: T.Text
, helpText :: T.Text
, helpPageHref :: T.Text
} deriving (Eq, Show, Data, Typeable)
type RecNo = Int
-- | Packages are packages.
data Packages = Packages
{ packages :: [Package]
, failures :: [(RecNo, String)]
} deriving(Eq, Show)
instance FromJSON Packages where
parseJSON (Array a) = do
pack <- return $ V.toList (V.map fromJSON a) :: Parser [Result Package]
let good = map (\(Success x) -> x) $ filter isSuccess pack
bad = map (\(n, Error x) -> (n, x)) $ filter isFailed (zip [1..] pack)
return $ Packages good bad
parseJSON _ = mzero
isFailed (_, Error _) = True
isFailed _ = False
isSuccess (Success _) = True
isSuccess (Error _) = False
instance FromJSON Package where
parseJSON (Object o) =
Package <$> o .: "package"
<*> o .: "displayName"
<*> o .: "defaultVersionName"
<*> o .:? "description" .!= "No description"
<*> o .:? "url" .!= ""
<*> o .:? "license" .!= ""
<*> liftM T.toLower (o .:? "categories" .!= "")
<*> return ""
<*> return ""
<*> liftM (map T.strip . T.splitOn (T.pack ",") . T.toLower)
(o .:? "keywords" .!= "")
<*> do
v <- o .: "versions"
vl <- liftM V.toList $ V.mapM parseJSON v :: Parser [Version]
return $ HM.fromList $ map (\x ->
(version x, x)) vl :: Parser (HM.HashMap T.Text Version)
<*> return emptyVersion
parseJSON _ = mzero
instance FromJSON Version where
parseJSON (Object o) =
Version <$> o .: "versionName"
<*> liftM T.toLower (o .: "full")
<*> o .:? "help" .!= ""
<*> return ""
emptyPackage = Package T.empty T.empty T.empty T.empty T.empty
T.empty T.empty T.empty T.empty [T.empty]
(HM.fromList [(T.empty, emptyVersion)]) emptyVersion
emptyVersion = Version T.empty T.empty T.empty T.empty
-- | Fetch the default Version object from a package
getDefaultVersion p =
let x = HM.lookup (defaultVersionName p) (versions p) in
fromMaybe emptyVersion x
unspace = T.filter (/=' ')