Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_debug.py
blob: 5fd8fcff80b5aaeccae0eb9935dd91a7c183bccd (plain)
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
# coding: UTF-8
# Copyright 2009 Thomas Jourdan
#
# 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 3 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging
import sys
import traceback
import os
import time

"""Add support for design by contract to all classes except blacklist."""
# default path for testing on local machine
DEBUG_ACTIVITY_PATH = '/home/strom/minimal/activities/Kandid.activity'
DEBUG_PROFILE_PATH = '/home/strom/.sugar/1/'
DBC_BLACK_LIST = ['activity', 'ka_debug', 'kandidtube', 'setup',
                  'test_suite', 'test_enumerator']
_logger = None
_start_time = time.time()
_last_clock = 0.0
_try_once, locale_testrun = False, False
if os.path.exists(DEBUG_ACTIVITY_PATH):
    locale_testrun = True
    _try_once = True

is_DbC_activated = False

def _avtivate_logger():
    """Activate logger."""
    global _logger
    if not _logger:
        _logger = logging.getLogger('Kandid')
        _logger.setLevel(logging.DEBUG)

def info(msg):
    """Log an info message."""
    global _last_clock, locale_testrun
    clock_now = time.clock()
    if locale_testrun:
        print 'debug', int((time.time()-_start_time)*1000), \
                       int((clock_now-_last_clock)*1000), ':', msg
    _last_clock = clock_now
    _avtivate_logger()
    _logger.debug(msg)
#    _logger.debug(caller())

def err(msg):
    """Log an error message."""
    global _last_clock, locale_testrun
    clock_now = time.clock()
    if locale_testrun:
        print 'error', int((time.time()-_start_time)*1000), \
                       int((clock_now-_last_clock)*1000), ':', msg
        _last_clock = clock_now
    _avtivate_logger()
    _logger.error(msg)

_ref_list = []
def dot_start():
    global _ref_list
    _ref_list = []

def dot_id(obj):
    result = '"' + str(type(obj)) + ' ' + str(id(obj)) + '"'
    return result.replace("'>", '').replace("<class '", '').replace("<type '", '')

def dot_ref(anchor, ref):
    result = '\n' + anchor + dot_id(ref)
    for referenced in _ref_list:
        if id(ref) == referenced:
            result += ' [color=red ]'
            break
    _ref_list.append(id(ref))
    result += ' ;'
    try:
        result += '\n' + ref.dot()
    finally:
        return result
    
def print_call_stack():
#    try:
#        raise TypeError("")
#    except TypeError:
#        traceback.print_stack()
    pass

def contains_by_id(this_list, this_element):
    """Returns True if element in list. Comparison is done using id().
    Only for use in design by contact statements."""
    for elem in this_list:
        if id(elem) == id(this_element):
            return True
    return False

if _try_once:
    try:
        _try_once = False
        import contract, doctest
        def enable_contact(module_name):
            """Enables design by contact for a module."""
            if module_name not in DBC_BLACK_LIST:
                a_modul = __import__(module_name)
                contract.checkmod(module_name)
                doctest.testmod(a_modul)
                print module_name, 
            
        bundle_path = DEBUG_ACTIVITY_PATH
        if 'SUGAR_BUNDLE_PATH' in os.environ:
            from sugar.activity import activity
            bundle_path = activity.get_bundle_path()
        print 'enable_contact',
        for element in os.listdir(bundle_path):
            if element.endswith('.py') \
               and os.path.isfile(os.path.join(bundle_path, element)):
                name_parts = element.split('.')
                if len(name_parts) == 2:
                    enable_contact(name_parts[0])
        is_DbC_activated = True
    except ImportError:
        is_DbC_activated = False
        print "unsupported design by contract"