글쓴이 보관물: litcoder

Jinja2의 UnicodeDecodeError

App engine을 이용하는 project 중에 Jinja2 template 부분에서 다음과 같이 UnicodeDecodeError가 발생하는 문제가 생겼다.

Traceback (most recent call last):
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)J
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "<<APP_ENGINE>>/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "<<PROJECT_TOP>>/main.py", line 141, in get
    r = t.render(events)
  File "<<APP_ENGINE>>/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "<<PROJECT_TOP>>/templates/parser.html", line 21, in top-level template code
    <td>{{titles[loop.index0]}}</td>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 9: ordinal not in range(128)

Jinja2 web page의 설명문서에 따르면, Jinja2는 내부적으로 Unicode를 사용하기 때문에 template에는 unicode object를 넘겨주어야 한다고 설명하고 있는데, 내가 project에서 사용하는 web page는  cp949 encoding을 사용하기 때문에 문제가 발생한다.

따라서 이것은 Jinja2 template으로 data를 넘기기 전에 unicode()를 이용해서 cpc949를 uinicode로 변환한 값을 넘겨주면 해결할 수 있다. 다음은 실제로 수정된 code로 cp949로 encoding된 문자열을  unicode로 변환해서 추가하는 것이다.

titles.append(sch.mTitle) -> titles.append(unicode(sch.mTitle, 'cp949'))

[Tip] Ubuntu 12.04 Eclipse tooltip 색상변경

Ubuntu(12.04)에서 eclipse를 설치하고 사용하다보면 tooltip이 어두운 배경색에 검은색 글씨로 되어 있어서 잘 안 보인다. 화면 밝기를 밝게하면 불가능 한 건 아니지만, class member를 쉽게 보여주기는 context menu를 실눈 뜨고 한 참 동안 쳐다보는게 여간 불편한게 아니다. 분명 internet 어딘가에는 나와 같은 불편을 겪는 사람이 있을텐데라고 생각하면서도 어떤 keyword로 검색해야 하는지 몰라서 여러 번 실패하고 나서야 드디어 찾았다.

How to change tooltip background color in Unity?

최근에 추가된 몇 개의 solution들은 시험해 보지는 못했으나, 아래의 script는 효과가 있었다.

#/bin/sh
# Tooltip fix
# A script to fix themes files in Ubuntu 11.10
#  to have readable tooltips in applications such
#  as eclipse.
# The script edits the gtk.css, settings.ini and gtkrc files
# Author: Victor Pillac
# http://victorpillac.wordpress.com

#if [ $EUID -ne 0 ]; then
#  echo "This script must be run as root" 1>&2
#  exit 1
#fi  

path=/usr/share/themes
theme=Ambiance

if [ $# = 1 ]; then
  theme=$1
fi

echo "Fixing tooltips for theme $theme"
echo " (you can select a different theme by passing its name as argument)"
sed -i 's/tooltip_bg_color #000000/tooltip_bg_color #f5f5b5/g' $path/$theme/gtk-3.0/gtk.css
sed -i 's/tooltip_fg_color #ffffff/tooltip_fg_color #000000/g' $path/$theme/gtk-3.0/gtk.css
sed -i 's/tooltip_bg_color:#000000/tooltip_bg_color:#f5f5b5/g' $path/$theme/gtk-3.0/settings.ini
sed -i 's/tooltip_fg_color:#ffffff/tooltip_fg_color:#000000/g' $path/$theme/gtk-3.0/settings.ini
sed -i 's/tooltip_bg_color:#000000/tooltip_bg_color:#f5f5b5/g' $path/$theme/gtk-2.0/gtkrc
sed -i 's/tooltip_fg_color:#ffffff/tooltip_fg_color:#000000/g' $path/$theme/gtk-2.0/gtkrc
echo "Done"

PlantUML war file 설치

그동안 UML을 그려야할 필요가 있을때 StarUML을 써왔는데, Windows만 지원하는 software라는 한계도 있고 해서 얼마 전 부터는 우연히 알게된 PlantUML을 사용하고 있다. 이 software는 마치 LaTex 처럼 text로 UML을 그리기 위한 description을 작성하면 이를 기반해서 UML을 그려준다. 문법도 그다지 어렵지 않고 직관적이어서 사용하기도 쉬운 편이다.

작성한 text를 UML로 그리기 위한 client program들은 여러가지 형태가 지원되는데 보다 자세한 목록은 여기에서  확인할 수 있다. 내가 그동안 사용하던 것은 Chrome extension이었는데 class diagram들이 조금씩 복잡해 지면서  여러개의 file로 쪼갤 필요가 있게 되니 ‘!include‘ directive를 사용할 수 없는 문제가 있었다. 아마도 Chrome이 보안 문제로 extension의 local file 간접 access하는 것을 막은게 아닐까 싶다. 그래서 servlet으로 설치하는 방법을 사용하기로 했다. 이전에 Opengrok을 설치한 적이 있으니 설치 환경은 ubuntu 12.04와 tomcat이 이미 설치된 상태 이다.

  • 필요한 package들의 설치
    : PlantUML servlet을 돌리려면 graphviz package가 필요한데 이것이 설치되어 있지 않으면 다음과 같은 오류가 발생한다. 그리고 war file을 쉽게 deploy하기 위해 tomcat-admin package를 설치했다.
    SC_plantuml_dot_file_not_exists_err
sudo apt-get install tomcat6-admin graphviz
  • PlantUML war file을 download 받기
    PlantUML server release page에서 war file을 download 받는다.
  • Tomcat admin 설정
    : Tomcat admin으로 접속하려면 amdin 계정이 필요하니 만약 계정이 없다면 tomcat-users.xml file을 편집해서 계정을 추가해 준다. 이 파일에는 비밀번호를 평문으로 함께 기록해야 하기는 하지만, root와 tomcat외에는 read permission이 없으므로 다른 계정에 의해 이 파일이 읽히지는 않는다. 계정을 추가해준 이후에는 tomcat server를 재실행한다.

    sudo vi /var/lib/tomcat6/conf/tomcat-users.xml
 <!--
 NOTE: By default, no user is included in the "manager-gui" role required
 to operate the "/manager/html" web application. If you wish to use this a pp,
 you must define such a user - the username and password are arbitrary.
 -->
 <!--
 NOTE: The sample user and role entries below are wrapped in a comment
 and thus are ignored when reading this file. Do not forget to remove
 <!.. ..> that surrounds them.
 -->
 <role rolename="admin"/>
 <user username="admin" password="ADMIN_PASSWORD" roles="admin,manager,manager-gui"/>
 </tomcat-users>
  • Tomcat admin page로 부터 deploy: 모든 준비가 되었으니 tomcat admin page (http://SERVER:8080/manager/html) 에서 war file을 선택하고 deploy한다.
  • Service page 접속
    : Web browser에서 service page (http://SERVER:8080/plantuml)로 접속한다.