robot
最新文章(10)
Mqskit 和其它相關工具
CPython 的 GC 二、三事
寫 Mecurial Extension 是件快樂的事!
Mozilla 台灣辨公室徵人啟事
關於 Apple 的兩項專利
core dump 之前的 frame
怎麼發出 beep 聲?
先承認你要找的是奴才吧!
程式碼要清的多乾淨?
FreeBSD 的 Thread-Local Storage 實作
首頁
新編
最新留言
Entries RSS
重要關鍵字(10)
coding (122)
Python (93)
FreeBSD (71)
WEB (61)
URL (48)
hardware (46)
javascript (36)
Linux (34)
blog (30)
C++ (16)
所有關鍵字
新增 URL
寫 Mecurial Extension 是件快樂的事!
by thinker
2 Columns
關鍵字:
Python
coding
為 Mecurial 寫 extension 的快樂,是冬獼入溫泉,夏童跳冷澗,炎午熏風吹, 雪後暖陽會一般的舒暢。為什麼? 是 $Python$ 的淋漓,智慧烘照之後發出的芬芳, 產生了體內芬多精,至於腦內瑪琲產生也! 雖然是很唬爛,但不中也不遠矣! hg extension 兮 $Python$ module 兮,毋遠矣! 納 cmdtable 變數,是 hg subcommand 的列表也。如 hg commit 、 hg pull 皆是。hg extension 設 cmdtable,使 hg 知 subcommand。有例為證 {{{#!python cmdtable = { 'bzexport': (bzexport, [('d', 'description', '', 'Bugzilla attachment description'), ('c', 'comment', '', 'Comment to add with the attachment'), ('e', 'edit-comment', None, 'Open a text editor to specify a comment to add with the attachment.'), ('r', 'review', '', 'List of users to request review from (comma-separated search strings)')], _('hg bzexport [options] [REV] [BUG]')), 'bzbug': (bzbug, [], _('hg bzbug [BUG]')), 'bzmap': (bzmap, [('r', 'remove', None, 'remove a patch from mapping'), ('n', 'new', None, 'add a new patch to mapping'), ('', 'reviewers', '', 'set reviewers for this patch (comma separated)', _('EMAILS')), ('m', 'message', '', 'description of the attachment', _('TEXT'))], _('hg bzmap [[ATTACH] LOCAL]')), 'bzpush': (bzpush, [], _('hg bzpush')), } }}} cmdtable 概 dictionary 也。subcommand 容器也。key 為名,value 為 3-tuple,納 command function、definition of options 與 usage description 也。'bzbug' 為例, bzbug 概 command function 也。 bzbug 為例 {{{#!python def bzbug(ui, repo, *args, **opts): '''set working directory for a bug. ''' if len(args) > 1: ui.write_err('Error: need exactly one argument!\n') return api_server = ui.config("bzexport", "api_server", "https://api-dev.bugzilla.$mozilla$.org/latest/") init_bzdata(ui, repo) bzdata = load_bzdata(ui, repo) if len(args) == 1: bzdata['bug'] = int(args[0]) bzupdate(ui, repo, bzdata) save_bzdata(ui, repo, bzdata) pass ui.write('Bug ID %d\n' % bzdata['bug']) pass }}} ui 者, message I/O API,設定值讀取/設定 API 也。 {{{#!python ui.config("bzexport", "api_server", "...default value...") }}} 讀取 api_server section 之 api_server 變數,概 .hgrc 所納者。 repo 者, repository 也。版本管系統的 $database$。 供 repository 的存取介面和 API 也。 repo.join() 取 .hg/ 目錄下之檔案也。例 {{{#!python repo.join('foo.txt') }}} 得 .hg/foo.txt 之完整路徑也。概,用者毋知 repository 路徑,亦可得。 repo.wjoin() 得 working directory 路徑也。 repo 提供 repository 之操作也,如 repo.commit() 、repo.push() 之類。 功能數如繁星,不及備載。 使用者,可在 hgrc 裡掛 linkname:[hooks] http://mercurial.selenic.com/wiki/Hook ,extension 亦可。如 {{{#!python def uisetup(ui): ui.setconfig('hooks', 'post-qrename', bzpost_qrename_hook) ui.setconfig('hooks', 'post-qdelete', bzpost_qdelete_hook) pass }}} uisetup() 在 extension 載入自動呼叫。 此例,bzpost_qrename_hook 與 bzpost_qdelete_hook,於 qrename 和 qdelete 後執行。 {{{#!python def bzpost_qrename_hook(ui, repo, hooktype, **args): if args['result']: return old_name, new_name = tuple(args['pats']) bzrename_patch(ui, repo, old_name, new_name) pass }}} 使用 hook,先於指定 command 或其後,執行特定程式。 == 結語 == 善哉! 上智者,好少之數、簡之形。 hg 之形簡,修改之工少,皆為上智之智也。
最後更新時間: 2011-11-29 00:53:30 CST |
引用
查詢:
COMMENTS: