Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sublime, sublime_plugin
- import random
- import time
- class PushidCommand(sublime_plugin.TextCommand):
- def run(self, edit):
- self.view.insert(edit, self.view.sel()[0].begin(), PushID().next_id())
- class PushID(object):
- PUSH_CHARS = ('-0123456789'
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- '_abcdefghijklmnopqrstuvwxyz')
- def __init__(self):
- self.lastPushTime = 0
- self.lastRandChars = [0,0,0,0,0,0,0,0,0,0,0,0];
- def next_id(self):
- now = int(time.time() * 1000)
- duplicateTime = (now == self.lastPushTime)
- self.lastPushTime = now
- timeStampChars = ["", "", "", "", "", "", "", ""]
- for i in range(7, -1, -1):
- timeStampChars[i] = self.PUSH_CHARS[now % 64]
- now = int(now / 64)
- if (now != 0):
- raise ValueError('We should have converted the entire timestamp.')
- uid = ''.join(timeStampChars)
- if not duplicateTime:
- for i in range(12):
- self.lastRandChars[i] = int(random.random() * 64)
- else:
- for i in range(11, -1, -1):
- if self.lastRandChars[i] == 63:
- self.lastRandChars[i] = 0
- else:
- break
- self.lastRandChars[i] += 1
- for i in range(12):
- uid += self.PUSH_CHARS[self.lastRandChars[i]]
- if len(uid) != 20:
- raise ValueError('Length should be 20.')
- return uid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement