<s> import re <EOL> from datetime import datetime <EOL> import os <EOL> import time <EOL> import subprocess <EOL> from ajenti . api import * <EOL> from ajenti . plugins . main . api import SectionPlugin <EOL> from ajenti . plugins . services . api import ServiceMultiplexor <EOL> from ajenti . ui import on <EOL> from ajenti . ui . binder import Binder <EOL> from ajenti . util import platform_select <EOL> from reconfigure . configs . base import Reconfig <EOL> from reconfigure . parsers import SSVParser <EOL> from reconfigure . builders import BoundBuilder <EOL> from reconfigure . nodes import Node , PropertyNode <EOL> from reconfigure . items . bound import BoundData <EOL> open_ntpd_conf = '<STR_LIT>' <EOL> ntpd_conf = '<STR_LIT>' <EOL> def isopenntpd ( ) : <EOL> return not ServiceMultiplexor . get ( ) . get_one ( '<STR_LIT>' ) is None <EOL> class NTPDData ( BoundData ) : <EOL> pass <EOL> class ServerData ( BoundData ) : <EOL> def template ( self ) : <EOL> return Node ( <EOL> '<STR_LIT>' , <EOL> Node ( '<STR_LIT>' , PropertyNode ( '<STR_LIT:value>' , '<STR_LIT>' ) ) , <EOL> Node ( '<STR_LIT>' , PropertyNode ( '<STR_LIT:value>' , '<STR_LIT>' ) ) , <EOL> ) <EOL> NTPDData . bind_collection ( '<STR_LIT>' , selector = lambda x : x . children [ <NUM_LIT:0> ] . get ( '<STR_LIT:value>' ) . value == '<STR_LIT>' , <EOL> item_class = ServerData ) <EOL> ServerData . bind_property ( '<STR_LIT:value>' , '<STR_LIT:address>' , path = lambda x : x . children [ <NUM_LIT:1> ] ) <EOL> class NTPDConfig ( Reconfig ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> k = { <EOL> '<STR_LIT>' : SSVParser ( ) , <EOL> '<STR_LIT>' : BoundBuilder ( NTPDData ) , <EOL> } <EOL> k . update ( kwargs ) <EOL> Reconfig . __init__ ( self , ** k ) <EOL> @ plugin <EOL> class NTPDPlugin ( SectionPlugin ) : <EOL> def get_tz_debian ( self ) : <EOL> return open ( '<STR_LIT>' ) . read ( ) . strip ( ) <EOL> def get_tz_nondebian ( self ) : <EOL> return os . path . realpath ( '<STR_LIT>' ) [ len ( '<STR_LIT>' ) : ] if os . path . islink ( <EOL> '<STR_LIT>' ) else '<STR_LIT>' <EOL> def set_tz_debian ( self , timezone ) : <EOL> open ( '<STR_LIT>' , '<STR_LIT:w>' ) . write ( timezone + '<STR_LIT:\n>' ) <EOL> def set_tz_nondebian ( self , timezone ) : <EOL> tz = os . path . join ( '<STR_LIT>' , timezone ) <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> os . symlink ( tz , '<STR_LIT>' ) <EOL> openntpd = isopenntpd ( ) <EOL> service_name = platform_select ( <EOL> default = '<STR_LIT>' if openntpd else '<STR_LIT>' , <EOL> centos = '<STR_LIT>' if openntpd else '<STR_LIT>' , <EOL> mageia = '<STR_LIT>' , <EOL> ) <EOL> get_tz = platform_select ( <EOL> debian = get_tz_debian , <EOL> default = get_tz_nondebian , <EOL> ) <EOL> set_tz = platform_select ( <EOL> debian = set_tz_debian , <EOL> default = set_tz_nondebian , <EOL> ) <EOL> def init ( self ) : <EOL> self . title = _ ( '<STR_LIT>' ) <EOL> self . icon = '<STR_LIT:time>' <EOL> self . category = _ ( '<STR_LIT>' ) <EOL> self . append ( self . ui . inflate ( '<STR_LIT>' ) ) <EOL> self . find ( '<STR_LIT>' ) . name = self . service_name <EOL> self . find ( '<STR_LIT>' ) . reload ( ) <EOL> self . config = NTPDConfig ( path = platform_select ( <EOL> default = open_ntpd_conf if self . openntpd else ntpd_conf , <EOL> centos = '<STR_LIT>' if self . openntpd else ntpd_conf , <EOL> freebsd = '<STR_LIT>' if self . openntpd else ntpd_conf , <EOL> ) ) <EOL> self . binder = Binder ( None , self ) <EOL> self . available_zones = [ ] <EOL> for d , dirs , files in os . walk ( '<STR_LIT>' , followlinks = False ) : <EOL> for f in files : <EOL> if f != '<STR_LIT>' : <EOL> self . available_zones . append ( os . path . join ( d , f ) ) <EOL> self . available_zones = [ x [ len ( '<STR_LIT>' ) : ] for x in self . available_zones ] <EOL> self . available_zones . sort ( ) <EOL> self . find ( '<STR_LIT>' ) . new_item = lambda c : ServerData ( ) <EOL> def on_page_load ( self ) : <EOL> self . refresh ( ) <EOL> def refresh ( self ) : <EOL> self . config . load ( ) <EOL> self . now = int ( time . time ( ) ) <EOL> self . timezone = self . get_tz ( ) <EOL> self . binder . setup ( self ) . populate ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_set ( self ) : <EOL> self . binder . update ( ) <EOL> d = datetime . fromtimestamp ( self . now ) <EOL> s = d . strftime ( '<STR_LIT>' ) <EOL> self . set_tz ( self . timezone ) <EOL> subprocess . call ( [ '<STR_LIT:date>' , s ] ) <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_sync ( self ) : <EOL> self . binder . update ( ) <EOL> if len ( self . config . tree . servers ) == <NUM_LIT:0> : <EOL> self . context . notify ( '<STR_LIT:error>' , _ ( '<STR_LIT>' ) ) <EOL> return <EOL> server = self . config . tree . servers [ <NUM_LIT:0> ] . address <EOL> output = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' , server ] ) <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> self . context . notify ( '<STR_LIT:info>' , output ) <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> self . binder . update ( ) <EOL> self . config . save ( ) <EOL> self . refresh ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> ServiceMultiplexor . get ( ) . get_one ( self . service_name ) . restart ( ) </s>
