Implement the __rolevars method

This commit is contained in:
enricobacis 2015-09-10 01:16:55 +02:00
parent e9afed5f21
commit 3dddc535d5

View File

@ -5,6 +5,7 @@
__author__ = 'BgLUG' __author__ = 'BgLUG'
__email__ = 'info@bglug.it' __email__ = 'info@bglug.it'
from collections import namedtuple
from contextlib import contextmanager, closing from contextlib import contextmanager, closing
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from sqlite3 import connect from sqlite3 import connect
@ -14,9 +15,13 @@ from ConfigParser import ConfigParser
from Crypto.Hash import MD5 from Crypto.Hash import MD5
from Crypto.Cipher import AES from Crypto.Cipher import AES
import base64 import base64
import json
import sys
import re import re
import os import os
import sys
# namedtuple used by Nethserver's Network Package Manager
Package = namedtuple('Package', ['name', 'role', 'rule', 'description'])
class MyApp: class MyApp:
def __init__(self, configfile = '/etc/mac2hostname.ini'): def __init__(self, configfile = '/etc/mac2hostname.ini'):
@ -103,23 +108,19 @@ class MyApp:
(role,) = cursor.execute("SELECT role FROM client WHERE hostname = '%s' AND mac = '%s'" % (hostname, macaddress)).fetchone() (role,) = cursor.execute("SELECT role FROM client WHERE hostname = '%s' AND mac = '%s'" % (hostname, macaddress)).fetchone()
return role return role
# Mocks up variables to be returned. # Parse a json pkg retrieved by Nethserver's Network Package Manager into a Package namedtuple
# When will be ready Enrico's work on the interface, this should be simpler def parse_package(pkg):
# to implement. return Package(pkg['name'], pkg['props']['Role'], pkg['props']['Rule'], pkg['props']['Description'])
# Variables returned to the client
def __rolevars(self, role=None): def __rolevars(self, role=None):
retval = {} retval = {'role': role, 'addpkg': [], 'delpkg': []}
if role == 'client': try:
retval = { db = Popen(['db', 'packages', 'getjson'], stdout=PIPE).communicate()[0]
'role': role, for pkg in map(parse_package, json.loads(db)):
'addpkg': [ 'gimp' ], if pkg.role == role:
'delpkg': [] result['addpkg' if pkg.rule == 'add' else 'delpkg'].append(str(pkg.name))
} except: pass
elif role == 'docenti':
retval = {
'role': role,
'addpkg': [],
'delpkg': []
}
return retval return retval
# Instance methods AKA routes # Instance methods AKA routes