<s> import gevent <EOL> import logging <EOL> import random <EOL> import traceback <EOL> import ajenti <EOL> from ajenti . api import * <EOL> from ajenti . api . sensors import Sensor <EOL> from ajenti . ui . binder import CollectionAutoBinding <EOL> from ajenti . ui import on , UIElement , p <EOL> from ajenti . users import UserManager , PermissionProvider <EOL> from ajenti . plugins . main . api import SectionPlugin , intent <EOL> from api import DashboardWidget <EOL> from updater import AjentiUpdater <EOL> @ plugin <EOL> class Dash ( SectionPlugin ) : <EOL> default_classconfig = { '<STR_LIT>' : [ ] } <EOL> def init ( self ) : <EOL> self . title = _ ( '<STR_LIT>' ) <EOL> self . category = '<STR_LIT>' <EOL> self . icon = '<STR_LIT>' <EOL> self . order = <NUM_LIT:0> <EOL> self . append ( self . ui . inflate ( '<STR_LIT>' ) ) <EOL> self . dash = self . find ( '<STR_LIT>' ) <EOL> self . dash . _section = self <EOL> self . dash . on ( '<STR_LIT>' , self . on_reorder ) <EOL> self . allow_updates = True <EOL> self . autorefresh = False <EOL> self . find ( '<STR_LIT>' ) . platform = ajenti . platform_unmapped <EOL> self . find ( '<STR_LIT>' ) . distro = ajenti . platform_string <EOL> def post_widget_bind ( o , c , i , u ) : <EOL> u . find ( '<STR_LIT>' ) . on ( '<STR_LIT>' , self . on_add_widget_click , i ) <EOL> self . find ( '<STR_LIT>' ) . post_item_bind = post_widget_bind <EOL> classes = [ <EOL> x for x in DashboardWidget . get_classes ( ) <EOL> if not x . hidden and <EOL> UserManager . get ( ) . has_permission ( self . context , WidgetPermissions . name_for ( x ) ) <EOL> ] <EOL> CollectionAutoBinding ( <EOL> sorted ( classes , key = lambda x : x . name ) , <EOL> None , self . find ( '<STR_LIT>' ) ) . populate ( ) <EOL> self . context . session . spawn ( self . worker ) <EOL> AjentiUpdater . get ( ) . check_for_updates ( self . update_check_callback ) <EOL> def worker ( self ) : <EOL> while True : <EOL> if self . active and self . autorefresh and self . allow_updates : <EOL> self . refresh ( ) <EOL> gevent . sleep ( <NUM_LIT:5> ) <EOL> def update_check_callback ( self , updates ) : <EOL> logging . debug ( '<STR_LIT>' % updates ) <EOL> self . find ( '<STR_LIT>' ) . visible = ( updates != [ ] ) and self . context . session . identity == '<STR_LIT:root>' <EOL> def install_updates ( self , updates ) : <EOL> AjentiUpdater . get ( ) . run_update ( updates ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def install_update ( self ) : <EOL> AjentiUpdater . get ( ) . check_for_updates ( self . install_updates ) <EOL> def on_page_load ( self ) : <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_refresh ( self ) : <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_auto_refresh ( self ) : <EOL> self . autorefresh = not self . autorefresh <EOL> self . find ( '<STR_LIT>' ) . pressed = self . autorefresh <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_dialog_open ( self ) : <EOL> self . find ( '<STR_LIT>' ) . visible = True <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_dialog_close ( self , button ) : <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> def on_add_widget_click ( self , cls , config = None ) : <EOL> self . add_widget ( cls , config ) <EOL> def add_widget ( self , cls , config = None ) : <EOL> self . find ( '<STR_LIT>' ) . visible = False <EOL> self . classconfig [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:class>' : cls . classname , <EOL> '<STR_LIT>' : random . randrange ( <NUM_LIT:0> , <NUM_LIT:2> ) , <EOL> '<STR_LIT:index>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : config , <EOL> } ) <EOL> self . save_classconfig ( ) <EOL> self . refresh ( ) <EOL> @ intent ( '<STR_LIT>' ) <EOL> def add_widget_intent ( self , cls = None , config = None ) : <EOL> self . add_widget ( cls , config ) <EOL> self . activate ( ) <EOL> def refresh ( self ) : <EOL> self . find ( '<STR_LIT>' ) . hostname = Sensor . find ( '<STR_LIT>' ) . value ( ) <EOL> self . dash . empty ( ) <EOL> for widget in self . classconfig [ '<STR_LIT>' ] : <EOL> for cls in DashboardWidget . get_classes ( ) : <EOL> if cls . classname == widget [ '<STR_LIT:class>' ] : <EOL> if not UserManager . get ( ) . has_permission ( self . context , WidgetPermissions . name_for ( cls ) ) : <EOL> continue <EOL> try : <EOL> instance = cls . new ( <EOL> self . ui , <EOL> container = widget [ '<STR_LIT>' ] , <EOL> index = widget [ '<STR_LIT:index>' ] , <EOL> config = widget [ '<STR_LIT>' ] , <EOL> ) <EOL> except Exception as e : <EOL> traceback . print_exc ( ) <EOL> instance = CrashedWidget . new ( <EOL> self . ui , <EOL> container = widget [ '<STR_LIT>' ] , <EOL> index = widget [ '<STR_LIT:index>' ] , <EOL> config = widget [ '<STR_LIT>' ] , <EOL> ) <EOL> instance . classname = cls . classname <EOL> instance . set ( e ) <EOL> instance . on ( '<STR_LIT>' , self . on_widget_config , widget , instance ) <EOL> instance . on ( '<STR_LIT>' , self . on_widget_reconfigure , widget , instance ) <EOL> instance . on ( '<STR_LIT>' , self . on_widget_delete , widget , instance ) <EOL> self . dash . append ( instance ) <EOL> def on_widget_config ( self , config , instance ) : <EOL> config [ '<STR_LIT>' ] = instance . config <EOL> self . save_classconfig ( ) <EOL> self . refresh ( ) <EOL> def on_widget_reconfigure ( self , widget , instance ) : <EOL> instance . begin_configuration ( ) <EOL> def on_widget_delete ( self , widget , instance ) : <EOL> self . classconfig [ '<STR_LIT>' ] . remove ( widget ) <EOL> self . save_classconfig ( ) <EOL> self . refresh ( ) <EOL> def on_reorder ( self , indexes ) : <EOL> if len ( indexes ) == <NUM_LIT:0> : <EOL> return <EOL> cfg = { '<STR_LIT>' : [ ] } <EOL> for container , items in indexes . iteritems ( ) : <EOL> idx = <NUM_LIT:0> <EOL> for item in items : <EOL> item = self . dash . find_uid ( item ) <EOL> if item : <EOL> item . container = container <EOL> item . index = idx <EOL> idx += <NUM_LIT:1> <EOL> cfg [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:class>' : item . classname , <EOL> '<STR_LIT>' : item . container , <EOL> '<STR_LIT:index>' : item . index , <EOL> '<STR_LIT>' : item . config , <EOL> } ) <EOL> self . classconfig = cfg <EOL> self . save_classconfig ( ) <EOL> self . refresh ( ) <EOL> @ plugin <EOL> class DashboardDash ( UIElement ) : <EOL> typeid = '<STR_LIT>' <EOL> def on_drag_start ( self ) : <EOL> self . _section . allow_updates = False <EOL> def on_drag_stop ( self ) : <EOL> self . _section . allow_updates = True <EOL> @ p ( '<STR_LIT>' ) <EOL> @ p ( '<STR_LIT>' ) <EOL> @ p ( '<STR_LIT>' ) <EOL> @ plugin <EOL> class DashboardHeader ( UIElement ) : <EOL> typeid = '<STR_LIT>' <EOL> @ plugin <EOL> class CrashedWidget ( DashboardWidget ) : <EOL> hidden = True <EOL> def init ( self ) : <EOL> self . append ( self . ui . create ( '<STR_LIT:label>' , id = '<STR_LIT:text>' ) ) <EOL> def set ( self , e ) : <EOL> self . find ( '<STR_LIT:text>' ) . text = '<STR_LIT>' + str ( e ) <EOL> @ plugin <EOL> class WidgetPermissions ( PermissionProvider ) : <EOL> def get_name ( self ) : <EOL> return _ ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def name_for ( widget ) : <EOL> return '<STR_LIT>' % widget . __name__ <EOL> def get_permissions ( self ) : <EOL> return sorted ( [ <EOL> ( WidgetPermissions . name_for ( x ) , _ ( x . name ) ) <EOL> for x in DashboardWidget . get_classes ( ) <EOL> if not x . hidden <EOL> ] , key = lambda x : x [ <NUM_LIT:1> ] ) </s>
