LoginSignup
8
8

More than 5 years have passed since last update.

Pythonでpitを使う時の注意点

Posted at

pitを使ってみてプチハマり

ソースにアカウントIDやパスワードを書かず、実行時に入力を求められるpit。
セキュリティ上とても優れたライブラリなのですが、Pythonでの使用時にプチハマりしたのでメモしておきます。

試したpytonとpitのバージョンはこちら。

$ python -V
Python 2.7.9

$ pip freeze | grep pit
pit==0.3

実行時にエディタが起動しない

最低限の記述はこちら。

import pit

opts = {
    'require': {
        'id': 'your id',
        'password': 'your password',
    }
}
account = pit.Pit.get('test.account', opts)

print account['id']
print account['password']

でも、実際に実行してみるとエラー。
なぜか初回実行時にエディタが起動しません。
id、password共入力していないので当然、値取得時にKeyErrorになります。

>>> import pit
>>>
>>> opts = {
...     'require': {
...         'id': 'your id',
...         'password': 'your password',
...     }
... }
>>> account = pit.Pit.get('test.account', opts)
>>> print account['id']

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'id'

デフォルトの状態ではエディタが指定されていないので、viを指定する必要がありました。

if not os.environ.get('EDITOR'):
    os.environ['EDITOR'] = 'vi'

修正して実行。
viが起動され、入力可能になって正常終了しました。

>>> import pit
>>> import os
>>>
>>> if not os.environ.get('EDITOR'):
...     os.environ['EDITOR'] = 'vi'
...
>>> opts = {
...     'require': {
...         'id': 'your id',
...         'password': 'your password',
...     }
... }
>>> account = pit.Pit.get('test.account', opts)
>>> print account['id']
hoge
>>> print account['password']
hogehoge

エディタに謎の文字(!!python/unicode)が表示される

これを実行すると・・・

from __future__ import absolute_import
from __future__ import unicode_literals

import pit
import os

if not os.environ.get('EDITOR'):
    os.environ['EDITOR'] = 'vi'

opts = {
    'require': {
        'id': 'your id',
        'password': 'your password',
    }
}
account = pit.Pit.get('test.account', opts)

print account['id']
print account['password']
!!python/unicode 'id': !!python/unicode 'your id'
!!python/unicode 'password': !!python/unicode 'your password'

viでのid、password入力時に謎の文字が付きます。
結論から言うと、この行があることが問題でしたので、消すことにより解消します。

from __future__ import unicode_literals

unicode文字を引数で渡してしまうと「!!python/unicode」という文字が表示されてしまいます。
なので、unicode_literalsを指定していないとしても、こう記述しても同じ現象になります。

opts = {
    u'require': {
        u'id': u'your id',
        u'password': u'your password',
    }
}
account = pit.Pit.get('test.account', opts)
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8