def check_table_dimensions(self, rows, header_rows, stub_columns): if len(rows) < header_rows: error = self.state_machine.reporter.error( '%s header row(s) specified but only %s row(s) of data ' 'supplied ("%s" directive).' % (header_rows, len(rows), self.name), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) if len(rows) == header_rows > 0: error = self.state_machine.reporter.error( 'Insufficient data supplied (%s row(s)); no data remaining ' 'for table body, required by "%s" directive.' % (len(rows), self.name), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) for row in rows: if len(row) < stub_columns: error = self.state_machine.reporter.error( '%s stub column(s) specified but only %s columns(s) of ' 'data supplied ("%s" directive).' % (stub_columns, len(row), self.name), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) if len(row) == stub_columns > 0: error = self.state_machine.reporter.error( 'Insufficient data supplied (%s columns(s)); no data remaining ' 'for table body, required by "%s" directive.' % (len(row), self.name), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error)
def get_column_widths(self, max_cols): if type(self.widths) == list: if len(self.widths) != max_cols: error = self.state_machine.reporter.error( '"%s" widths do not match the number of columns in table ' '(%s).' % (self.name, max_cols), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) col_widths = self.widths elif max_cols: col_widths = [100 // max_cols] * max_cols else: error = self.state_machine.reporter.error( 'No table data detected in CSV file.', nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) if self.widths == 'auto': widths = 'auto' elif self.widths: # "grid" or list of integers widths = 'given' else: widths = self.widths return widths, col_widths
def raiseError(self, msg): error = self.directive.state_machine.reporter.error( msg , nodes.literal_block(self.directive.block_text , self.directive.block_text) , line = self.directive.lineno ) raise SystemMessagePropagation(error)
def get_csv_data(self): """ Get CSV data from the directive content, from an external file, or from a URL reference. """ encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) if self.content: # CSV data is from directive content. if 'file' in self.options or 'url' in self.options: error = self.state_machine.reporter.error( '"%s" directive may not both specify an external file and' ' have content.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source = self.content.source(0) csv_data = self.content elif 'file' in self.options: # CSV data is from an external file. if 'url' in self.options: error = self.state_machine.reporter.error( 'The "file" and "url" options may not be simultaneously' ' specified for the "%s" directive.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) source = os.path.normpath( os.path.join(source_dir, self.options['file'])) source = utils.relative_path(None, source) try: self.state.document.settings.record_dependencies.add(source) csv_file = io.FileInput( source_path=source, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler), handle_io_errors=None) csv_data = csv_file.read().splitlines() except IOError, error: severe = self.state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s.' % (self.name, error), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(severe)
def check_list_content(node, name, options, content, lineno, block_text, state_machine): if len(node) != 1 or not isinstance(node[0], nodes.bullet_list): error = state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'exactly one bullet list expected.' % name, nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) list_node = node[0] # Check for a uniform two-level bullet list: for item_index in range(len(list_node)): item = list_node[item_index] if len(item) != 1 or not isinstance(item[0], nodes.bullet_list): error = state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'two-level bullet list expected, but row %s does not contain ' 'a second-level bullet list.' % (name, item_index + 1), nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) elif item_index: # ATTN pychecker users: num_cols is guaranteed to be set in the # "else" clause below for item_index==0, before this branch is # triggered. if len(item[0]) != num_cols: error = state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'uniform two-level bullet list expected, but row %s does ' 'not contain the same number of items as row 1 (%s vs %s).' % (name, item_index + 1, len(item[0]), num_cols), nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) else: num_cols = len(item[0]) col_widths = get_column_widths(num_cols, name, options, lineno, block_text, state_machine) if len(col_widths) != num_cols: error = state_machine.reporter.error( 'Error parsing "widths" option of the "%s" directive: ' 'number of columns does not match the table data (%s vs %s).' % (name, len(col_widths), num_cols), nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) return num_cols, col_widths
def check_requirements(name, lineno, block_text, state_machine): if not csv: error = state_machine.reporter.error( 'The "%s" directive is not compatible with this version of ' 'Python (%s). Requires the "csv" module, new in Python 2.3.' % (name, sys.version.split()[0]), nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error)
def get_json_data(self): """ Get JSON data from the directive content, from an external file, or from a URL reference. """ if self.arguments: filename, pointer = self._splitpointer(self.arguments[0]) else: filename = None pointer = '' if self.content: schema, source = self.from_content(filename) elif filename and filename.startswith('http'): schema, source = self.from_url(filename) elif filename: schema, source = self.from_file(filename) else: raise self.error( '"%s" directive has no content or a reference to an external file.' % self.name) try: schema = self.ordered_load(schema) except Exception as error: error = self.state_machine.reporter.error( '"%s" directive encountered a the following error while parsing the data.\n %s' % (self.name, SafeString("".join(format_exception_only( type(error), error)))), nodes.literal_block(schema, schema), line=self.lineno) raise SystemMessagePropagation(error) if pointer: try: schema = resolve_pointer(schema, pointer) except KeyError: error = self.state_machine.reporter.error( '"%s" directive encountered a KeyError when trying to resolve the pointer' ' in schema: %s' % (self.name, SafeString(pointer)), nodes.literal_block(schema, schema), line=self.lineno) raise SystemMessagePropagation(error) return schema, source, pointer
def get_column_widths(self, max_cols): if 'widths' in self.options: col_widths = self.options['widths'] if len(col_widths) != max_cols: error = self.state_machine.reporter.error( '"%s" widths do not match the number of columns in table ' '(%s).' % (self.name, max_cols), nodes.literal_block( self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) elif max_cols: col_widths = [100 // max_cols] * max_cols else: error = self.state_machine.reporter.error( 'No table data detected in CSV file.', nodes.literal_block( self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) return col_widths
def get_column_widths(max_cols, name, options, lineno, block_text, state_machine): if options.has_key('widths'): col_widths = options['widths'] if len(col_widths) != max_cols: error = state_machine.reporter.error( '"%s" widths do not match the number of columns in table (%s).' % (name, max_cols), nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) elif max_cols: col_widths = [100 / max_cols] * max_cols else: error = state_machine.reporter.error( 'No table data detected in CSV file.', nodes.literal_block(block_text, block_text), line=lineno) raise SystemMessagePropagation(error) return col_widths
def get_column_widths(self, n_cols): if isinstance(self.widths, list): if len(self.widths) != n_cols: # TODO: use last value for missing columns? error = self.state_machine.reporter.error( '"%s" widths do not match the number of columns in table ' '(%s).' % (self.name, n_cols), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) col_widths = self.widths elif n_cols: col_widths = [100 // n_cols] * n_cols else: error = self.state_machine.reporter.error( 'No table data detected in CSV file.', nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) return col_widths
def get_column_widths(self, max_cols): if isinstance(self.widths, list): if len(self.widths) != max_cols: error = self.state_machine.reporter.error( '"%s" widths do not match the number of columns in table ' "(%s)." % (self.name, max_cols), nodes.literal_block(self.block_text, self.block_text), line=self.lineno, ) raise SystemMessagePropagation(error) col_widths = self.widths elif max_cols: col_widths = [100 // max_cols] * max_cols else: error = self.state_machine.reporter.error( "No table data detected in CSV file.", nodes.literal_block(self.block_text, self.block_text), line=self.lineno, ) raise SystemMessagePropagation(error) return col_widths
def _get_rows_with_included_cols(self, rows, included_cols_list): prepared_rows = [] for row in rows: try: idx_row = [row[i] for i in included_cols_list] prepared_rows.append(idx_row) except IndexError: error = self.state_machine.reporter.error( 'One or more indexes of included_cols are not valid. ' 'The CSV data does not contain that many columns.') raise SystemMessagePropagation(error) return prepared_rows, len(included_cols_list)
def from_content(self, filename): if filename: error = self.state_machine.reporter.error( '"%s" directive may not both specify an external file and' ' have content.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source = self.content.source(0) data = '\n'.join(self.content) return data, source
def check_list_content(self, node): if len(node) != 1 or not isinstance(node[0], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' "exactly one bullet list expected." % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno, ) raise SystemMessagePropagation(error) list_node = node[0] num_cols = 0 # Check for a uniform two-level bullet list: for item_index in range(len(list_node)): item = list_node[item_index] if len(item) != 1 or not isinstance(item[0], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' "two-level bullet list expected, but row %s does not " "contain a second-level bullet list." % (self.name, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno, ) raise SystemMessagePropagation(error) elif item_index: if len(item[0]) != num_cols: error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' "uniform two-level bullet list expected, but row %s " "does not contain the same number of items as row 1 " "(%s vs %s)." % (self.name, item_index + 1, len(item[0]), num_cols), nodes.literal_block(self.block_text, self.block_text), line=self.lineno, ) raise SystemMessagePropagation(error) else: num_cols = len(item[0]) col_widths = self.get_column_widths(num_cols) return num_cols, col_widths
def check_list_content(self, node): if len(node) != 1 or not isinstance(node[0], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'exactly one bullet list expected.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) list_node = node[0] # Check for a uniform two-level bullet list: for item_index in range(len(list_node)): item = list_node[item_index] if len(item) != 1 or not isinstance(item[0], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'two-level bullet list expected, but row %s does not ' 'contain a second-level bullet list.' % (self.name, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) elif item_index: # ATTN pychecker users: num_cols is guaranteed to be set in the # "else" clause below for item_index==0, before this branch is # triggered. if len(item[0]) != num_cols: error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'uniform two-level bullet list expected, but row %s ' 'does not contain the same number of items as row 1 ' '(%s vs %s).' % (self.name, item_index + 1, len(item[0]), num_cols), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) else: num_cols = len(item[0]) return num_cols
def run(self): from oauth2_provider import settings defaults = sorted([(k, v) for k, v in settings.DEFAULTS.items() if not k.startswith('_')]) opt_type = self.options.get('type') if opt_type == 'table': return self._build_table(defaults) elif opt_type == 'definitions': return self._build_definition_list(defaults) else: error = self.state_machine.reporter.error( "type must be 'table' or 'definitions'") raise SystemMessagePropagation(error)
def get_rows(self): """ Get rows as list of lists or array from the directive content """ if not self.content: error = self.state_machine.reporter.warning( 'The "%s" directive requires content; none supplied.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) want_new = True if 'newcontext' in self.options else False context = self.get_context(want_new) source = self.content.source(0) output = eval_code('\n'.join(self.content), context) rows = self._process_output(output) rows = [] if rows is None else rows return self._process_rows(rows, source), source
def get_grid(self, node): if len(node) != 1 or not isinstance(node[0], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'exactly one bullet list expected.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) grid = [] list_node = node[0] for item_index in range(len(list_node)): item = list_node[item_index] nitems = len(item) if not nitems or nitems > 2 or not isinstance( item[-1], nodes.bullet_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'two-level bullet list expected, but row %s does not ' 'contain a second-level bullet list.' % (self.name, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) elif nitems == 2 and not isinstance(item[0], nodes.field_list): error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'expected options as a field list, but row %s does not ' 'start with a field list.' % (self.name, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) if nitems == 1: row = ({}, [col.children for col in item[0]]) else: opts = {} for opt in item[0]: key = opt[0].rawsource value = opt[1].rawsource.strip() if key != 'flex-widths': error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' 'received "%s" but only "flex-widths" is supported as ' 'an option on row %s.' % (self.name, key, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) try: sep = ',' if ',' in value else ' ' fwidths = [int(x) for x in value.split(sep)] if any(x not in self.valid_flexes for x in fwidths) or len(fwidths) != len( item[1]): raise ValueError('invalid flex widths') opts['flex-widths'] = fwidths except: error = self.state_machine.reporter.error( 'Error parsing content block for the "%s" directive: ' '"flex-widths" expects a list of %s widths with possible values of (1,2,3,4,5).' 'The input "%s" for row %s is malformed.' % (self.name, len(item[1]), value, item_index + 1), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) row = (opts, [col.children for col in item[1]]) grid.append(row) return grid
elif 'url' in self.options: # CSV data is from a URL. # Do not import urllib2 at the top of the module because # it may fail due to broken SSL dependencies, and it takes # about 0.15 seconds to load. import urllib2 source = self.options['url'] try: csv_text = urllib2.urlopen(source).read() except (urllib2.URLError, IOError, OSError, ValueError), error: severe = self.state_machine.reporter.severe( 'Problems with "%s" directive URL "%s":\n%s.' % (self.name, self.options['url'], error), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(severe) csv_file = io.StringInput( source=csv_text, source_path=source, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler)) csv_data = csv_file.read().splitlines() else: error = self.state_machine.reporter.warning( 'The "%s" directive requires content; none supplied.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) return csv_data, source if sys.version_info < (3, ):
def get_csv_data(self): """ Get CSV data from the directive content, from an external file, or from a URL reference. """ encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) error_handler = self.state.document.settings.input_encoding_error_handler if self.content: # CSV data is from directive content. if 'file' in self.options or 'url' in self.options: error = self.state_machine.reporter.error( '"%s" directive may not both specify an external file and' ' have content.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source = self.content.source(0) csv_data = self.content elif 'file' in self.options: # CSV data is from an external file. if 'url' in self.options: error = self.state_machine.reporter.error( 'The "file" and "url" options may not be simultaneously' ' specified for the "%s" directive.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) source = os.path.normpath( os.path.join(source_dir, self.options['file'])) source = utils.relative_path(None, source) try: self.state.document.settings.record_dependencies.add(source) csv_file = io.FileInput(source_path=source, encoding=encoding, error_handler=error_handler) csv_data = csv_file.read().splitlines() except IOError as error: severe = self.state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s.' % (self.name, SafeString(error)), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(severe) elif 'url' in self.options: # CSV data is from a URL. # Do not import urllib2 at the top of the module because # it may fail due to broken SSL dependencies, and it takes # about 0.15 seconds to load. import urllib.request, urllib.error, urllib.parse source = self.options['url'] try: csv_text = urllib.request.urlopen(source).read() except (urllib.error.URLError, IOError, OSError, ValueError) as error: severe = self.state_machine.reporter.severe( 'Problems with "%s" directive URL "%s":\n%s.' % (self.name, self.options['url'], SafeString(error)), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(severe) csv_file = io.StringInput( source=csv_text, source_path=source, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler)) csv_data = csv_file.read().splitlines() else: error = self.state_machine.reporter.warning( 'The "%s" directive requires content; none supplied.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) return csv_data, source