Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial support for a list of variants + Add verbose flag #3

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ You can make changes in the UI and create a dump of your `dconf` file at any tim
dconf dump / > dconf.settings
```

> Note: For now, only types supported by Home Manager as specified [here](https://github.com/rycee/home-manager/blob/master/modules/lib/gvariant.nix) are supported. If there's enough interest, we might be able to work on supporting the [full specification](https://developer.gnome.org/glib/stable/gvariant-text.html).

### Run

Once compiled and installed (`cabal new-install`), you can use it as follows:
Expand All @@ -79,12 +81,13 @@ Type `--help` for some more information.
```shell
dconf2nix - Convert dconf files to Nix

Usage: dconf2nix (-i|--input ARG) (-o|--output ARG)
Usage: dconf2nix (-i|--input ARG) (-o|--output ARG) [-v|--verbose]
Convert a dconf file into a Nix file, as expected by Home Manager.

Available options:
-i,--input ARG Path to the dconf file (input)
-o,--output ARG Path to the Nix output file (to be created)
-v,--verbose Verbose mode (debug)
-h,--help Show this help text
```

Expand Down
2 changes: 1 addition & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import DConf2Nix ( dconf2nix )

main :: IO ()
main = runArgs >>= \case
(Args i o) -> dconf2nix i o
(Args i o v) -> dconf2nix i o v
7 changes: 7 additions & 0 deletions data/dconf.settings
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,12 @@ type-format='category'
window-position=(345, 79)
window-size=(1231, 902)

[org/gnome/Weather]
locations=[<(uint32 2, <('Gdańsk', 'EPGD', true, [(0.94916821905848536, 0.32230414101938371)], [(0.94858644845891815, 0.32579479952337237)])>)>, <(uint32 2, <('Gdynia, Działdowo County, Warmian-Masurian Voivodeship', '', false, [(0.93027949445787339, 0.34699627038777753)], [(0.93861053042695397, 0.35744550775024858)])>)>, <(uint32 2, <('Gdynia, Pomeranian Voivodeship', '', false, [(0.95149239024756216, 0.32358882203124067)], [(0.94858644845891815, 0.32579479952337237)])>)>]

[org/gnome/shell/weather]
automatic-location=true
locations=[<(uint32 2, <('Gdańsk', 'EPGD', true, [(0.94916821905848536, 0.32230414101938371)], [(0.94858644845891815, 0.32579479952337237)])>)>, <(uint32 2, <('Gdynia, Działdowo County, Warmian-Masurian Voivodeship', '', false, [(0.93027949445787339, 0.34699627038777753)], [(0.93861053042695397, 0.35744550775024858)])>)>, <(uint32 2, <('Gdynia, Pomeranian Voivodeship', '', false, [(0.95149239024756216, 0.32358882203124067)], [(0.94858644845891815, 0.32579479952337237)])>)>]

[system/locale]
region='en_US.UTF-8'
9 changes: 9 additions & 0 deletions output/dconf.nix
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ in
"window-size" = mkTuple [ 1231 902 ];
};

"org/gnome/Weather" = {
"locations" = "[<(uint32 2, <('Gdańsk', 'EPGD', true, [(0.94916821905848536, 0.32230414101938371)], [(0.94858644845891815, 0.32579479952337237)])>)>, <(uint32 2, <('Gdynia, Działdowo County, Warmian-Masurian Voivodeship', '', false, [(0.93027949445787339, 0.34699627038777753)], [(0.93861053042695397, 0.35744550775024858)])>)>, <(uint32 2, <('Gdynia, Pomeranian Voivodeship', '', false, [(0.95149239024756216, 0.32358882203124067)], [(0.94858644845891815, 0.32579479952337237)])>)>]";
};

"org/gnome/shell/weather" = {
"automatic-location" = true;
"locations" = "[<(uint32 2, <('Gdańsk', 'EPGD', true, [(0.94916821905848536, 0.32230414101938371)], [(0.94858644845891815, 0.32579479952337237)])>)>, <(uint32 2, <('Gdynia, Działdowo County, Warmian-Masurian Voivodeship', '', false, [(0.93027949445787339, 0.34699627038777753)], [(0.93861053042695397, 0.35744550775024858)])>)>, <(uint32 2, <('Gdynia, Pomeranian Voivodeship', '', false, [(0.95149239024756216, 0.32358882203124067)], [(0.94858644845891815, 0.32579479952337237)])>)>]";
};

"system/locale" = {
"region" = "en_US.UTF-8";
};
Expand Down
5 changes: 5 additions & 0 deletions src/CommandLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ where

import DConf.Data ( InputFilePath(..)
, OutputFilePath(..)
, Verbosity(..)
)
import Options.Applicative

data Args = Args
{ input :: InputFilePath
, output :: OutputFilePath
, verbose :: Verbosity
}

args :: Parser Args
Expand All @@ -25,6 +27,9 @@ args =
"Path to the Nix output file (to be created)"
)
)
<*> flag Normal
Verbose
(long "verbose" <> short 'v' <> help "Verbose mode (debug)")

runArgs :: IO Args
runArgs = execParser opts
Expand Down
14 changes: 10 additions & 4 deletions src/DConf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ dconf :: Parsec Text () Value
dconf = choice
[vBool, vInt, vDouble, vUint32, vInt64, vEmptyString, vString, vTuple, vAny]

-- There is no support for variants in HM yet so we parse them as String
vListOfVariant :: Parsec Text () Value
vListOfVariant = try $ do
try $ lookAhead (string "[<")
S . T.pack <$> manyTill anyToken (try $ lookAhead endOfLine)

vList :: Parsec Text () Value
vList = try $ do
char '['
Expand All @@ -89,18 +95,18 @@ dconfHeader = do
where tokens = choice $ many1 <$> [char '/', char '-', alphaNum]

dconfValue :: Parsec Text () Value
dconfValue = vList <|> dconf
dconfValue = vListOfVariant <|> vList <|> dconf

vKey :: Parsec Text () Key
vKey = Key . T.pack <$> manyTill (choice [alphaNum, char '-']) (char '=')

-- To debug insert `parserTraced "name" $` before the parser
entryParser :: Parsec Text () Entry
entryParser = do
h <- dconfHeader <* endOfLine
kv <- many1 ((,) <$> vKey <*> (dconfValue <* endOfLine))
optional endOfLine
pure $ Entry h (Map.fromList kv)

dconfParser :: Parsec Text () [Entry]
dconfParser = manyTill entryParser eof
dconfParser :: Verbosity -> Parsec Text () [Entry]
dconfParser Normal = manyTill entryParser eof
dconfParser Verbose = parserTraced "dconf" $ dconfParser Normal
2 changes: 2 additions & 0 deletions src/DConf/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Data.Text ( Text )
newtype InputFilePath = InputFilePath FilePath deriving Show
newtype OutputFilePath = OutputFilePath FilePath deriving Show

data Verbosity = Normal | Verbose

newtype Nix = Nix { unNix :: Text } deriving Show

newtype Key = Key Text deriving (Eq, Ord, Show)
Expand Down
6 changes: 3 additions & 3 deletions src/DConf2Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import DConf ( dconfParser )
import qualified Nix
import Text.Parsec.Text ( parseFromFile )

dconf2nix :: InputFilePath -> OutputFilePath -> IO ()
dconf2nix (InputFilePath input) (OutputFilePath output) = do
parseFromFile dconfParser input >>= \case
dconf2nix :: InputFilePath -> OutputFilePath -> Verbosity -> IO ()
dconf2nix (InputFilePath input) (OutputFilePath output) v = do
parseFromFile (dconfParser v) input >>= \case
Left err -> error (show err)
Right xs -> do
T.writeFile output Nix.renderHeader
Expand Down