# 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
    """ Renders objects stored in ZODB by their OID (optionally also transaction ID),
    in case when POSKeyErrors appear. Helps troubleshoot ZODB storage issues. """
    implements(ICmdArgumentsSyntax)

    command('dbcat')

    def arguments(self):
        parser = VirtualConsoleArgumentParser()
        parser.add_argument('oids', nargs='+', help="List of OIDs to retrieve from DB")
        parser.add_argument('--tid', nargs='?', help="Transaction ID which the objects to retrieve for")
        return parser

    @require_admins_only
    @db.ro_transact
    def execute(self, args):
        for oid in args.oids:
            try:
                obj = db.load_object(oid, args.tid)
            except Exception:
                self.write("No such object: %s\n" % oid)
            else:
                self._do_dbcat(obj, None)

    def _get_data(self, obj):
        for key in dir(obj):
            if key in ('__doc__',
                       '__dict__',
                       '_p_oid',
                       '_p_changed',
                       '_p_estimated_size',

# 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

    """ Renders objects stored in ZODB by their OID (optionally also transaction ID),
    in case when POSKeyErrors appear. Helps troubleshoot ZODB storage issues. """
    implements(ICmdArgumentsSyntax)

    command('dbcat')

    def arguments(self):
        parser = VirtualConsoleArgumentParser()
        parser.add_argument('oids', nargs='+', help="List of OIDs to retrieve from DB")
        parser.add_argument('--tid', nargs='?', help="Transaction ID which the objects to retrieve for")
        return parser

    @require_admins_only
    @db.ro_transact
    def execute(self, args):
        for oid in args.oids:
            try:
                obj = db.load_object(oid, args.tid)
            except Exception:
                self.write("No such object: %s\n" % oid)
            else:
                self._do_dbcat(obj, None)

    def _get_data(self, obj):
        for key in dir(obj):
            if key in ('__doc__',
                       '__dict__',
                       '_p_oid',
                       '_p_changed',
                       '_p_estimated_size',

                       '_p_jar',