genutils.py 5.91 KB
Newer Older
nextime's avatar
nextime committed
1
###########################################################################
nextime's avatar
nextime committed
2
# Copyright (c) 2011-2017 Franco (nextime) Lanza <franco@nexlab.it>
nextime's avatar
nextime committed
3
#
nextime's avatar
nextime committed
4
# This file is part of nexlibs.
nextime's avatar
nextime committed
5 6 7 8 9 10 11 12 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
#
# domotikad is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################


try:
   import hashlib
   md5 = hashlib
   md5.new = hashlib.md5
   sha1 = hashlib.sha1
except:
   import md5
   import sha1


class ConvenienceCaller(object):
   """
      This metaclass build and abstraction of an object instance so
      that other objects can call it as it is a module, and permit
      to the original object to setup a specific callback
      to the caller so it can manage the calls originated by the caller
      with local methods
   """
   def __init__(self, callerfunc):
      """
         @params callerfunc: function/method to be used as callback
                             inside the original class of the abstract object

      """
      self.callerfunc=callerfunc

   def __getattribute__(self, name):
      """
         This do the magic, transforming every called method in a call
         to the callerfunction of the original object

         This method isn't intended to be called directly!
      """
      callerfunc = object.__getattribute__(self, 'callerfunc')
      return callerfunc(name)


from ConfigParser import SafeConfigParser


def revlist(l): l.reverse(); return l

class FakeObject(object):
   pass

class configFile(SafeConfigParser):

   def __init__(self, config):
      self.configfile = config
      SafeConfigParser.__init__(self)

   def readConfig(self):
      return self.read(self.configfile)

   def writeConfig(self):
      fd = open(self.configfile, "w")
      self.write(fd)
      fd.close()
      return True


   def getOptions(self, section):
      res = {}
      for opt in self.items(section):
         try:
            res[opt[0]] = opt[1]
         except:
            pass
      return res


class CircularList(list):
    """
    A list that wraps around instead of throwing an index error.
    
    Works like a regular list:
    >>> cl = CircularList([1,2,3])
    >>> cl
    [1, 2, 3]
    
    >>> cl[0]
    1
    
    >>> cl[-1]
    3
    
    >>> cl[2]
    3
    
    Except wraps around:
    >>> cl[3]
    1
    
    >>> cl[-4]
    3
    
    Slices work
    >>> cl[0:2]
    [1, 2]
    
    but only in range.
    """
    def __getitem__(self, key):
        
        # try normal list behavior
        try:
            return super(CircularList, self).__getitem__(key)
        except IndexError:
            pass
        # key can be either integer or slice object,
        # only implementing int now.
        try:
            index = int(key)
            index = index % self.__len__()
            return super(CircularList, self).__getitem__(index)
        except ValueError:
            raise TypeError


class CircularList2(list):
    
   def __init__(self, sequence):
      list.__init__(self, sequence)
      self.i = 0
            
   def set_index(self, i):
      if i not in range(len(self)):
         raise IndexError, 'Can\'t set index out of range'
      else:
         self.i = i
            
   def next(self, n=1):
      if self == []:
         return None
      if n < 0:
         return self.prev(abs(n))    
      if self.i not in range(len(self)):
         self.i = len(self) - 1 
      if self.i + n >= len(self):
         i = self.i
         self.set_index(0)
         return self.next(n - len(self) + i)
      else:
         self.set_index(self.i + n)
         return self[self.i]
            
   def prev(self, n=1):
      if self == []:
         return None
      if n < 0:
         return self.next(abs(n))
      if self.i not in range(len(self)):
         self.i = len(self) - 1 
      if self.i - n < 0:
         i = self.i
         self.set_index(len(self) - 1)
         return self.prev(n - i - 1)
      else:
         self.i -= n
         return self[self.i]


class SliceCircular(CircularList2):

   def getdata(self, howmany=1):
      if(howmany > len(self)):
         howmany=len(self)
      i=self.i
      distance=int((howmany-1)/2)
      ret=[]
      self.prev(distance+1)
      while howmany > 0:
         ret.append(self.next())
         howmany-=1
      self.i=i
      return ret


def invertWord(word):
   import struct
   return struct.pack('<2B', struct.unpack('<2B', str(word[:2]))[1], 
                             struct.unpack('<2B', str(word[:2]))[0])


def isIp(addr):
   ip=addr.split(".")
   if len(ip)==4:
      for n in ip:
         if not unicode(n).isnumeric() or int(n) <0 or int(n) > 255:
            return False
      return True
   return False


def is_number(s):
   try:
      float(s) # for int, long and float
   except ValueError:
      try:
         complex(s) # for complex
      except ValueError:
         return False
   except:
      return False
   return True


def isTrue(d):
   if str(d).lower() in ["1", "true", "yes", "si", "y"]:
      return True
   return False


def board_syspwd(cfgpwd):
   if len(cfgpwd)>4:
      return cfgpwd
   return 'domotika'

def devs_adminpwd(cfgpwd):
   if len(cfgpwd)>4:
      return cfgpwd
   return 'domotika'


def ip_match(mask, ip):
   if mask=='255.255.255.255' or mask=='0.0.0.0':
      return True
   else:
      return mask==ip

def hashPwd(pwd):
   if pwd and len(pwd)>3:
      s=sha1()
      s.update(pwd)
      return s.hexdigest()
   return False