2012-04-04 3 views
1

にWindows上で作品をインポートではなく:などは、プログラムで次のようにだから私は、ディレクトリツリーを持っているLinuxの

pluginlist.py 
plugins/ 
    __init__.py 
    plugin1.py 
    plugin2.py 
    ... 

そしてplugin1のそれぞれから同じ名前の辞書を連結したい、plugin2で、

(pluginlist.pyから)次のように私はこれをやっている

方法は次のとおりです。

import os 

pluginFolderName = "plugins" 
pluginFlag = "##&plugin&##" 

commands = {} 

os.chdir(os.path.abspath(pluginFolderName)) 

for file in os.listdir(os.getcwd()): 
    if os.path.isfile(file) and os.path.splitext(file)[1] == ".py": 
     fileo = open(file, 'r') 
     firstline = fileo.readline() 
     if firstline == "##&plugin&##\n": 
      plugin_mod = __import__("plugins.%s" % os.path.splitext(file)[0]) 
      import_command = "plugin_commands = plugin_mod.%s" %  os.path.splitext(file)[0] 
      exec import_command 
      commands = dict(commands.items() + plugin_commands.commands.items()) 
print commands 

(印刷はテスト目的のためにそこにあるコマンド)

Windows上で実行すると適切なコマンド辞書が得られますが、それをLinux(Ubuntu Server)で実行すると空の辞書が得られます。

+1

'exec'ではなく' plugin_commands = getattr(plugin_mod、os.path.splitext(file)[0]) 'を使用できませんか? – Blender

答えて

0

私の問題を解明しました! Linuxがプラグインファイルへの絶対パスを必要としていたので、テストは機能しませんでした。だからファイルのすべてのインスタンスを置換すると

os.path.join(os.getcwd(), file) 

すべてを修正するようです。

2

試してください:あなたは__import__('A.B')を呼び出すと

for file in os.listdir(os.getcwd()): 
    basename, ext = os.path.splitext(file) 
    if os.path.isfile(file) and ext == ".py": 
     with open(file, 'r') as fileo: 
      firstline = fileo.readline() 
      if firstline.startswith("##&plugin&##"): 
       plugin_mod = __import__("plugins.%s" % basename, fromlist = [True]) 
       plugin_commands = getattr(plugin_mod, basename) 
       commands.update(plugin_commands.commands) 

、パッケージAが返されます。 __import__('A.B', fromlist = [True])に電話すると、モジュールBが返されます。あなたはBがほしいと思う。 WindowsとLinuxの両方で、fromlistを空でないリストに設定する必要があります。

+0

これは失敗した理由ではありませんでしたが、それでももっと慣用的にやっていましたので、ありがとう – glittershark

0

ソースコードにWindows CRLF行末がありますか? print repr(firstline)を追加して、\nの代わりに\r\nで終わらないことを確認してください。

0

私はプラグインはまた、識別子

が不足している場合に警告を表示しますif文でelse:支店を置く、あなたはおそらくチェックはあなたの問題を解決するときにfirstline.strip()を呼び出し、行末を気にしません問題

最後に、pedantically、あなただけの代わりにテストされていないos.chdir()

を使用してのpluginFolderNameパスにfileに参加することができます。

pluginFolderName = "plugins" 
pluginFlag = "##&plugin&##" 

pluginFolderName = os.path.abspath(pluginFolderName) 

commands = {} 
for file in os.listdir(pluginFolderName): 
    # Construct path to file (instead of relying on the cwd) 
    file = os.path.join(pluginFolderName, file) 

    if os.path.isfile(file) and os.path.splitext(file)[1] == ".py": 
     fileo = open(file, 'r') 
     firstline = fileo.readline() 

     if firstline.strip() == pluginFlag: 
      # Strip firstline to ignore trailing whitespace 

      plugin_mod = __import__("plugins.%s" % os.path.splitext(file)[0]) 
      plugin_cmds = getattr(plugin_mod, os.path.splitext(file)[0]) 
      commands.update(plugin_cmds) 
     else: 
      print "Warning: %s is missing identifier %r, first line was %r" % (
       file, PLUGIN_IDENTIFIER, firstline) 
関連する問題