Skip to content

Commit

Permalink
Lint update
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed May 19, 2015
1 parent 53219f7 commit 3050e00
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 62 deletions.
16 changes: 16 additions & 0 deletions .prospector.yml
@@ -0,0 +1,16 @@
strictness: medium
doc-warnings: true
test-warnings: false
max-line-length: 120
ignore-patterns:
- docs
- docs_theme
- lib/desktop
dodgy:
run: false
pep257:
disable:
- D202
pylint:
disable:
- import-error
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ python:
install:
- pip install flake8
- pip install flake8_docstrings
- pip install pep8-naming
- pip install nose
- npm install -g jshint
script:
Expand Down
2 changes: 1 addition & 1 deletion ExportBbcode.py
Expand Up @@ -176,7 +176,7 @@ def setup(self, **kwargs):
self.csm = ColorSchemeMatcher(
scheme_file,
ignore_gutter=True,
filter=(lambda x: ColorSchemeTweaker().tweak(x, kwargs["filter"]))
color_filter=(lambda x: ColorSchemeTweaker().tweak(x, kwargs["filter"]))
)
(
self.bground, self.fground, self.sbground,
Expand Down
55 changes: 41 additions & 14 deletions ExportHtml.py
Expand Up @@ -29,9 +29,9 @@
import time
import webbrowser
import re
import json
from ExportHtml.HtmlAnnotations import get_annotations
import ExportHtml.lib.desktop as desktop
import json
from ExportHtml.lib.color_scheme_matcher import ColorSchemeMatcher
from ExportHtml.lib.color_scheme_tweaker import ColorSchemeTweaker
from ExportHtml.lib.notify import notify
Expand Down Expand Up @@ -285,7 +285,7 @@ def getjs(file_name):
else:
with open(path.join(JS_DIR, file_name), "r") as f:
code = f.read()
except:
except Exception:
pass
return code.replace('\r', '')

Expand All @@ -309,7 +309,7 @@ def getcss(file_name, options):
final_code += code[last_pt:m.start()] + options[m.group(1)]
last_pt = m.end()
final_code += code[last_pt:]
except:
except Exception:
pass

return final_code.replace('\r', '')
Expand Down Expand Up @@ -357,6 +357,31 @@ def run(self, **kwargs):
ExportHtml(view).run(**kwargs)


class OpenHtml:

"""Open either a temporary HTML or one at the save location."""

def __init__(self, file_name, save_location=None):
"""Initialize."""

self.file_name = file_name
self.save_location = save_location

def __enter__(self):
"""Setup HTML file."""

if self.save_location is not None:
self.file = open(self.file_name, "w")
else:
self.file = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=self.file_name)
return self.file

def __exit__(self, type, value, traceback):
"""Tear down HTML file."""

self.file.close()


class ExportHtml(object):

"""ExportHtml."""
Expand Down Expand Up @@ -473,7 +498,7 @@ def setup(self, **kwargs):
scheme_file,
ignore_gutter=(not kwargs["style_gutter"]),
track_dark_background=True,
filter=(lambda x: ColorSchemeTweaker().tweak(x, kwargs["filter"]))
color_filter=(lambda x: ColorSchemeTweaker().tweak(x, kwargs["filter"]))
)

if kwargs["shift_brightness"]:
Expand Down Expand Up @@ -592,7 +617,7 @@ def convert_view_to_html(self, html):
for line in self.view.split_by_newlines(sublime.Region(self.pt, self.size)):
self.size = line.end()
empty = not bool(line.size())
line = self.convert_line_to_html(html, empty)
line = self.convert_line_to_html(empty)
html.write(self.print_line(line, self.curr_row))
self.curr_row += 1

Expand Down Expand Up @@ -722,7 +747,7 @@ def format_text(self, line, text, color, bgcolor, style, empty, annotate=False):
)
line.append(code)

def convert_line_to_html(self, html, empty):
def convert_line_to_html(self, empty):
"""Convert the line to its HTML representation."""

line = []
Expand Down Expand Up @@ -921,6 +946,13 @@ def add_comments_table(self, html):
html.write(ANNOTATION_FOOTER)
html.write(ANNOTATION_TBL_END)

def open_html(self, x, save_location):
"""Open html file."""
if save_location is not None:
return open(x, "w")
else:
return tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=x)

def run(self, **kwargs):
"""Run command."""

Expand All @@ -934,8 +966,8 @@ def run(self, **kwargs):
fname = self.view.file_name()
if (
((fname is None or not path.exists(fname)) and save_location == ".") or
not path.exists(save_location)
or not path.isdir(save_location)
not path.exists(save_location) or
not path.isdir(save_location)
):
html_file = ".html"
save_location = None
Expand All @@ -950,12 +982,7 @@ def run(self, **kwargs):
else:
html_file = ".html"

if save_location is not None:
open_html = lambda x: open(x, "w")
else:
open_html = lambda x: tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=x)

with open_html(html_file) as html:
with OpenHtml(html_file, save_location) as html:
self.write_header(html)
self.write_body(html)
if inputs["clipboard_copy"]:
Expand Down
3 changes: 2 additions & 1 deletion docs/contributing.md
Expand Up @@ -69,11 +69,12 @@ There are a couple of dependencies that must be present before running the tests
nosetests .
```

3. Linting is performed on the entire project with `flake8` and `flake8_docstrings`. These can be installed via:
3. Linting is performed on the entire project with `flake8`, `flake8_docstrings`, `pep8-naming`. These can be installed via:

```
pip install flake8
pip install flake8_docstrings
pip install pep8-naming
```

Linting is performed with the following command:
Expand Down
6 changes: 6 additions & 0 deletions docs/usage.md
Expand Up @@ -7,6 +7,12 @@ Configuring and using ExportHtml
# Exporting HTML
ExportHtml comes with a number of default commands available, but these can be overridden in the settings file. Or you can create commands directly outside of the settings file bound to the command palette, key bindings, or even the menu.

!!! tip "Tip"
| Parameter | Type |Description |
|-----------|------|------------|
| numbers | boolean | Display line numbers in the gutter. |
| multi_select | boolean | If multiple regions are selected in a document, only export what is under those selections. By default only the first selection is recognized. Default is `false`. |

If adding a command to the settings file, it goes under the `html_panel` setting. These configurations will appear under the `Export to HTML: Show Export Menu` command palette command.

```javascript
Expand Down
16 changes: 8 additions & 8 deletions lib/color_scheme_matcher.py
Expand Up @@ -26,14 +26,14 @@
from __future__ import absolute_import
import sublime
import re
from .rgba import RGBA
from os import path
from collections import namedtuple
ST3 = int(sublime.version()) >= 3000
if not ST3:
from plistlib import readPlist
else:
from plistlib import readPlistFromBytes
from .rgba import RGBA
from os import path
from collections import namedtuple


class SchemeColors(
Expand Down Expand Up @@ -69,18 +69,18 @@ class ColorSchemeMatcher(object):

"""Determine color scheme colors and style for text in a Sublime view buffer."""

def __init__(self, scheme_file, ignore_gutter=False, track_dark_background=False, filter=None):
def __init__(self, scheme_file, ignore_gutter=False, track_dark_background=False, color_filter=None):
"""Initialize."""
if filter is None:
filter = self.filter
if color_filter is None:
color_filter = self.filter
self.color_scheme = path.normpath(scheme_file)
self.scheme_file = path.basename(self.color_scheme)
if ST3:
self.plist_file = filter(
self.plist_file = color_filter(
readPlistFromBytes(sublime.load_binary_resource(sublime_format_path(self.color_scheme)))
)
else:
self.plist_file = filter(
self.plist_file = color_filter(
readPlist(sublime.packages_path() + self.color_scheme.replace('Packages', ''))
)
self.scheme_file = scheme_file
Expand Down
6 changes: 3 additions & 3 deletions lib/color_scheme_tweaker.py
Expand Up @@ -51,12 +51,12 @@ def _filter_colors(self, *args, **kwargs):
try:
assert(fg is not None)
rgba_fg = RGBA(fg)
except:
except Exception:
rgba_fg = fg
try:
assert(bg is not None)
rgba_bg = RGBA(bg)
except:
except Exception:
rgba_bg = bg

for f in self.filters:
Expand All @@ -79,7 +79,7 @@ def _filter_colors(self, *args, **kwargs):
bg = rgba.get_rgb() + ("%02X" % int((255.0 * value)))
try:
rgba_bg = RGBA(bg)
except:
except Exception:
rgba_bg = bg
return (
rgba_fg.get_rgba() if isinstance(rgba_fg, RGBA) else rgba_fg,
Expand Down
4 changes: 2 additions & 2 deletions lib/notify.py
Expand Up @@ -2,8 +2,8 @@
import sublime
try:
from SubNotify.sub_notify import SubNotifyIsReadyCommand as Notify
except:
class Notify:
except Exception:
class Notify(object):

"""Fallback Notify for when SubNotify is not defined."""

Expand Down
69 changes: 37 additions & 32 deletions lib/rgba.py
Expand Up @@ -38,6 +38,7 @@ def _split_channels(self, s):
"""Split the color into color channels: red, green, blue, alpha."""

def alpha_channel(alpha):
"""Get alpha channel."""
return int(alpha, 16) if alpha else 0xFF

m = self.color_pattern.match(s)
Expand Down Expand Up @@ -159,43 +160,47 @@ def sepia(self):
b = clamp(int((self.r * .272) + (self.g * .534) + (self.b * .131)), 0, 255) & 0xFF
self.r, self.g, self.b = r, g, b

def _get_overage(self, c):
"""Get overage."""

if c < 0.0:
o = 0.0 + c
c = 0.0
elif c > 255.0:
o = c - 255.0
c = 255.0
else:
o = 0.0
return o, c

def _distribute_overage(self, c, o, s):
"""Distribute overage."""

channels = len(s)
if channels == 0:
return c
parts = o / len(s)
if "r" in s and "g" in s:
c = c[0] + parts, c[1] + parts, c[2]
elif "r" in s and "b" in s:
c = c[0] + parts, c[1], c[2] + parts
elif "g" in s and "b" in s:
c = c[0], c[1] + parts, c[2] + parts
elif "r" in s:
c = c[0] + parts, c[1], c[2]
elif "g" in s:
c = c[0], c[1] + parts, c[2]
else: # "b" in s:
c = c[0], c[1], c[2] + parts
return c

def brightness(self, factor):
"""
Adjust the brightness by the given factor.
Brightness is determined by percieved luminance.
"""

def get_overage(c):
if c < 0.0:
o = 0.0 + c
c = 0.0
elif c > 255.0:
o = c - 255.0
c = 255.0
else:
o = 0.0
return o, c

def distribute_overage(c, o, s):
channels = len(s)
if channels == 0:
return c
parts = o / len(s)
if "r" in s and "g" in s:
c = c[0] + parts, c[1] + parts, c[2]
elif "r" in s and "b" in s:
c = c[0] + parts, c[1], c[2] + parts
elif "g" in s and "b" in s:
c = c[0], c[1] + parts, c[2] + parts
elif "r" in s:
c = c[0] + parts, c[1], c[2]
elif "g" in s:
c = c[0], c[1] + parts, c[2]
else: # "b" in s:
c = c[0], c[1], c[2] + parts
return c

channels = ["r", "g", "b"]
total_lumes = clamp(self.luminance() + (255.0 * factor) - 255.0, 0.0, 255.0)

Expand All @@ -212,10 +217,10 @@ def distribute_overage(c, o, s):
components = [float(self.r) + pts, float(self.g) + pts, float(self.b) + pts]
count = 0
for c in channels:
overage, components[count] = get_overage(components[count])
overage, components[count] = self._get_overage(components[count])
if overage:
slots.remove(c)
components = list(distribute_overage(components, overage, slots))
components = list(self._distribute_overage(components, overage, slots))
count += 1

self.r = clamp(int(round(components[0])), 0, 255) & 0xFF
Expand Down
1 change: 0 additions & 1 deletion mkdocs.yml
Expand Up @@ -10,7 +10,6 @@ pages:
theme: readthedocs
theme_dir: doc_theme
markdown_extensions:
- markdown.extensions.toc
- markdown.extensions.extra
- markdown.extensions.admonition
- markdown.extensions.smarty
Expand Down

0 comments on commit 3050e00

Please sign in to comment.