def loadDocumentation(path, name, *apps): assert path try: if public.verbose: log_utility.info('Loading documentation "%s"...' % path) # Prepare the documentation registrar and namespace wrappers and exec the module # The last app passed in is considered the primary one appPrimary = apps[-1] docRegistrar = doc.Registrar(appPrimary.name) wrappers = [] syms = {} for app in apps: wrappers.append(NamespaceWrapper(app, name, path, docRegistrar, False)) syms[app.namespace] = wrappers[-1] doc.setStrucTextSymbols(syms) # Documentation in non-function-bearing modules is added to the "guide" if appPrimary.namespace == public.program.namespace: props = {'all': name, 'guide': name} else: props = {} doc.parserStrucText.parse(open(path).read()) node = doc.parserStrucText.take() node.setProp('module', name) docRegistrar.add(node) docRegistrar.register() except Exception, e: log_utility._tracebackException('Failed to load "%s"' % path, e, 0, 0, True)
def main(version): if public.program.name != public.engine.name and version is not None: public.program.version = version args = getArgs() if public.verbose: log_utility.info('args = %s' % args) # Load scripts and warn about duplicates loadScripts() global duplicateFunctions if len(duplicateFunctions) > 0: duplicateFunctions.sort() for dup in duplicateFunctions: log_utility.warning('Ignoring duplicate "%s" from "%s"' % (dup.name(), dup.path())) # Display the quick start guide if running the base program with no arguments if public.engine.name and len(args) == 0: execute('help()') sys.exit(1) for argIn in args: try: execute(argIn) except public.ExcBase, e: if e.traceback: log_utility._tracebackException(None, e, 0, 0, False) else: msgs = str(e).split('\n') log_utility.error('Command: "%s"' % argIn, *msgs) except doc.ExcBase, e: msgs = str(e).split('\n') log_utility.error('Command: "%s"' % argIn, *msgs)
def __init__(self, pathRaw, createHome, dirsPathAdd, symsPublic = {}, symsDoc = {}, book = None, version = versionEng): self.path = os.path.abspath(pathRaw) self.name = os.path.splitext(os.path.basename(pathRaw))[0] self.dir = os.path.dirname(os.path.realpath(self.path)) self.home = os.path.expanduser('~/.%s') % self.name if createHome and not os.path.isdir(self.home): log_utility.info('Creating directory "%s"...' % self.home) os.mkdir(self.home) self.shareGlobal = os.path.join('/usr/share' , self.name) self.shareLocal = os.path.join('/usr/local/share', self.name) self.libGlobal = os.path.join('/usr/lib' , self.name) self.libLocal = os.path.join('/usr/local/lib' , self.name) self.etc = os.path.join('/etc' , self.name) self.subdirScript = '%s.d' % self.name self.dirs = [self.home] self.dirsPath = [self.home] # For install-free use look for script subdirs here or one up (for bin directory) if os.path.isdir(os.path.join(os.path.dirname(self.dir), self.subdirScript)): self.dirs.append(os.path.dirname(self.dir)) self.dirsPath.append(os.path.dirname(self.dir)) if os.path.isdir(os.path.join(self.dir, self.subdirScript)): self.dirs.append(self.dir) self.dirsPath.append(self.dir) self.dirs.extend([ self.libLocal, self.libGlobal, self.shareLocal, self.shareGlobal, self.etc ]) self.dirsPath.extend([ self.libLocal, self.libGlobal, self.shareLocal, self.shareGlobal, self.etc ]) self.dirsScript = [os.path.join(dir, self.subdirScript) for dir in self.dirs] self.namespace = self.name.upper() self.exports = ExportedModules() self.book = book self.version = version self.types = {} self.symsPublic = symsPublic self.symsDoc = symsDoc if dirsPathAdd: self.dirsPath.extend(dirsPathAdd)
def execute(sCmd): if public.verbose: log_utility.info('Execute: "%s"' % sCmd) # Restrict scope to public functions and runtime symbols. public.program.symsExec = copy.copy(symsCore) public.program.symsExec.update(public.engine.exports.getAll()) public.program.symsExec[public.engine.namespace] = public if public.program.name != public.engine.name: public.program.symsExec.update(public.program.exports.getAll()) if public.verbose: log_utility.info('=============== Symbols ===============') names = public.program.symsExec.keys() names.sort() for name in names: log_utility.info('%15s: %s' % (name, public.program.symsExec[name])) log_utility.info('=======================================') exec sCmd in public.program.symsExec
def loadScript(path, name, isCore, *apps): assert path try: if public.verbose: log_utility.info('Loading script "%s"...' % path) # Prepare the documentation registrar and namespace wrappers and exec the module # The last app passed in is considered the primary one appPrimary = apps[-1] docRegistrar = doc.Registrar(appPrimary.name) wrappers = [] syms = {} for app in apps: wrappers.append(NamespaceWrapper(app, name, path, docRegistrar, isCore)) syms[app.namespace] = wrappers[-1] doc.setStrucTextSymbols(syms) execfile(path, syms) # Warn about classes flagged for export (should be in a type module) # Export newly-discovered classes of appropriate ancestry namesOrig = [app.namespace for app in apps] for nameNew in syms: if nameNew not in namesOrig and inspect.isclass(syms[nameNew]): for exportedClass in exportedClasses: sym = syms[nameNew] if issubclass(sym, exportedClass): if public.verbose: log_utility.info('Register type "%s.%s"' % (appPrimary.namespace, nameNew)) appPrimary.types[nameNew] = sym # Create function objects for empty decorators (invoked without parens). # Register all discovered functions and track duplicates. if appPrimary.exports.has(name): export = appPrimary.exports.get(name) else: export = appPrimary.exports.add(name, path, None) global duplicateFunctions for wrapper in wrappers: for decorator in wrapper._decoExport: if decorator.function is None: func = decorator.args[0] decorator.args = decorator.args[1:] decorator._register(func) if decorator.isCore: if public.verbose: log_utility.info('Register core function "%s"' % decorator.function.nameShort) global symsCore if decorator.function.nameShort not in symsCore: symsCore[decorator.function.nameShort] = decorator.function else: if public.verbose: log_utility.info('Register function "%s.%s"' % (appPrimary.namespace, decorator.function.name)) if not export.addFunction(decorator.function): duplicateFunctions.append(DuplicateFunction(decorator.function)) # Register documentation if export.countFunctions() > 0: # Documentation in function-bearing modules is added to the "reference" if appPrimary.namespace == public.program.namespace: props = {'all': name, 'reference': name} else: props = {} # For consistent TOC sorting need core to be None, rather than False. if isCore: nodeModule = docRegistrar.wrap( form = 'wrapper', heading = 'Module: (%s)' % name, core = name, **props ) else: nodeModule = docRegistrar.wrap( form = 'wrapper', heading = 'Module: %s' % name, module = name, toc = True, **props ) nodesBody = [] for function in export.iterFunctionsSorted(): if not function.isInternal: nodesDoc = [] if function.doc: nodesDoc.append(function.doc) for wrapper in wrappers: nodesDoc.append(wrapper._nodesFunc.get(function.nameShort, [])) nodesBody.append( doc.Node( form = 'wrapper', heading = 'Function: %s' % function.getProto(), content = FunctionNode(function, nodesDoc), function = function.name, toc = True, ) ) nodeModule.add(doc.Node(content = nodesBody)) else: # Documentation in non-function-bearing modules is added to the "guide" if appPrimary.namespace == public.program.namespace: props = {'all': name, 'guide': name} else: props = {} if isCore: docRegistrar.wrap(form = 'wrapper', core = name, **props) else: docRegistrar.wrap(form = 'wrapper', module = name, **props) docRegistrar.register() except public.ExcLoad, e: log_utility._tracebackException('Failed to load "%s"' % path, e, 1, 1, False)
def __call__(self): if public.verbose: log_utility.info('Loading documentation in "%s" on demand' % self.path) loadDocumentation(self.path, self.name, *self.apps)
def __call__(self): if public.verbose: log_utility.info('Loading "%s" on demand' % self.path) loadScript(self.path, self.name, False, *self.apps)
def loadScripts(): if public.verbose: log_utility.info('Load %s modules from %s' % (public.program.name, public.program.dirsScript)) if public.program.name != public.engine.name: log_utility.info('Load %s modules from %s' % (public.engine.name, public.engine.dirsScript)) log_utility.info('Load Python modules from %s' % sys.path) # TODO: Load on demand, rather than sorting to get "_..." files containing # arg types and exceptions to come first. # For now type modules are just script modules that load early. # 1) Load engine core modules loaded = set() for dirCmdo in public.engine.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extCore) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate engine core module "%s"' % path) else: loadScript(path, name, True, public.engine) loaded.add(name) # 2) Load named engine modules loaded = set() for dirCmdo in public.engine.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extModule) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate engine module "%s"' % path) else: loader = ScriptLoader(path, name, public.engine) public.engine.exports.add(name, path, loader) loaded.add(name) # 3) Load engine documentation modules loaded = set() for dirCmdo in public.engine.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extDoc) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate engine documentation module "%s"' % path) else: loader = DocumentationLoader(path, name, public.engine) public.engine.exports.add(name, path, loader) loaded.add(name) if public.program.name != public.engine.name: # 4) Load app core modules loaded = set() for dirCmdo in public.program.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extCore) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate core module "%s"' % path) else: loadScript(path, name, True, public.engine, public.program) loaded.add(name) # 5) Load named app modules loaded = set() for dirCmdo in public.program.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extModule) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate module "%s"' % path) else: loader = ScriptLoader(path, name, public.engine, public.program) public.program.exports.add(name, path, loader) loaded.add(name) # 6) Load app documentation modules loaded = set() for dirCmdo in public.program.dirsScript: paths = glob.glob(os.path.join(dirCmdo, '*%s') % public.extDoc) paths.sort() for path in paths: name = os.path.splitext(os.path.split(path)[1])[0] if name in loaded: if public.verbose: log_utility.info('Skipping duplicate documentation module "%s"' % path) else: loader = DocumentationLoader(path, name, public.engine, public.program) public.engine.exports.add(name, path, loader) loaded.add(name)
loadScripts() global duplicateFunctions if len(duplicateFunctions) > 0: duplicateFunctions.sort() for dup in duplicateFunctions: log_utility.warning('Ignoring duplicate "%s" from "%s"' % (dup.name(), dup.path())) # Display the quick start guide if running the base program with no arguments if public.engine.name and len(args) == 0: execute('help()') sys.exit(1) for argIn in args: try: execute(argIn) except public.ExcBase, e: if e.traceback: log_utility._tracebackException(None, e, 0, 0, False) else: msgs = str(e).split('\n') log_utility.error('Command: "%s"' % argIn, *msgs) except doc.ExcBase, e: msgs = str(e).split('\n') log_utility.error('Command: "%s"' % argIn, *msgs) except public.ExcQuit, e: log_utility.info('<quit>') sys.exit(1) except Exception, e: skipTop = 4 if public.verbose: skipTop = 0 log_utility._tracebackException('Command: %s' % argIn, e, skipTop, 0, True)