# Path: yubiadmin/util/app.py
# class App(object):
#     sections = []
#     priority = 50
# 
#     @property
#     def name(self):
#         self.__class__.name = sys.modules[self.__module__].__file__ \
#             .split('/')[-1].rsplit('.', 1)[0]
#         return self.name
# 
#     def __call__(self, request):
#         section_name = request.path_info_pop()
# 
#         if not section_name:
#             return self.redirect('/%s/%s' % (self.name, self.sections[0]))
# 
#         if not hasattr(self, section_name):
#             raise exc.HTTPNotFound
# 
#         sections = [{
#             'name': section,
#             'title': (getattr(self, section).__doc__ or section.capitalize()
#                       ).strip(),
#             'active': section == section_name,
#             'advanced': bool(getattr(getattr(self, section), 'advanced',
#                                      False))
#         } for section in self.sections]
# 
#         request.environ['yubiadmin.response'].extend('content', render(
#             'app_base',
#             name=self.name,
#             sections=sections,
#             title='YubiAdmin - %s - %s' % (self.name, section_name)
#         ))
# 
#         resp = getattr(self, section_name)(request)
#         if isinstance(resp, Response):
#             return resp
#         request.environ['yubiadmin.response'].extend('page', resp)
# 
#     def redirect(self, url):
#         raise exc.HTTPSeeOther(location=url)
# 
#     def render_forms(self, request, forms, template='form',
#                      success_msg='Settings updated!', **kwargs):
#         alerts = []
#         if not request.params:
#             for form in filter(lambda x: hasattr(x, 'load'), forms):
#                 form.load()
#         else:
#             errors = False
#             for form in forms:
#                 form.process(request.params)
#                 errors = not form.validate() or errors
#             if not errors:
#                 try:
#                     if success_msg:
#                         alerts = [{'type': 'success', 'title': success_msg}]
#                     for form in filter(lambda x: hasattr(x, 'save'), forms):
#                         form.save()
#                 except Exception as e:
#                     alerts = [{'type': 'error', 'title': 'Error:',
#                                'message': str(e)}]
#             else:
#                 alerts = [{'type': 'error', 'title': 'Invalid data!'}]
# 
#         return render(template, target=request.path, fieldsets=forms,
#                       alerts=alerts, **kwargs)
# 
# def render(tmpl, **kwargs):
#     return TemplateBinding(tmpl, **kwargs)
# 
# Path: yubiadmin/util/system.py
# def run(cmd):
#     p = subprocess.Popen(['sh', '-c', cmd], stdout=subprocess.PIPE,
#                          stderr=subprocess.PIPE)
#     output = p.communicate()
#     return p.returncode, output[0]
# 
# Path: yubiadmin/apps/dashboard.py
# def panel(title, content, link=None, level=None):
#     return {
#         'title': title,
#         'content': content,
#         'link': link,
#         'level': level
#     }


import os
import subprocess
from webob import Response
from threading import Timer
from yubiadmin.util.app import App, render
from yubiadmin.util.system import run
from yubiadmin.apps.dashboard import panel
                'There are <strong>%d</strong> updates available' % updates,
                '/%s/general' % self.name,
                'info'
            )

        _, time = run('date "+%a, %d %b %Y %H:%M"')
        _, result = run('uptime')
        rest = [x.strip() for x in result.split('up', 1)][1]
        parts = [x.strip() for x in rest.split(',')]
        uptime = parts[0] if not 'days' in parts[0] else '%s, %s' % \
            tuple(parts[:2])
        yield panel('System', 'Date: %s<br />Uptime: %s' %
                   (time, uptime), level='info')

    def general(self, request):
        alerts = []
        if needs_restart():
            alerts.append({'message': 'The machine needs to reboot.',
                           'type': 'error'})
        return render('/sys/general', alerts=alerts, updates=get_updates())

    def update(self, request):
        run('apt-get update')
        return self.redirect('/sys')

    def dist_upgrade(self, request):
        if get_updates():
            return Response(app_iter=Updater())
        else:
            alerts = [{'message': 'Software is up to date!'}]

# Path: yubiadmin/util/app.py
# class App(object):
#     sections = []
#     priority = 50
# 
#     @property
#     def name(self):
#         self.__class__.name = sys.modules[self.__module__].__file__ \
#             .split('/')[-1].rsplit('.', 1)[0]
#         return self.name
# 
#     def __call__(self, request):
#         section_name = request.path_info_pop()
# 
#         if not section_name:
#             return self.redirect('/%s/%s' % (self.name, self.sections[0]))
# 
#         if not hasattr(self, section_name):
#             raise exc.HTTPNotFound
# 
#         sections = [{
#             'name': section,
#             'title': (getattr(self, section).__doc__ or section.capitalize()
#                       ).strip(),
#             'active': section == section_name,
#             'advanced': bool(getattr(getattr(self, section), 'advanced',
#                                      False))
#         } for section in self.sections]
# 
#         request.environ['yubiadmin.response'].extend('content', render(
#             'app_base',
#             name=self.name,
#             sections=sections,
#             title='YubiAdmin - %s - %s' % (self.name, section_name)
#         ))
# 
#         resp = getattr(self, section_name)(request)
#         if isinstance(resp, Response):
#             return resp
#         request.environ['yubiadmin.response'].extend('page', resp)
# 
#     def redirect(self, url):
#         raise exc.HTTPSeeOther(location=url)
# 
#     def render_forms(self, request, forms, template='form',
#                      success_msg='Settings updated!', **kwargs):
#         alerts = []
#         if not request.params:
#             for form in filter(lambda x: hasattr(x, 'load'), forms):
#                 form.load()
#         else:
#             errors = False
#             for form in forms:
#                 form.process(request.params)
#                 errors = not form.validate() or errors
#             if not errors:
#                 try:
#                     if success_msg:
#                         alerts = [{'type': 'success', 'title': success_msg}]
#                     for form in filter(lambda x: hasattr(x, 'save'), forms):
#                         form.save()
#                 except Exception as e:
#                     alerts = [{'type': 'error', 'title': 'Error:',
#                                'message': str(e)}]
#             else:
#                 alerts = [{'type': 'error', 'title': 'Invalid data!'}]
# 
#         return render(template, target=request.path, fieldsets=forms,
#                       alerts=alerts, **kwargs)
# 
# def render(tmpl, **kwargs):
#     return TemplateBinding(tmpl, **kwargs)
# 
# Path: yubiadmin/util/system.py
# def run(cmd):
#     p = subprocess.Popen(['sh', '-c', cmd], stdout=subprocess.PIPE,
#                          stderr=subprocess.PIPE)
#     output = p.communicate()
#     return p.returncode, output[0]
# 
# Path: yubiadmin/apps/dashboard.py
# def panel(title, content, link=None, level=None):
#     return {
#         'title': title,
#         'content': content,
#         'link': link,
#         'level': level
#     }


# Path: yubiadmin/apps/sys.py
import os
import subprocess
from webob import Response
from threading import Timer
from yubiadmin.util.app import App, render
from yubiadmin.util.system import run
from yubiadmin.apps.dashboard import panel

                'There are <strong>%d</strong> updates available' % updates,
                '/%s/general' % self.name,
                'info'
            )

        _, time = run('date "+%a, %d %b %Y %H:%M"')
        _, result = run('uptime')
        rest = [x.strip() for x in result.split('up', 1)][1]
        parts = [x.strip() for x in rest.split(',')]
        uptime = parts[0] if not 'days' in parts[0] else '%s, %s' % \
            tuple(parts[:2])
        yield panel('System', 'Date: %s<br />Uptime: %s' %
                   (time, uptime), level='info')

    def general(self, request):
        alerts = []
        if needs_restart():
            alerts.append({'message': 'The machine needs to reboot.',
                           'type': 'error'})
        return render('/sys/general', alerts=alerts, updates=get_updates())

    def update(self, request):
        run('apt-get update')
        return self.redirect('/sys')

    def dist_upgrade(self, request):
        if get_updates():
            return Response(app_iter=Updater())
        else:
            alerts = [{'message': 'Software is up to date!'}]

            return render('/sys/general', alerts=alerts)