Beispiel #1
0
def ListCommands():
    """List all available commands.

    Example:

    >>> ListCommands()
    """
    session.log.info('Available commands:')
    items = []
    for name, obj in session.getExportedObjects():
        if getattr(obj, 'is_usercommand', False):
            real_func = getattr(obj, 'real_func', obj)
            if getattr(real_func, 'is_hidden', False):
                continue
            if real_func.__name__ != name:
                # it's an alias, don't show it again
                continue
            if hasattr(real_func, 'help_arglist'):
                argspec = '(%s)' % real_func.help_arglist
            else:
                argspec = formatArgs(real_func)
            docstring = real_func.__doc__ or ' '
            signature = real_func.__name__ + argspec
            if len(signature) > 50:
                signature = signature[:47] + '...'
            if not real_func.__name__.startswith('_'):
                items.append((signature, docstring.splitlines()[0]))
    items.sort()
    printTable(('name', 'description'), items, session.log.info)
Beispiel #2
0
def process_signature(app, objtype, fullname, obj, options, args, retann):
    # for user commands, fix the signature:
    # a) use original function for functions wrapped by decorators
    # b) use designated "helparglist" for functions that have it
    if objtype == 'function' and fullname.startswith('nicos.commands.'):
        while hasattr(obj, 'real_func'):
            obj = obj.real_func
        if hasattr(obj, 'help_arglist'):
            return '(' + obj.help_arglist + ')', retann
        else:
            return formatArgs(obj), retann
Beispiel #3
0
 def gen_funchelp(self, func):
     ret = []
     real_func = getattr(func, 'real_func', func)
     if hasattr(real_func, 'help_arglist'):
         argspec = '(%s)' % real_func.help_arglist
     else:
         argspec = formatArgs(real_func)
     ret.append(
         self.gen_heading('Help on the %s command' % real_func.__name__))
     ret.append('<p class="usage">Usage: <tt>' +
                escape_html(real_func.__name__ + argspec) + '</tt></p>')
     docstring = '\n'.join(formatDocstring(real_func.__doc__ or ''))
     ret.append(self.gen_markup(docstring))
     return ''.join(ret)
Beispiel #4
0
    def __new__(mcs, name, bases, attrs):
        # set source class for parameters
        if 'parameters' in attrs:
            for pinfo in itervalues(attrs['parameters']):
                pinfo.classname = attrs['__module__'] + '.' + name
        for base in bases:
            if hasattr(base, 'parameters'):
                for pinfo in itervalues(base.parameters):
                    if pinfo.classname is None:
                        pinfo.classname = base.__module__ + '.' + base.__name__
        newtype = type.__new__(mcs, name, bases, attrs)
        if '__constructed__' in attrs:
            return newtype
        for entry in newtype.__mergedattrs__:
            newentry = {}
            for base in reversed(bases):
                if hasattr(base, entry):
                    newentry.update(getattr(base, entry))
            newentry.update(attrs.get(entry, {}))
            setattr(newtype, entry, newentry)

        # add usermethods to registry, check names of methods to comply with
        # coding style
        for aname in attrs:
            if aname.startswith(('_', 'do')):
                continue
            value = getattr(newtype, aname)
            if not isinstance(value, (types.FunctionType, types.MethodType)):
                continue
            args = formatArgs(value, strip_self=True)
            if value.__doc__:
                docline = value.__doc__.strip().splitlines()[0]
            else:
                docline = ''
            newtype.methods[aname] = (args, docline, newtype,
                                      hasattr(value, 'is_usermethod'))

        return newtype
Beispiel #5
0
 def handle_signature(self, sig, signode):
     self.object = command_wrappers[sig].orig_function
     sig = '%s%s' % (sig, formatArgs(self.object, strip_self=True))
     return PyFunction.handle_signature(self, sig, signode)
Beispiel #6
0
    def gen_helpindex(self):
        ret = [
            '<p class="menu">'
            '<a href="#commands">Commands</a>&nbsp;&nbsp;|&nbsp;&nbsp;'
            '<a href="#devices">Devices</a>&nbsp;&nbsp;|&nbsp;&nbsp;'
            '<a href="#setups">Setups</a></p>'
        ]
        ret.append('<p>Welcome to the NICOS interactive help!</p>')
        cmds = []
        for name, obj in session.getExportedObjects():
            if not hasattr(obj, 'is_usercommand'):
                continue
            real_func = getattr(obj, 'real_func', obj)
            if real_func.__name__.startswith('_'):
                continue
            if getattr(real_func, 'is_hidden', False):
                continue
            if real_func.__name__ != name:
                # it's an alias, don't show it again
                continue
            if hasattr(real_func, 'help_arglist'):
                argspec = '(%s)' % real_func.help_arglist
            else:
                argspec = formatArgs(real_func)
            signature = '<tt><a href="cmd:%s">%s</a></tt><small>' % \
                ((real_func.__name__,)*2) + escape_html(argspec) + '</small>'
            docstring = escape_html(real_func.__doc__ or ' ').splitlines()[0]
            cmds.append('<tr><td>%s</td><td>%s</td></tr>' %
                        (signature, docstring))
        cmds.sort()
        ret.append(self.gen_heading('NICOS commands', 'commands'))
        ret.append('<p>These commands are currently available.</p>')
        ret.append('<table width="100%">'
                   '<tr><th>Name</th><th>Short description</th></tr>')
        ret.extend(cmds)
        ret.append('</table>')
        ret.append(self.gen_heading('Devices', 'devices'))
        ret.append(
            '<p>These are the currently loaded high-level devices.  Use '
            '<a href="cmd:AddSetup">AddSetup()</a> or the "Setup" '
            'window to add more devices.</p>')
        ret.append('<table width="100%"><tr><th>Name</th><th>Type</th>'
                   '<th>From setup</th><th>Description</th></tr>')
        setupinfo = session.getSetupInfo()
        devsetups = {}
        for sname, info in iteritems(setupinfo):
            if info is None:
                continue
            for devname in info['devices']:
                devsetups[devname] = sname
        for devname in sorted(session.explicit_devices, key=lower):
            dev = session.devices[devname]
            ret.append(
                '<tr><td><tt><a href="dev:%s">%s</a></tt></td>'
                '<td>%s</td><td>%s</td><td>%s</td>' %
                (dev, dev, dev.__class__.__name__, devsetups.get(
                    devname, ''), escape_html(dev.description)))
        ret.append('</table>')
        ret.append(self.gen_heading('Setups', 'setups'))
        ret.append('<p>These are the available setups.  Use '
                   '<a href="cmd:AddSetup">AddSetup()</a> to load an '
                   'additional setup or <a href="cmd:NewSetup">NewSetup()</a>'
                   ' to load one or more completely new ones.</p>')

        def devlink(devname):
            if devname in session.devices:
                return '<a href="dev:%s">%s</a>' % (escape_html(devname),
                                                    escape_html(devname))
            return escape_html(devname)

        def listsetups(group):
            setups = []
            for setupname, info in sorted(iteritems(session.getSetupInfo())):
                if info is None or info['group'] != group:
                    continue
                setups.append(
                    '<tr><td><tt>%s</tt></td><td>%s</td>'
                    '<td>%s</td><td>%s</td></tr>' %
                    (setupname, setupname in session.loaded_setups and 'yes'
                     or '', escape_html(info['description']), ', '.join(
                         map(devlink, sorted(info['devices'], key=lower)))))
            ret.append('<table width="100%"><tr><th>Name</th><th>Loaded</th>'
                       '<th>Description</th><th>Devices</th></tr>')
            ret.extend(setups)
            ret.append('</table>')

        ret.append('<h4>Basic instrument setups</h4>')
        listsetups('basic')
        ret.append('<h4>Optional setups</h4>')
        listsetups('optional')
        ret.append('<h4>Plug-and-play setups</h4>')
        listsetups('plugplay')
        return ''.join(ret)