<s> import AutoCopyright . constants <EOL> import datetime <EOL> import re <EOL> import sublime <EOL> import os <EOL> from AutoCopyright . CopyrightCommand import CopyrightCommand <EOL> from AutoCopyright . Exception import MissingOwnerException <EOL> class UpdateCopyrightCommand ( CopyrightCommand ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view ) : <EOL> """<STR_LIT>""" <EOL> CopyrightCommand . __init__ ( self , view ) <EOL> self . patterns = None <EOL> self . matched_pattern = None <EOL> self . edit = None <EOL> def run ( self , edit ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . edit = edit <EOL> self . __update_copyright ( ) <EOL> except MissingOwnerException : <EOL> self . handle_missing_owner_exception ( ) <EOL> def __find_copyright ( self ) : <EOL> """<STR_LIT>""" <EOL> patterns = self . get_patterns ( ) <EOL> for pattern in patterns : <EOL> region = self . view . find ( pattern , <NUM_LIT:0> ) <EOL> if region is not None and self . __is_in_comment ( region ) : <EOL> self . matched_pattern = pattern <EOL> return region <EOL> return None <EOL> def __get_old_year ( self , region , pattern ) : <EOL> """<STR_LIT>""" <EOL> text = self . view . substr ( region ) <EOL> match = re . match ( pattern , text ) <EOL> return match . group ( <NUM_LIT:1> ) <EOL> def get_owners ( self ) : <EOL> """<STR_LIT>""" <EOL> owners = self . settings . get ( AutoCopyright . constants . SETTING_OWNERS ) <EOL> if not owners or len ( owners ) == <NUM_LIT:0> : <EOL> raise MissingOwnerException ( ) <EOL> if type ( owners ) . __name__ == "<STR_LIT>" : <EOL> return [ owners ] <EOL> return owners <EOL> def get_fileName ( self ) : <EOL> """<STR_LIT>""" <EOL> fileName = sublime . active_window ( ) . active_view ( ) . file_name ( ) <EOL> if fileName is not None : <EOL> fileName = os . path . basename ( fileName ) <EOL> return fileName <EOL> else : <EOL> return "<STR_LIT>" <EOL> def get_patterns ( self ) : <EOL> """<STR_LIT>""" <EOL> owners = self . get_owners ( ) <EOL> fileName = self . get_fileName ( ) <EOL> if self . patterns is None : <EOL> self . patterns = { } <EOL> for owner in owners : <EOL> self . patterns [ self . format_pattern ( "<STR_LIT>" , owner , fileName ) ] = owner <EOL> return self . patterns <EOL> def __is_in_comment ( self , region ) : <EOL> """<STR_LIT>""" <EOL> point = region . begin ( ) <EOL> if self . view . scope_name ( point ) . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> comment_region = self . view . extract_scope ( point ) <EOL> if comment_region . begin ( ) <= region . begin ( ) and comment_region . end ( ) >= region . end ( ) : <EOL> return True <EOL> return False <EOL> def __replace_copyright ( self , region ) : <EOL> """<STR_LIT>""" <EOL> if region is not None : <EOL> pattern = self . matched_pattern <EOL> oldYear = self . __get_old_year ( region , pattern ) <EOL> newYear = str ( datetime . date . today ( ) . year ) <EOL> if oldYear != newYear : <EOL> self . __replace_match ( region , oldYear , newYear ) <EOL> self . matched_pattern = None <EOL> def __replace_match ( self , region , oldYear , newYear ) : <EOL> """<STR_LIT>""" <EOL> owner = self . patterns [ self . matched_pattern ] <EOL> fileName = self . get_fileName ( ) ; <EOL> message = self . format_text ( oldYear + "<STR_LIT:->" + newYear , owner , fileName ) <EOL> self . view . replace ( self . edit , region , message ) <EOL> self . edit = None <EOL> def __update_copyright ( self ) : <EOL> """<STR_LIT>""" <EOL> region = self . __find_copyright ( ) <EOL> self . __replace_copyright ( region ) </s>
