Skip to content

Commit

Permalink
Run code through black
Browse files Browse the repository at this point in the history
One should generally avoid applying large, style-only changes to
existing code bases because they tend to make a complete mess of our
ability to use 'git-blame' and related tools effectively. With that
said, the code base is in pretty bad shape owing to its sheer age and
we'd otherwise be fixing this stuff manually for years. Bite the bullet
and run all the code through black [1] (specifically, 'black -S', to
avoid unnecessary conversion of single quotes to double quotes) to
address the worst of the issues. A future change will address the
remaining issues like use of wildcard imports and commented out (dead)
code.

Note that black defaults to a max line length of 88, not 79 [2], which
means we need to configure flake8 to allow this.

[1] https://pypi.org/project/black/
[2] https://black.readthedocs.io/en/stable/the_black_code_style.html

Signed-off-by: Stephen Finucane <stephen@that.guru>
Closes: #724
Closes: #711
  • Loading branch information
stephenfin committed Jun 15, 2020
1 parent 56005c9 commit 684da51
Show file tree
Hide file tree
Showing 47 changed files with 3,209 additions and 2,074 deletions.
1 change: 1 addition & 0 deletions rst2pdf/__init__.py
@@ -1,6 +1,7 @@
# See LICENSE.txt for licensing terms
try:
import pkg_resources

try:
version = pkg_resources.get_distribution('rst2pdf').version
except pkg_resources.ResolutionError:
Expand Down
33 changes: 18 additions & 15 deletions rst2pdf/aafigure_directive.py
Expand Up @@ -34,7 +34,8 @@
from .opt_imports import aafigure
from .log import log

WARNED=False
WARNED = False


class Aanode(Element):
children = ()
Expand All @@ -52,30 +53,30 @@ def gen_flowable(self, style_options):
# explicit :option: always precedes
options.update(self.options)
visitor = aafigure.process(
'\n'.join(self.content),
aafigure.pdf.PDFOutputVisitor,
options=options)
'\n'.join(self.content), aafigure.pdf.PDFOutputVisitor, options=options,
)
return renderPDF.GraphicsFlowable(visitor.drawing)


class Aafig(rst.Directive):
"""
Directive to insert an ASCII art figure to be rendered by aafigure.
"""

has_content = True
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = dict(
scale = float,
line_width = float,
background = str,
foreground = str,
fill = str,
name = str,
aspect = float,
textual = directives.flag,
proportional = directives.flag,
scale=float,
line_width=float,
background=str,
foreground=str,
fill=str,
name=str,
aspect=float,
textual=directives.flag,
proportional=directives.flag,
)

def run(self):
Expand All @@ -87,8 +88,10 @@ def run(self):
if aafigure is not None:
return [Aanode(self.content, self.options)]
if not WARNED:
log.error('To render the aafigure directive correctly, please install aafigure')
WARNED=True
log.error(
'To render the aafigure directive correctly, please install aafigure'
)
WARNED = True
return [literal_block(text='\n'.join(self.content))]


Expand Down
47 changes: 27 additions & 20 deletions rst2pdf/basenodehandler.py
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# See LICENSE.txt for licensing terms
#$URL$
#$Date$
#$Revision$
# $URL$
# $Date$
# $Revision$

'''
This module provides one useful class: NodeHandler
Expand Down Expand Up @@ -77,6 +77,7 @@ class creation behavior by defining a couple of helper functions.
_baseclass can be explicitly set inside the class definition, in
which case MetaHelper will not override it.
'''

def __new__(clstype, name, bases, clsdict):
# Our base class is the first base in the class definition which
# uses MetaHelper, or None if no such base exists.
Expand Down Expand Up @@ -144,8 +145,10 @@ def _classinit(cls):
t = repr(target)
old = repr(cls.dispatchdict[target])
new = repr(self)
log.debug('Dispatch handler %s for node type %s overridden by %s' %
(old, t, new))
log.debug(
'Dispatch handler %s for node type %s overridden by %s'
% (old, t, new)
)
cls.dispatchdict[target] = self

@staticmethod
Expand All @@ -159,11 +162,10 @@ def getclassname(obj):
def log_unknown(self, node, during):
if not hasattr(self, 'unkn_node'):
self.unkn_node = set()
cln=self.getclassname(node)
cln = self.getclassname(node)
if not cln in self.unkn_node:
self.unkn_node.add(cln)
log.warning("Unkn. node (self.%s): %s [%s]",
during, cln, nodeid(node))
log.warning("Unkn. node (self.%s): %s [%s]", during, cln, nodeid(node))
try:
log.debug(node)
except (UnicodeDecodeError, UnicodeEncodeError):
Expand Down Expand Up @@ -214,9 +216,12 @@ def getstyle(self, client, node, style):
if client.styles.StyleSheet.has_key(node['classes'][0]):
style = client.styles[node['classes'][0]]
else:
log.info("Unknown class %s, ignoring. [%s]",
node['classes'][0], nodeid(node))
except TypeError: # Happens when a docutils.node.Text reaches here
log.info(
"Unknown class %s, ignoring. [%s]",
node['classes'][0],
nodeid(node),
)
except TypeError: # Happens when a docutils.node.Text reaches here
pass

if style is None or style == client.styles['bodytext']:
Expand All @@ -228,13 +233,12 @@ def getelements(self, client, node, style):
elements = self.gather_elements(client, node, style)

# Make all the sidebar cruft unreachable
#if style.__dict__.get('float','None').lower() !='none':
#node.elements=[Sidebar(node.elements,style)]
#elif 'width' in style.__dict__:
# if style.__dict__.get('float','None').lower() !='none':
# node.elements=[Sidebar(node.elements,style)]
# elif 'width' in style.__dict__:

if 'width' in style.__dict__:
elements = [BoundByWidth(style.width,
elements, style, mode="shrink")]
elements = [BoundByWidth(style.width, elements, style, mode="shrink")]

return elements

Expand All @@ -247,13 +251,13 @@ def elemdispatch(self, client, node, style=None):
try:
for i in node['ids']:
client.pending_targets.append(i)
except TypeError: #Happens with docutils.node.Text
except TypeError: # Happens with docutils.node.Text
pass

elements = self.getelements(client, node, style)

if node.line and client.debugLinesPdf:
elements.insert(0,TocEntry(client.depth-1,'LINE-%s'%node.line))
elements.insert(0, TocEntry(client.depth - 1, 'LINE-%s' % node.line))
node.elements = elements
return elements

Expand All @@ -270,8 +274,11 @@ def get_text(self, client, node, replaceEnt):

def apply_smartypants(self, text, smarty, node):
# Try to be clever about when to use smartypants
if node.__class__ in (docutils.nodes.paragraph,
docutils.nodes.block_quote, docutils.nodes.title):
if node.__class__ in (
docutils.nodes.paragraph,
docutils.nodes.block_quote,
docutils.nodes.title,
):
return smartypants(text, smarty)
return text

Expand Down
1 change: 0 additions & 1 deletion rst2pdf/config.py
Expand Up @@ -28,7 +28,6 @@ def getValue(section, key, default=None):


class ConfigError(Exception):

def __init__(self, modulename, msg):
self.modulename = modulename
self.msg = msg
Expand Down
5 changes: 4 additions & 1 deletion rst2pdf/counter_off_role.py
Expand Up @@ -2,10 +2,13 @@

from docutils.nodes import Text, target


def counter_fn(name, rawtext, text, lineno, inliner, options={}, content=[]):
return [], []

counter_fn.content=False

counter_fn.content = False

from docutils.parsers.rst import roles

roles.register_canonical_role('counter', counter_fn)
20 changes: 12 additions & 8 deletions rst2pdf/counter_role.py
Expand Up @@ -7,24 +7,28 @@

class CounterNode(Text):
children = ()

def __init__(self, data, rawsource=''):
if ':' in data:
self.name, value = [s.lower() for s in data.split(':')][:2]
self.value=int(value)
self.value = int(value)
else:
self.name=data.lower()
self.value=values.get(self.name,1)
values[self.name]=self.value+1
self.name = data.lower()
self.value = values.get(self.name, 1)
values[self.name] = self.value + 1

def astext(self):
return str(self.value)


def counter_fn(name, rawtext, text, lineno, inliner, options={}, content=[]):
n=CounterNode(text)
s='%s-%s'%(n.name, n.value)
return [target(ids=[s]),n], []
n = CounterNode(text)
s = '%s-%s' % (n.name, n.value)
return [target(ids=[s]), n], []

counter_fn.content=True

counter_fn.content = True

from docutils.parsers.rst import roles

roles.register_canonical_role('counter', counter_fn)

0 comments on commit 684da51

Please sign in to comment.