File: util.py

package info (click to toggle)
pydance 1.0.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 8,120 kB
  • ctags: 731
  • sloc: python: 7,440; makefile: 71; ruby: 49; ansic: 21; sh: 7
file content (92 lines) | stat: -rw-r--r-- 3,292 bytes parent folder | download | duplicates (2)
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
import fnmatch
import os
import string

# 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.
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)

# 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.
# FIXME: Why aren't we using some sort of tree walk here? (Oh yeah, it's
# Python 2.3 only. Fix that for 1.2.)
def find(path, patterns):
  matches = []
  path = os.path.abspath(os.path.expanduser(path))

  if os.path.isdir(path):
    list = os.listdir(path)
    for f in list:
      filepath = os.path.join(path, f)
      if os.path.isdir(filepath):
        matches.extend(find(filepath, patterns))
      else:
        for pattern in patterns:
          if fnmatch.fnmatch(filepath.lower(), pattern):
            matches.append(filepath)
            break
  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 in range(len(parts)):
    nonletter = 0
    has_vowels = False
    for l in parts[i]:
      if l not in letters: nonletter += 1
      if l in vowels: has_vowels = True
    if float(nonletter) / len(parts[i]) < 1.0/3:
      if parts[i] == parts[i].upper() and has_vowels:
        parts[i] = parts[i].lower()
        if parts[i] not in uncapped:
          parts[i] = parts[i].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)