Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Nick Sarbicki Newsgroups: comp.lang.python Subject: Basic plugin architecture Date: Wed, 06 Apr 2016 09:05:10 +0000 Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de URWXIaFWdKs1nCNy9TGIpArulSv2aa2C/4qhuHE7G4rg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'classes.': 0.07; 'dynamically': 0.07; 'works.': 0.07; 'dropdown': 0.09; 'imported': 0.09; 'modules.': 0.09; 'def': 0.13; 'django': 0.13; 'received:74.125.82.44': 0.15; 'importing': 0.15; 'user.': 0.15; '.py': 0.16; 'drag': 0.16; 'folder.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simple.': 0.16; 'later': 0.16; '>': 0.18; 'import': 0.24; 'module': 0.25; 'wonder': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'idea': 0.28; "skip:' 10": 0.28; 'issues.': 0.29; "i'm": 0.30; 'classes': 0.30; 'folder': 0.30; 'relative': 0.30; 'skip:g 30': 0.30; 'anyone': 0.32; 'point': 0.33; 'hopefully': 0.33; 'structure': 0.34; 'file': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'possible': 0.36; 'basic': 0.36; 'to:addr:python-list': 0.36; 'being': 0.37; 'wanted': 0.37; 'drop': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'caused': 0.61; 'providing': 0.62; '8bit%:50': 0.66; 'accessed': 0.66; 'here': 0.66; 'skip:\xe2 10': 0.70; '8bit%:40': 0.72; '8bit%:43': 0.72; 'as:': 0.79; 'ideas.': 0.84; 'plugins': 0.84; '\xe2\x94\x82': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=xQpntlgXeQNG3iENsV1X6r3Wcsq6KsynOGYw9C9P1g0=; b=V9IkNzpMmZwZRCDVnyMzriZmoJBMNqO6/eAHxyAKcojtb208J0Z0cfCiMYQdUD+Afs EMEUfl9P4k2n8hvSPxloi8eGbdeXhrr3uAzMalbgmxz3GPquXMXqD6qYI9HqMYPaseXv IMPxCjZDAESF/dQXal0ePeYkOap0FfxDE6N7JhlmQUG/atItzmhP7xkGQJqGlqvwmQ5d BHDM9xM6gRaTp0FXHFpxXVBKhNorgmGlVMQAy4dbgxaUmIIJ/rCqty05uGlXQ3spZ+2p IjUuBdGCht4VLettOKz1U9S1k9wNjg/UUZsNGY77527eGROrPBu1T6Cb4UbDegZoQ+N/ h9DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=xQpntlgXeQNG3iENsV1X6r3Wcsq6KsynOGYw9C9P1g0=; b=GFgi0mbmvg17b82LLT4Y0ptZeaeXBNxSwG23zj0X2bEfrZcKAVBFbM5ZgDOJt+1Vvc OrkpwoI4WUiwwV4/MF8DSwGNFtb72kA1+JqA6vQl2QYiugyBM/wz1JzXTuHDu14YCkNs 8589wDBB0Ots57bJDTPZo1IX1lv3mZnWrGKlSXtsTrMz4FThXremG8pT63P2YFDy25lX FqQvkSzbbrjdA2KAiyDD2NTyOInBSUONp9BZ/PKyx7RwBchK2md3tyBTcVKtLH5Px5BH Po8/pDFEiToaRvsCaOOX7fVwk2hHIf9nRcS6M84t7HefelWdVRfdZnwXK0Vlwe0PupbU NLTg== X-Gm-Message-State: AD7BkJI0faEGKpxHeXDQiAFl1i8iiBqx/iVYI+9Gkk+z9bFF9VIOJI1QQ/bdUrx4nLSAr9u70tKdPHhKq8ragQ== X-Received: by 10.194.89.70 with SMTP id bm6mr17426105wjb.0.1459933520393; Wed, 06 Apr 2016 02:05:20 -0700 (PDT) X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com comp.lang.python:106562 Hi, Question on how people here would design a basic plugin architecture: I'm playing with the idea of having a pluggable system where the users can create some simple classes which can then be accessed in a Django app. I want to make it as __simple__ as possible for the user. Hopefully to the point of being able to make a .py file and then drag and drop it into the plugins folder. The result should be a dropdown later on with the list of available classes. Providing the list of classes once imported should be simple. Actually importing the classes dynamically has caused a few issues. I have a solution but wanted to know if anyone had better ideas. Folder structure is: =E2=94=9Crun.py =E2=94=94loader/ =E2=94=9C=E2=94=80plugins/ =E2=94=82 =E2=94=9C=E2=94=80__init__.py =E2=94=82 =E2=94=9C=E2=94=80plugin1.py =E2=94=82 =E2=94=94=E2=94=80plugin2.py =E2=94=9C=E2=94=80__init__.py =E2=94=9C=E2=94=80models.py =E2=94=9C=E2=94=80views.py =E2=94=94=E2=94=80etc.py My current solution is: ########loader/plugins/__init__.py######## > import glob > import importlib > import os > > def is_valid_plugin(plugin): > return os.path.isfile(plugin) and not os.path.basename(plugin).startswith('_') > > package =3D 'loader.plugins' > > plugins_to_import =3D [plugin for plugin in glob.glob(os.path.dirname(__file__) + '/*.py') if is_valid_plugin(plugin)] > > plugins =3D [importlib.import_module('.' + os.path.basename(plugin)[:-3], package) for plugin in plugins_to_import] In relative terms I'm looking at the packages from the same position as run.py, so would import as: > from loader import plugins plugins.plugins would then be a list of imported plugin modules. With this I can use the __dict__ of each plugin module in plugins.plugins to find which classes we want to use. This works. But I wonder if there is a better way. Any suggestions? - Nick.