Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up to match pep8 style guide

  • Loading branch information...
commit 7aeffc621039e833c96ce6c7f83c14ecd5912592 1 parent 823e638
@skuroda authored
View
2  AdvancedNewFile.py
@@ -20,5 +20,3 @@
else:
from advanced_new_file import reloader
from advanced_new_file.commands import *
-
-
View
18 advanced_new_file/anf_util.py
@@ -61,6 +61,7 @@
TOP_LEVEL_SPLIT_CHAR = ":"
IS_ST3 = int(sublime.version()) > 3000
+
def generate_creation_path(settings, base, path, append_extension=False):
if PLATFORM == "windows":
if not re.match(WIN_ROOT_REGEX, base):
@@ -87,11 +88,12 @@ def generate_creation_path(settings, base, path, append_extension=False):
full_path += settings.get(DEFAULT_EXTENSION_SETTING)
return full_path
+
def get_settings(view):
settings = sublime.load_settings("AdvancedNewFile.sublime-settings")
project_settings = {}
local_settings = {}
- if view != None:
+ if view is not None:
project_settings = view.settings().get('AdvancedNewFile', {})
for setting in SETTINGS:
@@ -101,16 +103,24 @@ def get_settings(view):
if key in SETTINGS:
if key == "alias":
if IS_ST3:
- local_settings[key] = dict(local_settings[key].items() | project_settings.get(key).items())
+ local_settings[key] = dict(
+ local_settings[key].items() |
+ project_settings.get(key).items()
+ )
else:
- local_settings[key] = dict(local_settings[key].items() + project_settings.get(key).items())
+ local_settings[key] = dict(
+ local_settings[key].items() +
+ project_settings.get(key).items()
+ )
else:
local_settings[key] = project_settings[key]
else:
- print("AdvancedNewFile[Warning]: Invalid key '%s' in project settings.", key)
+ print("AdvancedNewFile[Warning]: Invalid key " +
+ "'%s' in project settings.", key)
return local_settings
+
def get_project_folder_data(use_folder_name):
folders = []
folder_entries = []
View
62 advanced_new_file/commands/command_base.py
@@ -22,18 +22,21 @@ def __init__(self, window):
else:
self.platform = NixPlatform()
-
def __generate_default_root(self):
- default_root = self.__get_default_root(self.settings.get(DEFAULT_ROOT_SETTING))
+ default_root = self.__get_default_root(
+ self.settings.get(DEFAULT_ROOT_SETTING))
if default_root == "path":
- self.root = os.path.expanduser(self.settings.get(DEFAULT_PATH_SETTING))
+ self.root = os.path.expanduser(
+ self.settings.get(DEFAULT_PATH_SETTING))
default_root = ""
return self.split_path(default_root)
def __generate_alias_root(self):
- alias_root = self.__get_default_root(self.settings.get(ALIAS_ROOT_SETTING), True)
+ alias_root = self.__get_default_root(
+ self.settings.get(ALIAS_ROOT_SETTING), True)
if alias_root == "path":
- self.alias_root = os.path.expanduser(self.settings.get(ALIAS_PATH_SETTING))
+ self.alias_root = os.path.expanduser(
+ self.settings.get(ALIAS_PATH_SETTING))
alias_root = ""
return self.split_path(alias_root, True)
@@ -81,7 +84,8 @@ def __get_aliases(self):
def __get_default_root(self, string, is_alias=False):
root = ""
self.alias_folder_index = self.settings.get(ALIAS_FOLDER_INDEX_SETTING)
- self.default_folder_index = self.settings.get(DEFAULT_FOLDER_INDEX_SETTING)
+ self.default_folder_index = self.settings.get(
+ DEFAULT_FOLDER_INDEX_SETTING)
if string == "home":
root = "~/"
elif string == "current":
@@ -110,7 +114,7 @@ def split_path(self, path="", is_alias=False):
try:
root, path = self.platform.split(path)
# Parse if alias
- if TOP_LEVEL_SPLIT_CHAR in path and root == None:
+ if TOP_LEVEL_SPLIT_CHAR in path and root is None:
parts = path.rsplit(TOP_LEVEL_SPLIT_CHAR, 1)
root, path = self.__translate_alias(parts[0])
path_list = []
@@ -123,10 +127,11 @@ def split_path(self, path="", is_alias=False):
root, path_offset = self.platform.parse_nix_path(root, path)
path = path[path_offset:]
# Parse if tilde used
- elif re.match(HOME_REGEX, path) and root == None:
+ elif re.match(HOME_REGEX, path) and root is None:
root = os.path.expanduser("~")
path = path[2:]
- elif re.match(r"^\.{1,2}[/\\]", path) and self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False):
+ elif (re.match(r"^\.{1,2}[/\\]", path) and
+ self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False)):
path_index = 2
root = os.path.dirname(self.view.file_name())
if re.match(r"^\.{2}[/\\]", path):
@@ -135,7 +140,7 @@ def split_path(self, path="", is_alias=False):
path = path[path_index:]
# Default
- if root == None:
+ if root is None:
if is_alias:
root = self.alias_root
folder_index = self.alias_folder_index
@@ -160,9 +165,10 @@ def __translate_alias(self, path):
join_index = len(split_path) - 1
target = path
root_found = False
+ use_folder_name = self.settings.get(USE_FOLDER_NAME_SETTING)
while join_index >= 0 and not root_found:
# Folder aliases
- for name, folder in get_project_folder_data(self.settings.get(USE_FOLDER_NAME_SETTING)):
+ for name, folder in get_project_folder_data(use_folder_name):
if name == target:
root = folder
root_found = True
@@ -172,7 +178,8 @@ def __translate_alias(self, path):
if alias == target:
alias_path = self.aliases.get(alias)
if re.search(HOME_REGEX, alias_path) is None:
- root = self.platform.get_alias_absolute_path(self.alias_root, alias_path)
+ root = self.platform.get_alias_absolute_path(
+ self.alias_root, alias_path)
if root is not None:
break
root = os.path.expanduser(alias_path)
@@ -189,7 +196,8 @@ def __translate_alias(self, path):
else:
# Add to index so we re
join_index += 2
- return os.path.abspath(root), TOP_LEVEL_SPLIT_CHAR.join(split_path[join_index:])
+ return (os.path.abspath(root),
+ TOP_LEVEL_SPLIT_CHAR.join(split_path[join_index:]))
def input_panel_caption(self):
return ""
@@ -201,7 +209,8 @@ def show_filename_input(self, initial):
)
self.input_panel_view.set_name(VIEW_NAME)
- self.input_panel_view.settings().set("auto_complete_commit_on_tab", False)
+ self.input_panel_view.settings().set("auto_complete_commit_on_tab",
+ False)
self.input_panel_view.settings().set("tab_completion", False)
self.input_panel_view.settings().set("translate_tabs_to_spaces", False)
self.input_panel_view.settings().set("anf_panel", True)
@@ -215,11 +224,13 @@ def __update_filename_input(self, path_in):
if path_in.endswith("\t"):
new_content = self.completion.completion(path_in.replace("\t", ""))
if new_content != path_in:
- self.input_panel_view.run_command("anf_replace", {"content": new_content})
+ self.input_panel_view.run_command("anf_replace",
+ {"content": new_content})
else:
base, path = self.split_path(path_in)
- creation_path = generate_creation_path(self.settings, base, path, True)
+ creation_path = generate_creation_path(self.settings, base, path,
+ True)
if self.settings.get(SHOW_PATH_SETTING, False):
self.update_status_message(creation_path)
@@ -249,9 +260,9 @@ def entered_filename(self, filename):
base, path = self.split_path(filename)
file_path = generate_creation_path(self.settings, base, path, True)
# Check for invalid alias specified.
- if TOP_LEVEL_SPLIT_CHAR in filename and \
- not (PLATFORM == "windows" and re.match(WIN_ROOT_REGEX, base)) and \
- not (PLATFORM != "windows" and re.match(NIX_ROOT_REGEX, base)):
+ is_valid = (TOP_LEVEL_SPLIT_CHAR in filename and
+ self.platform.is_absolute_path(base))
+ if is_valid:
if base == "":
error_message = "Current file cannot be resolved."
else:
@@ -264,7 +275,8 @@ def open_file(self, file_path):
new_view = None
if os.path.isdir(file_path):
if not re.search(r"(/|\\)$", file_path):
- sublime.error_message("Cannot open view for '" + file_path + "'. It is a directory. ")
+ sublime.error_message("Cannot open view for '" + file_path +
+ "'. It is a directory. ")
else:
new_view = self.window.open_file(file_path)
return new_view
@@ -277,7 +289,7 @@ def refresh_sidebar(self):
pass
def clear(self):
- if self.view != None:
+ if self.view is not None:
self.view.erase_status("AdvancedNewFile")
self.view.erase_status("AdvancedNewFile2")
@@ -313,14 +325,13 @@ def create_folder(self, path):
if self.is_python:
creation_path = os.path.join(entry, '__init__.py')
open(creation_path, 'a').close()
- if file_permissions != "":
+ if file_permissions != "":
os.chmod(creation_path, int(file_permissions, 8))
- if folder_permissions != "":
+ if folder_permissions != "":
os.chmod(entry, int(folder_permissions, 8))
-
def get_cursor_path(self):
- if self.view == None:
+ if self.view is None:
return ""
view = self.view
@@ -337,7 +348,6 @@ def get_cursor_path(self):
return path
-
def _find_open_file(self, file_name):
window = self.window
if IS_ST3:
View
13 advanced_new_file/commands/delete_file_command.py
@@ -6,7 +6,8 @@
from .command_base import AdvancedNewFileBase
-class AdvancedNewFileDelete(AdvancedNewFileBase, sublime_plugin.WindowCommand, GitCommandBase):
+class AdvancedNewFileDelete(AdvancedNewFileBase, sublime_plugin.WindowCommand,
+ GitCommandBase):
def __init__(self, window):
super(AdvancedNewFileDelete, self).__init__(window)
@@ -25,9 +26,9 @@ def entered_file_action(self, path):
self._delete_file(path)
def update_status_message(self, creation_path):
- if self.view != None:
- self.view.set_status("AdvancedNewFile", "Delete file at %s " % \
- creation_path)
+ if self.view is not None:
+ self.view.set_status("AdvancedNewFile", "Delete file at %s " %
+ creation_path)
else:
sublime.status_message("Delete file at %s" % creation_path)
@@ -51,7 +52,9 @@ def _delete_file(self, filepath):
if not sublime.ok_cancel_dialog("Delete this file?\n%s" % filepath):
return
- if self.file_tracked_by_git(filepath) and self.settings.get(VCS_MANAGEMENT_SETTING):
+ vcs_tracking = (self.file_tracked_by_git(filepath) and
+ self.settings.get(VCS_MANAGEMENT_SETTING))
+ if vcs_tracking:
self._git_rm(filepath)
else:
self.window.run_command("delete_file", {"files": [filepath]})
View
5 advanced_new_file/commands/helper_commands.py
@@ -1,12 +1,15 @@
import sublime
import sublime_plugin
+
class AnfReplaceCommand(sublime_plugin.TextCommand):
def run(self, edit, content):
self.view.replace(edit, sublime.Region(0, self.view.size()), content)
+
class AdvancedNewFileCommand(sublime_plugin.WindowCommand):
- def run(self, is_python=False, initial_path=None, rename=False, rename_file=None):
+ def run(self, is_python=False, initial_path=None,
+ rename=False, rename_file=None):
args = {}
if rename:
args["is_python"] = is_python
View
39 advanced_new_file/commands/move_file_command.py
@@ -8,7 +8,8 @@
from ..anf_util import *
-class AdvancedNewFileMove(AdvancedNewFileBase, sublime_plugin.WindowCommand, GitCommandBase):
+class AdvancedNewFileMove(AdvancedNewFileBase, sublime_plugin.WindowCommand,
+ GitCommandBase):
def __init__(self, window):
super(AdvancedNewFileMove, self).__init__(window)
@@ -19,18 +20,28 @@ def run(self, is_python=False, initial_path=None, rename_file=None):
path = self.settings.get(RENAME_DEFAULT_SETTING)
current_file = self.view.file_name()
- current_file_name = os.path.basename(self.view.file_name()) if current_file else ""
+ if current_file:
+ current_file_name = os.path.basename(self.view.file_name())
+ else:
+ current_file_name = ""
+
path = path.replace("<filepath>", current_file)
path = path.replace("<filename>", current_file_name)
- self.show_filename_input(path if len(path) > 0 else self.generate_initial_path())
+ self.show_filename_input(
+ path if len(path) > 0 else self.generate_initial_path())
def input_panel_caption(self):
caption = 'Enter a new path for current file'
view = self.window.active_view()
+ self.original_name = None
if view is not None:
- self.original_name = os.path.basename(view.file_name()) if view.file_name is not None else ""
- else:
+ view_file_name = view.file_name()
+ if view_file_name:
+ self.original_name = os.path.basename(view_file_name)
+
+ if self.original_name is None:
self.original_name = ""
+
if self.is_python:
caption = '%s (creates __init__.py in new dirs)' % caption
return caption
@@ -40,7 +51,8 @@ def _git_mv(self, from_filepath, to_filepath):
args = ["mv", filename, to_filepath]
result = self.run_command(args, path)
if result != 0:
- sublime.error_message("Git move of %s to %s failed" % (from_filepath, to_filepath))
+ sublime.error_message("Git move of %s to %s failed" %
+ (from_filepath, to_filepath))
def entered_file_action(self, path):
attempt_open = True
@@ -50,7 +62,8 @@ def entered_file_action(self, path):
self.create_folder(directory)
except OSError as e:
attempt_open = False
- sublime.error_message("Cannot create '" + path + "'. See console for details")
+ sublime.error_message("Cannot create '" + path + "'." +
+ " See console for details")
print("Exception: %s '%s'" % (e.strerror, e.filename))
if attempt_open:
@@ -100,11 +113,12 @@ def _move_action(self, from_file, to_file):
shutil.move(from_file, to_file)
def update_status_message(self, creation_path):
- if self.view != None:
- if os.path.isdir(creation_path) or os.path.basename(creation_path) == "" :
+ if self.view is not None:
+ if (os.path.isdir(creation_path) or
+ os.path.basename(creation_path) == ""):
creation_path = os.path.join(creation_path, self.original_name)
- self.view.set_status("AdvancedNewFile", "Moving file to %s " % \
- creation_path)
+ self.view.set_status("AdvancedNewFile", "Moving file to %s " %
+ creation_path)
else:
sublime.status_message("Moving file to %s" % creation_path)
@@ -113,7 +127,8 @@ class AdvancedNewFileMoveAtCommand(sublime_plugin.WindowCommand):
def run(self, files):
if len(files) != 1:
return
- self.window.run_command("advanced_new_file_move", {"rename_file": files[0]})
+ self.window.run_command("advanced_new_file_move",
+ {"rename_file": files[0]})
def is_visible(self, files):
return len(files) == 1
View
14 advanced_new_file/commands/new_file_command.py
@@ -3,6 +3,7 @@
from .command_base import AdvancedNewFileBase
from ..anf_util import *
+
class AdvancedNewFileNew(AdvancedNewFileBase, sublime_plugin.WindowCommand):
def __init__(self, window):
super(AdvancedNewFileNew, self).__init__(window)
@@ -25,24 +26,27 @@ def entered_file_action(self, path):
self.create(path)
except OSError as e:
attempt_open = False
- sublime.error_message("Cannot create '" + path + "'. See console for details")
+ sublime.error_message("Cannot create '" + path +
+ "'. See console for details")
print("Exception: %s '%s'" % (e.strerror, e.filename))
if attempt_open:
self.open_file(path)
def update_status_message(self, creation_path):
- if self.view != None:
- self.view.set_status("AdvancedNewFile", "Creating file at %s " % \
- creation_path)
+ if self.view is not None:
+ self.view.set_status("AdvancedNewFile", "Creating file at %s " %
+ creation_path)
else:
sublime.status_message("Creating file at %s" % creation_path)
+
class AdvancedNewFileNewAtCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
if len(dirs) != 1:
return
path = dirs[0] + os.sep
- self.window.run_command("advanced_new_file_new", {"initial_path": path})
+ self.window.run_command("advanced_new_file_new",
+ {"initial_path": path})
def is_visible(self, dirs):
return len(dirs) == 1
View
12 advanced_new_file/completions/completion_base.py
@@ -2,6 +2,7 @@
import os
from ..anf_util import *
+
class GenerateCompletionListBase(object):
"""docstring for GenerateCompletionListBase"""
def __init__(self, command):
@@ -11,11 +12,17 @@ def __init__(self, command):
self.aliases = command.aliases
self.settings = command.settings
+ def is_home(self, path):
+ return re.match(r"^~[/\\]", path)
+
+ def is_alias(self, path_):
+ return self.top_level_split_char in path
+
def generate_completion_list(self, path_in):
alias_list = []
dir_list = []
file_list = []
- if self.top_level_split_char in path_in or re.match(r"^~[/\\]", path_in):
+ if self.is_alias(path_in) or self.is_home(path_in):
pass
else:
directory, filename = os.path.split(path_in)
@@ -47,7 +54,8 @@ def generate_completion_list(self, path_in):
return sorted(completion_list), alias_list, dir_list, file_list
def generate_project_auto_complete(self, base):
- folder_data = get_project_folder_data(self.settings.get(USE_FOLDER_NAME_SETTING))
+ folder_data = get_project_folder_data(
+ self.settings.get(USE_FOLDER_NAME_SETTING))
if len(folder_data) > 1:
folders = [x[0] for x in folder_data]
return self.generate_auto_complete(base, folders)
View
9 advanced_new_file/completions/nix_completion.py
@@ -5,6 +5,7 @@
from .completion_base import GenerateCompletionListBase
from ..anf_util import *
+
class NixCompletion(GenerateCompletionListBase):
def __init__(self, command):
super(NixCompletion, self).__init__(command)
@@ -12,12 +13,13 @@ def __init__(self, command):
def completion(self, path_in):
pattern = r"(.*[/\\:])(.*)"
- completion_list, alias_list, dir_list, file_list = self.generate_completion_list(path_in)
+ (completion_list, alias_list,
+ dir_list, file_list) = self.generate_completion_list(path_in)
new_content = path_in
if len(completion_list) > 0:
common = os.path.commonprefix(completion_list)
match = re.match(pattern, path_in)
- if match :
+ if match:
new_content = re.sub(pattern, r"\1", path_in)
new_content += common
else:
@@ -25,7 +27,8 @@ def completion(self, path_in):
if len(completion_list) > 1:
dir_list = map(lambda s: s + "/", dir_list)
alias_list = map(lambda s: s + ":", alias_list)
- status_message_list = sorted(list(dir_list) + list(alias_list) + file_list)
+ status_message_list = sorted(list(dir_list) +
+ list(alias_list) + file_list)
sublime.status_message(", ".join(status_message_list))
else:
if completion_list[0] in alias_list:
View
14 advanced_new_file/completions/windows_completion.py
@@ -2,6 +2,7 @@
from .completion_base import GenerateCompletionListBase
from ..anf_util import *
+
class WindowsCompletion(GenerateCompletionListBase):
def __init__(self, command):
super(WindowsCompletion, self).__init__(command)
@@ -14,7 +15,8 @@ def completion(self, path_in):
self.offset = (self.offset + 1) % len(self.completion_list)
else:
# Generate new completion list
- self.completion_list, self.alias_list, self.dir_list, self.file_list = self.generate_completion_list(path_in)
+ (self.completion_list, self.alias_list, self.dir_list,
+ self.file_list) = self.generate_completion_list(path_in)
self.offset = 0
if len(self.completion_list) == 0:
@@ -23,7 +25,7 @@ def completion(self, path_in):
else:
self.completion_list = [path_in]
match = re.match(pattern, path_in)
- if match :
+ if match:
completion = self.completion_list[self.offset]
if self.settings.get(COMPLETE_SINGLE_ENTRY_SETTING):
if len(self.completion_list) == 1:
@@ -31,7 +33,7 @@ def completion(self, path_in):
completion += ":"
elif completion in self.dir_list:
completion += "/"
- new_content = re.sub(pattern, r"\1" , path_in)
+ new_content = re.sub(pattern, r"\1", path_in)
new_content += completion
first_token = False
else:
@@ -49,9 +51,11 @@ def completion(self, path_in):
if first_token:
if self.view is not None:
if completion in self.alias_list:
- self.view.set_status("AdvancedNewFile2", "Alias Completion")
+ self.view.set_status(
+ "AdvancedNewFile2", "Alias Completion")
elif completion in self.dir_list:
- self.view.set_status("AdvancedNewFile2", "Directory Completion")
+ self.view.set_status(
+ "AdvancedNewFile2", "Directory Completion")
self.prev_text = new_content
else:
self.prev_text = None
View
4 advanced_new_file/platform/nix_platform.py
@@ -3,6 +3,7 @@
from ..anf_util import *
+
class NixPlatform():
def split(self, path):
return None, path
@@ -14,3 +15,6 @@ def get_alias_absolute_path(self, root, path):
if re.search(NIX_ROOT_REGEX, path):
return os.path.join(root, path)
return None
+
+ def is_absolute_path(self, path):
+ return re.match(NIX_ROOT_REGEX, path) is not None
View
4 advanced_new_file/platform/windows_platform.py
@@ -3,6 +3,7 @@
from ..anf_util import *
+
class WindowsPlatform(object):
"""docstring for WindowsPlatform"""
def __init__(self, view):
@@ -31,3 +32,6 @@ def get_alias_absolute_path(self, root, path):
if re.search(WIN_ROOT_REGEX, path):
return os.path.join(root, path)
return None
+
+ def is_absolute_path(self, path):
+ return re.match(WIN_ROOT_REGEX, path) is not None
Please sign in to comment.
Something went wrong with that request. Please try again.