forked from ewindisch/pydance
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
131 lines (111 loc) · 4.56 KB
/
util.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fnmatch
import os
import string
from i18n import *
# This is the standard order to present difficulties in.
# DDR (USA home version) calls "beginner" "standard". Ignore that.
# Beginner, light, basic, another, standard, trick, maniac, heavy, challenge,
# and oni are from DDR. Smaniac is from DWI; s-maniac is a very common typo.
# Hardcore is from pydance. Para and expert are from PPP. Easy, and hard are
# from TM and PIU; medium is from TM; crazy is from PIU.
#We need a dummy difficulty list to include it to .pot file
DIFFICULTY_LIST_I18N = [_("BEGINNER"),_("EASY"),_("LIGHT"),_("BASIC"),_("PARA"),
_("ANOTHER"),_("NORMAL"),_("MEDIUM"),_("STANDARD"),
_("TRICK"),_("DOUBLE"),_("HARD"),_("MANIAC"),_("HEAVY"),
_("HARDCORE"),_("CHALLENGE"),_("ONI"),_("SMANIAC"),
_("S-MANIAC"),_("CRAZY"),_("EXPERT")]
DIFFICULTY_LIST = ["BEGINNER","EASY","LIGHT","BASIC","PARA",
"ANOTHER","NORMAL","MEDIUM","STANDARD",
"TRICK","DOUBLE","HARD","MANIAC","HEAVY",
"HARDCORE","CHALLENGE","ONI","SMANIAC",
"S-MANIAC","CRAZY","EXPERT"]
def difficulty_sort(a, b):
if a in DIFFICULTY_LIST and b in DIFFICULTY_LIST:
return cmp(DIFFICULTY_LIST.index(a), DIFFICULTY_LIST.index(b))
elif a in DIFFICULTY_LIST: return -1
elif b in DIFFICULTY_LIST: return 1
else: return cmp(a, b)
def difficulty_sort_key(k):
try: return DIFFICULTY_LIST.index(k)
except ValueError: return len(DIFFICULTY_LIST)
# Return the subtitle of a song...
def find_subtitle(title):
for pair in [("[", "]"), ("(", ")"), ("~", "~"), ("-", "-")]:
if pair[0] in title and title[-1] == pair[1]:
l = title[0:-1].rindex(pair[0])
if l != 0:
subtitle = title[l:]
title = title[:l]
return title, subtitle
else: return title, ""
# FIXME: We should inline this. Really.
# Or not, Psyco does it for us, basically.
def toRealTime(bpm, steps):
return steps*0.25*60.0/bpm
# Search the directory specified by path recursively for files that match
# the shell wildcard pattern. A list of all matching file names is returned,
# with absolute paths.
# dedup_level: 0 => do not eliminate duplicates
# 1 => if multiple files with the same basename but different
# extensions are found within the same directory, only
# one file will be kept. Files matching patterns earlier
# in the patterns list will be preferred over files matching
# later patterns.
def find(path, patterns, dedup_level):
root = os.path.abspath(os.path.expanduser(path))
matches = []
for path,dirs,files in os.walk(root):
local_matches = []
for fn in files:
filepath = os.path.join(path, fn)
for pattern in patterns:
if fnmatch.fnmatch(filepath.lower(), pattern):
local_matches.append(filepath)
break
for m in local_matches:
skip = False
if dedup_level > 0:
base,ext = os.path.splitext(m)
for m2 in local_matches:
if m2 == m:
continue
base2,ext2 = os.path.splitext(m2)
if ext != "" and base == base2:
for pattern in patterns:
# if m matches "better" pattern, don't skip m
if fnmatch.fnmatch(m.lower(), pattern):
break
# if m2 matches "better" pattern, skip m
if fnmatch.fnmatch(m2.lower(), pattern):
skip = True
break
if not skip:
matches.append(m)
return matches
# This uses a bunch of heuristics to come up with a good titlecased
# string. Python's titlecase function sucks.
def titlecase(title):
nonletter = 0
uncapped = ("in", "a", "the", "is", "for", "to", "by", "of", "de", "la")
vowels = "aeiouyAEIOUY"
letters = string.letters + "?!'" # Yeah, those aren't letters, but...
parts = title.split()
if len(parts) == 0: return ""
for i,p in enumerate(parts):
nonletter = 0
has_vowels = False
for l in p:
if l not in letters: nonletter += 1
if l in vowels: has_vowels = True
if float(nonletter) / len(p) < 1.0/3:
if p == p.upper() and has_vowels:
p = parts[i] = p.lower()
if p not in uncapped:
p = parts[i] = p.capitalize()
# Capitalize the first and last words in the name, unless they are
# are "stylistically" lowercase.
for i in (0, -1):
if parts[i] != parts[i].lower() or parts[i] in uncapped:
oldparts = parts[i]
parts[i] = parts[i][0].capitalize() + oldparts[1:]
return " ".join(parts)