Skip to content

Commit

Permalink
Clean up to match pep8 style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
skuroda committed Dec 27, 2013
1 parent 823e638 commit 7aeffc6
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 65 deletions.
2 changes: 0 additions & 2 deletions AdvancedNewFile.py
Expand Up @@ -20,5 +20,3 @@
else:
from advanced_new_file import reloader
from advanced_new_file.commands import *


18 changes: 14 additions & 4 deletions advanced_new_file/anf_util.py
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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 = []
Expand Down
62 changes: 36 additions & 26 deletions advanced_new_file/commands/command_base.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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 = []
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 ""
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -337,7 +348,6 @@ def get_cursor_path(self):

return path


def _find_open_file(self, file_name):
window = self.window
if IS_ST3:
Expand Down
13 changes: 8 additions & 5 deletions advanced_new_file/commands/delete_file_command.py
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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]})
Expand Down
5 changes: 4 additions & 1 deletion 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
Expand Down
39 changes: 27 additions & 12 deletions advanced_new_file/commands/move_file_command.py
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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

0 comments on commit 7aeffc6

Please sign in to comment.