<commit_before>import sublime, sublime_plugin
import os, datetime
from string import Template
import re

sets_file = "FileHeader.sublime-settings"
fh_settings = sublime.load_settings(sets_file)

cright_re = re.compile("(.*Copyright \(c\) )[0-9]+(.*)")

def UserName(  ) :
  return fh_settings.get("user", "")

def Date(  ) :
  return datetime.date.today().year

def DateTime(  ) :
  t = datetime.datetime.now()
  return(t.strftime("%d/%m/%Y %I:%M %p"))

def HeaderFile(  ) :
  filePath = os.path.join(sublime.packages_path(), "User/FileHeader.txt")
  if not os.access(filePath, os.R_OK) :
    filePath = os.path.join(sublime.packages_path(), "FileHeader/FileHeader.txt")

  return filePath

class FileHeaderCommand( sublime_plugin.TextCommand ) :
  """Add copyright/info text to beginning of file."""

  def FileName( self ) :
    fn = self.view.name()
    if not fn :
      fn = self.view.file_name()
    if fn :
      fn = os.path.split(fn)[1]
    else:
      fn = "NoFileName"

    return fn

  def Comment( self ) :
    shell_vars = self.view.meta_info("shellVariables", 0)
    c = "//"

    if shell_vars :
      for v in shell_vars :
        if v["name"] == "TM_COMMENT_START" :
          value = v["value"]
          if value :
            c = value.strip()

    return c

  def Do_AddHeader( self ) :
    filePath = HeaderFile()
    hf = open(filePath)
    if hf :
      contents = hf.read()
      hf.close()
      tp = Template(contents)
      cr = Date()
      c = self.Comment()
      fn = self.FileName()
      u = UserName()
      dt = DateTime()
      contents = tp.safe_substitute(YEAR=cr, FILENAME=fn, USER=u, DATETIME=dt, CC=c)
      self.view.insert(self.edit, 0, contents)

  def GUN_Done( self, aName ) :
    fh_settings.set("user", aName)
    sublime.save_settings(sets_file)
    self.Do_AddHeader()

  def AddHeader( self ) :
    name = UserName()
    if name == "" :
      #GUN_Done will run self.Do_AddHeader().  We are going to continue execution
      #NOTE: self.edit will end before GUN_Done is called.
      self.view.window().show_input_panel("Enter user name:", name, self.GUN_Done, None, None)
    else:
      self.Do_AddHeader()

  def ReplaceDate( self ) :
    vw = self.view
    for i in range(2, 16) :
      lp = vw.text_point(i, 0)
      lr = vw.line(lp)
      lt = vw.substr(lr)
      match = cright_re.search(lt)
      if match != None :
        # print "1: %s\r\n" % match.group(1)
        # print "2: %s\r\n" % match.group(2)
        string = match.group(1) + "2012" + match.group(2)
        vw.replace(self.edit, lr, string)
        break

  def HasHeader( self ) :
    vw = self.view
    lp = vw.text_point(1, 0)
    lr = vw.line(lp)
    lt = vw.substr(lr)
    return (lt.find("ZENIMAX") != -1)

  def run( self, edit ) :
    self.edit = self.view.begin_edit('FileHeader')
    try:
      #Look for file header.
      if self.HasHeader() :
        print "Replacint Date"
        self.ReplaceDate()
      else:
        # If it doesn't exist then load header from file, set date/time and add it.
        self.AddHeader()
    finally:
      self.view.end_edit(self.edit)<commit_msg>Minor fixes and adjustments.<commit_after>import sublime, sublime_plugin
import os, datetime
from string import Template
import re

sets_file = "FileHeader.sublime-settings"
fh_settings = sublime.load_settings(sets_file)

cright_re = re.compile("(.*Copyright \(c\) )[0-9]+(.*)")

def UserName(  ) :
  return fh_settings.get("user", "")

def Date(  ) :
  return datetime.date.today().year

def DateTime(  ) :
  t = datetime.datetime.now()
  return(t.strftime("%d/%m/%Y %I:%M %p"))

def HeaderFile(  ) :
  filePath = os.path.join(sublime.packages_path(), "User/FileHeader.txt")
  if not os.access(filePath, os.R_OK) :
    filePath = os.path.join(sublime.packages_path(), "FileHeader/FileHeader.txt")

  return filePath

class FileHeaderCommand( sublime_plugin.TextCommand ) :
  """Add copyright/info text to beginning of file."""

  def FileName( self ) :
    fn = self.view.name()
    if not fn :
      fn = self.view.file_name()
    if fn :
      fn = os.path.split(fn)[1]
    else:
      fn = "NoFileName"

    return fn

  def Comment( self ) :
    shell_vars = self.view.meta_info("shellVariables", 0)
    c = "//"

    if shell_vars :
      for v in shell_vars :
        if v["name"] == "TM_COMMENT_START" :
          value = v["value"]
          if value :
            c = value.strip()

    return c

  def Do_AddHeader( self ) :
    filePath = HeaderFile()
    hf = open(filePath)
    if hf :
      contents = hf.read()
      hf.close()
      tp = Template(contents)
      cr = Date()
      c = self.Comment()
      fn = self.FileName()
      u = UserName()
      dt = DateTime()
      contents = tp.safe_substitute(YEAR=cr, FILENAME=fn, USER=u, DATETIME=dt, CC=c)
      self.view.insert(self.edit, 0, contents)

  def GUN_Done( self, aName ) :
    fh_settings.set("user", aName)
    sublime.save_settings(sets_file)
    self.Do_AddHeader()

  def AddHeader( self ) :
    name = UserName()
    if name == "" :
      #GUN_Done will run self.Do_AddHeader().  We are going to continue execution
      #NOTE: self.edit will end before GUN_Done is called.
      self.view.window().show_input_panel("Enter user name:", name, self.GUN_Done, None, None)
    else:
      self.Do_AddHeader()

  def ReplaceDate( self ) :
    vw = self.view
    for i in range(2, 16) :
      lp = vw.text_point(i, 0)
      lr = vw.line(lp)
      lt = vw.substr(lr)
      match = cright_re.search(lt)
      if match != None :
        # print "1: %s\r\n" % match.group(1)
        # print "2: %s\r\n" % match.group(2)
        string = match.group(1) + "2012" + match.group(2)
        vw.replace(self.edit, lr, string)
        break

  def HasHeader( self ) :
    vw = self.view
    lp = vw.text_point(1, 0)
    lr = vw.line(lp)
    lt = vw.substr(lr)
    return (lt.find("Copyright") != -1)

  def run( self, edit ) :
    self.edit = self.view.begin_edit('FileHeader')
    try:
      #Look for file header.
      if self.HasHeader() :
        #print "Replacing Date"
        self.ReplaceDate()
      else:
        # If it doesn't exist then load header from file, set date/time and add it.
        self.AddHeader()
    finally:
      self.view.end_edit(self.edit)
{'fraction_non_alphanumeric': 0.13961661341853035, 'fraction_numerical': 0.007028753993610224, 'mean_word_length': 2.4880222841225628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7607153', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2210, 'n_words': 532}