Skip to content

Commit

Permalink
Internal: Fix some pylint errors and sort imports.
Browse files Browse the repository at this point in the history
IMPORTS

1. As suggested by google the imports are grouped into

   a) system imports
   b) global imports (ST API)
   c) package imports

   where possible and sorted by name.

2. ImportError is no longer catched from local relative imports as python 2
   raises a ValueError only, if relative import fails. ImportError might be
   something worse, we want to see on ST3.

3. Generally changed all system- & global imports not to import functions
   or objects where possible. Use simple import <library> instead.

DOCSTRINGS

Added/fixed some docstrings to comply with pydocstyle and pylint.

GITIGNORE

Added some entries to the .gitignore

_*.vcs-cache_ is created by SublimeLinter
_*.sublime-workspace_ mustn't be included in a commit, too.
  • Loading branch information
deathaxe committed Feb 25, 2017
1 parent 255454d commit 1b6b1d5
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -33,3 +33,7 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# Sublime Text
*.sublime-workspace
*.vcs-cache
15 changes: 8 additions & 7 deletions git_gutter.py
@@ -1,4 +1,4 @@
from sublime_plugin import TextCommand
import sublime_plugin

try:
from .git_gutter_settings import settings
Expand All @@ -10,7 +10,7 @@
from .git_gutter_jump_to_changes import GitGutterJumpToChanges
from .git_gutter_popup import show_diff_popup
from .git_gutter_show_diff import GitGutterShowDiff
except (ImportError, ValueError):
except ValueError:
from git_gutter_settings import settings
from git_gutter_handler import GitGutterHandler
from git_gutter_compare import (
Expand All @@ -22,9 +22,10 @@
from git_gutter_show_diff import GitGutterShowDiff


class GitGutterCommand(TextCommand):
class GitGutterCommand(sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
TextCommand.__init__(self, *args, **kwargs)
"""Initialize GitGutterCommand object."""
sublime_plugin.TextCommand.__init__(self, *args, **kwargs)
self.git_handler = GitGutterHandler(self.view)
self.show_diff_handler = GitGutterShowDiff(self.git_handler)
# Last enabled state for change detection
Expand All @@ -51,12 +52,12 @@ def is_enabled(self, **kwargs):
elif view.settings().get("repl"):
valid = False
# Don't handle binary files
elif view.encoding() in ('Hexadecimal'):
elif view.encoding() == 'Hexadecimal':
valid = False
else:
# Validate work tree on certain events only
validate = any(event in ('load', 'activated', 'post-save')
for event in kwargs.get('event_type', []))
for event in kwargs.get('event_type', []))
# Don't handle files outside a repository
if not self.git_handler.work_tree(validate):
valid = False
Expand Down Expand Up @@ -119,7 +120,7 @@ def _handle_subcommand(self, **kwargs):
assert False, 'Unhandled sub command "%s"' % action


class GitGutterBaseCommand(TextCommand):
class GitGutterBaseCommand(sublime_plugin.TextCommand):
def is_enabled(self, **kwargs):
return self.view.settings().get('git_gutter_enabled', False)

Expand Down
5 changes: 2 additions & 3 deletions git_gutter_compare.py
@@ -1,5 +1,4 @@
from functools import partial

import functools
import sublime


Expand All @@ -26,7 +25,7 @@ def item_to_commit(self, item):
def _show_quick_panel(self, results):
if results:
self.git_handler.view.window().show_quick_panel(
results, partial(self._on_select, results))
results, functools.partial(self._on_select, results))

def _on_select(self, results, selected):
if 0 > selected < len(results):
Expand Down
2 changes: 1 addition & 1 deletion git_gutter_events.py
Expand Up @@ -5,7 +5,7 @@

try:
from .git_gutter_settings import settings
except (ImportError, ValueError):
except ValueError:
from git_gutter_settings import settings

try:
Expand Down
32 changes: 17 additions & 15 deletions git_gutter_handler.py
@@ -1,35 +1,34 @@
import codecs
import os.path
import subprocess
import re
import codecs
import functools
import subprocess
import tempfile
import zipfile

try:
from io import BytesIO
except (ImportError, ValueError):
except ImportError:
from cStringIO import StringIO as BytesIO

try:
from subprocess import TimeoutExpired
_HAVE_TIMEOUT = True
except ImportError:
class TimeoutExpired(Exception):
pass
_HAVE_TIMEOUT = False

import sublime

try:
from .git_gutter_settings import settings
from .modules import path
from .modules.promise import Promise
except (ImportError, ValueError):
except ValueError:
from git_gutter_settings import settings
from modules import path
from modules.promise import Promise

try:
from subprocess import TimeoutExpired
_HAVE_TIMEOUT = True
except:
class TimeoutExpired(Exception):
pass
_HAVE_TIMEOUT = False

# The view class has a method called 'change_count()'
_HAVE_VIEW_CHANGE_COUNT = hasattr(sublime.View, "change_count")

Expand Down Expand Up @@ -293,7 +292,8 @@ def write_file(output):
return self.git_compare_commit(refs).then(check_commit)
return check_commit(refs)

def process_diff(self, diff_str):
@staticmethod
def process_diff(diff_str):
r"""Parse unified diff with 0 lines of context.
Returns:
Expand Down Expand Up @@ -341,7 +341,6 @@ def process_diff(self, diff_str):

def diff_str(self):
"""Run git diff against view and decode the result then."""

def decode_diff(results):
encoding = self._get_view_encoding()
try:
Expand Down Expand Up @@ -545,6 +544,7 @@ def git_file_commits(self):
return self.run_command(args)

def git_branches(self):
"""Query all branches of the file's repository."""
args = [
settings.git_binary_path,
'for-each-ref',
Expand All @@ -555,6 +555,7 @@ def git_branches(self):
return self.run_command(args)

def git_tags(self):
"""Query all tags of the file's repository."""
args = [
settings.git_binary_path,
'show-ref',
Expand All @@ -564,6 +565,7 @@ def git_tags(self):
return self.run_command(args)

def git_current_branch(self):
"""Query the current branch of the file's repository."""
args = [
settings.git_binary_path,
'rev-parse',
Expand Down
2 changes: 1 addition & 1 deletion git_gutter_jump_to_changes.py
@@ -1,6 +1,6 @@
try:
from .git_gutter_settings import settings
except (ImportError, ValueError):
except ValueError:
from git_gutter_settings import settings


Expand Down
9 changes: 4 additions & 5 deletions git_gutter_popup.py
Expand Up @@ -3,16 +3,15 @@

try:
if int(sublime.version()) < 3080:
raise Exception("No popup available.")
raise ImportError("No popup available.")

import difflib
import html
from functools import partial
import jinja2
import mdpopups

_MDPOPUPS_INSTALLED = True
import mdpopups
import jinja2
except:
except ImportError:
_MDPOPUPS_INSTALLED = False

if _MDPOPUPS_INSTALLED:
Expand Down
18 changes: 11 additions & 7 deletions git_gutter_settings.py
@@ -1,7 +1,7 @@
import os
import shutil
import sublime
from sublime_plugin import ApplicationCommand
import sublime_plugin

STVER = int(sublime.version())
ST3 = STVER >= 3000
Expand All @@ -11,13 +11,14 @@ def plugin_loaded():
settings.load_settings()


class GitGutterOpenFileCommand(ApplicationCommand):
class GitGutterOpenFileCommand(sublime_plugin.ApplicationCommand):
"""This is a wrapper class for SublimeText's `open_file` command.
The task is to hide the command in menu if `edit_settings` is available.
"""

def run(self, file):
@staticmethod
def run(file):
"""Expand variables and open the resulting file.
NOTE: For some unknown reason the `open_file` command doesn't expand
Expand All @@ -31,22 +32,25 @@ def run(self, file):
file = file.replace('${platform}', platform_name)
sublime.run_command('open_file', {'file': file})

def is_visible(self):
@staticmethod
def is_visible():
"""Return True to to show the command in command pallet and menu."""
return STVER < 3124


class GitGutterEditSettingsCommand(ApplicationCommand):
class GitGutterEditSettingsCommand(sublime_plugin.ApplicationCommand):
"""This is a wrapper class for SublimeText's `open_file` command.
Hides the command in menu if `edit_settings` is not available.
"""

def run(self, **kwargs):
@staticmethod
def run(**kwargs):
"""Expand variables and open the resulting file."""
sublime.run_command('edit_settings', kwargs)

def is_visible(self):
@staticmethod
def is_visible():
"""Return True to to show the command in command pallet and menu."""
return STVER >= 3124

Expand Down
24 changes: 11 additions & 13 deletions git_gutter_show_diff.py
@@ -1,20 +1,18 @@
import os
import sublime

try:
# avoid exceptions if dependency is not yet satisfied
import jinja2.environment
_HAVE_JINJA2 = True
except (ImportError, ValueError):
except ImportError:
_HAVE_JINJA2 = False

import sublime

try:
from .git_gutter_settings import settings
except (ImportError, ValueError):
except ValueError:
from git_gutter_settings import settings

ST3 = int(sublime.version()) >= 3000
_ICON_EXT = '.png' if ST3 else ''
_ICON_EXT = '.png' if int(sublime.version()) >= 3000 else ''


class GitGutterShowDiff(object):
Expand Down Expand Up @@ -168,15 +166,15 @@ def _contents_to_regions(self, contents):
return (
# deleted regions
self._deleted_lines_to_regions(
first_line, del_lines, lines_regions, protected)
first_line, del_lines, lines_regions, protected) +
# inserted regions
+ [self._lines_to_regions(
first_line, ins_lines, lines_regions, protected)]
[self._lines_to_regions(
first_line, ins_lines, lines_regions, protected)] +
# modified regions
+ [self._lines_to_regions(
first_line, mod_lines, lines_regions, protected)]
[self._lines_to_regions(
first_line, mod_lines, lines_regions, protected)] +
# untracked / ignored regions
+ [] + [])
[] + [])

def _get_modified_region(self, first_line, last_line):
"""Create a list of all line start points in the modified Region.
Expand Down
1 change: 1 addition & 0 deletions modules/path.py
Expand Up @@ -2,6 +2,7 @@

try:
from nt import _getfinalpathname

def realpath(path):
"""Resolve symlinks and return real path to file.
Expand Down

0 comments on commit 1b6b1d5

Please sign in to comment.