-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
91 lines (72 loc) · 2.22 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
"""
@title: helpers.py
@summary: all those little tools that have no other home
@author: AltSheets
@version: v10
@license Giveback license v05 http://altsheets.ddns.net/give/
@requires only stuff from python itself
@see chaincountdown.py for explanations & donation addresses
"""
from time import gmtime, strftime
import os, tempfile, getpass, sys, datetime
def theNowInReadable():
return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
def dirnames():
currentDir = os.path.join(os.path.dirname(__file__),"")
tempDir = os.path.join(tempfile.mkdtemp(), "")
return currentDir, tempDir
def username():
"""Try to get the real username (not the environment variable).
Only in Linux - for that 'import pwd'.
If that fails (i.e. Windows), use getpass.
"""
try:
import pwd
username = pwd.getpwuid( os.getuid() )[ 0 ]
except (ImportError, AttributeError):
# for Windows:
username = getpass.getuser()
return username
def suffix(f):
return os.path.splitext(f)[1]
def nameWOsuffix(f):
return os.path.splitext(f)[0]
def filesInFolder(path, suffixlist=None):
files=os.listdir(path)
if suffixlist!=None:
files=[f for f in files if suffix(f) in suffixlist]
return files
def test_filesInFolder():
print filesInFolder("html")
fonts = os.path.join(dirnames()[0], "fonts")
print filesInFolder(fonts, (".ttf",))# ".css") )
def replacedInDict(defaults, overwrite):
"to take default values unless options are specified"
result=defaults.copy()
for k,v in overwrite.items():
result[k]=v
return result
def cutUpText(text, maxLen):
"cuts each line at max length"
resultText=""
for line in text.split("\n"):
while True:
resultText += line[:maxLen] + "\n"
line = line[maxLen:]
if len(line) <=maxLen:
break
if line!="": resultText += line + "\n"
return resultText
def mkdirIfNotExist(dirname):
try:
os.mkdir(dirname)
except:
return False
return True
if __name__ == "__main__":
print theNowInReadable()
print dirnames()
print username()
print
test_filesInFolder()