try: import resource except ImportError: pass # Will fail on Win32 systems import time from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ from debug_toolbar.panels import DebugPanel class TimerDebugPanel(DebugPanel): """ Panel that displays the time a response took in milliseconds. """ name = 'Timer' try: # if resource module not available, don't show content panel resource except NameError: has_content = False has_resource = False else: has_content = True has_resource = True def process_request(self, request): self._start_time = time.time() if self.has_resource: self._start_rusage = resource.getrusage(resource.RUSAGE_SELF) def process_response(self, request, response): self.total_time = (time.time() - self._start_time) * 1000 if self.has_resource: self._end_rusage = resource.getrusage(resource.RUSAGE_SELF) def nav_title(self): return _('Time') def nav_subtitle(self): # TODO l10n if self.has_resource: utime = self._end_rusage.ru_utime - self._start_rusage.ru_utime stime = self._end_rusage.ru_stime - self._start_rusage.ru_stime return 'CPU: %0.2fms (%0.2fms)' % ((utime + stime) * 1000.0, self.total_time) else: return 'TOTAL: %0.2fms' % (self.total_time) def title(self): return _('Resource Usage') def url(self): return '' def _elapsed_ru(self, name): return getattr(self._end_rusage, name) - getattr(self._start_rusage, name) def content(self): utime = 1000 * self._elapsed_ru('ru_utime') stime = 1000 * self._elapsed_ru('ru_stime') vcsw = self._elapsed_ru('ru_nvcsw') ivcsw = self._elapsed_ru('ru_nivcsw') minflt = self._elapsed_ru('ru_minflt') majflt = self._elapsed_ru('ru_majflt') # these are documented as not meaningful under Linux. If you're running BSD # feel free to enable them, and add any others that I hadn't gotten to before # I noticed that I was getting nothing but zeroes and that the docs agreed. :-( # # blkin = self._elapsed_ru('ru_inblock') # blkout = self._elapsed_ru('ru_oublock') # swap = self._elapsed_ru('ru_nswap') # rss = self._end_rusage.ru_maxrss # srss = self._end_rusage.ru_ixrss # urss = self._end_rusage.ru_idrss # usrss = self._end_rusage.ru_isrss # TODO l10n on values rows = ( (_('User CPU time'), '%0.3f msec' % utime), (_('System CPU time'), '%0.3f msec' % stime), (_('Total CPU time'), '%0.3f msec' % (utime + stime)), (_('Elapsed time'), '%0.3f msec' % self.total_time), (_('Context switches'), '%d voluntary, %d involuntary' % (vcsw, ivcsw)), # ('Memory use', '%d max RSS, %d shared, %d unshared' % (rss, srss, urss + usrss)), # ('Page faults', '%d no i/o, %d requiring i/o' % (minflt, majflt)), # ('Disk operations', '%d in, %d out, %d swapout' % (blkin, blkout, swap)), ) context = self.context.copy() context.update({ 'rows': rows, }) return render_to_string('debug_toolbar/panels/timer.html', context) n13' href='#n13'>13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
use strict;
use Irssi 20020101.0250 ();

use vars qw($VERSION %IRSSI);
$VERSION = "0.2";
%IRSSI = (
    authors     => "Ian Peters",
    contact     => "itp\@ximian.com",
    name        => "Connect Command",
    description => "run arbitrary shell commands while [dis]connecting to a server",
    license     => "Public Domain",
    url         => "http://irssi.org/",
    changed     => "2017-03-18"
);

my %preconn_actions;
my %postconn_actions;
my %disconn_actions;

sub load_actions {
  my $fi;

  open $fi, '<', "$ENV{HOME}/.irssi/connectcmd_actions";

  while (<$fi>) {
    my @lines = split "\n";
    foreach my $line (@lines) {
      my ($server, $type, $action) = split ":", $line;
      if ($type eq "preconn") {
	      $preconn_actions{$server} = $action;
      } elsif ($type eq "postconn") {
	      $postconn_actions{$server} = $action;
      } elsif ($type eq "disconn") {
	      $disconn_actions{$server} = $action;
      }
    }
  }

  close $fi;
}

sub save_actions {
  my $fa;
  open $fa, q{>}, "$ENV{HOME}/.irssi/connectcmd_actions";

  foreach my $server (keys %preconn_actions) {
    print $fa "$server:preconn:$preconn_actions{$server}\n";
  }
  foreach my $server (keys %postconn_actions) {
    print $fa "$server:postconn:$postconn_actions{$server}\n";
  }
  foreach my $server (keys %disconn_actions) {
    print $fa "$server:disconn:$disconn_actions{$server}\n";
  }

  close $fa;
}

sub sig_server_looking {
  my ($server) = @_;

  if (my $action = $preconn_actions{$server->{'address'}}) {
    system ($action);
  }
}

sub sig_server_connected {
  my ($server) = @_;

  if (my $action = $postconn_actions{$server->{'address'}}) {
    system ($action);
  }
}

sub sig_server_disconnected {
  my ($server) = @_;

  if (my $action = $disconn_actions{$server->{'address'}}) {
    system ($action);
  }
}

sub cmd_connectcmd {
  my ($data, $server, $witem) = @_;
  
  #my ($op, $type, $server, $action) = split " ", $data;
  $data =~ m/^(\S*)\s+(\S*)\s+(\S*)\s+(.*)$/;
  my $op=$1;
  my $type=$2;
  my $server=$3;
  my $action=$4;

  $op = lc $op;

  if (!$op) {
    Irssi::print ("No operation given");
  } elsif ($op eq "add") {
    if (!$type) {
      Irssi::print ("Type not specified [preconn|postconn|disconn]");
    } elsif (!$server) {
      Irssi::print ("Server not specified");
    } elsif (!$action) {
      Irssi::print ("Action not specified");
    } else {
      if ($type eq "preconn") {
	$preconn_actions{$server} = $action;
	Irssi::print ("Added preconnect action of $action on $server");
	save_actions;
      } elsif ($type eq "postconn") {
	$postconn_actions{$server} = $action;
	Irssi::print ("Added postconnect action of $action on $server");
	save_actions;
      } elsif ($type eq "disconn") {
	$disconn_actions{$server} = $action;
	Irssi::print ("Added disconnect action of $action on $server");
	save_actions;
      } else {
	Irssi::print ("Unrecognized trigger $type [preconn|postconn|disconn]");
      }
    }
  } elsif ($op eq "remove") {
    if (!$type) {
      Irssi::print ("Type not specified [preconn|postconn|disconn]");
    } elsif (!$server) {
      Irssi::print ("Server not specified");
    } else {
      if ($type eq "preconn") {
	delete ($preconn_actions{$server});
	Irssi::print ("Removed preconnect action on $server");
	save_actions;
      } elsif ($type eq "postconn") {
	delete ($postconn_actions{$server});
	Irssi::print ("Removed postconnect action on $server");
	save_actions;
      } elsif ($type eq "disconn") {
	delete ($disconn_actions{$server});
	Irssi::print ("Removed disconnect action on $server");
	save_actions;
      } else {
	Irssi::print ("Unrecognized trigger $type [preconn|postconn|disconn]");
      }
    }
  } elsif ($op eq "list") {
    Irssi::print ("Preconnect Actions:");
    foreach my $server (keys %preconn_actions) {
      Irssi::print ("$server  $preconn_actions{$server}");
    }
    Irssi::print ("Postconnect Actions:");
    foreach my $server (keys %postconn_actions) {
      Irssi::print ("$server  $postconn_actions{$server}");
    }
    Irssi::print ("Disconnect Actions:");
    foreach my $server (keys %disconn_actions) {
      Irssi::print ("$server  $disconn_actions{$server}");
    }
  }
}

load_actions();

Irssi::command_bind ('connectcmd', 'cmd_connectcmd');

Irssi::signal_add ('server looking', 'sig_server_looking');
Irssi::signal_add ('server connected', 'sig_server_connected');
Irssi::signal_add ('server disconnected', 'sig_server_disconnected');