1

好吧,我卡住了。我一直在尝试从 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 吗?

4

1 回答 1

2

我建议最好的办法是将 aura 保存为 JSON 格式,而不是使用ShowandRead实例。这可能并不比在 Python 中制作一次性解析器更容易,但它将是一个更持久的社区变革。编辑:如果不是很明显,我的意见是最好只使用 Haskell,所以你需要做的就是fmap read (readFile path)解析,但我习惯于对于许多短时间的程序员来说这不是一个实用的选择-线。

第一步:获取光环代码

git clone https://github.com/fosskers/aura.git
cd aura

第 2 步:找到它在哪里 serealize 命名的东西PkgState

grep 'writeFile' * -R

show第三步:通过 aeson 包修改格式(当前使用的)为使用 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-depsaura.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 解码留给你。

于 2013-02-14T06:39:11.643 回答