def privileges_get(cursor, user, host):
 output = {}
 cursor.execute('SHOW GRANTS FOR %s@%s', (user, host))
 grants = cursor.fetchall()
 def pick(x):
  if (x == 'ALL PRIVILEGES'):
   return 'ALL'
  else:
   return x
 for grant in grants:
  res = re.match("GRANT (.+) ON (.+) TO '.*'@'.+'( IDENTIFIED BY PASSWORD '.+')? ?(.*)", grant[0])
  if (res is None):
   raise InvalidPrivsError(('unable to parse the MySQL grant string: %s' % grant[0]))
  privileges = res.group(1).split(', ')
  privileges = [pick(x) for x in privileges]
  if ('WITH GRANT OPTION' in res.group(4)):
   privileges.append('GRANT')
  if ('REQUIRE SSL' in res.group(4)):
   privileges.append('REQUIRESSL')
  db = res.group(2)
  output[db] = privileges
 return output

def privileges_get ( cursor , user , host ) : output = { } cursor . execute ( 'SHOW GRANTS FOR %s@%s' , ( user , host ) ) grants = cursor . fetchall ( ) def pick ( x ) : if ( x == 'ALL PRIVILEGES' ) : return 'ALL' else : return x for grant in grants : res = re . match ( "GRANT (.+) ON (.+) TO '.*'@'.+'( IDENTIFIED BY PASSWORD '.+')? ?(.*)" , grant [ 0 ] ) if ( res is None ) : raise InvalidPrivsError ( ( 'unable to parse the MySQL grant string: %s' % grant [ 0 ] ) ) privileges = res . group ( 1 ) . split ( ', ' ) privileges = [ pick ( x ) for x in privileges ] if ( 'WITH GRANT OPTION' in res . group ( 4 ) ) : privileges . append ( 'GRANT' ) if ( 'REQUIRE SSL' in res . group ( 4 ) ) : privileges . append ( 'REQUIRESSL' ) db = res . group ( 2 ) output [ db ] = privileges return output
mysql doesnt have a better method of getting privileges aside from the show grants query syntax .