1

I am trying to set up a snippet in Sublime Text 2 that will expand to the following:

/**
* @version   $Id: ${1:current_file_name.extension} ${2:random_4_digit_number} ${3:YYYY-MM-DD} ${4:time_in_UTC_24} ${5:current_logged-in_user} $
* @author    Company http://example.com
* @copyright Copyright (C) 2007 - ${6:current_year} Company
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/

The above snippet has cursor stops. If all the data can be automated, then I wouldn't need any cursor stops.

The stops map as follows:

${1:current_file_name.extension}

Automatically pastes the name of the current file being edited.

${2:random_4_digit_number}

A randomly-generated number from 0000 to 9999

${3:YYYY-MM-DD}

The current date using - separator.

${4:time_in_UTC_24}

The current time in UTC 24-hour format including seconds using : separator.

${5:current_logged-in_user}

The currently logged in user

${6:current_year}

The current year

Any advice or help would be greatly appreciated.

Ali Samii
  • 1,672
  • 4
  • 28
  • 49

1 Answers1

3

It is probably not possible with a snippet, however I wrote a plugin to accomplish what you want. In Sublime, click Tools > New Plugin. Replace the example code with the following. Name it "add_license_stamp.py" and save it in your Packages folder (not in Packages/User). Also, add a keybinding in your keymap file. To run the command, place your cursor where you want it and press the keybinding:

Keybinding:

{ "keys": ["ctrl+shift+9"], "command": "add_license_stamp" }

Plugin:

import sublime, sublime_plugin
import os
import datetime
import random
import getpass

''' Add license stamp
/**
* @version   $Id: ${1:current_file_name.extension} ${2:random_4_digit_number} ${3:YYYY-MM-DD} ${4:time_in_UTC_24} ${5:current_logged-in_user} $
* @author    Company http://example.com
* @copyright Copyright (C) 2007 - ${6:current_year} Company
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/
'''


class AddLicenseStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        company_name = "BobCo"
        company_site = "http://bobco.com"

        file_path = self.view.file_name()
        file_name = os.path.basename(file_path)
        year = datetime.datetime.utcnow().strftime("%Y")
        date_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
        random_number = str(random.randrange(0000, 9999)).zfill(4)
        user = getpass.getuser()

        license = "/**\n"
        license += "* @version   $Id: " + file_name + " " + random_number + " " + date_time + " " + user + " $\n"
        license += "* @author    " + company_name + " " + company_site + "\n"
        license += "* @copyright Copyright (C) 2007 - " + year + " " + company_name + "\n"
        license += "* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only\n"
        license += "*/\n"

        self.view.replace(edit, self.view.sel()[0], license)

(note: python requires a blank line after your code)

Replace "BobCo" with your company name. I'm not sure of the best way to get the current user name, I used this question: Is there a portable way to get the current username in Python? . They say it is compatible with the major OSes. If not do something similar to how I did the company name. And manually set it per user. Also, I don't know what UTC 24-hour format is. But I just used the time in 24 hour format.

Edit

I changed now() to utcnow() to get the utc date/time. I added date/time formatting. I added zfill(4) to the random number to pad it with zeros if under 4 digits. You can highlight the current stamp and hit the key binding to update it. You could also get fancy and automatically replace on save, but beyond the current scope. You would have to use a regex to find the current stamp. Then activate the script upon save instead of run.

Community
  • 1
  • 1
d_rail
  • 4,109
  • 32
  • 37
  • UTC 24 hour stands for "Coordinated Universal Time" and is the more accurate way of referring to Greenwich Mean Time (GMT). It is UTC as a compromise between the English and French Acronyms (CUT and TUC). For Example, I am adding this comment at 22:34 UTC – Ali Samii Mar 29 '13 at 22:34
  • Saving it in the Packages directory is fine, however, if I want to save it in a subfolder of Packages, it doesn't work. How would I get Sublime Text 2 to recognise the plugin if I want to put it in a subdirectory such as `Packages/Add License Stamp` in order to try and keep the Packages directory uncluttered? – Ali Samii Mar 29 '13 at 22:50
  • Putting it in a subfolder in Packages works for me. That is how a package you install from "Sublime Package Control" does it. Just not in a subfolder in Packages/User – d_rail Mar 29 '13 at 22:55
  • Also, it is adding the date and time without any separators. `20130329 235333`. How do I change that to `2013-03-29 23:53:33 UTC`? – Ali Samii Mar 29 '13 at 22:56
  • I was afraid it meant greenwich. That means the system time will have to be converted based on your local time zone. Also, depending if your users are in America, you have to deal with daylight savings time which will make it more difficult. I will see if I can fix it. – d_rail Mar 29 '13 at 22:59
  • I changed the import statement at the top, `import os import sublime import sublime_plugin import datetime import random import getpass` and it worked in the subfolder. Probably made no difference, but it wasn't working the other way. Do you know what the command or call would be for getting the UTC time? UTC is not affected by Daylight Savings Time. – Ali Samii Mar 29 '13 at 22:59
  • Would it be possible to query the time from an internet server? Most internet servers would relay the UTC time along with all other times, I believe. – Ali Samii Mar 29 '13 at 23:00
  • 1
    I'm looking into it. But, I assume you can't get the utc time directly. You would have to convert from your computer time. – d_rail Mar 29 '13 at 23:01
  • This site, for example, displays the UTC time. http://www.worldtimeserver.com/current_time_in_UTC.aspx – Ali Samii Mar 29 '13 at 23:02
  • I also noticed that the random number, if less than `1000`, for example, `415` drops the leading `0`. How can the string always have 4 digits (0000 through 9999) rather than (0 through 9999)? – Ali Samii Mar 29 '13 at 23:06
  • 1
    There is a utcnow that I saw here: http://stackoverflow.com/questions/4563272/how-to-convert-a-python-utc-datetime-to-a-local-datetime-using-only-python-stand. Seems to be working. – d_rail Mar 29 '13 at 23:23
  • OT: Thanks for the fix [over here](http://stackoverflow.com/a/8738696/479863), your edit was correct so I don't know why they rejected it. – mu is too short Apr 10 '13 at 00:40
  • @muistooshort Thanks for the upvote on this one, I guess it worked out. – d_rail Apr 10 '13 at 03:00
  • 1
    Enhanced @d_rail code to allow for project specific configurations and add copyright info each time a file is saved: https://github.com/gitsome/sublimeAutoCopyRight – John David Five Jan 30 '16 at 03:44