def mask_check(self, address, mask, base=16, part_len=16, delimiter=':'): if (int(mask) % part_len) != 0: raise StandardError( '{0} is not valid mask, should be multiples of {1}'.format( mask, part_len)) part_count = int(mask) / part_len for part in address.split(delimiter): part_value = int(part, base) if part != '' else 0 part_count -= 1 if part_count < 0 and part_value != 0: raise StandardError( 'address part {0} should be 0'.format(part))
def __new__(mcs, name, bases, attrs): if name == 'Model': return type.__new__(mcs, name, bases, attrs) table_name = attrs.get('__table__', None) or name logging.info('found model: %s (table: %s)' % (name, table_name)) mappings = dict() fields = [] primary_key = None for key, value in attrs.items(): if isinstance(value, Field): logging.info(' found mapping : %s ==> %s' % (key, value)) mappings[key] = value if value.primary_key: # 找到主键 if primary_key: raise StandardError( 'Duplicate primary key for field : %s' % key) primary_key = key else: fields.append(key) if not primary_key: raise StandardError('Primary key not found') for key in mappings.keys(): attrs.pop(key) escaped_fields = list(map(lambda f: '%s' % f, fields)) attrs['__mappings__'] = mappings # 保存属性和列的映射关系 attrs['__table__'] = table_name attrs['__primary_key__'] = primary_key # 主键属性名 attrs['__fields__'] = fields # 除主键以外的属性名 attrs['__select__'] = "select '%s', '%s' from '%s'" % ( primary_key, ','.join(escaped_fields), table_name) attrs['__insert__'] = "insert into '%s' (%s, '%s') values (%s)" % ( table_name, ','.join(escaped_fields), primary_key, create_args_string(len(escaped_fields) + 1)) attrs['__update__'] = "update '%s' set %s where '%s'=?" % ( table_name, ','.join( map(lambda f: "'%s'=?" % (mappings.get(f).name or f), fields)), primary_key) attrs['__delete__'] = "delete from '%s' where '%s'=?" % (table_name, primary_key) return type.__new__(mcs, name, bases, attrs)
def __init__(self, cache_dir=None): if not self.instance: raise StandardError( "Set the 'instance' class attribute to an appropriate label.") if cache_dir is None: raise StandardError( 'Set cache_dir to the directory for the requests cache.') self.parser = OptionParser() self._add_parser_options() (self.options, self.args) = self.parser.parse_args() self._process_parser_options() self.instance = self.get_or_create(Instance, label=self.instance) self.cache_dir = os.path.join(cache_dir, self.instance.label) try: os.makedirs(self.cache_dir) except: pass self.requests = requests_cache.core.CachedSession(cache_dir)
def ipv6_check(self, a): XMLValidator.log.debug('validating ipv6 address: {0}'.format(a)) mask_pos = a.find('/') if mask_pos > 0: a_mask = a[mask_pos + 1:] a = a[:mask_pos] self.mask_check(a, a_mask) numbers = a.split(':') max_range = (2**16) - 1 for n in numbers: # if n == '' then the number is 0000 which is always smaller than max_range if n != '' and int(n, 16) > max_range: raise StandardError( 'number: {0} in ipv6 address: {1} larger than: {2}'.format( n, a, max_range))
def ipv4_check(self, a): XMLValidator.log.debug('validating ipv4 address: {0}'.format(a)) mask_pos = a.find('/') if mask_pos > 0: a_mask = a[mask_pos + 1:] a = a[:mask_pos] self.mask_check(a, a_mask, 10, 8, '.') numbers = a.split('.') max_range = (2**8) - 1 for n in numbers: if int(n) > max_range: raise StandardError( 'octet: {0} in ipv4 address: {1} larger than: {2}'.format( n, a, max_range))
def has_prerequisite(self, key, values, convert_from, flow_dict): XMLValidator.log.debug('checking prerequisite: {0} - {1}'.format( key, values)) try: flow_value_raw = flow_dict[key] # if prerequisites values are [None] we don't care about actual value if values != [None]: flow_value = int(flow_value_raw, convert_from) if flow_value not in values: raise StandardError() XMLValidator.log.info( 'prerequisite {0}: {1} to value {2} validated successfully'. format(key, values, flow_value_raw)) except KeyError: XMLValidator.log.error( 'can\'t find element: {0} in xml {1} or in keywords {2}'. format(key, self.xml_string, self.mkwd.keys())) self.xml_ok = False except ValueError or TypeError: # flow_value_raw is string that cannot be converted to decimal or hex number or None if flow_value_raw not in values: XMLValidator.log.error( 'can\'t find element: {0} with value value: {1} ' 'in expected values {2}'.format(key, flow_value_raw, values)) self.xml_ok = False else: XMLValidator.log.info( 'prerequisite {0}: {1} to value {2} validated successfully' .format(key, values, flow_value_raw)) except StandardError: XMLValidator.log.error( 'can\'t find element: {0} with value value: {1} ' 'in expected values {2}'.format(key, flow_value, values)) self.xml_ok = False