[Docstring-checkins] CVS: dps/dps utils.py,1.17,1.18
Status: Pre-Alpha
Brought to you by:
goodger
From: David G. <go...@us...> - 2002-03-13 02:43:51
|
Update of /cvsroot/docstring/dps/dps In directory usw-pr-cvs1:/tmp/cvs-serv3007/dps/dps Modified Files: utils.py Log Message: - Removed 'parseattributes()' in favour of 'extract_extension_attributes()' which uses field_list syntax. - Renamed exceptions. Index: utils.py =================================================================== RCS file: /cvsroot/docstring/dps/dps/utils.py,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** utils.py 11 Mar 2002 03:37:55 -0000 1.17 --- utils.py 13 Mar 2002 02:43:47 -0000 1.18 *************** *** 36,50 **** Multiple reporting categories [#]_ may be set, each with its own warning ! and error thresholds, debugging switch, and warning stream. Categories are ! hierarchically-named strings that look like attribute references: 'spam', ! 'spam.eggs', 'neeeow.wum.ping'. The 'spam' category is the ancestor of ! 'spam.bacon.eggs'. Unset categories inherit stored values from their ! closest ancestor category that has been set. ! When a system message is generated, the stored values from its category ! (or ancestor if unset) are retrieved. The system message level is compared ! to the thresholds stored in the category, and a warning or error is ! generated as appropriate. Debug messages are produced iff the stored debug ! switch is on. Message output is sent to the stored warning stream. .. [#]_ The concept of "categories" was inspired by the log4j project: --- 36,55 ---- Multiple reporting categories [#]_ may be set, each with its own warning ! and error thresholds, debugging switch, and warning stream (collectively a ! `ConditionSet`). Categories are hierarchically-named strings that look ! like attribute references: 'spam', 'spam.eggs', 'neeeow.wum.ping'. The ! 'spam' category is the ancestor of 'spam.bacon.eggs'. Unset categories ! inherit stored conditions from their closest ancestor category that has ! been set. ! When a system message is generated, the stored conditions from its ! category (or ancestor if unset) are retrieved. The system message level is ! compared to the thresholds stored in the category, and a warning or error ! is generated as appropriate. Debug messages are produced iff the stored ! debug switch is on. Message output is sent to the stored warning stream. ! ! The default category is '' (empty string). By convention, Writers should ! retrieve reporting conditions from the 'writer' category (which, unless ! explicitly set, defaults to the conditions of the default category). .. [#]_ The concept of "categories" was inspired by the log4j project: *************** *** 178,236 **** ! class AttributeParsingError(Exception): pass ! class BadAttributeLineError(AttributeParsingError): pass ! class BadAttributeDataError(AttributeParsingError): pass ! class DuplicateAttributeError(AttributeParsingError): pass ! def parseattributes(lines, attributespec): """ ! Return a dictionary mapping attribute names to converted values. :Parameters: ! - `lines`: List of one-line strings of the form:: ! ! ['[name1=value1 name2=value2]', '[name3="value 3"]'] ! ! - `attributespec`: Dictionary mapping known attribute names to a conversion function such as `int` or `float`. :Exceptions: - `KeyError` for unknown attribute names. ! - `ValueError` for invalid attribute values (raised by conversion function). - `DuplicateAttributeError` for duplicate attributes. ! - `BadAttributeLineError` for input lines not enclosed in brackets. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). """ ! attlist = extractattributes(lines) ! attdict = assembleattributes(attlist, attributespec) return attdict ! def extractattributes(lines): """ ! Return a list of attribute (name, value) pairs. :Parameter: ! `lines`: List of one-line strings of the form:: ! ! ['[name1=value1 name2=value2]', '[name3="value 3"]'] :Exceptions: ! - `BadAttributeLineError` for input lines not enclosed in brackets. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). """ attlist = [] ! for line in lines: ! line = line.strip() ! if line[:1] != '[' or line[-1:] != ']': ! raise BadAttributeLineError( ! 'input line not enclosed in "[" and "]"') ! line = line[1:-1].strip() ! attlist += extract_name_value(line) return attlist def extract_name_value(line): """ --- 183,275 ---- ! class ExtensionAttributeError(Exception): pass ! class BadAttributeLineError(ExtensionAttributeError): pass ! class BadAttributeDataError(ExtensionAttributeError): pass ! class DuplicateAttributeError(ExtensionAttributeError): pass ! def extract_extension_attributes(field_list, attribute_spec): """ ! Return a dictionary mapping extension attribute names to converted values. :Parameters: ! - `field_list`: A flat field list without field arguments, where each ! field body consists of a single paragraph only. ! - `attribute_spec`: Dictionary mapping known attribute names to a conversion function such as `int` or `float`. :Exceptions: - `KeyError` for unknown attribute names. ! - `ValueError` for invalid attribute values (raised by the conversion function). - `DuplicateAttributeError` for duplicate attributes. ! - `BadAttributeError` for input lines not enclosed in brackets. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). """ ! attlist = extract_attributes(field_list) ! attdict = assemble_attribute_dict(attlist, attribute_spec) return attdict ! def extract_attributes(field_list): """ ! Return a list of attribute (name, value) pairs from field names & bodies. :Parameter: ! `field_list`: A flat field list without field arguments, where each ! field body consists of a single paragraph only. :Exceptions: ! - `BadAttributeError` for input lines not enclosed in brackets.? - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). """ attlist = [] ! for field in field_list: ! if len(field) != 2: ! raise BadAttributeError( ! 'extension attribute field may not contain field arguments') ! name = field[0].astext().lower() ! body = field[1] ! if len(body) != 1 or not isinstance(body[0], nodes.paragraph) \ ! or len(body[0]) != 1 or not isinstance(body[0][0], nodes.Text): ! raise BadAttributeDataError( ! 'extension attribute field body may consist of\n' ! 'a single paragraph only (attribute "%s")' % name) ! data = body[0][0].astext() ! attlist.append((name, data)) return attlist + def assemble_attribute_dict(attlist, attspec): + """ + Return a mapping of attribute names to values. + + :Parameters: + - `attlist`: A list of (name, value) pairs (the output of + `extract_attributes()`). + - `attspec`: Dictionary mapping known attribute names to a + conversion function such as `int` or `float`. + + :Exceptions: + - `KeyError` for unknown attribute names. + - `DuplicateAttributeError` for duplicate attributes. + - `ValueError` for invalid attribute values (raised by conversion + function). + """ + attributes = {} + for name, value in attlist: + convertor = attspec[name] # raises KeyError if unknown + if attributes.has_key(name): + raise DuplicateAttributeError('duplicate attribute "%s"' % name) + try: + attributes[name] = convertor(value) + except ValueError, detail: + raise ValueError('(attribute "%s") %s' % (name, detail)) + return attributes + + + class NameValueError(Exception): pass + + def extract_name_value(line): """ *************** *** 238,243 **** :Exception: ! `BadAttributeDataError` for invalid attribute data (missing name, ! missing data, bad quotes, etc.). """ attlist = [] --- 277,282 ---- :Exception: ! `NameValueError` for invalid input (missing name, missing data, bad ! quotes, etc.). """ attlist = [] *************** *** 245,265 **** equals = line.find('=') if equals == -1: ! raise BadAttributeDataError('missing "="') attname = line[:equals].strip() if equals == 0 or not attname: ! raise BadAttributeDataError( 'missing attribute name before "="') line = line[equals+1:].lstrip() if not line: ! raise BadAttributeDataError( 'missing value after "%s="' % attname) if line[0] in '\'"': endquote = line.find(line[0], 1) if endquote == -1: ! raise BadAttributeDataError( 'attribute "%s" missing end quote (%s)' % (attname, line[0])) if len(line) > endquote + 1 and line[endquote + 1].strip(): ! raise BadAttributeDataError( 'attribute "%s" end quote (%s) not followed by ' 'whitespace' % (attname, line[0])) --- 284,304 ---- equals = line.find('=') if equals == -1: ! raise NameValueError('missing "="') attname = line[:equals].strip() if equals == 0 or not attname: ! raise NameValueError( 'missing attribute name before "="') line = line[equals+1:].lstrip() if not line: ! raise NameValueError( 'missing value after "%s="' % attname) if line[0] in '\'"': endquote = line.find(line[0], 1) if endquote == -1: ! raise NameValueError( 'attribute "%s" missing end quote (%s)' % (attname, line[0])) if len(line) > endquote + 1 and line[endquote + 1].strip(): ! raise NameValueError( 'attribute "%s" end quote (%s) not followed by ' 'whitespace' % (attname, line[0])) *************** *** 277,303 **** return attlist - def assembleattributes(attlist, attributespec): - """ - Return a mapping of attribute names to values. - - :Parameters: - - `attlist`: A list of (name, value) pairs (the output of - `extractattributes()`). - - `attributespec`: Dictionary mapping known attribute names to a - conversion function such as `int` or `float`. - - :Exceptions: - - `KeyError` for unknown attribute names. - - `DuplicateAttributeError` for duplicate attributes. - - `ValueError` for invalid attribute values (raised by conversion - function). - """ - attributes = {} - for name, value in attlist: - convertor = attributespec[name] # raises KeyError if unknown - if attributes.has_key(name): - raise DuplicateAttributeError('duplicate attribute "%s"' % name) - attributes[name] = convertor(value) # raises ValueError if invalud - return attributes def normname(name): --- 316,319 ---- |