Thursday, December 20, 2012

How to use Shovel with Django

Here is my attempt to use Shovel (Rake, for Python) in Django.

Shovel can be used as an alternative to Django built-in Command. The reasons to use Shovel instead of the Command are:

1. "Rake, for Python" sounds cool.
2. Shovel is light-weight.
3. I can find all tasks in one place (the shovel/ directory).
4. I hate writing a list of make_option.
5. I hate inheriting LabelCommand, BaseCommand, NoArgsCommand, etc.

Okay, you get it... The reasons to use Shovel are actually: (i) "Rake, for Python" sounds cool, and (ii) I am lazy.

I didn't use the term "replacement for Django Command" because we cannot simply invoke Shovel task in the Django codebase using django.core.management.call_command(). (But we can have work-around, like making a Command to proxy the call)

Let's get back to my attempt to mix the two. I placed the shovel/ folder inside the Django project.

djangoproject/
  - manage.py
  - settings.py
  - urls.py
  - shovel/ 

And I setup the Django environment at the top of the Shovel task file.
try: # Load the django environment
    import imp
    import os
    import sys

    me = os.path.abspath(os.path.dirname(__file__))
    module_info = imp.find_module('context', [me])
    imp.load_module('context', *module_info)
except:
    print >> sys.stderr, 'Cannot setup Python environment from context.py'
At last, the setup injects a directory to Python sys.path and use Django function to load the settings.py.
def setup_django_env(path):
    from django.core.management import setup_environ
    try:
        module_info = imp.find_module('settings', [path])
        settings = imp.load_module('settings', *module_info)

        setup_environ(settings)
    except ImportError:
        msg = "Error: Can't find 'settings.py' in configured PYTHON_PATH", path
        print >> sys.stderr, msg
        sys.exit(1)

# assume shovel/ directory is placed at the same level of settings.py
setup_django_env(os.getcwd())
This is a common technique to integrate Django with other Python gears as well. By applying this technique, I can freely use any Django functions / models in Shovel.

NOTE 1: The name of Shovel task file cannot collide with any module/app name of the Django project.

NOTE 2: We can place the shovel/ folder outside the Django project but we have to use proper Python path and import statement (e.g. use `from themix.things.models import Thing` instead of `from things.models import Thing`).

My attempt can be found at - https://github.com/mrkschan/shovel-django-mix.

Friday, January 20, 2012

git-fix-whitespace series 1: Knowing about `git diff -p`

In the last post, the first requirement of the project (https://github.com/mrkschan/git-fix-whitespace) has been settled. The next requirement of this project is to read the git-diff patch in order to find any line changes that violate the whitespace rules specified in git config.

In a typical git-diff patch (see below), there are a few major parts.
  1. Line 1 provides metadata about the modified file. It gives the file path to the file, rooted at the git repository.
  2. Line 2 provides metadata about the git index and the file object discretionary access control list.
  3. Line 3 and 4 provides metadata about which file path is old and which is new.
  4. Line 5, 14, and 24 are metadata that tells which part of the file is modified. Let's take an example to explain this - "@@ -33,8 +33,7 @@ def sanitize_diff(git_diff):". "-33,8" tells that there is a diff hunk with 8 lines starts at line 33 of the old version of the file. "+33,7" tells that there is a diff hunk with 7 lines starts at line 33 of the new version of the file. As a result, the new version of the file gets one line fewer than the old version.
  5. The rest of the patch is the content in the modified file. Those lines are either prefixed by ' ', '-', or '+'. ' ' means no modification, '-' means removed line, and '+' means added line. Note, there is no line replacement since it is represented by '-' lines followed by '+' lines (see line 28-31).
After knowing the structure of a git-diff patch, the next step is to read and write the modified file.

Sunday, January 15, 2012

git-fix-whitespace series 0: GitPython vs libgit2

This is the first post of the git-fix-whitespace series. In this series, I will put some notes about working on the project - https://github.com/mrkschan/git-fix-whitespace. (NOTE: This series is a by-product of the git-fix-whitespace project, since my blog need some updates :P)

First of all, let me introduce the rationale of working on this git-fix-whitespace project. Most of the time, as a Python developer, I hate tab indentation and trailing whitespaces (read pep8). I know there are existing tools to "proof-read" a file using certain whitespacing rules. However, I insist to create my own tool to achieve the goal. Reason: I just wanna have a pet project that I LOVE to keep working on.

The very first requirement of this project is to support the configuration directives of git (see `man git-config` and look for `core.whitespace`). Hence, I need to find a tool that can read the git configuration files (both the user level git config file ~/.gitconfig and repository level git config file /.git/config). By using Google, I got GitPython and libgit2/pygit2. At first, I try to read the libgit2 python binding to see if I can read whitespace configuration by simple api calls. But it seems that it did not support that yet (as of 2012-Jan-15). Then I move on to GitPython. Gotcha! There's simple api call to read the core.whitespace configurations :) As a result, git-fix-whitespace got a dependency on GitPython at the moment.

Saturday, November 05, 2011

話說的好滑腸粉

聽阿媽話說, 97年果時有位茶樓點心大廚, 因為人工高被人炒魷, 結果到了沙田博康村的大排檔拉腸粉. 今日, 本來是要跟阿媽行山去川龍飲茶的日子, 但因為唔多想坐車入荃灣咁遠, 結果是從獅子山行入沙田... 朝早7:30出門口, 9:30到了博康村. 剛剛好趕得上腸粉時段 (NOTE: 果間野 d 即拉腸粉, 只限每天早上十時前供應).
這即拉腸粉的滑口程度, 絕對比米豬蓮添女子運的腸粉好得多... And, 添女子運沒有花生醬及甜醬供應....

Thursday, May 12, 2011

Stats for Firefox 4 Release Party on May 14

As you may know, we're going to have a Firefox 4 Release Party this Saturday in Hong Kong!! (see http://opensource.hk/node/666 :)

I would like to share with you some interesting stats we collected from the registration :)

At first, let's see who's coming :)



Most of us are user of Firefox :) You can find contributor as well :)

Then, which OS we use most???



Feeling sorry to "vista" ... what is it -.-? Anyway, what are we going to do this Saturday ?? hehe... see below :)

Monday, February 14, 2011

happy valentine's






http://mrkschan.blogspot.com/2011/02/prep-for-monday.html

Saturday, February 12, 2011

屎忽痕, 行山飲"早"茶

昨晚, 阿爸忽然話今朝行山去什麼 "川龍" 飲早茶.. 小弟見屎忽痕, 又 assume 了要行山去吃的 實會係正野 ... 很自然的話要跟住去.

今晨七時出門口 .. 坐mtr到荃灣, 由荃灣地鐵站行上圓玄學院. (那時大約是8點了)

由圓玄學院出發, 經山路到川龍大約為 3Km. 不要以為很輕鬆... 從圓玄學院走上大帽山的樓梯, 約一公里長 -.- 跟阿媽慢慢走了40分鐘, 才到那什麼觀景台.



之後再走一會輕鬆的山路, 就差不多到川龍了.






上到茶樓, 見到好耐無見的雀籠.. 對上一次與雀共茶, 應該係細路仔果時, 同屋企人去藍田的德田村冬菇亭了. 自從有禽流感之後, 都好耐無試過聽雀仔聲飲茶.

在茶樓叫的東西不是很多, 其中的芋角同客家豬肉最為正 :P 那個芋角入面, 有d馬蹄, 很爽口. 而那個豬肉, 不是很肥. 豬肉像三文治一樣, 中間夾了一片芋頭.






三個人.. 70$ 落樓.

小記:


坐車落荃灣果陣, 經過了曹公潭戶外康樂中心. 我不記得是小學還是中學去過呢到. 最記得係.. 有一晚在裡面不知道干嘛通宵後.. 到了那 canteen 吃早餐... 那個早餐是我一生人入面 (暫時來說) ... 最難食的一餐 -.-"
 
© 2009 Emptiness Blogging. All Rights Reserved | Powered by Blogger
Design by psdvibe | Bloggerized By LawnyDesignz