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 sys
|
||||||
import os
|
import os
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
import time
|
||||||
|
import tempfile
|
||||||
|
|
||||||
def make_dir(d):
|
def make_dir(d):
|
||||||
if not os.path.exists(d):
|
if not os.path.exists(d):
|
||||||
|
|
@ -44,26 +43,13 @@ def get_editor(use_v):
|
||||||
if use_v and v:
|
if use_v and v:
|
||||||
return v
|
return v
|
||||||
return e
|
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):
|
def run(editor, sk_path, filename=None):
|
||||||
if not filename:
|
if not filename:
|
||||||
t = time.localtime()
|
t = time.localtime()
|
||||||
filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-XXXXXX"
|
filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-"
|
||||||
fp = os.path.join(sk_path, filename)
|
fd, fp = tempfile.mkstemp(prefix=filename, dir=sk_path)
|
||||||
fp = mkstemp(fp)
|
os.close(fd)
|
||||||
else:
|
else:
|
||||||
fp = os.path.join(sk_path, filename)
|
fp = os.path.join(sk_path, filename)
|
||||||
if fp != None:
|
if fp != None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue