Factory method in python
<p>I'm working to the following little project:
<a href="https://github.com/AndreaCrotti/project-organizer" rel="nofollow">https://github.com/AndreaCrotti/project-organizer</a></p>

<p>Which in short aims to manage many different projects much more easily.
One of the useful things is a way to detect automatically the kind of project that I'm working on, to set up correctly some commands.</p>

<p>At the moment I'm using a classmethod "match" function and a detect function which  iterates over the various "match".
I'm sure there might be a better design for this, but can't find it.</p>

<p>Any ideas?</p>

<pre><code>class ProjectType(object):
    build_cmd = ""

    @classmethod
    def match(cls, _):
        return True


class PythonProject(ProjectType):
    build_cmd = "python setup.py develop --user"

    @classmethod
    def match(cls, base):
        return path.isfile(path.join(base, 'setup.py'))


class AutoconfProject(ProjectType):
    #TODO: there should be also a way to configure it
    build_cmd = "./configure &amp;&amp; make -j3"

    @classmethod
    def match(cls, base):
        markers = ('configure.in', 'configure.ac', 'makefile.am')
        return any(path.isfile(path.join(base, x)) for x in markers)


class MakefileOnly(ProjectType):
    build_cmd = "make"

    @classmethod
    def match(cls, base):
        # if we can count on the order the first check is not useful
        return (not AutoconfProject.match(base)) and \
            (path.isfile(path.join(base, 'Makefile')))


def detect_project_type(path):
    prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)
    for p in prj_types:
        if p.match(path):
            return p()
</code></pre>
