110 lines
2.3 KiB
Python
110 lines
2.3 KiB
Python
#!/usr/bin/env python
|
|
|
|
class Package:
|
|
|
|
_com_pad = 40;
|
|
|
|
# (1) Constructs a new package
|
|
#
|
|
# @name<String> Package name
|
|
# @state<char> Package state ('x', ' ', '.')
|
|
# @com<String> Package comm (default: None)
|
|
# @ppa<(String, String)> Package ppa (default: None)
|
|
# @deb<Path> Path to local deb file (if deb) (default: None)
|
|
# @pad<int> Number of pad spaces (default: 2)
|
|
#
|
|
#--------------------------------------------------------#
|
|
def __init__(self, name=None, sta=None, com=None, ppa=None, deb=False, pad=2):
|
|
# (1) Arguments check
|
|
#--------------------------------------------------------#
|
|
# (1) Is name set #
|
|
if( name == None ):
|
|
raise Exception('Missing argument "name" !');
|
|
|
|
# (2) deb set but not a bool #
|
|
if( not isinstance(deb, bool) ):
|
|
raise Exception('Argument "deb" must be a boolean !');
|
|
|
|
# (3) Check pad is (+) int #
|
|
try:
|
|
pad = int(pad);
|
|
|
|
if( pad < 0 ):
|
|
raise Exception('Argument "pad" must be a positive integer !');
|
|
|
|
except ValueError:
|
|
raise Exception('Argument "pad" must be a positive integer !');
|
|
|
|
# (4) Check state #
|
|
if( sta != 'x' and sta != '.' and sta != ' ' ):
|
|
raise Exception('Argument "sta" is invalid or missing !');
|
|
|
|
|
|
|
|
# (2) Set attributes
|
|
#--------------------------------------------------------#
|
|
self._name = name;
|
|
self._sta = sta;
|
|
self._com = com;
|
|
self._ppa = "%s/%s" % (ppa[0], ppa[1]) if ppa != None else None;
|
|
self._deb = deb;
|
|
self._pad = pad;
|
|
|
|
|
|
|
|
# (2) Getters
|
|
def getName(self):
|
|
return self._name;
|
|
|
|
def getDeb(self):
|
|
return self._deb;
|
|
|
|
def getPpa(self):
|
|
return self._ppa;
|
|
|
|
def getPad(self):
|
|
return self._pad;
|
|
|
|
def getCom(self):
|
|
return self._com;
|
|
|
|
def isInstalled(self):
|
|
return self._sta == 'x';
|
|
|
|
def isRemoved(self):
|
|
return self._sta == '.';
|
|
|
|
def isPurged(self):
|
|
return self._sta == ' ';
|
|
|
|
|
|
|
|
def install(self):
|
|
self._sta = 'x';
|
|
|
|
def remove(self):
|
|
self._sta = '.';
|
|
|
|
def purge(self):
|
|
self._sta = ' ';
|
|
|
|
|
|
# (3) Serialize
|
|
#
|
|
#--------------------------------------------------------#
|
|
def serialize(self):
|
|
raw = '%s[%s] %s' % ( ' '*self._pad, self._sta, self._name);
|
|
|
|
if( self._ppa != None ):
|
|
raw += ' [%s]' % self._ppa;
|
|
elif( self._deb ):
|
|
raw += ' [deb]';
|
|
|
|
if( self._com != None ):
|
|
raw += ' ' * (self._com_pad - len(raw));
|
|
raw += '# %s' % self._com;
|
|
|
|
return raw;
|
|
|
|
|