Revision b711450c

b/Makefile.am
701 701
	src/Ganeti/OpParams.hs \
702 702
	src/Ganeti/Path.hs \
703 703
	src/Ganeti/Parsers.hs \
704
	src/Ganeti/PyValueInstances.hs \
704
	src/Ganeti/PyValue.hs \
705 705
	src/Ganeti/Query/Cluster.hs \
706 706
	src/Ganeti/Query/Common.hs \
707 707
	src/Ganeti/Query/Export.hs \
b/src/Ganeti/ConstantUtils.hs
32 32
import Data.Set (Set)
33 33
import qualified Data.Set as Set (difference, fromList, toList, union)
34 34

  
35
import Ganeti.THH (PyValue(..))
36
import Ganeti.PyValueInstances ()
35
import Ganeti.PyValue
37 36

  
38 37
-- | 'PythonChar' wraps a Python 'char'
39 38
newtype PythonChar = PythonChar { unPythonChar :: Char }
b/src/Ganeti/Hs2Py/ListConstants.hs.in
30 30

  
31 31
import Ganeti.Constants
32 32
import Ganeti.Hs2Py.GenConstants
33
import Ganeti.PyValueInstances ()
33
import Ganeti.PyValue ()
34 34

  
35 35
$(genPyConstants "pyConstants"
36 36
  (
b/src/Ganeti/OpCodes.hs
53 53

  
54 54
import qualified Ganeti.Hs2Py.OpDoc as OpDoc
55 55
import Ganeti.OpParams
56
import Ganeti.PyValueInstances ()
56
import Ganeti.PyValue ()
57 57
import Ganeti.Types
58 58
import Ganeti.Query.Language (queryTypeOpToRaw)
59 59

  
b/src/Ganeti/PyValue.hs
1
{-| PyValue contains instances for the 'PyValue' typeclass.
2

  
3
The typeclass 'PyValue' converts Haskell values to Python values.
4
This module contains instances of this typeclass for several generic
5
types.  These instances are used in the Haskell to Python generation
6
of opcodes and constants, for example.
7

  
8
-}
9

  
10
{-
11

  
12
Copyright (C) 2013 Google Inc.
13

  
14
This program is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License as published by
16
the Free Software Foundation; either version 2 of the License, or
17
(at your option) any later version.
18

  
19
This program is distributed in the hope that it will be useful, but
20
WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
General Public License for more details.
23

  
24
You should have received a copy of the GNU General Public License
25
along with this program; if not, write to the Free Software
26
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27
02110-1301, USA.
28

  
29
-}
30
{-# LANGUAGE ExistentialQuantification #-}
31
{-# LANGUAGE FlexibleInstances, OverlappingInstances,
32
             TypeSynonymInstances, IncoherentInstances #-}
33
module Ganeti.PyValue where
34

  
35
import Data.List (intercalate)
36
import Data.Map (Map)
37
import qualified Data.Map as Map
38
import qualified Data.Set as Set (toList)
39

  
40
import Ganeti.BasicTypes
41

  
42
-- * PyValue represents data types convertible to Python
43

  
44
-- | Converts Haskell values into Python values
45
--
46
-- This is necessary for the default values of opcode parameters and
47
-- return values.  For example, if a default value or return type is a
48
-- Data.Map, then it must be shown as a Python dictioanry.
49
class PyValue a where
50
  showValue :: a -> String
51

  
52
instance PyValue Bool where
53
  showValue = show
54

  
55
instance PyValue Int where
56
  showValue = show
57

  
58
instance PyValue Integer where
59
  showValue = show
60

  
61
instance PyValue Double where
62
  showValue = show
63

  
64
instance PyValue Char where
65
  showValue = show
66

  
67
instance (PyValue a, PyValue b) => PyValue (a, b) where
68
  showValue (x, y) = "(" ++ showValue x ++ "," ++ showValue y ++ ")"
69

  
70
instance (PyValue a, PyValue b, PyValue c) => PyValue (a, b, c) where
71
  showValue (x, y, z) =
72
    "(" ++
73
    showValue x ++ "," ++
74
    showValue y ++ "," ++
75
    showValue z ++
76
    ")"
77

  
78
instance PyValue String where
79
  showValue = show
80

  
81
instance PyValue a => PyValue [a] where
82
  showValue xs = "[" ++ intercalate "," (map showValue xs) ++ "]"
83

  
84
instance (PyValue k, PyValue a) => PyValue (Map k a) where
85
  showValue mp =
86
    "{" ++ intercalate ", " (map showPair (Map.assocs mp)) ++ "}"
87
    where showPair (k, x) = showValue k ++ ":" ++ showValue x
88

  
89
instance PyValue a => PyValue (ListSet a) where
90
  showValue = showValue . Set.toList . unListSet
91

  
92
-- * PyValue represents an unspecified value convertible to Python
93

  
94
-- | Encapsulates Python default values
95
data PyValueEx = forall a. PyValue a => PyValueEx a
96

  
97
instance PyValue PyValueEx where
98
  showValue (PyValueEx x) = showValue x
/dev/null
1
{-| PyValueInstances contains instances for the 'PyValue' typeclass.
2

  
3
The typeclass 'PyValue' converts Haskell values to Python values.
4
This module contains instances of this typeclass for several generic
5
types.  These instances are used in the Haskell to Python generation
6
of opcodes and constants, for example.
7

  
8
-}
9

  
10
{-
11

  
12
Copyright (C) 2013 Google Inc.
13

  
14
This program is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License as published by
16
the Free Software Foundation; either version 2 of the License, or
17
(at your option) any later version.
18

  
19
This program is distributed in the hope that it will be useful, but
20
WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
General Public License for more details.
23

  
24
You should have received a copy of the GNU General Public License
25
along with this program; if not, write to the Free Software
26
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27
02110-1301, USA.
28

  
29
-}
30
{-# LANGUAGE ExistentialQuantification #-}
31
{-# LANGUAGE FlexibleInstances, OverlappingInstances,
32
             TypeSynonymInstances, IncoherentInstances #-}
33
module Ganeti.PyValueInstances where
34

  
35
import Data.List (intercalate)
36
import Data.Map (Map)
37
import qualified Data.Map as Map
38
import qualified Data.Set as Set (toList)
39

  
40
import Ganeti.BasicTypes
41

  
42
-- * PyValue represents data types convertible to Python
43

  
44
-- | Converts Haskell values into Python values
45
--
46
-- This is necessary for the default values of opcode parameters and
47
-- return values.  For example, if a default value or return type is a
48
-- Data.Map, then it must be shown as a Python dictioanry.
49
class PyValue a where
50
  showValue :: a -> String
51

  
52
instance PyValue Bool where
53
  showValue = show
54

  
55
instance PyValue Int where
56
  showValue = show
57

  
58
instance PyValue Integer where
59
  showValue = show
60

  
61
instance PyValue Double where
62
  showValue = show
63

  
64
instance PyValue Char where
65
  showValue = show
66

  
67
instance (PyValue a, PyValue b) => PyValue (a, b) where
68
  showValue (x, y) = "(" ++ showValue x ++ "," ++ showValue y ++ ")"
69

  
70
instance (PyValue a, PyValue b, PyValue c) => PyValue (a, b, c) where
71
  showValue (x, y, z) =
72
    "(" ++
73
    showValue x ++ "," ++
74
    showValue y ++ "," ++
75
    showValue z ++
76
    ")"
77

  
78
instance PyValue String where
79
  showValue = show
80

  
81
instance PyValue a => PyValue [a] where
82
  showValue xs = "[" ++ intercalate "," (map showValue xs) ++ "]"
83

  
84
instance (PyValue k, PyValue a) => PyValue (Map k a) where
85
  showValue mp =
86
    "{" ++ intercalate ", " (map showPair (Map.assocs mp)) ++ "}"
87
    where showPair (k, x) = showValue k ++ ":" ++ showValue x
88

  
89
instance PyValue a => PyValue (ListSet a) where
90
  showValue = showValue . Set.toList . unListSet
91

  
92
-- * PyValue represents an unspecified value convertible to Python
93

  
94
-- | Encapsulates Python default values
95
data PyValueEx = forall a. PyValue a => PyValueEx a
96

  
97
instance PyValue PyValueEx where
98
  showValue (PyValueEx x) = showValue x
b/src/Ganeti/THH.hs
77 77
import Text.JSON.Pretty (pp_value)
78 78

  
79 79
import Ganeti.JSON
80
import Ganeti.PyValueInstances
80
import Ganeti.PyValue
81 81

  
82 82
import Data.Maybe
83 83
import Data.Functor ((<$>))

Also available in: Unified diff