-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCopyrightCommand.py
65 lines (50 loc) · 2.44 KB
/
CopyrightCommand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# Copyright (c) 2012-2013 by Lifted Studios. All Rights Reserved.
#
import AutoCopyright.constants
import AutoCopyright.helper
import os
import re
import shutil
import sublime
import sublime_plugin
class CopyrightCommand(sublime_plugin.TextCommand):
"""Common functionality for the Auto Copyright command classes."""
def __init__(self, view):
"""Initializes the CopyrightCommand class."""
self.settings = sublime.load_settings(AutoCopyright.constants.SETTINGS_FILE)
self.view = view
self.selected_owner = None
def format_pattern(self, year, owner, fileName):
"""Creates a search pattern for the copyright text."""
text = self.format_text("yyyear", "ooowner", "fffileName")
text = re.escape(text)
text = text.replace("yyyear", year)
text = text.replace("fffileName", fileName)
pattern = text.replace("ooowner", owner)
return pattern
def format_text(self, year, owner, fileName):
"""Formats the text of the copyright message."""
if year is None:
raise TypeError("year cannot be None.")
if len(owner) == 0:
raise TypeError("owner cannot be empty.")
text = self.settings.get(AutoCopyright.constants.SETTING_COPYRIGHT_MESSAGE)
text = text.replace("%y", str(year))
text = text.replace("%o", owner)
text = text.replace("%f", fileName)
return text
def handle_missing_owner_exception(self):
"""Opens the settings file and suggests the user edit it with the proper owner name."""
fileName = sublime.active_window().active_view().file_name()
if fileName is not None and fileName.endswith(AutoCopyright.constants.SETTINGS_FILE):
return
AutoCopyright.helper.error_message(AutoCopyright.constants.ERROR_MISSING_OWNER)
user_settings_path = os.path.join(sublime.packages_path(), AutoCopyright.constants.SETTINGS_PATH_USER)
user_settings_filename = os.path.join(user_settings_path, AutoCopyright.constants.SETTINGS_FILE)
if not os.path.exists(user_settings_path):
os.makedirs(user_settings_path)
if not os.path.exists(user_settings_filename):
default_settings_filename = os.path.join(sublime.packages_path(), constants.PLUGIN_NAME, constants.SETTINGS_FILE)
shutil.copy(default_settings_filename, user_settings_filename)
sublime.active_window().open_file(user_settings_filename)