# This file is part of Moksha.
# Copyright (C) 2008-2009  Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
:mod:`moksha.widgets.source` -- Source Code Widget
==================================================

The SourceCodeWidget takes a Widget instance, and
returns the source code, in HTML.

.. moduleauthor:: Luke Macken <lmacken@redhat.com>
"""

import moksha
import inspect

from tw.api import Widget
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

class SourceCodeWidget(Widget):
    params = {
        'widget': 'The name of the widget',
        'module': 'Whether to display the entire module',
        'code': 'The actual source code',
    }
    template = "${code}"
    engine_name = 'mako'
    container_options = {'width': 600, 'height': 500, 'title': 'View Source',
                         'icon': 'comment.png', 'top': 80, 'left': 250}
    hidden = True
    module = False

    def update_params(self, d):
        super(SourceCodeWidget, self).update_params(d)
        d.widget = moksha.get_widget(d.widget)
        title = d.widget.__class__.__name__
        if d.module:
            obj = __import__(d.widget.__module__, globals(), locals(),
                             [d.widget.__module__])
        else:
            obj = d.widget.__class__
        source = inspect.getsource(obj)
        d.code = highlight(source, PythonLexer(),
                           HtmlFormatter(full=True))

code_widget = SourceCodeWidget('code_widget')

@@ -722,17 +722,16 @@
 nses/%3E.%0A
-%0A
 %22%22%22%0A:mod
@@ -1026,16 +1026,60 @@
  Widget%0A
+from twisted.python.reflect import namedAny%0A
 from pyg
@@ -1333,24 +1333,66 @@
 re module',%0A
+        'source': 'Optional source code',%0A
         'cod
@@ -1407,16 +1407,25 @@
  actual 
+rendered 
 source c
@@ -1426,24 +1426,80 @@
 urce code',%0A
+        'title': ' An optional title for the document',%0A
     %7D%0A    te
@@ -1577,18 +1577,18 @@
 idth': 6
-00
+25
 , 'heigh
@@ -1595,11 +1595,11 @@
 t': 
-500
+675
 , 't
@@ -1675,17 +1675,17 @@
  'top': 
-8
+5
 0, 'left
@@ -1687,18 +1687,18 @@
 'left': 
-25
+30
 0%7D%0A    h
@@ -1728,16 +1728,33 @@
  = False
+%0A    title = None
 %0A%0A    de
@@ -1838,55 +1838,8 @@
 (d)%0A
-        d.widget = moksha.get_widget(d.widget)%0A
     
@@ -1893,15 +1893,19 @@
  if 
-d.modul
+not d.sourc
 e:%0A 
@@ -1919,80 +1919,172 @@
     
-obj = __import__(d.widget.__module__, globals(), locals(),%0A             
+try:%0A                d.widget = moksha.get_widget(d.widget)%0A            except Exception, e:%0A                d.widget = namedAny(d.widget)%0A            if d.module:%0A
     
@@ -2099,9 +2099,23 @@
     
-%5B
+obj = namedAny(
 d.wi
@@ -2133,11 +2133,14 @@
 le__
-%5D
 )%0A
+    
     
@@ -2157,24 +2157,28 @@
             
+    
 obj = d.widg
@@ -2198,16 +2198,22 @@
         
+  
+  d.
 source =
@@ -2236,16 +2236,112 @@
 ce(obj)%0A
+        html_args = %7B'full': True%7D%0A        if d.title:%0A            html_args%5B'title'%5D = d.title%0A
         
@@ -2359,16 +2359,18 @@
 ghlight(
+d.
 source, 
@@ -2387,35 +2387,8 @@
 r(),
-%0A                          
  Htm
@@ -2402,17 +2402,19 @@
 ter(
-full=True
+**html_args
 ))%0A%0A

Improve our SourceCodeWidget