好吧,我被卡住了。我一直在尝试从Aura的保存功能中加载带有序列化字符串的文件,然后进入一个对象来使用它。
该文件如下所示:
PkgState {timeOf = SimpleTime {yearOf = 2013, monthOf = February, dayOf = 13, hourOf = 15, minuteOf = 38}, pkgsOf = fromList [("a52dec",[0,7,4,6]),("aalib",[1,4,5,9]),("abs",[2,4,4,1]),("acl",[2,2,51,3]),("acsccid",[1,0,4,1]),("alsa-lib",[1,0,26,1]),("alsa-oss",[1,0,25,1]),("alsa-plugins",[1,0,26,1]),("alsa-utils",[1,0,26,1]),("alsaequal",[0,6,7]),("android-sdk-platform-tools",[16,0,1,2]),("android-udev",[1,6,1]),("anki-beta",[2,0,7,1]),("apache",[2,2,23,1]),("apr",[1,4,6,1]),("apr-util",[1,5,1,1]),("archey",[20121216,1]),("archlinux-keyring",[20130127,1]),("archlinux-themes-slim",[1,2,3]),("aria2",[1,16,3,1]),("asciidoc",[8,6,8,1]),("aspell",[0,60,6,1,1]),("aspell-en",[7,1,2]),("at-spi2-atk",[2,6,2,1]),("at-spi2-core",[2,6,3,1]),("atk",[2,6,0,1]),("attr",[2,4,46,2]),("aura",[1,1,3,0,1]),("autoconf",[2,69,1]),("autofs",[5,0,7,1]),("automake",[1,13,1,1]),("avahi",[0,6,31,5]),("babl",[0,1,10,1]),("bash",[4,2,42,3]),("bc",[1,6,8]),("beecrypt",[4,2,1,5]),("binutils",[2,23,1,3]),("bison",[2,7,1]),("blueman",[1,23,5]),("bluez",[4,101,1]),("bridge-utils",[1,5,1]),("bzip2",[1,0,6,4]),("c-ares",[1,9,1,1]),("ca-certificates",[20130119,1]),("ca-certificates-java",[20120608,1]),("cackey",[0,6,8,1]),("cairo",[1,12,12,2]),("cairo-perl",[1,82,2]),("caps",[0,9,6,1]),("cdparanoia",[10,2,4]),("cdrdao",[1,2,3,6]),("cdrkit",[1,1,11,2]),("chromium",[24,0,1312,70,1]),("chromium-pepper-flash",[11,6,602,167,1]),("cifs-utils",[5,9,1]),("cloog",[0,18,0,1])}
这已被削减。我可以手动解析它(这很可能会很痛苦)。是否有一些可以直接开始并获得对象的东西?
还是因为Aura是用haskell编写的,我应该只使用haskell吗?
最佳答案
我建议最好的方法是使Aura以JSON格式保存,而不是使用Show
和Read
实例。这可能比在Python中创建一次性解析器要容易得多,但是它将是更持久的社区变化。编辑:如果不是很明显,我认为仅使用Haskell确实是最好的选择,因此您要做的就是fmap read (readFile path)
进行解析,但是我习惯于对于许多程序员来说这不是一个实用的选择较短的时间线。
步骤1:获取光环代码
git clone https://github.com/fosskers/aura.git
cd aura
第2步:找到将“
PkgState
”东西化的位置:grep 'writeFile' * -R
步骤3:通过aeson包将格式(当前正在使用
show
)修改为使用JSON:+{-# LANGUAGE OverloadedStrings #-}
+import qualified Data.ByteString.Lazy.Char8 as BL
+ import Data.Aeson
...
- liftIO $ writeFile filename (show state)
+ liftIO $ BL.writeFile filename (encode state)
步骤4:步骤3确实没有完成,我们需要
ToJSON
实例才能使对encode
的调用正常工作。+{-# LANGUAGE OverloadedStrings #-}
+ import Data.Aeson
+ instance ToJSON PkgState of
+ toJSON (PkgState t p) = object [ "timeOf" .= t, "pkgsOf" .= p ]
并在
Aura/Time.hs
中:+ {-# LANGUAGE OverloadedStrings #-}
...
+ import Data.Aeson
...
+ instance ToJSON SimpleTime of
+ toJSON (SimpleTime y m d h n) = object [ "yearOf" .= y
+ , "monthOf" .= fromEnum m
+ , "dayOf" .= d
+ , "hourOf" .= h
+ , "minuteOf" .= n
+ ]
步骤5:要进行此构建,我们需要将aeson软件包添加到
build-deps
中的aura.cabal
中。步骤6:这可能会按您的目的正常工作,但是最好使Aura能够加载JSON编码的文件:
-import Data.Maybe (mapMaybe)
+import Data.Maybe (mapMaybe, fromMaybe)
+import Control.Applicative
+import Control.Monad
...
+instance ToJSON PkgState where
+ toJSON (PkgState t p) = object [ "timeOf" .= t, "pkgsOf" .= p ]
+
+instance FromJSON PkgState where
+ parseJSON (Object v) = PkgState <$> v .: "timeOf" <*> v .: "pkgsOf"
+ parseJSON _ = mzero
+
...
-readState name = liftIO (read `liftM` readFile (stateCache </> name))
-
+readState name = liftIO ((fromMaybe err . decode) `liftM` BL.readFile (stateCache </> name))
+ where err = error "Package State unrecognized"
并再次在
Aura/Time.hs
+
+instance FromJSON SimpleTime where
+ parseJSON (Object v) = SimpleTime <$> v .: "yearOf"
+ <*> (toEnum `fmap` (v .: "monthOf"))
+ <*> v .: "dayOf"
+ <*> v .: "hourOf"
+ <*> v .: "minuteOf"
+
我将这个JSON对象的python解码交给您。