use tempfile.mkstemp instead of homebrew solution
This commit is contained in:
parent
a90c9244c1
commit
f210bd0535
1 changed files with 5 additions and 19 deletions
24
skratch.py
24
skratch.py
|
|
@ -2,10 +2,9 @@
|
|||
|
||||
import sys
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import argparse
|
||||
import time
|
||||
import tempfile
|
||||
|
||||
def make_dir(d):
|
||||
if not os.path.exists(d):
|
||||
|
|
@ -44,26 +43,13 @@ def get_editor(use_v):
|
|||
if use_v and v:
|
||||
return v
|
||||
return e
|
||||
|
||||
def mkstemp(fp):
|
||||
if fp[-6:] != "XXXXXX":
|
||||
print(f'Error: mkstemp got filepath: {fp}')
|
||||
return None
|
||||
for x in range(0,1000):
|
||||
r = ''.join(random.choices(string.ascii_letters, k=6))
|
||||
fp = fp[:-6] + r
|
||||
if not os.path.exists(fp):
|
||||
return fp
|
||||
print("Error: Could not create new file, try running skratch -c and try again")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def run(editor, sk_path, filename=None):
|
||||
if not filename:
|
||||
t = time.localtime()
|
||||
filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-XXXXXX"
|
||||
fp = os.path.join(sk_path, filename)
|
||||
fp = mkstemp(fp)
|
||||
filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-"
|
||||
fd, fp = tempfile.mkstemp(prefix=filename, dir=sk_path)
|
||||
os.close(fd)
|
||||
else:
|
||||
fp = os.path.join(sk_path, filename)
|
||||
if fp != None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue