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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
{-# OPTIONS -fallow-overlapping-instances #-}
{- arch-tag: Python dict-like objects
Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
{- |
Module : Python.Objects.Dict
Copyright : Copyright (C) 2005 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen,
Maintainer : jgoerzen\@complete.org
Stability : provisional
Portability: portable
Python dict-like objects
Written by John Goerzen, jgoerzen\@complete.org
This module can be used to access Python dicts and dict-like objects such as
dbm databases. For a higher-level interface to creating and working with these
dbm interfaces, please see the functions in "MissingPy.AnyDBM". Also,
for functions that use this, please see "Database.AnyDBM".
-}
module Python.Objects.Dict (PyDict,
mkPyDict,
fromPyDict)
where
import Python.ForeignImports
import Python.Objects
import Python.Utils
import Foreign
import Foreign.C.Types
import Python.Exceptions
import Database.AnyDBM
import Python.Types
{- | The basic type for a Python dict or dict-like object. -}
newtype PyDict = PyDict
PyObject -- Main dict object
{- | Takes a 'PyObject' representing a Python dict or dict-like objext
and makes it into a 'PyDict'. -}
mkPyDict :: PyObject -> PyDict
mkPyDict o = PyDict o
{- | Takes a 'PyDict' and returns its internal 'PyObject'. -}
fromPyDict :: PyDict -> PyObject
fromPyDict (PyDict o) = o
{- | Wrap an operation, raising exceptions in the IO monad as appropriate. -}
pydwrap :: PyDict -> (PyObject -> IO a) -> IO a
pydwrap (PyDict pyobj) func = catchPy (func pyobj) exc2ioerror
{- | Give it a CPyObject instead. -}
cpydwrap :: PyDict -> (Ptr CPyObject -> IO a) -> IO a
cpydwrap x func = pydwrap x (\y -> withPyObject y func)
instance AnyDBM PyDict where
insertA h k v =
do ko <- toPyObject k
kv <- toPyObject v
withPyObject ko (\ck ->
withPyObject kv (\cv ->
cpydwrap h (\cdict ->
pyObject_SetItem cdict ck cv >>= checkCInt >> return()
)
)
)
deleteA h k =
do ko <- toPyObject k
withPyObject ko (\ck ->
cpydwrap h (\cdict ->
pyObject_DelItem cdict ck >>= checkCInt >> return ()))
lookupA h k =
do ko <- toPyObject k
withPyObject ko (\ck ->
cpydwrap h (\cdict ->
do r <- pyObject_GetItem cdict ck
if r == nullPtr
then do pyErr_Clear -- Ignore this exception
return Nothing
else do retval <- fromCPyObject r >>= fromPyObject
return $ Just retval
))
{-
toListA h =
This used to be:
pydwrap h fromPyObject
but some *dbm's are incompatible with that. Sigh. -}
keysA h =
cpydwrap h (\ch ->
do keysobj <- pyMapping_Keys ch >>= fromCPyObject
keys <- (fromPyObject keysobj)::IO [String]
return keys
)
flushA h = pydwrap h (\pyo -> do h <- hasattr pyo "sync"
if h
then runMethodHs pyo "sync" noParms noKwParms
else return ()
)
closeA h = pydwrap h (\pyo -> do h <- hasattr pyo "close"
if h
then runMethodHs pyo "close" noParms noKwParms
else return ()
)
|