2015/01/14

sdeditでシーケンス図をかきかきして、Sphinxドキュメントに貼り付ける

Sphinxでインラインでシーケンス図を書くには、sphinxcontrib-seqdiagのほかに、sphinxcontrib-sdeditという選択肢もあります。

seqdiagの直感性は最強なのですが、ちょっといろいろ省略が入った図とか、条件文が入った図とかをつくろうとした時につまづいてしまうことが多いので、とりあえずsdeditのほうも試してみました。

(ちなみに、sdeditはすごく書きにくい上に、絵がダサいです。なので、ほんとに凝った図を作りたいとき以外はおすすめしません)


LinuxやMacだとそんなにハマることはない気がしますが、
Windows上のPython3でだと、いろいろとハマりどころがあります。

ハマりどころ1
 pip install しようとすると、PILのインストールが出来ずにコケる
Downloading/unpacking PIL
  Could not find any downloads that satisfy the requirement PIL
  Some insecure and unverifiable files were ignored (use --allow-unverified PIL to allow).
Cleaning up...


→ https://github.com/liori/sphinxcontrib-sdedit.git に、Pillowを使うよううまく改造した(らしい)sphinxcontrib-sdeditがあります。
pip install git+https://github.com/liori/sphinxcontrib-sdedit.git
とすれば、この特殊なsphinxcontrib-sdeditをインストールすることができます。

ハマりどころ2
 Unicode系のエラー。bytesとstrは足し算できんぞ!てきな。
@@ -70,10 +70,10 @@
     """
     Render sequence diagram into a PNG or PDF output file.
     """
-    hashkey = code.encode('utf-8') + str(options) + \
+    hashkey = code + str(options) + \
               str(self.builder.config.sdedit_args)
-    ofname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), format)
-    ifname = '%s-%s.sd' % (prefix, sha(hashkey).hexdigest())
+    ofname = '%s-%s.%s' % (prefix, sha(hashkey.encode("utf-8")).hexdigest(), format)
+    ifname = '%s-%s.sd' % (prefix, sha(hashkey.encode("utf-8")).hexdigest())
     infn = os.path.join(self.builder.outdir, ifname)
     if hasattr(self.builder, 'imgpath'):
         # HTML
@@ -92,8 +92,6 @@
     ensuredir(os.path.dirname(outfn))
     ensuredir(os.path.dirname(infn))
-    inputfile = open(infn, "w")
-    if isinstance(code, unicode):
-        code = code.encode('utf-8')
+    inputfile = open(infn, "w", encoding="utf-8")
     inputfile.write(code)
     inputfile.close()

こんなかんじでPython3対応させます


ハマりどころ3
 except OSError, err の構文エラー

sdedit.pyの例外構文をPython3対応すればよいです。
except XXError, err → except XXError as err


ハマりどころ4
 PILで「TypeError: integer argument expected, got float」

これはPILが悪いわけではなくて、使う側(sdedit.py)が悪い。
Python2では 3/2 = 1,  3.0/2 = 1.5
 のようになっていたのが
Python3では 3//2=1,  3/2 = 1.5
 のように'/'の意味が変わっているので、/を//に修正します。
@@ -161,7 +161,7 @@
 
 def html_tag_with_thumbnail(self, code, fname, new_width, image, imgcls):
     width, height = image.size
-    new_height = height * new_width / width 
+    new_height = height * new_width // width 
     image.resize((new_width, new_height), Image.ANTIALIAS)
     image.filter(ImageFilter.DETAIL)
     dir, file = os.path.split(fname)

これで、グラフが出力されるようになると思います。

sdedit.pyの修正結果はアップしておきます→https://gist.github.com/yi01/9d5a0480a50271c3794f