| 3 | | import os, sys |
| 4 | | |
| 5 | | import Tkinter as tk |
| 6 | | from tkFileDialog import askopenfilename, askdirectory |
| 7 | | |
| 8 | | def _ask(method, *args, **kwargs): |
| 9 | | root = tk.Tk() |
| 10 | | kwargs["parent"] = root |
| 11 | | path = method(*args, **kwargs) |
| 12 | | root.destroy() |
| 13 | | |
| 14 | | if path == "": # Nothing selected. |
| 15 | | sys.exit() |
| 16 | | |
| 17 | | return os.path.normpath(path) |
| 18 | | |
| 19 | | def askdirectory2(*args, **kwargs): |
| 20 | | return _ask(askdirectory, *args, **kwargs) |
| | 3 | import os, sys, string |
| 23 | | def askopenfilename2(*args, **kwargs): |
| 24 | | return _ask(askopenfilename, *args, **kwargs) |
| | 6 | def human_filesize(bytes): |
| | 7 | """ |
| | 8 | >>> human_filesize(1*1024) |
| | 9 | '1.0KB' |
| | 10 | >>> human_filesize(1*1024*1024) |
| | 11 | '1.0MB' |
| | 12 | >>> human_filesize(1.5*1024*1024) |
| | 13 | '1.5MB' |
| | 14 | """ |
| | 15 | bytes = float(bytes) |
| | 16 | for unit in ['bytes','KB','MB','GB','TB']: |
| | 17 | if bytes < 1000: |
| | 18 | return "%s%s" % (round(bytes,1), unit) |
| | 19 | bytes /= 1024.0 |
| | 20 | |
| | 21 | |
| | 22 | |
| | 23 | |
| | 24 | ALLOW_CHARS = string.ascii_letters + string.digits + "+-,." |
| | 25 | |
| | 26 | def make_slug(txt, join_string=" ", allow_chars=ALLOW_CHARS): |
| | 27 | """ |
| | 28 | delete all non-ALLOW_CHARS characters |
| | 29 | |
| | 30 | >>> make_slug("a test") |
| | 31 | 'a test' |
| | 32 | >>> make_slug("") |
| | 33 | '' |
| | 34 | >>> make_slug("A other test 1*2/3\\4/*/*/5") |
| | 35 | 'A other test 1 2 3 5' |
| | 36 | """ |
| | 37 | parts = [""] |
| | 38 | for char in txt: |
| | 39 | if char not in allow_chars: |
| | 40 | if parts[-1] != "": |
| | 41 | # No double "-" e.g.: "foo - bar" -> "foo-bar" not "foo---bar" |
| | 42 | parts.append("") |
| | 43 | else: |
| | 44 | parts[-1] += char |
| | 45 | |
| | 46 | item_name = join_string.join(parts) |
| | 47 | item_name = item_name.strip(join_string) |
| | 48 | |
| | 49 | return item_name |
| | 50 | |
| | 51 | |
| | 52 | |
| | 53 | def makeUnique(item_name, name_list, max_no=1000): |
| | 54 | """ |
| | 55 | returns a unique shortcut. |
| | 56 | - delete all non-ALLOW_CHARS characters. |
| | 57 | - if the shotcut already exists in name_list -> add a sequential number |
| | 58 | |
| | 59 | >>> makeUnique("two", ["one", "two", "three"]) |
| | 60 | 'two1' |
| | 61 | >>> makeUnique("two", ["one", "three"]) |
| | 62 | 'two' |
| | 63 | >>> makeUnique("two", ["one", "two", "two1", "three"]) |
| | 64 | 'two2' |
| | 65 | >>> makeUnique("", []) |
| | 66 | '' |
| | 67 | """ |
| | 68 | name_list2 = [i.lower() for i in name_list] |
| | 69 | |
| | 70 | # make double shortcut unique (add a new free sequential number) |
| | 71 | if item_name.lower() in name_list2: |
| | 72 | for i in xrange(1, max_no): |
| | 73 | testname = "%s%i" % (item_name, i) |
| | 74 | if testname.lower() not in name_list2: |
| | 75 | item_name = testname |
| | 76 | break |
| | 77 | |
| | 78 | return item_name |
| | 79 | |
| | 80 | |
| | 81 | if __name__ == "__main__": |
| | 82 | import doctest |
| | 83 | doctest.testmod(verbose=False) |
| | 84 | print "DocTest end." |