I would like to hear your opinion about the attached module.

I see two uses:
- testing: we could write some tests as it is done in mercurial
- for menu and icon handling in C/C++: The C wrapper would start the
executable in the beginning and communicate via stdin and stdout.
Caching of status queries is already done. 

Possible queries:
state of files
global and repository configuration
menu structure (nonrepo, repo and drag)

can be started directly or as a mercurial extension with the command
hg_state (maybe interesting for cutehg?)

More documentation is in the code

Greetings
Simon H.
#!/usr/bin/python

#hg_state.py
#
#

"""
hg_state reads commands from stdin and writes answert to stdout
see function hg_state for details

can be started from cmd line (python hg_state.py),
used as an extension for mrcurial with the command hg_state
or used as a module
"""

import sys, os.path
import mercurial.hg

try: from tortoise import cachethg
except: cachethg = None
try: from tortoise import menuthg
except: menuthg = None

class nullwrite:
    def write(*args):
        pass
    def writelines(*args):
        pass
    def flush(*args):
        pass
    def close(*args):
        pass
    closed = False

def get_config(ui, path):
    if not path:
        return ';'.join(ui.cdata.sections())
    parts = path.split(';', 3)
    if len(parts) == 1:
        return ';'.join([ c[0] + '=' + c[1] for c in ui.configitems(parts[0])])
    elif len(parts) == 2:
        return ui.config(parts[0], parts[1], '')
    else:
        return ui.config(parts[0], parts[1], parts[2])

def list_menu(menu, level=0):
    menus = []
    for entry in menu:
        if entry.isSubmenu():
            menus.append('%dSUBMENU' % level)
            menus.extend(list_menu(entry.menus, level+1))
        else:
            menus.append('%d%s' % (level, entry.hgcmd))
    return menus

def hg_state(ui = None):
    """
reads commands on stdin and returns the result on stdout
each line input results in one line output

input                                 -> output
------------------------------------------------------
abspath                               -> s=state
=                                     -> u=config section;...
=section                              -> u=config name=config value;...
=section;name[;default]               -> u=config value
:repopath[;section[;name[;default]]]  -> u=config of repo (see above)
;cwd[;file]...                        -> m=menu for paths
,srcpath;destpath                     -> m=drag menu
"""

    if ui is None:
        import mercurial.ui
        ui = mercurial.ui.ui()

    stdin = sys.stdin
    stdout = sys.stdout
    stderr = sys.stderr

    if ui.debugflag or ui.verbose:
        ui.updateopts(interactive=False)
        sys.stdout = stderr
    else:
        ui.updateopts(quiet=True, interactive=False)
        sys.stdout = nullwrite()

    menus = None

    while True:
        try:
            line = stdin.readline()
            if line == '':
                break
            line = line.strip() # remove \n
            if os.path.isabs(line):
                stdout.write('s=' + cachethg.get_states(line) + '\n')
                continue
            l = line and line[0] or ''
            if l == '=':
                stdout.write('u=' + get_config(ui, line[1:]) + '\n')
            elif l == ':':
                parts = line[1:].split(';', 2)
                repo = mercurial.hg.repository(ui, parts[0])
                if len(parts) == 1:
                    parts.append('')
                stdout.write('u=' + get_config(repo.ui, parts[1]) + '\n')
                repo = None
            elif l == ';':
                menus = menus or menuthg.menuThg()
                files = line[1:].split(';')
                states = cachethg.get_states(files[0])
                if not states or states == ' ':
                    menu = menus.get_norepo_commands(files[0], files[1:])
                else:
                    repo = menuthg.open_repo(files[0])
                    menu = menus.get_commands(repo, files[0], files[1:])
                stdout.write('m=' + ';'.join(list_menu(menu)) + '\n')
                del menu
            elif l == ',':
                menus = menus or menuthg.menuThg()
                files = line[1:].split(';')
                menu = menus.get_commands_dragdrop(files[:-1], files[-1])
                stdout.write('m=' + ';'.join(list_menu(menu)) + '\n')
                del menu
            elif l == '?':
                def yn(val):
                    return val and 'y' or 'n'
                stdout.write('?=cache:%s;menu:%s\n' % (yn(cachethg), yn(menuthg)))
            elif l == '':
                stdout.write('\n')
            else:
                stdout.write('i=\n')
                stderr.write('Invalid: ' + line + '\n')
        except Exception, e:
            stdout.write('e=\n')
            stderr.write(repr(e)+'\n')

    sys.stdout = stdout

cmdtable = {
    'hg_state':
        (hg_state, []),
}

if __name__ == '__main__':
    hg_state()
else:
    import mercurial.commands
    mercurial.commands.norepo += " hg_state"
------------------------------------------------------------------------------
_______________________________________________
Tortoisehg-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tortoisehg-develop

Reply via email to