def initialize(self, route): rpath = self.request.path[len(route):].strip('/') parts = rpath.split('/', 1) self.cname = parts[0] + os.sep self.added = '' if len(parts) == 1: with self._plugin_lock: if self._cname_valid( parts[0]) and parts[0] not in self._plugin_map: url = find_docs_url(parts[0], build_if_needed=False) if self.cname.startswith('openmdao.'): root = os.path.join( get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if url.startswith('file://'): url = url[7:] self.added = os.path.dirname(url)[len(root) + 1:] else: root = os.path.dirname(url) default = os.path.basename(url) self._plugin_map[parts[0]] = (root, default, self.added) root, default, self.added = self._plugin_map[parts[0]] super(PluginDocsHandler, self).initialize(root, default)
def initialize(self, route): rpath = self.request.path[len(route):].strip('/') parts = rpath.split('/', 1) self.cname = parts[0] + os.sep self.added = '' if len(parts) == 1: with self._plugin_lock: if self._cname_valid( parts[0]) and parts[0] not in self._plugin_map: url = find_docs_url(parts[0], build_if_needed=False) if self.cname.startswith('openmdao.'): if (url.endswith("egg")): # url points to docs in a release version, so use docs packaged with openmdao.main root = os.path.join( os.path.dirname(openmdao.main.__file__), "docs") else: # url points to docs in a developer version, so use locally built docs root = os.path.join( get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if url.startswith('file://'): url = url[7:] self.added = os.path.dirname(url)[len(root) + 1:] else: root = os.path.dirname(url) default = os.path.basename(url) self._plugin_map[parts[0]] = (root, default, self.added) root, default, self.added = self._plugin_map[parts[0]] super(PluginDocsHandler, self).initialize(root, default)
def initialize(self, route): rpath = self.request.path[len(route) :].strip("/") parts = rpath.split("/", 1) self.cname = parts[0] + os.sep self.added = "" if len(parts) == 1: with self._plugin_lock: if self._cname_valid(parts[0]) and parts[0] not in self._plugin_map: url = find_docs_url(parts[0], build_if_needed=False) # Remove "file://" from URL url = url[7:] if self.cname.startswith("openmdao."): if self.regex.search(url): # url points to docs in a release version, so use docs packaged with openmdao.main root = os.path.join(os.path.dirname(openmdao.main.__file__), "docs") else: # url points to docs in a developer version, so use locally built docs root = os.path.join(get_ancestor_dir(sys.executable, 3), "docs", "_build", "html") self.added = os.path.dirname(url)[len(root) + 1 :] else: root = os.path.dirname(url) default = os.path.basename(url) self._plugin_map[parts[0]] = (root, default, self.added) root, default, self.added = self._plugin_map[parts[0]] super(PluginDocsHandler, self).initialize(root, default)
def build_docs(parser=None, options=None, args=None): """A script (openmdao build_docs) points to this. It generates the Sphinx documentation for openmdao. """ global _is_release if args and parser: print_sub_help(parser, 'build_docs') return -1 if options is not None and hasattr(options, 'version') and options.version: version = options.version shtitle = 'OpenMDAO Documentation v%s' % version _is_release = True else: _is_release = False try: tag, ncommits, commit = get_rev_info() version = "%s-%s-%s" % (tag, ncommits, commit) shtitle = 'OpenMDAO Documentation (%s commits after version %s)' % (ncommits, tag) except: # try to get commit id try: top = get_ancestor_dir(sys.executable, 3) if '-OpenMDAO-Framework-' in top: commit = top.split('-')[-1] version = "dev - commit id: %s" % commit shtitle = "OpenMDAO Documentation (commit id %s)" % commit else: raise RuntimeError("can't find commit id") except: version = "?-?-?" shtitle = "OpenMDAO Documentation (unknown revision)" branchdir, docdir, bindir = _get_dirnames() startdir = os.getcwd() if not os.path.isdir(docdir): raise RuntimeError('doc directory '+docdir+' not found') _write_src_docs(branchdir, docdir) _make_license_table(docdir) os.chdir(docdir) try: # make necessary directories if os.path.isdir(os.path.join('_build', 'html')): shutil.rmtree(os.path.join('_build', 'html'), onerror=onerror) if os.path.isdir(os.path.join('_build', 'doctrees')): shutil.rmtree(os.path.join('_build', 'doctrees'), onerror=onerror) os.makedirs(os.path.join('_build', 'html')) os.makedirs(os.path.join('_build', 'doctrees')) sphinx.main(argv=['-P', '-b', 'html', '-Dhtml_short_title=%s' % shtitle, '-Dversion=%s' % version, '-Drelease=%s' % version, '-d', os.path.join(docdir, '_build', 'doctrees'), docdir, os.path.join(docdir, '_build', 'html')]) finally: os.chdir(startdir)
def initialize(self, route): rpath = self.request.path[len(route):].strip('/') parts = rpath.split('/', 1) self.cname = parts[0] + os.sep self.added = '' if len(parts) == 1: with self._plugin_lock: if self._cname_valid(parts[0]) and parts[0] not in self._plugin_map: url = find_docs_url(parts[0], build_if_needed=False) if self.cname.startswith('openmdao.'): if(url.endswith("egg")): # url points to docs in a release version, so use docs packaged with openmdao.main root = os.path.join(os.path.dirname(openmdao.main.__file__), "docs") else: # url points to docs in a developer version, so use locally built docs root = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if url.startswith('file://'): url = url[7:] self.added = os.path.dirname(url)[len(root) + 1:] else: root = os.path.dirname(url) default = os.path.basename(url) self._plugin_map[parts[0]] = (root, default, self.added) root, default, self.added = self._plugin_map[parts[0]] super(PluginDocsHandler, self).initialize(root, default)
def __init__(self, secret=None, external=False): # locate the docs, so that the /docs url will point to the appropriate # docs, either for the current release or the current development build if is_dev_build(): docpath = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') else: import openmdao.main docpath = os.path.join(os.path.dirname(openmdao.main.__file__), 'docs') handlers = [ web.url(r'/', web.RedirectHandler, {'url': '/projects', 'permanent': False}), web.url(r'/login', LoginHandler), web.url(r'/logout', LogoutHandler), web.url(r'/exit', ExitHandler), web.url(r'/docs/plugins/(.*)', PluginDocsHandler, {'route': '/docs/plugins/'}), web.url(r'/docs/(.*)', web.StaticFileHandler, {'path': docpath, 'default_filename': 'index.html'}), ] handlers.extend(proj.handlers) handlers.extend(wksp.handlers) if secret is None: secret = os.urandom(1024) app_path = os.path.dirname(os.path.abspath(__file__)) app_settings = { 'login_url': '/login', 'static_path': os.path.join(app_path, 'static'), 'template_path': os.path.join(app_path, 'templates'), 'cookie_secret': secret, 'debug': True, } user_dir = get_user_dir() self.project_dir = os.path.join(user_dir, 'projects') ensure_dir(self.project_dir) session_dir = os.path.join(user_dir, 'sessions') ensure_dir(session_dir) self.session_manager = TornadoSessionManager(secret, session_dir) self.server_manager = ZMQServerManager('openmdao.gui.consoleserver.ConsoleServer', external) # External termination normally only used during GUI testing. if sys.platform == 'win32': # Fake SIGTERM by polling for a .sigterm file. self._exit_requested = False self._poller = threading.Thread(target=self._sigterm_poller, name='SIGTERM poller') self._poller.daemon = True self._poller.start() else: signal.signal(signal.SIGTERM, self._sigterm_handler) super(App, self).__init__(handlers, **app_settings)
def __init__(self, secret=None): # locate the docs, so that the /docs url will point to the appropriate # docs, either for the current release or the current development build if is_dev_build(): idxpath = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') doc_handler = web.StaticFileHandler doc_handler_options = { 'path': idxpath, 'default_filename': 'index.html' } else: # look for docs online import openmdao.util.releaseinfo version = openmdao.util.releaseinfo.__version__ idxpath = 'http://openmdao.org/releases/%s/docs/index.html' % version doc_handler = web.RedirectHandler doc_handler_options = { 'url': idxpath, 'permanent': False } handlers = [ web.url(r'/login', LoginHandler), web.url(r'/logout', LogoutHandler), web.url(r'/exit', ExitHandler), web.url(r'/docs/plugins/(.*)', PluginDocsHandler, { 'route': '/docs/plugins/' }), web.url(r'/docs/(.*)', doc_handler, doc_handler_options ), web.url(r'/', web.RedirectHandler, { 'url':'/projects', 'permanent':False }) ] handlers.extend(proj.handlers) handlers.extend(wksp.handlers) if secret is None: secret = os.urandom(1024) app_path = os.path.dirname(os.path.abspath(__file__)) app_settings = { 'login_url': '/login', 'static_path': os.path.join(app_path, 'static'), 'template_path': os.path.join(app_path, 'templates'), 'cookie_secret': secret, 'debug': True, } user_dir = get_user_dir() self.project_dir = os.path.join(user_dir, 'projects') ensure_dir(self.project_dir) session_dir = os.path.join(user_dir, 'sessions') ensure_dir(session_dir) self.session_manager = TornadoSessionManager(secret, session_dir) self.server_manager = ZMQServerManager('openmdao.gui.consoleserver.ConsoleServer') global _MGR _MGR = self.server_manager signal.signal(signal.SIGTERM, self._sigterm_handler) super(App, self).__init__(handlers, **app_settings)
def __init__(self, secret=None): # locate the docs, so that the /docs url will point to the appropriate # docs, either for the current release or the current development build if is_dev_build(): idxpath = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') doc_handler = web.StaticFileHandler doc_handler_options = { 'path': idxpath, 'default_filename': 'index.html' } else: # look for docs online import openmdao.util.releaseinfo version = openmdao.util.releaseinfo.__version__ idxpath = 'http://openmdao.org/releases/%s/docs/index.html' % version doc_handler = web.RedirectHandler doc_handler_options = { 'url': idxpath, 'permanent': False } handlers = [ web.url(r'/login', LoginHandler), web.url(r'/logout', LogoutHandler), web.url(r'/exit', ExitHandler), web.url(r'/docs/plugins/(.*)', PluginDocsHandler, { 'route': '/docs/plugins/' }), web.url(r'/docs/(.*)', doc_handler, doc_handler_options ), web.url(r'/', web.RedirectHandler, { 'url':'/projects', 'permanent':False }) ] handlers.extend(proj.handlers) handlers.extend(wksp.handlers) if secret is None: secret = os.urandom(1024) app_path = os.path.dirname(os.path.abspath(__file__)) app_settings = { 'login_url': '/login', 'static_path': os.path.join(app_path, 'static'), 'template_path': os.path.join(app_path, 'templates'), 'cookie_secret': secret, 'debug': True, } user_dir = get_user_dir() self.project_dir = os.path.join(user_dir, 'projects') ensure_dir(self.project_dir) session_dir = os.path.join(user_dir, 'sessions') ensure_dir(session_dir) self.session_manager = TornadoSessionManager(secret, session_dir) self.server_manager = ZMQServerManager('openmdao.gui.consoleserver.ConsoleServer') super(App, self).__init__(handlers, **app_settings)
def initialize(self, route): rpath = self.request.path[len(route):].strip('/') parts = rpath.split('/',1) self.cname = parts[0] + os.sep self.added = '' if len(parts) == 1: with self._plugin_lock: if self._cname_valid(parts[0]) and parts[0] not in self._plugin_map: url = find_docs_url(parts[0], build_if_needed=False) if self.cname.startswith('openmdao.'): root = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if url.startswith('file://'): url = url[7:] self.added = os.path.dirname(url)[len(root)+1:] else: root = os.path.dirname(url) default = os.path.basename(url) self._plugin_map[parts[0]] = (root, default, self.added) root, default, self.added = self._plugin_map[parts[0]] super(PluginDocsHandler, self).initialize(root, default)
def create(self, typ, version=None, server=None, res_desc=None, **ctor_args): """Create and return an instance of the specified type, or None if this Factory can't satisfy the request. """ if server is None and res_desc is None and typ in self.analyzer.class_map: with self._lock: fpath = self.analyzer.class_map[typ].fname modpath = self.analyzer.fileinfo[fpath][0].modpath if os.path.getmtime(fpath) > self.analyzer.fileinfo[fpath][ 1] and modpath in sys.modules: reload(sys.modules[modpath]) if fpath not in self.imported: sys.path = [ get_ancestor_dir(fpath, len(modpath.split('.'))) ] + sys.path try: __import__(modpath) except ImportError as err: return None finally: sys.path = sys.path[1:] mod = sys.modules[modpath] visitor = self.analyzer.fileinfo[fpath][0] self._get_mod_ctors(mod, fpath, visitor) try: ctor = self.imported[fpath][1][typ] except KeyError: return None return ctor(**ctor_args) return None
def build_docs(parser=None, options=None, args=None): """A script (openmdao build_docs) points to this. It generates the Sphinx documentation for openmdao. """ global _is_release if args and parser: print_sub_help(parser, 'build_docs') return -1 if options is not None and hasattr(options, 'version') and options.version: version = options.version shtitle = 'OpenMDAO Documentation v%s' % version _is_release = True else: _is_release = False try: tag, ncommits, commit = get_rev_info() version = "%s-%s-%s" % (tag, ncommits, commit) shtitle = 'OpenMDAO Documentation (%s commits after version %s)' % ( ncommits, tag) except: # try to get commit id try: top = get_ancestor_dir(sys.executable, 3) if '-OpenMDAO-Framework-' in top: commit = top.split('-')[-1] version = "dev - commit id: %s" % commit shtitle = "OpenMDAO Documentation (commit id %s)" % commit else: raise RuntimeError("can't find commit id") except: version = "?-?-?" shtitle = "OpenMDAO Documentation (unknown revision)" branchdir, docdir, bindir = _get_dirnames() startdir = os.getcwd() if not os.path.isdir(docdir): raise RuntimeError('doc directory ' + docdir + ' not found') _write_src_docs(branchdir, docdir) _make_license_table(docdir) os.chdir(docdir) try: # make necessary directories if os.path.isdir(os.path.join('_build', 'html')): shutil.rmtree(os.path.join('_build', 'html'), onerror=onerror) if os.path.isdir(os.path.join('_build', 'doctrees')): shutil.rmtree(os.path.join('_build', 'doctrees'), onerror=onerror) os.makedirs(os.path.join('_build', 'html')) os.makedirs(os.path.join('_build', 'doctrees')) sphinx.main(argv=[ '-P', '-b', 'html', '-Dhtml_short_title=%s' % shtitle, '-Dversion=%s' % version, '-Drelease=%s' % version, '-d', os.path.join(docdir, '_build', 'doctrees'), docdir, os.path.join(docdir, '_build', 'html') ]) finally: os.chdir(startdir)
def _plugin_docs(plugin_name): """Returns a url for the Sphinx docs for the named plugin. The plugin must be importable in the current environment. plugin_name: str Name of the plugin distribution, module, or class. """ parts = plugin_name.split('.') if len(parts) == 1: # assume it's a class name and try to find unambiguous module modname = None # loop over available types to find a class name that matches for name, version in get_available_types(): mname, cname = name.rsplit('.', 1) if cname == plugin_name: if modname and modname != mname: raise RuntimeError("Can't determine module for class '%s'" " unambiguously. found in %s" % (cname, [mname, modname])) modname = mname parts = modname.split('.') if modname is None: # didn't find a class, so assume plugin_name is a dist name parts = [plugin_name, plugin_name] for i in range(len(parts)-1): mname = '.'.join(parts[:len(parts)-i]) try: __import__(mname) mod = sys.modules[mname] modname = mname break except ImportError: pass else: # Possibly something in contrib that's a directory. try: __import__(plugin_name) mod = sys.modules[plugin_name] modname = plugin_name except ImportError: raise RuntimeError("Can't locate package/module '%s'" % plugin_name) if modname.startswith('openmdao.'): # lookup in builtin docs fparts = mod.__file__.split(os.sep) pkg = '.'.join(modname.split('.')[:2]) anchorpath = '/'.join(['srcdocs', 'packages', '%s.html#module-%s' % (pkg, modname)]) if any([p.endswith('.egg') and p.startswith('openmdao.') for p in fparts]): # this is a release version, so use online docs url = '/'.join(['http://openmdao.org/releases/%s/docs' % __version__, anchorpath]) else: # it's a developer version, so use locally built docs htmldir = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if not os.path.isfile(os.path.join(htmldir, 'index.html')): #make sure the local docs are built print "local docs not found.\nbuilding them now...\n" check_call(['openmdao', 'build_docs']) url = 'file://'+os.path.join(htmldir, anchorpath) url = url.replace('\\', '/') else: url = os.path.join(os.path.dirname(os.path.abspath(mod.__file__)), 'sphinx_build', 'html', 'index.html') return url
def _plugin_docs(plugin_name, browser=None): """This brings up the Sphinx docs for the named plugin using the specified browser. The plugin must be importable in the current environment. plugin_name: str Name of the plugin distribution, module, or class. browser: str (optional) Name of the browser (according to the webbrowser library) to use to view the plugin docs. If none is specified, the platform default browser will be used. """ parts = plugin_name.split(".") if len(parts) == 1: # assume it's a class name and try to find unambiguous module modname = None # loop over available types to find a class name that matches for name, version in get_available_types(): mname, cname = name.rsplit(".", 1) if cname == plugin_name: if modname and modname != mname: raise RuntimeError( "Can't determine module for class '%s' unambiguously. found in %s" % (cname, [mname, modname]) ) modname = mname parts = modname.split(".") if modname is None: # didn't find a class, so assume plugin_name is a dist name parts = [plugin_name, plugin_name] for i in range(len(parts) - 1): mname = ".".join(parts[: len(parts) - i]) try: __import__(mname) mod = sys.modules[mname] modname = mname break except ImportError: pass else: # Possibly something in contrib that's a directory. try: __import__(plugin_name) mod = sys.modules[plugin_name] modname = plugin_name except ImportError: raise RuntimeError("Can't locate package/module '%s'" % plugin_name) if modname.startswith("openmdao."): # lookup in builtin docs fparts = mod.__file__.split(os.sep) pkg = ".".join(modname.split(".")[:2]) anchorpath = "/".join(["srcdocs", "packages", "%s.html#module-%s" % (pkg, modname)]) if any([p.endswith(".egg") and p.startswith("openmdao.") for p in fparts]): # this is a release version, so use online docs url = "/".join(["http://openmdao.org/releases/%s/docs" % __version__, anchorpath]) else: # it's a developer version, so use locally built docs htmldir = os.path.join(get_ancestor_dir(sys.executable, 3), "docs", "_build", "html") if not os.path.isfile(os.path.join(htmldir, "index.html")): # make sure the local docs are built print "local docs not found.\nbuilding them now...\n" check_call(["openmdao", "build_docs"]) url = "file://" + os.path.join(htmldir, anchorpath) url = url.replace("\\", "/") else: url = os.path.join(os.path.dirname(os.path.abspath(mod.__file__)), "sphinx_build", "html", "index.html") wb = webbrowser.get(browser) wb.open(url)
def _plugin_docs(plugin_name, browser=None): """This brings up the Sphinx docs for the named plugin using the specified browser. The plugin must be importable in the current environment. plugin_name: str Name of the plugin distribution, module, or class. browser: str (optional) Name of the browser (according to the webbrowser library) to use to view the plugin docs. If none is specified, the platform default browser will be used. """ parts = plugin_name.split('.') if len( parts ) == 1: # assume it's a class name and try to find unambiguous module modname = None # loop over available types to find a class name that matches for name, version in get_available_types(): mname, cname = name.rsplit('.', 1) if cname == plugin_name: if modname and modname != mname: raise RuntimeError( "Can't determine module for class '%s' unambiguously. found in %s" % (cname, [mname, modname])) modname = mname parts = modname.split('.') if modname is None: # didn't find a class, so assume plugin_name is a dist name parts = [plugin_name, plugin_name] for i in range(len(parts) - 1): mname = '.'.join(parts[:len(parts) - i]) try: __import__(mname) mod = sys.modules[mname] modname = mname break except ImportError: pass else: raise RuntimeError("Can't locate package/module '%s'" % plugin_name) if modname.startswith('openmdao.'): # lookup in builtin docs fparts = mod.__file__.split(os.sep) pkg = '.'.join(modname.split('.')[:2]) anchorpath = '/'.join( ['srcdocs', 'packages', '%s.html#module-%s' % (pkg, modname)]) if any( [p.endswith('.egg') and p.startswith('openmdao.') for p in fparts]): # this is a release version, so use online docs url = '/'.join([ 'http://openmdao.org/releases/%s/docs' % __version__, anchorpath ]) else: # it's a developer version, so use locally built docs htmldir = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if not os.path.isfile(os.path.join( htmldir, 'index.html')): #make sure the local docs are built print "local docs not found.\nbuilding them now...\n" check_call(['openmdao', 'build_docs']) url = 'file://' + os.path.join(htmldir, anchorpath) url = url.replace('\\', '/') else: url = os.path.join(os.path.dirname(os.path.abspath(mod.__file__)), 'sphinx_build', 'html', 'index.html') wb = webbrowser.get(browser) wb.open(url)
#url of the github api apiURL = 'https://api.github.com/' #default username to upload with (provided by user) username = "******" #password to log in with (will be provided by user) password = "" #should we notify the cookbook? updateCookbook = True #url of the cookbook to notify MUST END IN WITH '/' cookbookURL = 'http://openmdao.org/wordpress_NEWURL/cookbook/' #directory of tutorials tutorialsDir = os.path.join(get_ancestor_dir(__file__,5), "examples") def uploadGists (OpenMDAO_version): #first of all, ask for username/password for github if getUsernamePass(): return upload(OpenMDAO_version) else: return False def upload (OpenMDAO_version): # """ # file structure of tutorials: # # examples
def __init__(self, secret=None, external=False): # locate the docs, so that the /docs url will point to the appropriate # docs, either for the current release or the current development build if is_dev_build(): docpath = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') else: import openmdao.main docpath = os.path.join(os.path.dirname(openmdao.main.__file__), 'docs') handlers = [ web.url(r'/', web.RedirectHandler, { 'url': '/projects', 'permanent': False }), web.url(r'/login', LoginHandler), web.url(r'/logout', LogoutHandler), web.url(r'/exit', ExitHandler), web.url(r'/docs/plugins/(.*)', PluginDocsHandler, {'route': '/docs/plugins/'}), web.url(r'/docs/(.*)', web.StaticFileHandler, { 'path': docpath, 'default_filename': 'index.html' }) ] handlers.extend(proj.handlers) handlers.extend(wksp.handlers) if secret is None: secret = os.urandom(1024) app_path = os.path.dirname(os.path.abspath(__file__)) app_settings = { 'login_url': '/login', 'static_path': os.path.join(app_path, 'static'), 'template_path': os.path.join(app_path, 'templates'), 'cookie_secret': secret, 'debug': True, } user_dir = get_user_dir() self.project_dir = os.path.join(user_dir, 'projects') ensure_dir(self.project_dir) session_dir = os.path.join(user_dir, 'sessions') ensure_dir(session_dir) self.session_manager = TornadoSessionManager(secret, session_dir) self.server_manager = ZMQServerManager( 'openmdao.gui.consoleserver.ConsoleServer', external) # External termination normally only used during GUI testing. if sys.platform == 'win32': # Fake SIGTERM by polling for a .sigterm file. self._exit_requested = False self._poller = threading.Thread(target=self._sigterm_poller, name='SIGTERM poller') self._poller.daemon = True self._poller.start() else: signal.signal(signal.SIGTERM, self._sigterm_handler) super(App, self).__init__(handlers, **app_settings)
def find_docs_url(plugin_name=None, build_if_needed=True): """Returns a url for the Sphinx docs for the named plugin. The plugin must be importable in the current environment. plugin_name: str Name of the plugin distribution, module, or class. """ parts = plugin_name.split('.') if len( parts ) == 1: # assume it's a class name and try to find unambiguous module modname = None # loop over available types to find a class name that matches for name, version in get_available_types(): mname, cname = name.rsplit('.', 1) if cname == plugin_name: if modname and modname != mname: raise RuntimeError("Can't determine module for class '%s'" " unambiguously. found in %s" % (cname, [mname, modname])) modname = mname parts = modname.split('.') if modname is None: # didn't find a class, so assume plugin_name is a dist name parts = [plugin_name, plugin_name] for i in range(len(parts) - 1): mname = '.'.join(parts[:len(parts) - i]) try: __import__(mname) mod = sys.modules[mname] modname = mname break except ImportError: pass else: # Possibly something in contrib that's a directory. try: __import__(plugin_name) mod = sys.modules[plugin_name] modname = plugin_name except ImportError: raise RuntimeError("Can't locate package/module '%s'" % plugin_name) if modname.startswith('openmdao.'): # lookup in builtin docs fparts = mod.__file__.split(os.sep) pkg = '.'.join(modname.split('.')[:2]) anchorpath = '/'.join( ['srcdocs', 'packages', '%s.html#module-%s' % (pkg, modname)]) if any( [p.endswith('.egg') and p.startswith('openmdao.') for p in fparts]): # this is a release version, so use online docs url = '/'.join([ 'http://openmdao.org/releases/%s/docs' % __version__, anchorpath ]) else: # it's a developer version, so use locally built docs htmldir = os.path.join(get_ancestor_dir(sys.executable, 3), 'docs', '_build', 'html') if not os.path.isfile(os.path.join( htmldir, 'index.html')) and build_if_needed: #make sure the local docs are built print "local docs not found.\nbuilding them now...\n" check_call(['openmdao', 'build_docs']) url = 'file://' + os.path.join(htmldir, anchorpath) url = url.replace('\\', '/') else: url = os.path.join(os.path.dirname(os.path.abspath(mod.__file__)), 'sphinx_build', 'html', 'index.html') return url
def find_docs_url(plugin_name=None, build_if_needed=True): """Returns a url for the Sphinx docs for the named plugin. The plugin must be importable in the current environment. plugin_name: str Name of the plugin distribution, module, or class. """ parts = plugin_name.split(".") if len(parts) == 1: # assume it's a class name and try to find unambiguous module modname = None # loop over available types to find a class name that matches for name, version in get_available_types(): mname, cname = name.rsplit(".", 1) if cname == plugin_name: if modname and modname != mname: raise RuntimeError( "Can't determine module for class '%s'" " unambiguously. found in %s" % (cname, [mname, modname]) ) modname = mname parts = modname.split(".") if modname is None: # didn't find a class, so assume plugin_name is a dist name parts = [plugin_name, plugin_name] for i in range(len(parts) - 1): mname = ".".join(parts[: len(parts) - i]) try: __import__(mname) mod = sys.modules[mname] modname = mname modfile = os.path.abspath(mod.__file__) break except ImportError: # we may be able to locate the docs even if the import fails modfile = find_module(mname) modname = mname if modfile: break else: # Possibly something in contrib that's a directory. try: __import__(plugin_name) mod = sys.modules[plugin_name] modname = plugin_name modfile = os.path.abspath(mod.__file__) except ImportError: raise RuntimeError("Can't locate package/module '%s'" % plugin_name) url = "file://" if modname.startswith("openmdao."): # lookup in builtin docs import openmdao.main fparts = mod.__file__.split(os.sep) pkg = ".".join(modname.split(".")[:2]) anchorpath = "/".join(["srcdocs", "packages", "%s.html#module-%s" % (pkg, modname)]) if any([p.endswith(".egg") and p.startswith("openmdao.") for p in fparts]): # this is a release version, so use docs packaged with openmdao.main htmldir = os.path.join(os.path.dirname(openmdao.main.__file__), "docs") else: # it's a developer version, so use locally built docs htmldir = os.path.join(get_ancestor_dir(sys.executable, 3), "docs", "_build", "html") if not os.path.isfile(os.path.join(htmldir, "index.html")) and build_if_needed: # make sure the local docs are built print "local docs not found.\nbuilding them now...\n" check_call(["openmdao", "build_docs"]) url += os.path.join(htmldir, anchorpath) else: url += os.path.join(os.path.dirname(modfile), "sphinx_build", "html", "index.html") url = url.replace("\\", "/") return url