Exemplo n.º 1
0
    def GetPYFileList(dName, ext=".py"):
        """ Return .py files that are instanciable. 
		"""

        ### import are here because the simulator (PyDEVS or PyPDEVS) require it
        from DomainInterface.DomainBehavior import DomainBehavior

        try:
            name_list = getPYFileListFromInit(
                os.path.join(dName, '__init__.py'), ext)
            py_file_list = []

            for s in name_list:
                python_file = os.path.join(dName, s + ext)
                ### test if tmp is only composed by python file (case of the user write into the __init__.py file directory name is possible ! then we delete the directory names)
                if os.path.isfile(python_file):

                    cls = GetClass(python_file)

                    if cls is not None and not isinstance(cls, tuple):

                        ### only model that herite from DomainBehavior is shown in lib
                        if issubclass(cls, DomainBehavior):
                            py_file_list.append(s)
                        else:
                            sys.stderr.write(
                                _("%s not imported: Class is not DomainBehavior\n"
                                  % (s)))

                    ### If cls is tuple, there is an error but we load the model to correct it.
                    ### If its not DEVS model, the Dnd don't allows the instantiation and when the error is corrected, it don't appear before a update.
                    else:
                        py_file_list.append(s)

        except Exception as info:
            py_file_list = []
            # if dName contains a python file, __init__.py is forced
            for f in os.listdir(dName):
                if f.endswith(ext):
                    sys.stderr.write(
                        _("Exception, %s not imported: %s \n" % (dName, info)))
                    break

        return py_file_list
Exemplo n.º 2
0
    def GetModelList(self, dName):
        """ Get the list of files from dName directory.
		"""

        ### import are here because the simulator (PyDEVS or PyPDEVS) require it
        from DomainInterface.DomainBehavior import DomainBehavior

        ### list of py file from __init__.py
        if LibraryTree.EXT_LIB_PYTHON_FLAG:

            ### list of py file from url
            if dName.startswith('http'):

                o = urlparse(dName)
                c = httplib.HTTPConnection(o.netloc)
                c.request('GET', o.path + '/__init__.py')

                r = c.getresponse()
                code = r.read()

                if r.status == 200:
                    exec code
                    tmp = filter(
                        lambda s: s.replace('\n', '').replace('\t', '').
                        replace(',', '').replace('"', "").replace(
                            '\'', "").strip(), __all__)
                    ### test if the content of __init__.py file is python file (isfile equivalent)
                    py_file_list = [
                        s for s in tmp
                        if 'python' in urlopen(dName + '/' + s +
                                               '.py').info().type
                    ]

                else:
                    py_file_list = []

                return py_file_list
            else:
                try:
                    name_list = getFileListFromInit(
                        os.path.join(dName, '__init__.py'))
                    py_file_list = []

                    for s in name_list:
                        python_file = os.path.join(dName, s + '.py')
                        ### test if tmp is only composed by python file (case of the user write into the __init__.py file directory name is possible ! then we delete the directory names)
                        if os.path.isfile(python_file):

                            cls = GetClass(python_file)

                            if cls is not None and not isinstance(cls, tuple):

                                ### only model that herite from DomainBehavior is shown in lib
                                if issubclass(cls, DomainBehavior):
                                    py_file_list.append(s)
                                else:
                                    sys.stderr.write(
                                        _("%s not imported: Class is not DomainBehavior \n"
                                          % (s)))

                            ### If cls is tuple, there is an error but we load the model to correct it.
                            ### If its not DEVS model, the Dnd don't allows the instantiation and when the error is corrected, it don't appear before a update.
                            else:

                                py_file_list.append(s)

                except Exception, info:
                    py_file_list = []
                    # if dName contains a python file, __init__.py is forced
                    for f in os.listdir(dName):
                        if f.endswith('.py'):
                            sys.stderr.write(
                                _("%s not imported: %s \n" % (dName, info)))
                            break