예제 #1
0
def json2dict( jsonfile ):
    """Convert ``jsonfile`` to python dictionary. Return ``None`` if jsonfile
    is not found."""
    if isfile( jsonfile ) :
        txt = open(jsonfile).read()
        d = h.json_decode(txt) if txt else {}
    else :
        return {}
    if not isinstance( d, dict ) :
        raise Exception( "Expected %r as property of settings" % jsonfile )
    return d
예제 #2
0
    def config(self, **kwargs):
        """:meth:`pluggdapps.interfaces.IConfigDB.config` interface method.

        Keyword arguments,

        ``netpath``,
            Netpath, including subdomain-hostname and script-path, on which
            web-application is mounted. Optional.

        ``section``,
            Section name to get or set config parameter. Optional.

        ``name``,
            Configuration name to get or set for ``section``. Optional.

        ``value``,
            If present, this method was invoked for setting configuration
            ``name`` under ``section``. Optional.

        - if netpath, section, name and value kwargs are supplied, will update
          config-parameter `name` under webapp's `section` with `value`.
          Return the updated value.
        - if netpath, section, name kwargs are supplied, will return
          configuration `value` for `name` under webapp's `section`.
        - if netpath, section kwargs are supplied, will return dictionary of 
          all configuration parameters under webapp's section.
        - if netpath is supplied, will return the entire table as dictionary
          of sections and settings.
        - if netpath is not supplied, will use `section`, `name` and `value`
          arguments in the context of ``platform`` table.
        """
        if self.conn == None: return None

        netpath = kwargs.get('netpath', 'platform')
        section = kwargs.get('section', None)
        name = kwargs.get('name', None)
        value = kwargs.get('value', None)

        c = self.conn.cursor()
        if section:
            c.execute("SELECT * FROM '%s' WHERE section='%s'" %
                      (netpath, section))
            result = list(c)
            secsetts = h.json_decode(result[0][1]) if result else {}
            if name and value:
                secsetts[name] = value
                secsetts = h.json_encode(secsetts)
                c.execute("DELETE FROM '%s' WHERE section='%s'" %
                          (netpath, section))
                c.execute("INSERT INTO '%s' VALUES ('%s', '%s')" %
                          (netpath, section, secsetts))
                self.conn.commit()
                rc = value
            elif name:
                rc = secsetts[name]
            else:
                rc = secsetts
        else:
            c.execute("SELECT * FROM '%s'" % (netpath, ))
            rc = {section: h.json_decode(setts) for section, setts in list(c)}
        return rc
예제 #3
0
    def config( self, **kwargs ):
        """:meth:`pluggdapps.interfaces.IConfigDB.config` interface method.

        Keyword arguments,

        ``netpath``,
            Netpath, including subdomain-hostname and script-path, on which
            web-application is mounted. Optional.

        ``section``,
            Section name to get or set config parameter. Optional.

        ``name``,
            Configuration name to get or set for ``section``. Optional.

        ``value``,
            If present, this method was invoked for setting configuration
            ``name`` under ``section``. Optional.

        - if netpath, section, name and value kwargs are supplied, will update
          config-parameter `name` under webapp's `section` with `value`.
          Return the updated value.
        - if netpath, section, name kwargs are supplied, will return
          configuration `value` for `name` under webapp's `section`.
        - if netpath, section kwargs are supplied, will return dictionary of 
          all configuration parameters under webapp's section.
        - if netpath is supplied, will return the entire table as dictionary
          of sections and settings.
        - if netpath is not supplied, will use `section`, `name` and `value`
          arguments in the context of ``platform`` table.
        """
        if self.conn == None : return None

        netpath = kwargs.get( 'netpath', 'platform' )
        section = kwargs.get( 'section', None )
        name = kwargs.get( 'name', None )
        value = kwargs.get( 'value', None )

        c = self.conn.cursor()
        if section :
            c.execute(
                "SELECT * FROM '%s' WHERE section='%s'" % (netpath,section))
            result = list(c) 
            secsetts = h.json_decode( result[0][1] ) if result else {}
            if name and value :
                secsetts[name] = value
                secsetts = h.json_encode(secsetts)
                c.execute( "DELETE FROM '%s' WHERE section='%s'" % 
                           (netpath, section) )
                c.execute( "INSERT INTO '%s' VALUES ('%s', '%s')" %
                           (netpath, section, secsetts) )
                self.conn.commit()
                rc = value
            elif name :
                rc = secsetts[name]
            else :
                rc = secsetts
        else :
            c.execute( "SELECT * FROM '%s'" % (netpath,) )
            rc = {  section : h.json_decode( setts )
                                        for section, setts in list(c) }
        return rc