1 """Storage of tonal space analyses in a (relatively) quickly-accessible corpus.
2
3 Use the script bin/data/parsegs.py to create and store these sets from the
4 chord corpus.
5
6 """
7 """
8 ============================== License ========================================
9 Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding
10
11 This file is part of The Jazz Parser.
12
13 The Jazz Parser is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 The Jazz Parser is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with The Jazz Parser. If not, see <http://www.gnu.org/licenses/>.
25
26 ============================ End license ======================================
27
28 """
29 __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"
30
31 import os
32 import cPickle as pickle
33 from jazzparser import settings
34
35 FILE_EXTENSION = "anal"
38 """
39 Data structure to hold and store a set of tonal space analyses.
40
41 """
42 - def __init__(self, analyses, name="unnamed"):
43 """
44 @type analyses: list of (string,analysis) pairs
45 @param analyses: pairings of song names and analyses
46
47 """
48 self.analyses = [(str(song).lower(),anal) for (song,anal) in analyses]
49 self.name = name
50
52 return [anal for (song,anal) in self.analyses if song==song_name.lower()]
53
55 return zip(*self.analyses)[0]
56 songs = property(__get_songs)
57
59 return len(self.analyses)
60
62 return self.analyses[index]
63
64
65 @staticmethod
70 _filename = property(__get_my_filename)
71
72 @classmethod
74 """ Returns a list of the names of available stored sets. """
75 datadir = settings.ANALYSIS_DATA_DIR
76 if not os.path.exists(datadir):
77 return []
78
79 names = [name.rpartition(".") for name in os.listdir(datadir)]
80
81 return [name for name,__,ext in names if ext == FILE_EXTENSION]
82
84 """ Saves the set data to a file. """
85
86 data = {
87 'analyses' : self.analyses,
88 }
89 data = pickle.dumps(data, 2)
90 filename = self._filename
91
92 filedir = os.path.dirname(filename)
93 if not os.path.exists(filedir):
94 os.mkdir(filedir)
95
96 f = open(filename, 'w')
97 f.write(data)
98 f.close()
99
101 """ Removes all the data for the set. """
102 fn = self._filename
103 if os.path.exists(fn):
104 os.remove(fn)
105
106 @classmethod
107 - def load(cls, name):
121
124