Changeset 182:0d4d435866b6 in livinglogic.python.orasql

Show
Ignore:
Timestamp:
02/01/07 18:24:17 (13 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add class Privilege that models an object grant.

Add Connection.iterprivileges() for getting all privileges.

Add a script oragrant.py for copying privileges.

Update copyright year.

Files:
2 added
9 modified

Legend:

Unmodified
Added
Removed
  • NEWS.xml

    r180 r182  
     1<section><title>Changes in 1.13 (released 02/01/2007)</title> 
     2<ulist> 
     3<item>A new method <method>iterprivileges</method> has been added to 
     4<class>Connection</class>.</item> 
     5<item>A script <filename>oragrant.py</filename> has been added for copying 
     6privileges.</item> 
     7</ulist> 
     8</section> 
     9 
     10 
    111<section><title>Changes in 1.13 (released 11/06/2006)</title> 
    212<ulist> 
  • setup.py

    r179 r182  
    4747args=dict( 
    4848    name="ll-orasql", 
    49     version="1.13", 
     49    version="1.14", 
    5050    description="Utilities for working with cx_Oracle", 
    5151    long_description=DESCRIPTION, 
     
    6565            "oradiff = ll.orasql.scripts.oradiff:main", 
    6666            "oramerge = ll.orasql.scripts.oramerge:main", 
     67            "oragrant = ll.orasql.scripts.oragrant:main", 
    6768        ] 
    6869    ), 
     
    7273        "scripts/oradiff.py", 
    7374        "scripts/oramerge.py", 
     75        "scripts/oragrant.py", 
    7476    ], 
    7577    install_requires=[ 
    76         "ll-core >= 1.4", 
    77         "cx_Oracle >= 4.1.2", 
     78        "ll-core >= 1.7", 
     79        "cx_Oracle >= 4.2", 
    7880    ], 
    7981    namespace_packages=["ll"], 
  • src/ll/__init__.py

    r134 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 1999-2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 1999-2006 by Walter Dörwald 
     4## Copyright 1999-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 1999-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
  • src/ll/orasql/__init__.py

    r181 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2004-2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2004-2006 by Walter Dörwald 
     4## Copyright 2004-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2004-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
     
    4343 
    4444try: 
    45     from IPython.Extensions import astyle 
     45    import astyle 
    4646except ImportError: 
    4747    from ll import astyle 
     
    5050# ipipe support 
    5151try: 
    52     from IPython.Extensions import ipipe 
     52    import ipipe 
    5353except ImportError: 
    5454    ipipe = None 
     
    424424                        yield subobj 
    425425 
    426         funcs = (dosequences, dotables, doconstraints, doindexes, dosynonyms, doviews, domviews, docode) 
     426        funcs = [dosequences, dotables, doconstraints, doindexes, dosynonyms, doviews, domviews, docode] 
    427427        if mode == "drop": 
    428428            funcs = reversed(funcs) 
     
    431431            for obj in func(): 
    432432                yield obj 
     433 
     434    def iterprivileges(self, schema="user"): 
     435        """ 
     436        <par>Generator that yields object privileges granted by the current user 
     437        (or all users).</par> 
     438        <par><arg>schema</arg> specifies from which privileges should be 
     439        yielded:</par> 
     440        <dlist> 
     441        <term><lit>"user"</lit></term><item>Only object privileges granted by the 
     442        current user will be yielded.</item> 
     443        <term><lit>"all"</lit></term><item>All object privileges will be yielded.</item> 
     444        </dlist> 
     445        """ 
     446        if schema not in ("user", "all"): 
     447            raise UnknownSchemaError(schema) 
     448 
     449        cursor = self.cursor() 
     450 
     451        if schema == "all": 
     452            cursor.execute("select decode(grantor, user, null, grantor) as owner, privilege, decode(table_schema, user, '', table_schema || '.') || table_name as object, grantee from all_tab_privs order by table_schema, table_name, privilege") 
     453        else: 
     454            cursor.execute("select null as owner, privilege, decode(owner, user, '', owner || '.') || table_name as object, grantee from user_tab_privs where grantor=user order by owner, table_name, privilege") 
     455        for rec in cursor.fetchall(): 
     456            yield Privilege(rec.privilege, rec.object, rec.grantee, rec.owner, cursor.connection) 
    433457 
    434458 
     
    800824            yield (astyle.style_default, repr(self)) 
    801825 
    802     if hasattr(ipipe, "Descriptor"): 
    803         def __xattrs__(self, mode="default"): 
    804             yield "type" 
    805             yield "name" 
    806             yield "owner" 
    807             yield "connection" 
    808          
    809             if mode == "detail": 
    810                 yield "cdate()" 
    811                 yield "udate()" 
    812                 yield "-createddl()" 
    813                 yield "-dropddl()" 
    814                 yield "-iterreferences()" 
    815                 yield "-iterreferencedby()" 
    816                 yield "-iterreferencesall()" 
    817                 yield "-iterreferencedbyall()" 
    818     else: 
    819         def __xattrs__(self, mode="default"): 
    820             if mode == "detail": 
    821                 return ("type", "name", "owner", "connection", "cdate()", "udate()") 
    822             else: 
    823                 return ("type", "name", "owner", "connection") 
     826    def __xattrs__(self, mode="default"): 
     827        yield "type" 
     828        yield "name" 
     829        yield "owner" 
     830        yield "connection" 
     831     
     832        if mode == "detail": 
     833            yield "cdate()" 
     834            yield "udate()" 
     835            yield "-createddl()" 
     836            yield "-dropddl()" 
     837            yield "-iterreferences()" 
     838            yield "-iterreferencedby()" 
     839            yield "-iterreferencesall()" 
     840            yield "-iterreferencedbyall()" 
    824841 
    825842 
     
    10501067                yield obj 
    10511068 
    1052     if hasattr(ipipe, "Descriptor"): 
    1053         def __xattrs__(self, mode="default"): 
    1054             for attr in super(Table, self).__xattrs__(mode): 
    1055                 yield attr 
    1056             if mode=="detail": 
    1057                 yield "-itercolumns()" 
    1058                 yield "-iterrecords()" 
    1059                 yield "-itercomments()" 
    1060                 yield "-iterconstraints()" 
    1061                 yield "mview()" 
    1062     else: 
    1063         def __xattrs__(self, mode="default"): 
    1064             for attr in super(Table, self).__xattrs__(mode): 
    1065                 yield attr 
    1066             if mode=="detail": 
    1067                 yield "mview()" 
     1069    def __xattrs__(self, mode="default"): 
     1070        for attr in super(Table, self).__xattrs__(mode): 
     1071            yield attr 
     1072        if mode=="detail": 
     1073            yield "-itercolumns()" 
     1074            yield "-iterrecords()" 
     1075            yield "-itercomments()" 
     1076            yield "-iterconstraints()" 
     1077            yield "mview()" 
    10681078 
    10691079 
     
    13761386            for obj in super(Index, self).iterreferences(connection): 
    13771387                yield obj 
    1378  
    1379     def __xattrs__(self, mode="default"): 
    1380         for attr in super(Index, self).__xattrs__(mode): 
    1381             yield attr 
    1382         if mode == "detail": 
    1383             yield "constraint()" 
    13841388 
    13851389    def table(self, connection=None): 
     
    18771881            code += "\n" 
    18781882        return code 
     1883 
     1884 
     1885class Privilege(object): 
     1886    """ 
     1887    Models a database object privilege (i.e. a grant). 
     1888    """ 
     1889    def __init__(self, privilege, object, grantee, owner=None, connection=None): 
     1890        self.privilege = privilege 
     1891        self.object = object # might include an owner name 
     1892        self.grantee = grantee 
     1893        self.owner = owner 
     1894        self.connection = connection 
     1895 
     1896    def __repr__(self): 
     1897        if self.owner is not None: 
     1898            return "%s.%s(%r, %r, %r, %r)" % (self.__class__.__module__, self.__class__.__name__, self.privilege, self.object, self.grantee, self.owner) 
     1899        else: 
     1900            return "%s.%s(%r, %r, %r)" % (self.__class__.__module__, self.__class__.__name__, self.privilege, self.object, self.grantee) 
     1901 
     1902    def __str__(self): 
     1903        if self.owner is not None: 
     1904            return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.privilege, self.object, self.grantee, self.owner) 
     1905        else: 
     1906            return "%s(%r, %r, %r)" % (self.__class__.__name__, self.privilege, self.object, self.grantee) 
     1907 
     1908    def getconnection(self, connection): 
     1909        if connection is None: 
     1910            connection = self.connection 
     1911        if connection is None: 
     1912            raise TypeError("no connection available") 
     1913        return connection 
     1914 
     1915    def getcursor(self, connection): 
     1916        connection = self.getconnection(connection) 
     1917        return (connection, connection.cursor()) 
     1918 
     1919    def getconnectstring(self): 
     1920        if self.connection: 
     1921            return self.connection.connectstring() 
     1922        return None 
     1923    connectstring = property(getconnectstring) 
     1924 
     1925    def grantddl(self, connection=None, term=True, mapgrantee=True): 
     1926        """ 
     1927        &sql; to grant this privilege. If <arg>mapgrantee</arg> is a list 
     1928        or a dictionary and <lit><self/>.grantee</lit> is not in this list 
     1929        (or dictionary) no command will returned. If it's a dictionary and 
     1930        <lit><self/>.grantee</lit> is in it, the privilege will be granted 
     1931        to the user specified as the value instead of the original one. If 
     1932        <lit>mapgrantee</lit> is <lit>True</lit> (the default) the privilege 
     1933        will be granted to the original grantee. 
     1934        """ 
     1935        (connection, cursor) = self.getcursor(connection) 
     1936        if mapgrantee is True: 
     1937            grantee = self.grantee 
     1938        elif isinstance(mapgrantee, (list, tuple)): 
     1939            if self.grantee.lower() in map(str.lower, mapgrantee): 
     1940                grantee = self.grantee 
     1941            else: 
     1942                grantee = None 
     1943        else: 
     1944            mapgrantee = dict((key.lower(), value) for (key, value) in mapgrantee.iteritems()) 
     1945            grantee = mapgrantee.get(self.grantee.lower(), None) 
     1946        if grantee is None: 
     1947            return "" 
     1948        code = "grant %s on %s to %s" % (self.privilege, self.object, grantee) 
     1949        if term: 
     1950            code += ";\n" 
     1951        return code 
     1952 
     1953    def __xattrs__(self, mode="default"): 
     1954        yield "privilege" 
     1955        yield "object" 
     1956        yield "grantee" 
     1957        yield "owner" 
     1958        yield "connection" 
     1959        if mode == "detail": 
     1960            yield "-grantddl()" 
    18791961 
    18801962 
  • src/ll/orasql/scripts/oracreate.py

    r179 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2005/2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2005/2006 by Walter Dörwald 
     4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2005-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
  • src/ll/orasql/scripts/oradiff.py

    r179 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2005/2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2005/2006 by Walter Dörwald 
     4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2005-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
  • src/ll/orasql/scripts/oradrop.py

    r179 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2005/2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2005/2006 by Walter Dörwald 
     4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2005-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
  • src/ll/orasql/scripts/oramerge.py

    r179 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2005/2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2005/2006 by Walter Dörwald 
     4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2005-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
  • test/test_orasql.py

    r179 r182  
    22# -*- coding: iso-8859-1 -*- 
    33 
    4 ## Copyright 2004-2006 by LivingLogic AG, Bayreuth/Germany. 
    5 ## Copyright 2004-2006 by Walter Dörwald 
     4## Copyright 2004-2007 by LivingLogic AG, Bayreuth/Germany. 
     5## Copyright 2004-2007 by Walter Dörwald 
    66## 
    77## All Rights Reserved 
     
    6767    db = orasql.connect(dbname) 
    6868    list(db.iterfks()) 
     69 
     70 
     71def test_connection_iterprivileges(): 
     72    db = orasql.connect(dbname) 
     73    list(db.iterprivileges()) 
    6974 
    7075