-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUpdateCopyrightCommand.py
116 lines (92 loc) · 3.87 KB
/
UpdateCopyrightCommand.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#
# Copyright (c) 2012-2013 by Lifted Studios. All Rights Reserved.
#
import AutoCopyright.constants
import datetime
import re
import sublime
import os
from AutoCopyright.CopyrightCommand import CopyrightCommand
from AutoCopyright.Exception import MissingOwnerException
class UpdateCopyrightCommand(CopyrightCommand):
"""Updates the copyright text, if present."""
def __init__(self, view):
"""Initializes the update copyright command."""
CopyrightCommand.__init__(self, view)
self.patterns = None
self.matched_pattern = None
self.edit = None
def run(self, edit):
"""Searches for the copyright text and replaces it, if necessary."""
try:
self.edit = edit
self.__update_copyright()
except MissingOwnerException:
self.handle_missing_owner_exception()
def __find_copyright(self):
"""Finds the first matching copyright text."""
patterns = self.get_patterns()
for pattern in patterns:
region = self.view.find(pattern, 0)
if region is not None and self.__is_in_comment(region):
self.matched_pattern = pattern
return region
return None
def __get_old_year(self, region, pattern):
"""Extract the old year from the pre-existing copyright text."""
text = self.view.substr(region)
match = re.match(pattern, text)
return match.group(1)
def get_owners(self):
"""Gets the list of owners from the settings."""
owners = self.settings.get(AutoCopyright.constants.SETTING_OWNERS)
if not owners or len(owners) == 0:
raise MissingOwnerException()
if type(owners).__name__ == "unicode":
return [owners]
return owners
def get_fileName(self):
"""Get the actual file name."""
fileName = sublime.active_window().active_view().file_name()
if fileName is not None:
fileName = os.path.basename(fileName)
return fileName
else:
return "unknown-filename"
def get_patterns(self):
"""Gets the patterns to use to find the copyright text."""
owners = self.get_owners()
fileName = self.get_fileName()
if self.patterns is None:
self.patterns = {}
for owner in owners:
self.patterns[self.format_pattern("(\d+)(-\d+)?", owner, fileName)] = owner
return self.patterns
def __is_in_comment(self, region):
"""Determines if the entire region is encapsulated by a comment."""
point = region.begin()
if self.view.scope_name(point).find('comment') != -1:
comment_region = self.view.extract_scope(point)
if comment_region.begin() <= region.begin() and comment_region.end() >= region.end():
return True
return False
def __replace_copyright(self, region):
"""Replaces the copyright text by updating the year to span from the original year to the current one."""
if region is not None:
pattern = self.matched_pattern
oldYear = self.__get_old_year(region, pattern)
newYear = str(datetime.date.today().year)
if oldYear != newYear:
self.__replace_match(region, oldYear, newYear)
self.matched_pattern = None
def __replace_match(self, region, oldYear, newYear):
"""Replace the old copyright text with the new copyright text."""
owner = self.patterns[self.matched_pattern]
fileName = self.get_fileName();
message = self.format_text(oldYear + "-" + newYear, owner, fileName)
self.view.replace(self.edit, region, message)
self.edit = None
def __update_copyright(self):
"""Finds the copyright text and replaces it."""
region = self.__find_copyright()
self.__replace_copyright(region)