source: epoptes/trunk/fuentes/epoptes/core/structs.py @ 295

Last change on this file since 295 was 295, checked in by mabarracus, 3 years ago

copy trusty epoptes code

File size: 3.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4###########################################################################
5# Classes for creating client and group objects.
6#
7# Copyright (C) 2011 Fotis Tsamis <ftsamis@gmail.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FINESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# On Debian GNU/Linux systems, the complete text of the GNU General
23# Public License can be found in `/usr/share/common-licenses/GPL".
24###########################################################################
25
26clients = []
27class Client:
28    def __init__(self, type='', mac='', hostname='', alias='', users={}, hsystem=''):
29        self.type = type
30        self.mac = mac.upper()
31        self.hostname = hostname
32        self.alias = alias
33        self.users = users.copy()
34        self.hsystem = hsystem
35        clients.append(self)
36   
37    def set_offline(self):
38        self.hsystem = ''
39        self.users = {}
40        self.type = 'offline'
41        self.hostname = ''
42   
43    def is_offline(self):
44        """Return True only if there is no system or session handle
45        for this client but there is a MAC address.
46        """
47        return not(self.users and self.hsystem) and self.mac
48       
49    def get_name(self):
50        """Return the alias of the client or the hostname if the
51        alias is not set, or if both are unset (offline clients)
52        return the MAC address.
53        """
54        return self.alias or self.hostname or self.mac
55       
56    def set_name(self, name):
57        self.alias = name
58       
59    def add_user(self, username, realname, handle):
60        self.users[handle] = {'uname' : username, 'rname' : realname}
61
62
63class Group:
64    def __init__(self, name="New group", members={}):
65        self.name = name
66        #{<instance> : {'x_pos':342, 'y_pos':112, 'size':600}}
67        self.members = members.copy()
68   
69    def get_members(self):
70        """Return a list with all the clients that are members of the group"""
71        return self.members.keys()
72   
73    def has_client(self, client):
74        """Check if a client is member of the group"""
75        return client in self.members
76   
77    def add_client(self, client, **props):
78        """Add a client to the group"""
79        self.members[client] = {}
80        self.set_properties(client, **props)
81   
82    def remove_client(self, client):
83        """Remove a client from the group"""
84        del self.members[client]
85   
86    def set_properties(self, client, **props):
87        """Set x_pos, y_pos or size property for a client on the group"""
88        for prop, value in props.iteritems():
89            if isinstance(value, (int, float)):
90                self.members[client][prop] = value
91                #TODO: Save the new values to the disk.
92            else:
93                raise TypeError("The value of '%s' must be a number." % prop)
94               
95    def get_property(self, client, prop):
96        """Return the value of the 'prop' property for client"""
97        return self.members[client][prop]
98   
Note: See TracBrowser for help on using the repository browser.