Showing posts with label macro. Show all posts
Showing posts with label macro. Show all posts

Wednesday, October 22, 2008

Пример создания trac macros

Cтавим trac (на примере debian)

sudo apt-get install trac


Подготавливаем папку для тестов

cd@cd-laptop: cd
cd@cd-laptop: mkdir tests
cd@cd-laptop: cd tests
cd@cd-laptop: mkdir tracdir
cd@cd-laptop: trac-admin tracdir initenv
жмем энтер :)
Запускаем через stand-along
cd@cd-laptop: tracd /home/cd/tests/tracdir --port 8888
cd@cd-laptop: firefox http://127.0.0.1:8888/tracdir



Трак установлен, для тестирования нам такого хватит.

останавливаем

cd@cd-laptop: Ctrl+C
cd@cd-laptop:


Создаем macro Hello

cd plugins
touch Hello.py



Содержимое Hello.py

from trac.core import *
from trac.wiki.macros import WikiMacroBase
from trac.util import escape

__all__ = ['HelloMacro']

class HelloMacro(WikiMacroBase):
def expand_macro(self,formatter, name, args):
return "Hello %s" % args or '

Использование. Внутри тикета или wiki напишите


[[Hello(cd)]]

Результат

Hello cd

Т.е. для написания минимального макроса, достаточно отнаследоваться от WikiMacroBase, реализовать один метод, возращающий html.

Пример ставшего для меня полезным макроса.

1 from trac.core import *
2 from trac.wiki.macros import WikiMacroBase
3 from trac.util import escape
4 from trac.util.html import escape,html,plaintext
5 from genshi.builder import tag
6
7
8 __all__ = ['XxxMacro']
9
10
11 class RwebtableMacro(WikiMacroBase):
12 """
13 Show table schema
14 {{{
15 [[Xxx(tablename)]]
16 }}}
17
18 """
19 def expand_macro(self, formatter, name, args):
20 # args will be `None` if the macro is called without parenthesis.
21 table = args or ''
22 if not table :
23 return 'no table specified'
24
25 import MySQLdb
26 user = self.env.config.get('cddb', 'user')#требует опции в trac.ini секции [cddb]
27 host = self.env.config.get('cddb', 'host')
28 passwd = self.env.config.get('cddb', 'passwd')
29 dbname = self.env.config.get('cddb', 'dbname')
30 db = MySQLdb.connect(user=user, host=host, passwd=passwd, db=dbname)
31 cursor = db.cursor()
32 sql = "DESCRIBE %s" % table
33 cursor.execute(sql)
34 table = tag.table(border=1)
35 table.append(tag.tr( tag.th('Field'), tag.th('Type') , tag.th('Null') , tag.th('Key') , tag.th('Default'), tag.th('Extra') ) )
36 res = cursor.fetchall()
37 for row in res :
38 tr = tag.tr()
39 for i in row :
40 tr.append(tag.td(str(i)) )
41 table.append(tr)
42 return str(table)




Например
[[Xxxtable(country)]]

FieldType Null Key Default Extra
country_idint(11)NOPRINoneauto_increment
isovarchar(3)YESNone
namevarchar(50)YESNone
 
Каталог сайтов, Добавить сайт