This file is indexed.

/usr/share/pyshared/sqlkit/debug/debug.py is in python-sqlkit 0.9.5-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""
This module provides easy way to debug with print commands. This may not be
the best way to debug, many times ``pdb`` will be much more
powerfull. Nevertheless I found mysel in need of these functions...

The way to use this is

  1. in you library::

      dbg.write('text message', 'text2')

  2. in you application::

      dbg.debug(True)

write() - caller()
------------------
     
from now on, each time you use dbg.write()/dbg.show_caller() you'll see the
text you want to log only if you enabled it with dbg.debug(True)

If you want the log to happen in a gtk.TreeView window, you can specify:;

     import dbg
     dbg.debug(True, gtk=True)


Logging methods
---------------

Following recepe 198078 in the ASP Python CookBook (see in the code)
this module provides also a metaclass to trace the use of methods of a
particular class. It will be instantiated as::

   class A(object):
       __metaclass__ = dbg.LogTheMethods

for each method call a line will be logged (unless configured to be ignored)
with

    function called
    caller class
    arguments
    caller function
    line of code
    return code
    calling class
    time elapsed

IMPORTANT: since the logging occurs with metaclasses you need to import dbg and
           set debugging *before* importing the class you want to trace::

            import dbg
            dbg.debug(True, gtk=True)
            dbg.trace_class('SqlTable2')  # optional
            dbg.trace_function(exclude='cell_default_cb|cell_bool_cb')  # optional
            import sqlkit

TraceIt
-------

in case you need to understand who is changeing the value of a variable, you
can use TraceIt as::

    commit_allowed = dbg.TraceIt(True, name='commit_allowed', mode='rw')

and use it as a normal variable.  You'll get lines in your output stating
who changed the value of that variable, in the form::

     __str__/function_name:  old_value => new_value
     __get__/function_name:  value

  
"""

import sys, re
import new
# I had to hide this import inside functions otherwise import of sqlkit broke.
#from sqlkit.layout import misc  
import pango
import inspect
import time
import inspect
from  pdb import set_trace
import gobject

CLASS_INCLUDE = '^.*$'
CLASS_EXCLUDE = '^$'
FUNCTION_INCLUDE = re.compile('.*')
FUNCTION_EXCLUDE = re.compile('^$')

class FakeLogger(object):
    def add(self, *args,**kw):
        pass
    def write(self, *args, **kw):
         write(*args, **kw)
    def ret(self, iter, value):
        return value

DBGLogger = FakeLogger()
#DBGLogger = None
DBG = False

def test():
    global DBG
    print "DBG:", DBG
    
def debug(x=None, gtk=False):
    global DBGLogger
    global DBG
    
    if gtk == True:
        DBGLogger = get_gtk_logger(show=True)

    if x is True:
        DBG = 1
    if x is False:
        DBG = 0
    if isinstance(x , int):
        DBG = x

    
def write(*text, **kwargs):
    global DBG
    global DBGLogger
    if 'filter' not in kwargs:
        filter = None
    
    if 'sfilter' not in kwargs:
        sfilter = None
    
    if 'direct' not in kwargs:
        kwargs['direct'] = False

    if 'meth' not in kwargs:
        meth = caller()
    else:
        meth = kwargs['meth']
        
    if 'called_by' not in kwargs:
        called_by = ''
    else:
        called_by = kwargs['called_by']
        
    if DBG == 0:
        return
    if not text:
        ### FIXME: should go to DBGLogger...?
        print "%s (line %s) locals(): \n%s" % (caller(), caller(mode='lineno'),
                          dshow(caller(mode='loc', level=2), ret=True,
                                filter=filter, sfilter=sfilter,mute=True))
                                               
        return
    
    txt = ""
    for i in text:
        txt += " " + str(i)

    if not kwargs['direct'] and DBGLogger:
        try:
            instance=caller(mode='instance')
            Class_str = instance.nick
        except Exception, e:
            Class_str = ''
        
        DBGLogger.write(txt=txt, line=caller(mode='lineno'), meth=meth,
                        mode='write', Class=Class_str, caller=called_by)
    else:
        #print "dbg.write: QUI 2, DBGLogger:", DBGLogger, "direct:", kwargs['direct']
        print "%s (line %s): %s" % (caller(), caller(mode='lineno'), txt)
    
# def single_write(txt, called_by='', meth='', direct=False, **args):
#     """similar to write but accept just one text arg and some more args"""
#     global DBG
#     global DBGLogger
#     if 'filter' not in args:
#         filter = None
    
#     if 'sfilter' not in args:
#         sfilter = None
    
#     if DBG == 0:
#         return
#     if direct and DBGLogger:
#         DBGLogger.write(txt=txt, line=caller(mode='lineno'), meth=meth,
#                         mode='write', caller=called_by)
#     else:
#         print "%s (line %s): %s" % (caller(), caller(mode='lineno'), txt)


    
def caller(mode=None, level=2):
    """
    mode can be:
    lineno:  just return line number for the calling frame
    loc:     return the local variables as dict
    None:    return (class_name, caller_func)
    Class :   return class
    instance : return class if applicable
    """
    frame = sys._getframe(level)

    if mode == 'lineno':
        return frame.f_lineno
    
    if mode == 'loc':
        return frame.f_locals

    caller = frame.f_code.co_name
    class_name = ''


    try:
        a = frame.f_locals['self'].__class__
        class_name =  re.search("'(.*)'", str(a)).group(1) + "."

        if mode == 'Class':
            return a

        if mode == 'instance':
            return frame.f_locals['self']

    except:
        pass
    return "%s%s" % (class_name, caller)
    
def show_caller(filter=None, sfilter=None, loc=True):
    if DBG != 0:
        if loc:
            Locals = dshow(caller(mode='loc', level=3), ret=True,
                           filter=filter, sfilter=sfilter,mute=True)
        else:
            Locals = ''
            
        print "%s called from: %s (line %s) \n%s" % (
            caller(),
            caller(level=3), caller(level=3, mode='lineno'), Locals )
            
    
def dshow(diz, filter=None, sfilter=None, mute=False, ret=False, name=None):
    """print a dictionary in a friendly way
    filter:  only print keys that match 'filter'
    sfilter: only print keys that _don't_ match 'filter'
    """

    global DBG
    if DBG == 0:
        return
    txt = []
    if not mute:
        txt += [caller()]
    filtered = {}
    for k,v in diz.iteritems():
        if filter:
            if not re.search(filter, k):
                continue
        if sfilter:
            if re.search(sfilter, k):
                continue
        txt += ["    %s: %s" % (k, v)]
        filtered[k] = v
    if ret:
        return "\n".join(txt)
    else:
        try:
            iter = DBGLogger.write(meth=caller(), txt=name, mode='dict')
            for k,v in filtered.iteritems():
                DBGLogger.write(meth=k, txt=v, mode='value', parent=iter)
        except:
            print "\n".join(txt)

    
def ddir(obj, filter=None, sfilter=None, mute=False, ret=False):
    """print dir of an object in a readable fasion
    filter:  only print keys that match 'filter'
    sfilter: only print keys that _don't_ match 'filter'
    """

    global DBG
    if DBG == 0:
        return
    txt = []
    if not mute:
        txt += [caller()]
    for k in dir(obj):
        if filter:
            if not re.search(filter, k):
                continue
        if sfilter:
            if re.search(sfilter, k):
                continue
        v = getattr(obj, k)
        txt += ["    %s: %s" % (k, v)]
    if ret:
        return "\n".join(txt)
    else:
        print "\n".join(txt)

    
def warning(text):
    print text
    
def sql_debug(sql, params):
    msg = re.sub(':(?P<a>\S+)', '\'%(\g<a>)s\'', sql)
    #sd.debug("%s %s" % (DBG, params))
    print msg % params
    #write(msg % params)


indent = 0

### Trace
class TraceIt(object):
     """A data descriptor that sets and returns values
        normally and prints a message logging their access.
        Usi it in code like this::

           rowCommit = dbg.TraceIt(True, name='rowCommit', mode='rw')

     and use it as a normal variable.  You'll get lines in your output stating
     who changed the value of that variable, in the form::

     __str__/function_name:  old_value => new_value
     __get__/function_name:  value

     """

     def __init__(self, initval=None, name='var', mode='rw'):
         self.read = False
         if re.search('r', mode):
             self.read = True
             
         self.write = False
         if re.search('w', mode):
             self.write = True
             
         self.val = initval
         self.name = name

     def __get__(self, obj, objtype):

         if self.read:
             Caller = re.sub('.*\.([^\.]+$)', r'\1', caller(level=2))
             write("%s read %s" % (self.name, self.val),
                 called_by=Caller, meth='%s - r/%s' % (self.name, Caller))

         return self.val

     def __set__(self, obj, val):
         if self.write:
             Caller= re.sub('.*\.([^\.]+$)', r'\1', caller(level=2))
             write("%s: %s -> %s" % (self.name, self.val, val),
                 called_by=Caller, meth='%s - w/%s' % (self.name,Caller))
         self.val = val



### from a recepe in ASP:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078
def trace_class(ok=None, ko=None):
    """set pattern for classes that should be traced by LogTheMethods
    Rememeber that this command need to be issued before importing the package that
    should be traced
    """

    global CLASS_INCLUDE
    global CLASS_EXCLUDE
    if ok:
        CLASS_INCLUDE = ok

    if ko:
        CLASS_EXCLUDE = ko
        
def trace_function(exclude=None, include='.*'):
    """set pattern for functions/methods that should not be traced"""

    global FUNCTION_EXCLUDE
    global FUNCTION_INCLUDE

    if exclude:
        FUNCTION_EXCLUDE = re.compile(exclude)

    if not include == '.*':
        FUNCTION_INCLUDE = re.compile(exclude)
    
def logmethod(methodname):
    def _method(self,*argl,**argd):
        global indent
        global DBGLogger
        global DBG
        
        #print "DBG", methodname,  argl, argd
        #parse the arguments and create a string representation
        #import pdb; pdb.set_trace()
        args = []
        for item in argl:
            args.append('%s' % str(item))
        for key,item in argd.items():
            args.append('%s=%s' % (key,str(item)))
        argstr = ','.join(args)   
        
        caller1 = caller(level=1).replace('._method','')
        caller2 = caller(level=2)
        caller_class = caller1
        caller_meth = caller2
        
        try:
            class_str = self.nick
        except:
            class_str = caller_class
        if DBG:
            iter = DBGLogger.add(Class=class_str, meth=methodname, indent=indent,
                                 caller=caller_meth, line=caller(mode='lineno'),
                                 args=args, class_obj=caller(mode='Class'))
        
        indent += 1
        # do the actual method call
        try:
            f = getattr(self,'_H_%s' % methodname)

            returnval = f(*argl,**argd)
            
            indent -= 1
        except Exception, e:
            DBGLogger.add(Class=class_str, meth=str(e.__class__), indent=indent,
                                 caller=methodname, line=caller(mode='lineno'),
                                 args=[e], class_obj=caller(mode='Class'))
            indent -= 1
            raise

        if DBG:
            ## iter is not defined if the method si not traced
            if iter:
                DBGLogger.ret(iter, str(returnval))

        return returnval
    
    return _method


class LogTheMethods(gobject.GObjectMeta):
    def __new__(cls, classname, bases, classdict):
        global DBG
        if re.search(CLASS_INCLUDE, classname):
            if not re.search(CLASS_EXCLUDE, classname):
                pass
        else:
            return type.__new__(cls,classname,bases,classdict)

        meths = {}

        meths.update(dict(classdict.items()))

        for base in bases:
            for attr in dir(base):
                # don't override the methods...
                if attr not in meths:
                    meths[attr] = getattr(base, attr)

        for attr, item in meths.items():
            if not DBG:
                continue
            if attr.startswith('_H_') or attr.startswith('__'):
                continue

            if callable(item) and FUNCTION_INCLUDE.match(attr) and  not \
                   FUNCTION_EXCLUDE.match(attr):
                if '_H_%s'%attr not in meths: # prevent infinite loop
                    classdict['_H_%s'%attr] = item    # rebind the method
                    classdict[attr] = logmethod(attr) # replace  by wrapper
                    classdict[attr].__doc__ = item.__doc__ # replace  by wrapper

        ret = type.__new__(cls, classname, bases, classdict)

        return ret

def get_gtk_logger(show=True):
    import gtk_dbg

    return gtk_dbg.ShowLogs(show=show)

def get_logger():
    global DBGLogger
    return DBGLogger