def __new__(cls, arg, *args):
     '''This is necessary because tuple is an immutable data type, so
     inheritance rules are a bit funny.'''
     # I have some idea I should be using super() here
     if args:
         return tuple.__new__(cls, arg)
     else:
         a = stringtosockaddr(arg)
         return tuple.__new__(cls, a)
Exemple #2
0
 def __new__(cls, arg, *args):
     '''This is necessary because tuple is an immutable data type, so
     inheritance rules are a bit funny.'''
     # I have some idea I should be using super() here
     if args:
         return tuple.__new__(cls, arg)
     else:
         a = stringtosockaddr(arg)
         return tuple.__new__(cls, a)
Exemple #3
0
 def files(self):
     '''Read self.FEATURED_FILE, and for each label (starting at column 0)
     construct a dict of the (indented) addresses following it. Each dict
     value starts off as None, to be initialised as the connections are
     made.
     self.featured_servers[label] is set to its corresponding dict.
     A missing file is ignored but other errors - e.g. if the file is
     present but can't be read - are fatal.'''
     self.featured_servers = dict()
     # FIXME: use ConfigError where appropriate
     try:
         with open(self.FEATURED_FILE) as featured:
             errmsg = self.FEATURED_FILE + ':'
             self.log(LOG_DEBUG, 'Opened', self.FEATURED_FILE)
             label = ''
             lineno = 0
             for line in iter(l.rstrip() for l in featured):
                 lineno += 1
                 wheremsg = '{0}:{1}:'.format(self.FEATURED_FILE, lineno)
                 # ignore blank lines and comments
                 if not line or line.isspace() or \
                    line.lstrip().startswith('#'):
                     continue
                 # indented lines are server addresses
                 if line[0].isspace():
                     addr = line.lstrip()
                     if not label:
                         raise ConfigError(errmsg, 'Error: missing label')
                     try:
                         saddr = stringtosockaddr(addr)
                     except EnvironmentError as err:
                         # EnvironmentError covers socket.error and
                         # .gaierror without having to import them
                         raise ConfigError(wheremsg, "Error: couldn't "
                                           'convert', addr,
                                           'to address format:', err)
                     if saddr in self.featured_servers[label].keys():
                         self.log(LOG_PRINT, wheremsg, 'Warning:', saddr,
                                  'appears multiple times')
                     self.featured_servers[label][saddr] = None
                 # unindented lines start a new label
                 else:
                     # clean up the old label
                     if label:
                         if not self.featured_servers[label]:
                             raise ConfigError(
                                 wheremsg, 'Error: no '
                                 'addresses defined for', label)
                         else:
                             # print a message of the form
                             # featured.txt: 'Label': [server1, server2,...]
                             self.log(LOG_VERBOSE,
                                      self.FEATURED_FILE,
                                      repr(label),
                                      self.featured_servers[label].keys(),
                                      sep=': ')
                     label = line
                     for c in label:
                         # slashes are field seperators in
                         # getserversExtResponse
                         if c in '\\/':
                             raise ConfigError(
                                 wheremsg, 'Error:', 'label', repr(label),
                                 'contains '
                                 'invalid character:', c)
                     self.featured_servers[label] = dict()
             if label:
                 # print a message of the form
                 # featured.txt: 'Label': [server1, server2, ...]
                 self.log(LOG_VERBOSE,
                          self.FEATURED_FILE,
                          repr(label),
                          self.featured_servers[label].keys(),
                          sep=': ')
     except IOError as err:
         if err.errno != ENOENT:
             raise
 def files(self):
     '''Read self.FEATURED_FILE, and for each label (starting at column 0)
     construct a dict of the (indented) addresses following it. Each dict
     value starts off as None, to be initialised as the connections are
     made.
     self.featured_servers[label] is set to its corresponding dict.
     A missing file is ignored but other errors - e.g. if the file is
     present but can't be read - are fatal.'''
     self.featured_servers = dict()
     # FIXME: use ConfigError where appropriate
     try:
         with open(self.FEATURED_FILE) as featured:
             errmsg = self.FEATURED_FILE + ':'
             self.log(LOG_DEBUG, 'Opened', self.FEATURED_FILE)
             label = ''
             lineno = 0
             for line in iter(l.rstrip() for l in featured):
                 lineno += 1
                 wheremsg = '{0}:{1}:'.format(self.FEATURED_FILE, lineno)
                 # ignore blank lines and comments
                 if not line or line.isspace() or \
                    line.lstrip().startswith('#'):
                     continue
                 # indented lines are server addresses
                 if line[0].isspace():
                     addr = line.lstrip()
                     if not label:
                         raise ConfigError(errmsg, 'Error: missing label')
                     try:
                         saddr = stringtosockaddr(addr)
                     except EnvironmentError as err:
                         # EnvironmentError covers socket.error and
                         # .gaierror without having to import them
                         raise ConfigError(wheremsg, "Error: couldn't "
                                           'convert', addr,
                                           'to address format:', err)
                     if saddr in self.featured_servers[label].keys():
                         self.log(LOG_PRINT, wheremsg, 'Warning:',
                                  saddr, 'appears multiple times')
                     self.featured_servers[label][saddr] = None
                 # unindented lines start a new label
                 else:
                     # clean up the old label
                     if label:
                         if not self.featured_servers[label]:
                             raise ConfigError(wheremsg, 'Error: no '
                                               'addresses defined for',
                                               label)
                         else:
                             # print a message of the form
                             # featured.txt: 'Label': [server1, server2,...]
                             self.log(LOG_VERBOSE, self.FEATURED_FILE,
                                      repr(label),
                                      self.featured_servers[label].keys(),
                                      sep = ': ')
                     label = line
                     for c in label:
                         # slashes are field seperators in
                         # getserversExtResponse
                         if c in '\\/':
                             raise ConfigError(wheremsg, 'Error:',
                                      'label', repr(label), 'contains '
                                      'invalid character:', c)
                     self.featured_servers[label] = dict()
             if label:
                 # print a message of the form
                 # featured.txt: 'Label': [server1, server2, ...]
                 self.log(LOG_VERBOSE, self.FEATURED_FILE, repr(label),
                          self.featured_servers[label].keys(), sep = ': ')
     except IOError as err:
         if err.errno != ENOENT:
             raise