# Path: opennode/oms/endpoint/ssh/cmd/directives.py
# class command(martian.Directive):
#     """Use this directive in a class in order to set its command name.
#     Only classes marked with this directive will be valid commands.
# 
#     """
# 
#     scope = martian.CLASS
#     store = martian.ONCE
#     default = None
# 
# Path: opennode/oms/endpoint/ssh/cmd/security.py
# def require_admins_only(f):
#     @functools.wraps(f)
#     def _require_admins_only(self, args):
#         principals = map(lambda p: p.id, effective_principals(self.user))
# 
#         if 'admins' not in principals:
#             self.write('Permission denied: admins not in effective permissions: %s\n'
#                        % ', '.join(principals))
#             return
# 
#         return f(self, args)
#     return _require_admins_only


import datetime
from grokcore.component import implements
from zope.security.proxy import removeSecurityProxy
from opennode.oms.endpoint.ssh.cmd.base import Cmd
from opennode.oms.endpoint.ssh.cmd.directives import command
from opennode.oms.endpoint.ssh.cmd.security import require_admins_only
from opennode.oms.endpoint.ssh.cmdline import ICmdArgumentsSyntax, VirtualConsoleArgumentParser
from opennode.oms.model.model.base import IIncomplete
from opennode.oms.zodb import db
                except TypeError as e:
                    pass
                except AttributeError as e:
                    pass
                yield (key, 'Error: "%s"' % e)

    def _do_dbcat(self, obj, filename=None):
        data = list(self._get_data(obj))
        max_title_len = max(len(title) for title, _ in data)

        for title, value in data:
            if isinstance(value, dict):
                # security proxies don't mimic tuple() perfectly
                # thus cannot be passed to "%" directly
                pretty_value = ', '.join(['%s:%s' % tuple(i) for i in value.items()])
            elif hasattr(value, '__iter__'):
                strings = [str(i) for i in value]
                if not isinstance(value, tuple):
                    strings = sorted(strings)
                pretty_value = ', '.join(strings)
            elif title in ('mtime', 'ctime') and isinstance(value, float):
                pretty_value = datetime.datetime.fromtimestamp(value).isoformat()
            else:
                pretty_value = value

            self.write("%s\t%s\n" % ((title.encode('utf8') + ':').ljust(max_title_len),
                                     str(pretty_value).encode('utf8')))

        if IIncomplete.providedBy(obj):
            self.write("-----------------\n")

# Path: opennode/oms/endpoint/ssh/cmd/directives.py
# class command(martian.Directive):
#     """Use this directive in a class in order to set its command name.
#     Only classes marked with this directive will be valid commands.
# 
#     """
# 
#     scope = martian.CLASS
#     store = martian.ONCE
#     default = None
# 
# Path: opennode/oms/endpoint/ssh/cmd/security.py
# def require_admins_only(f):
#     @functools.wraps(f)
#     def _require_admins_only(self, args):
#         principals = map(lambda p: p.id, effective_principals(self.user))
# 
#         if 'admins' not in principals:
#             self.write('Permission denied: admins not in effective permissions: %s\n'
#                        % ', '.join(principals))
#             return
# 
#         return f(self, args)
#     return _require_admins_only


# Path: opennode/oms/endpoint/ssh/cmd/dbadmin.py
import datetime
from grokcore.component import implements
from zope.security.proxy import removeSecurityProxy
from opennode.oms.endpoint.ssh.cmd.base import Cmd
from opennode.oms.endpoint.ssh.cmd.directives import command
from opennode.oms.endpoint.ssh.cmd.security import require_admins_only
from opennode.oms.endpoint.ssh.cmdline import ICmdArgumentsSyntax, VirtualConsoleArgumentParser
from opennode.oms.model.model.base import IIncomplete
from opennode.oms.zodb import db

                except TypeError as e:
                    pass
                except AttributeError as e:
                    pass
                yield (key, 'Error: "%s"' % e)

    def _do_dbcat(self, obj, filename=None):
        data = list(self._get_data(obj))
        max_title_len = max(len(title) for title, _ in data)

        for title, value in data:
            if isinstance(value, dict):
                # security proxies don't mimic tuple() perfectly
                # thus cannot be passed to "%" directly
                pretty_value = ', '.join(['%s:%s' % tuple(i) for i in value.items()])
            elif hasattr(value, '__iter__'):
                strings = [str(i) for i in value]
                if not isinstance(value, tuple):
                    strings = sorted(strings)
                pretty_value = ', '.join(strings)
            elif title in ('mtime', 'ctime') and isinstance(value, float):
                pretty_value = datetime.datetime.fromtimestamp(value).isoformat()
            else:
                pretty_value = value

            self.write("%s\t%s\n" % ((title.encode('utf8') + ':').ljust(max_title_len),
                                     str(pretty_value).encode('utf8')))

        if IIncomplete.providedBy(obj):
            self.write("-----------------\n")

            self.write("This %s is incomplete.\n" % (type(removeSecurityProxy(obj)).__name__))