def test_load_1(self):
     # that "get_data" is called correctly on each thing
     test_wc = WorkflowManager([])
     test_wc._data = [mock.MagicMock(spec=IndexedPiece) for _ in xrange(5)]
     test_wc.load(u'pieces')
     for mock_piece in test_wc._data:
         mock_piece.get_data.assert_called_once_with([noterest.NoteRestIndexer])
     self.assertTrue(test_wc._loaded)
Esempio n. 2
0
 def test_intervals_2(self):
     # test all combinations of bwv77
     test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     actual = test_wm.run('intervals')
     exp_ind = list(IntervalsTests.EXPECTED_2.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_2[ind_item], actual[ind_item])
Esempio n. 3
0
 def test_ngrams_7(self):
     # test all two-part combinations of the test piece; 2-grams
     test_wm = WorkflowManager(['vis/tests/corpus/vis_Test_Piece.xml'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(0, 'n', 2)
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_7.index)
     act_ind = list(actual.index)
     self.assertEqual(len(exp_ind), len(act_ind))
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
         self.assertEqual(NGramsTests.EXPECTED_7[ind_item], actual[ind_item])
Esempio n. 4
0
 def test_ngrams_6(self):
     # test madrigal51 with all-voice 2-grams and rests
     test_wm = WorkflowManager(['vis/tests/corpus/madrigal51.mxl'])
     test_wm.settings(0, 'voice combinations', 'all')
     test_wm.settings(None, 'include rests', True)
     test_wm.load('pieces')
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_6.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(NGramsTests.EXPECTED_6[ind_item], actual[ind_item])
Esempio n. 5
0
 def test_intervals_3(self):
     # test all combinations of madrigal51 without rests
     test_wm = WorkflowManager(['vis/tests/corpus/madrigal51.mxl'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(None, 'include rests', False)
     actual = test_wm.run('intervals')
     exp_ind = list(IntervalsTests.EXPECTED_3.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_3[ind_item], actual[ind_item])
Esempio n. 6
0
 def test_ngrams_8(self):
     # test_ngrams_7 *but* with part combinations specified rather than 'all pairs'
     test_wm = WorkflowManager(['vis/tests/corpus/vis_Test_Piece.xml'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', '[[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]')
     test_wm.settings(0, 'n', 2)
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_7.index)  # yes, this *should* be EXPECTED_7
     act_ind = list(actual.index)
     self.assertEqual(len(exp_ind), len(act_ind))
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
         self.assertEqual(NGramsTests.EXPECTED_7[ind_item], actual[ind_item])
Esempio n. 7
0
 def test_ngrams_3(self):
     # test all voices of bwv77; 1-grams
     test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all')
     test_wm.settings(0, 'n', 1)
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_3.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(NGramsTests.EXPECTED_3[ind_item], actual[ind_item])
 def test_ngrams_1(self):
     # test the two highest voices of bwv77; 2-grams
     test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', '[[0, 1]]')
     test_wm.settings(0, 'n', 2)
     test_wm.settings(0, 'continuer', '_')
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_1.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(NGramsTests.EXPECTED_1[ind_item], actual[ind_item])
 def test_ngrams_2(self):
     # test all two-part combinations of bwv77; 5-grams
     test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(0, 'n', 5)
     test_wm.settings(0, 'continuer', '_')
     actual = test_wm.run('interval n-grams')[:10]
     exp_ind = list(NGramsTests.EXPECTED_2.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(NGramsTests.EXPECTED_2[ind_item], actual[ind_item])
 def test_intervals_2(self):
     """test all combinations of bwv77"""
     test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     actual = actual['aggregator.ColumnAggregator']
     exp_ind = list(IntervalsTests.EXPECTED_2.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_2[ind_item], actual[ind_item])
Esempio n. 11
0
 def test_ngrams_4(self):
     # test all voices of bwv2; 3-grams; simple intervals
     test_wm = WorkflowManager(['vis/tests/corpus/bwv2.xml'])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all')
     test_wm.settings(0, 'n', 2)
     test_wm.settings(None, 'simple intervals', True)
     actual = test_wm.run('interval n-grams')[:10]
     exp_ind = list(NGramsTests.EXPECTED_4.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(NGramsTests.EXPECTED_4[ind_item], actual[ind_item])
 def test_ngrams_9c(self):
     # same as 9b but 'interval quality' is set to False (by default).
     test_wm = WorkflowManager(['vis/tests/corpus/Kyrie_short.krn'])
     test_wm.load()
     test_wm.settings(0, 'voice combinations', '[[1,3]]')
     test_wm.settings(0, 'n', 2)
     test_wm.settings(0, 'offset interval', 2.0)
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_9c.index)
     act_ind = list(actual.index)
     self.assertEqual(len(exp_ind), len(act_ind))
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
         self.assertEqual(NGramsTests.EXPECTED_9c[ind_item], actual[ind_item])
 def test_intervals_3(self):
     """test all combinations of madrigal51 without rests"""
     test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'madrigal51.mxl')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(None, 'include rests', False)
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     actual = actual['aggregator.ColumnAggregator']
     exp_ind = list(IntervalsTests.EXPECTED_3.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_3[ind_item], actual[ind_item])
Esempio n. 14
0
 def test_load_3(self):
     # NB: this is more of an integration test
     test_wc = WorkflowManager([u'vis/tests/corpus/try_opus.krn'])
     test_wc.load('pieces')
     self.assertEqual(3, len(test_wc))
     # NOTE: we have to do this by digging until music21 imports metadata from **kern files, at
     #       which point we'll be able to use our very own metadata() method
     exp_names = [u'Alex', u'Sarah', u'Emerald']
     for i in xrange(3):
         # first Score gets some extra metadata
         which_el = 5 if i == 0 else 3
         piece = test_wc._data[i]._import_score()
         self.assertTrue(isinstance(piece[which_el], GlobalReference))
         self.assertEqual(u'COM', piece[which_el].code)
         self.assertEqual(exp_names[i], piece[which_el].value)
    def test_intervals_7(self):  # TODO: add a frequency-counted test
        """same as test_6 *but* no quality"""
        # NB: the "expected" was hand-counted
        expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'expecteds', 'bwv77', 'SA_intervals_nq.csv'),
                                   comment='#', index_col=0, header=[0, 1], quotechar="'", dtype='object')

        test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')])
        test_wm.load('pieces')
        test_wm.settings(0, 'voice combinations', '[[0, 1]]')
        test_wm.settings(None, 'count frequency', False)
        test_wm.settings(None, 'interval quality', False)
        actual = test_wm.run('intervals')

        self.assertEqual(1, len(actual))
        actual = actual[0].dropna()
        self.assertDataFramesEqual(expected, actual)
Esempio n. 16
0
 def test_intervals_2(self):
     """test all combinations of bwv77"""
     test_wm = WorkflowManager(
         [os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     actual = actual['aggregator.ColumnAggregator']
     exp_ind = list(IntervalsTests.EXPECTED_2.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_2[ind_item],
                          actual[ind_item])
 def test_ngrams_9b(self):
     # same as 9a but tests functionality of 'dynamic quality setting of continuer
     # when 'interval quality' is set to True.
     test_wm = WorkflowManager(['vis/tests/corpus/Kyrie_short.krn'])
     test_wm.load()
     test_wm.settings(0, 'voice combinations', '[[1,3]]')
     test_wm.settings(0, 'n', 2)
     test_wm.settings(0, 'offset interval', 2.0)
     test_wm.settings(0, 'interval quality', True)
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_9b.index)
     act_ind = list(actual.index)
     self.assertEqual(len(exp_ind), len(act_ind))
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
         self.assertEqual(NGramsTests.EXPECTED_9b[ind_item], actual[ind_item])
Esempio n. 18
0
 def test_intervals_3(self):
     """test all combinations of madrigal51 without rests"""
     test_wm = WorkflowManager(
         [os.path.join(VIS_PATH, 'tests', 'corpus', 'madrigal51.mxl')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(None, 'include rests', False)
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     actual = actual['aggregator.ColumnAggregator']
     exp_ind = list(IntervalsTests.EXPECTED_3.index)
     act_ind = list(actual.index)
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
     for ind_item in exp_ind:
         self.assertEqual(IntervalsTests.EXPECTED_3[ind_item],
                          actual[ind_item])
Esempio n. 19
0
def import_files(request):
    """
    Called with the /api/import URL to load files into the WorkflowManager and return the metadata
    it discovers.
    """
    filepaths = [piece.file.path for piece in Piece.objects.filter(user_id=request.session.session_key)]
    workm = WorkflowManager(filepaths)
    workm.load('pieces')
    request.session['workm'] = workm
    return [
        {"Filename": os.path.basename(workm.metadata(i, 'pathname')),
         "Title": workm.metadata(i, 'title'),
         "Part Names": workm.metadata(i, 'parts'),
         "Offset": None,
         "Part Combinations": None,
         "Repeat Identical": False}
        for i in xrange(len(workm))
    ], 200
 def test_ngrams_9a(self):
     # test the calculation of horizontal intervals when the lower voice is sustained
     # longer than the offset interval. A custom string is passed for horizontal
     # unisons resulting from a sustained lower voice. Regression test for:
     # https://github.com/ELVIS-Project/vis/issues/305
     test_wm = WorkflowManager(['vis/tests/corpus/Kyrie_short.krn'])
     test_wm.load()
     test_wm.settings(0, 'voice combinations', '[[1,3]]')
     test_wm.settings(0, 'n', 2)
     test_wm.settings(0, 'offset interval', 2.0)
     test_wm.settings(0, 'continuer', 'zamboni')
     actual = test_wm.run('interval n-grams')
     exp_ind = list(NGramsTests.EXPECTED_9a.index)
     act_ind = list(actual.index)
     self.assertEqual(len(exp_ind), len(act_ind))
     for ind_item in exp_ind:
         self.assertTrue(ind_item in act_ind)
         self.assertEqual(NGramsTests.EXPECTED_9a[ind_item], actual[ind_item])
 def test_intervals_5(self):
     """test all combinations of vis_Test_Piece"""
     test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'vis_Test_Piece.xml')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(None, 'include rests', True)
     expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'corpus', 'test_intervals_5.csv'),
                                index_col=0)
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     self.assertSequenceEqual(list(expected.columns), list(actual.columns))
     for col_name in expected.columns:
         self.assertEqual(len(expected[col_name]), len(actual[col_name]))
         for each_interval in expected[col_name].index:
             # NOTE: for whatever reason, the "expected" file always imports with an Int64Index,
             #       so .loc() won't find things properly unless we give the int64 index to the
             #       expected and the str index to the actual
             self.assertEqual(expected[col_name].loc[each_interval],
                              actual[col_name].loc[str(each_interval)])
Esempio n. 22
0
def import_files(request):
    """
    Called with the /api/import URL to load files into the WorkflowManager and return the metadata
    it discovers.
    """
    filepaths = [
        piece.file.path
        for piece in Piece.objects.filter(user_id=request.session.session_key)
    ]
    workm = WorkflowManager(filepaths)
    workm.load('pieces')
    request.session['workm'] = workm
    return [{
        "Filename": os.path.basename(workm.metadata(i, 'pathname')),
        "Title": workm.metadata(i, 'title'),
        "Part Names": workm.metadata(i, 'parts'),
        "Offset": None,
        "Part Combinations": None,
        "Repeat Identical": False
    } for i in xrange(len(workm))], 200
Esempio n. 23
0
 def test_intervals_5(self):
     """test all combinations of vis_Test_Piece"""
     test_wm = WorkflowManager(
         [os.path.join(VIS_PATH, 'tests', 'corpus', 'vis_Test_Piece.xml')])
     test_wm.load('pieces')
     test_wm.settings(0, 'voice combinations', 'all pairs')
     test_wm.settings(None, 'include rests', True)
     expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'corpus',
                                             'test_intervals_5.csv'),
                                index_col=0)
     actual = test_wm.run('intervals')
     self.assertEqual(1, len(actual.columns))
     self.assertSequenceEqual(list(expected.columns), list(actual.columns))
     for col_name in expected.columns:
         self.assertEqual(len(expected[col_name]), len(actual[col_name]))
         for each_interval in expected[col_name].index:
             # NOTE: for whatever reason, the "expected" file always imports with an Int64Index,
             #       so .loc() won't find things properly unless we give the int64 index to the
             #       expected and the str index to the actual
             self.assertEqual(expected[col_name].loc[each_interval],
                              actual[col_name].loc[str(each_interval)])
Esempio n. 24
0
    def test_intervals_6(self):  # TODO: add a frequency-counted test
        """test Soprano and Alto of bwv77; with quality; not counting frequency"""
        # NB: the "expected" was hand-counted
        expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'expecteds',
                                                'bwv77', 'SA_intervals.csv'),
                                   comment='#',
                                   index_col=0,
                                   header=[0, 1],
                                   quotechar="'")

        test_wm = WorkflowManager(
            [os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')])
        test_wm.load('pieces')
        test_wm.settings(0, 'voice combinations', '[[0, 1]]')
        test_wm.settings(None, 'count frequency', False)
        test_wm.settings(None, 'interval quality', True)
        actual = test_wm.run('intervals')

        self.assertEqual(1, len(actual))
        actual = actual[0].dropna()
        self.assertDataFramesEqual(expected, actual)
Esempio n. 25
0
class WorkflowWrapper(QAbstractTableModel):
    """
    This is a wrapper class for the :class:`vis.workflow.WorkflowManager` that allows its use as a
    :class:`QAbstractTableModel` for PyQt4. This class only wraps :meth:`metadata` and
    :meth:`setting`, which are those methods needed by PyQt. For all other :class:`Workflowmanager`
    methods, access the internally-stored instance with :meth:`get_workflow_manager`.

    ** How to Use the WorkflowWrapper: **

    The :class:`WorkflowWrapper` always returns valid values, and will not raise exceptions of its
    own. However, "valid" values are not always "correct" or "expected." We recommend you use the
    :class:`WorkflowWrapper` like this:

    #. Instantiate the object.
    #. Set the object as the model for its view.
    #. Call :meth:`insertRows` with the total number of pieces to be added.
    #. Call :meth:`setData` once per piece to set the pathname.
    #. Once each row has a pathname, the object will instantiate its internal
        :class:`WorkflowManager` instance and call its :meth:`load` method to import the score,
        run the :class:`NoteRestIndexer`, and save its metadata.
    #. Subsequent calls to :meth:`data` will return the most correct information available.

    ** How not to Use the WorkflowWrapper: **

    We recommend you do not use the :class:`WorkflowWrapper` like this:

    * Do not add pieces by calling :meth:`insertRows` then :meth:`setData` with the pathname, then
        :meth:`insertRows` then :meth:`setData` with the pathname, and so on. In this case, the
        :class:`WorkflowWrapper` would create a new :class:`WorkflowManager` after each call to
        :meth:`setData`, which would import each piece many times.
    * Do not add pieces after you modify a metadata or setting field. When you add a piece, a new
        :class:`WorkflowManager` instance is created. The new instance replaces any customized
        settings and metadata with the default values.
    * Do not call :meth:`data` before you add the pathnames of all pieces. Real metadata is only
        available after the :class:`WorkflowManager` is created, which happens after all the pieces
        have a pathname. If you call :meth:`data` when there is no :class:`WorkflowManager`, the
        return value will always be ``None``.

    ** Columns in the Data Model: **

    The :class:`WorkflowWrapper` creates a two-dimensional data model, where each row represents an
    :class:`IndexedPiece` stored in the :class:`WorkflowManager` and each column represents either
    a setting or a metadatum field. The following six fields are different for each piece, and
    should be displayed in the :class:`QTableView` widget:

    * filename
    * title
    * parts_list
    * offset_interval
    * parts_combinations
    * repeat_identical

    The :class:`WorkflowManager` additionally wraps these data fields, which are shared by all
    pieces, and will therefore not apear in the :class:`QTableView` widget:

    * quality
    * simple_ints

    Use class properties with those names to specify columns to :meth:`data` and :meth:`getData`.
    For example:

    >>> workm.data((0, WorkflowWrapper.title), Qt.DisplayRole)
    u'02_eleva'
    >>> workm.setData((0, WorkflowWrapper.title), u'Elevator Love Letter', Qt.EditRole)
    >>> workm.data((0, WorkflowWrapper.parts_list), Qt.DisplayRole)
    [u'Amy Milan', u'Torquil Campbell', u'Evan Cranley', u'Chris Seligman', u'Pat McGee']
    """

    # Public class variables to track which column has which data
    # NOTE: Update _num_cols whenever you change the number of columns,
    #       since this variable is used by columnCount().
    # NOTE: Update _header_names whenever you change the number or definition of
    #       columns, since this variale is used by headerData().
    _num_cols = 6
    _header_names = ['Path', 'Title', 'List of Part Names', 'Offset Interval',
                    'Part Combinations', 'Repeat Identical']
    # displayed fields
    filename = 0
    title = 1
    parts_list = 2
    offset_interval = 3
    parts_combinations = 4
    repeat_identical = 5

    # non-displayed fields
    quality = 100
    simple_ints = 101

    # instead of DisplayRole; used to tell data() to return the list of part names as a list
    ListRole = 4000

    # when a value hasn't been set, return a QVariant with this in it
    default_value = u'(unset)'

    def __init__(self, parent=QModelIndex()):
        """
        Create a new :class:`WorkflowWrapper` instance.
        """
        super(WorkflowWrapper, self).__init__()
        self._pathnames = []  # hold a list of pathnames for before the WorkflowManager
        self._workm = None  # hold the WorkflowManager
        self._settings_changed = False  # whether setData() was called (for settings_changed())
        self._when_done_import = None  # method to call when we're finished importing

    def rowCount(self, parent=QModelIndex()):
        """
        Return the number of pieces in this list. If the internal :class:`WorkflowManager` exists,
        this is the number of piece stored there; otherwise it is the number of places for
        pathnames.

        :returns: The number of pieces in this list.
        :rtype: ``int``
        """
        if self._workm is None:
            return len(self._pathnames)
        else:
            return len(self._workm)

    def columnCount(self, parent=QModelIndex()):
        "Return the number of columns in this WorkflowWrapper."
        return WorkflowWrapper._num_cols

    def data(self, index, role):
        """
        Get the data for the piece and metadatum or setting specified. Only the "parts_list" column
        responds to the WorkflowWrapper.ListRole, in which case a list of strings is returned
        instead of a comma-separated list of part names.

        :param index: The row-and-column index you wish to access. Either you can use a
            :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the
            index of the piece in the models, and the second element is one of the class properties
            described above in "Columns in the Data Model."
        :type index: :class:`QModelIndex` or 2-tuple of ``int``

        :param role: Either Qt.DisplayRole or WorkflowWrapper.ListRole
        :type role: ``int``

        :returns: The requested data or, if ``index`` or ``role`` is invalid, ``None``.
        :rtype: :class:`QVariant`

        .. note:: The method always returns a :class:`QVariant`. Access the Python object with the
            :meth:`toPyObject` method.

        .. note:: If the internal :class:`WorkflowManager` has not been instantiated, the return
            value is always ``None``.

        .. note:: This method never actually returns ``None``, but rather an empty :class:`QVariant`
            that will be ``None`` when you call :meth:`toPyObject` on it.
        """
        if self._workm is None or (Qt.DisplayRole != role and WorkflowWrapper.ListRole != role):
            return QVariant()

        # Set the row and column
        row = None
        column = None
        if isinstance(index, QModelIndex):
            # if the QModelIndex is invalid, we won't bother with it
            if not index.isValid():
                return QVariant()
            # otherwise, get the row and column from the QModelIndex
            row = index.row()
            column = index.column()
        else:
            row = index[0]
            column = index[1]

        # Verify the row and column
        if row >= self.rowCount() or column >= self._num_cols and \
        (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints):
            return QVariant()

        post = None
        if Qt.DisplayRole == role:
            # displayed fields
            if WorkflowWrapper.filename == column:
                post = self._workm.metadata(row, u'pathname')
            elif WorkflowWrapper.title == column:
                post = self._workm.metadata(row, u'title')
            elif WorkflowWrapper.parts_list == column:
                post = u', '.join(self._workm.metadata(row, u'parts'))
            elif WorkflowWrapper.offset_interval == column:
                post = self._workm.settings(row, u'offset interval')
            elif WorkflowWrapper.parts_combinations == column:
                post = self._workm.settings(row, u'voice combinations')
            elif WorkflowWrapper.repeat_identical == column:
                # the wording in the GUI and WorkflowManager has opposite meanings
                post = not self._workm.settings(row, u'filter repeats')
            # non-displayed fields
            elif WorkflowWrapper.quality == column:
                post = self._workm.settings(None, u'interval quality')
            elif WorkflowWrapper.simple_ints == column:
                post = self._workm.settings(None, u'simple intervals')
            else:
                post = QVariant()
        elif WorkflowWrapper.ListRole == role:
            if WorkflowWrapper.parts_list == column:
                post = self._workm.metadata(row, u'parts')
            else:
                post = QVariant()
        else:
            post = QVariant()

        if not isinstance(post, QVariant):
            if post is None:
                post = WorkflowWrapper.default_value
            post = QVariant(post)
        return post

    def headerData(self, section, orientation, role):
        """
        Return the column names for a WorkflowWrapper instance.

        Arguments:
        - section: the index of the column you want the name of
        - orientation: should be Qt.Horizontal; Qt.Vertical is ignored
        - role: should be Qt.DisplayRole; others are ignored

        If the section index is out of range, or the orientation or role is
        different than expected, an empty QVariant is returned.
        """
        # All of the column titles are stored as class variables. I decided to
        # use the class name here, rather than "self," just in case they were
        # accidentally changed in this instance. We do not want to allow that.
        if Qt.Horizontal == orientation and Qt.DisplayRole == role and \
        0 <= section < WorkflowWrapper._num_cols:
            return WorkflowWrapper._header_names[section]
        else:
            return QVariant()

    def setData(self, index, value, role):
        """
        Set the data for the piece and metadatum or setting specified. If the internal
            :class:`WorkflowManager` has not yet been created and this is the last pathname added,
            instantiate the :class:`WorkflowManager` and call :meth:`load`.

        :param index: The row-and-column index you wish to access. Either you can use a
            :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the
            index of the piece in the models, and the second element is one of the class properties
            described above in "Columns in the Data Model."
        :type index: :class:`QModelIndex` or 2-tuple of ``int``

        :param value: The desired value of the setting or metadatum. If you submit a
            :class:`QVariant`, we will call :meth:`toPyObject` before sending to the
            :class:`WorkflowManager`.
        :type value: :class:`QVariant` or any

        :param role: This should be Qt.EditRole.
        :type role: :class:`EditRole`

        :returns: Whether the data was successfully set.
        :rtype: ``True`` or ``False``

        .. note:: If the internal :class:`WorkflowManager` has not been instantiated, you can only
            set the ``pathname`` field. All other calls to :meth:`setData` will fail.
        """

        if Qt.EditRole != role:
            return False

        # Set the row and column
        row = None
        column = None
        if isinstance(index, QModelIndex):
            # if the QModelIndex is invalid, we won't bother with it
            if not index.isValid():
                return False
            # otherwise, get the row and column from the QModelIndex
            row = index.row()
            column = index.column()
        else:
            row = index[0]
            column = index[1]
            index = self.createIndex(row, column)

        # Verify the row and column
        if row >= self.rowCount() or column >= self._num_cols and \
        (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints):
            return False

        set_val = value.toPyObject() if isinstance(value, QVariant) else value

        # ensure we're trying to set a valid thing
        if self._workm is None:
            if WorkflowWrapper.filename != column:
                return False
            else:
                self._pathnames[row] = set_val
                ch_ind_1, ch_ind_2 = None, None
                if all(self._pathnames):
                    self._workm = WorkflowManager(self._pathnames)
                    self._workm.load(u'pieces')
                    # now that we imported, all the data's changed
                    ch_ind_1 = self.createIndex(0, 0)
                    ch_ind_2 = self.createIndex(len(self), self._num_cols - 1)
                    # let the GUI know
                    self._when_done_import()
                else:
                    # only one cell has changed
                    ch_ind_1 = ch_ind_2 = self.createIndex(row, column)
                self.dataChanged.emit(ch_ind_1, ch_ind_2)
                return True

        # displayed fields
        if WorkflowWrapper.filename == column:
            self._workm.metadata(row, u'pathname', set_val)
        elif WorkflowWrapper.title == column:
            self._workm.metadata(row, u'title', set_val)
        elif WorkflowWrapper.parts_list == column:
            self._workm.metadata(row, u'parts', set_val)
        elif WorkflowWrapper.offset_interval == column:
            self._workm.settings(row, u'offset interval', set_val)
        elif WorkflowWrapper.parts_combinations == column:
            self._workm.settings(row, u'voice combinations', set_val)
        elif WorkflowWrapper.repeat_identical == column:
            # the wording in the GUI and WorkflowManager has opposite meanings
            self._workm.settings(row, u'filter repeats', not set_val)
        # non-displayed fields
        elif WorkflowWrapper.quality == column:
            self._workm.settings(None, u'interval quality', set_val)
        elif WorkflowWrapper.simple_ints == column:
            self._workm.settings(None, u'simple intervals', set_val)
        else:
            return False
        self._settings_changed = True
        self.dataChanged.emit(index, index)
        return True

    def insertRows(self, row, count, parent=QModelIndex()):
        """
        Append new rows to the data model. Yes---append, not insert.

        :param row: An argument that will be ignored.
        :type row: any
        :param count: The number of rows you want to append.
        :type count: ``int``

        .. note:: If the internal :class:`WorkflowManager` already exists, it is destroyed and all
            metadata and settings are lost.

        .. note:: We recommend you add all the rows you will need before you call :meth:`setData`
            to set the pathnames of the newly-added rows.
        """
        if self._workm is not None:
            self._workm = None
        self.beginInsertRows(parent, len(self._pathnames), len(self._pathnames) + count - 1)
        self._pathnames.extend([None for _ in xrange(count)])
        self.endInsertRows()

    def removeRows(self, row, count, parent=QModelIndex()):
        """
        This is the opposite of insertRows(), and the arguments work in the same
        way.
        """
        pass

    def __len__(self):
        "Alias for rowCount()."
        return self.rowCount()

    def __getitem__(self, index):
        "It's __getitem__(), what do you want?!"
        return self._workm[index]

    def connect_workflow_signals(self, finished_import):
        """
        Connect the WorkflowManager's PyQt signals to the methods or functions given here.

        "finished_import" is for the WorkM's "finished_import" signal.
        """
        print('--> type: ' + str(finished_import))  # DEBUG
        self._when_done_import = finished_import
        #self._workm.finished_import.connect(finished_import)
        #pass

    def get_workflow_manager(self):
        """
        Get the internal :class:`WorkflowManager` instance.
        """
        return self._workm

    def settings_changed(self):
        """
        Know whether there has been a call to :meth:`setData` since the last time this method was
        called.
        """
        ret = self._settings_changed
        self._settings_changed = False
        return ret
Esempio n. 26
0
class WorkflowWrapper(QAbstractTableModel):
    """
    This is a wrapper class for the :class:`vis.workflow.WorkflowManager` that allows its use as a
    :class:`QAbstractTableModel` for PyQt4. This class only wraps :meth:`metadata` and
    :meth:`setting`, which are those methods needed by PyQt. For all other :class:`Workflowmanager`
    methods, access the internally-stored instance with :meth:`get_workflow_manager`.

    ** How to Use the WorkflowWrapper: **

    The :class:`WorkflowWrapper` always returns valid values, and will not raise exceptions of its
    own. However, "valid" values are not always "correct" or "expected." We recommend you use the
    :class:`WorkflowWrapper` like this:

    #. Instantiate the object.
    #. Set the object as the model for its view.
    #. Call :meth:`insertRows` with the total number of pieces to be added.
    #. Call :meth:`setData` once per piece to set the pathname.
    #. Once each row has a pathname, the object will instantiate its internal
        :class:`WorkflowManager` instance and call its :meth:`load` method to import the score,
        run the :class:`NoteRestIndexer`, and save its metadata.
    #. Subsequent calls to :meth:`data` will return the most correct information available.

    ** How not to Use the WorkflowWrapper: **

    We recommend you do not use the :class:`WorkflowWrapper` like this:

    * Do not add pieces by calling :meth:`insertRows` then :meth:`setData` with the pathname, then
        :meth:`insertRows` then :meth:`setData` with the pathname, and so on. In this case, the
        :class:`WorkflowWrapper` would create a new :class:`WorkflowManager` after each call to
        :meth:`setData`, which would import each piece many times.
    * Do not add pieces after you modify a metadata or setting field. When you add a piece, a new
        :class:`WorkflowManager` instance is created. The new instance replaces any customized
        settings and metadata with the default values.
    * Do not call :meth:`data` before you add the pathnames of all pieces. Real metadata is only
        available after the :class:`WorkflowManager` is created, which happens after all the pieces
        have a pathname. If you call :meth:`data` when there is no :class:`WorkflowManager`, the
        return value will always be ``None``.

    ** Columns in the Data Model: **

    The :class:`WorkflowWrapper` creates a two-dimensional data model, where each row represents an
    :class:`IndexedPiece` stored in the :class:`WorkflowManager` and each column represents either
    a setting or a metadatum field. The following six fields are different for each piece, and
    should be displayed in the :class:`QTableView` widget:

    * filename
    * title
    * parts_list
    * offset_interval
    * parts_combinations
    * repeat_identical

    The :class:`WorkflowManager` additionally wraps these data fields, which are shared by all
    pieces, and will therefore not apear in the :class:`QTableView` widget:

    * quality
    * simple_ints

    Use class properties with those names to specify columns to :meth:`data` and :meth:`getData`.
    For example:

    >>> workm.data((0, WorkflowWrapper.title), Qt.DisplayRole)
    u'02_eleva'
    >>> workm.setData((0, WorkflowWrapper.title), u'Elevator Love Letter', Qt.EditRole)
    >>> workm.data((0, WorkflowWrapper.parts_list), Qt.DisplayRole)
    [u'Amy Milan', u'Torquil Campbell', u'Evan Cranley', u'Chris Seligman', u'Pat McGee']
    """

    # Public class variables to track which column has which data
    # NOTE: Update _num_cols whenever you change the number of columns,
    #       since this variable is used by columnCount().
    # NOTE: Update _header_names whenever you change the number or definition of
    #       columns, since this variale is used by headerData().
    _num_cols = 6
    _header_names = [
        'Path', 'Title', 'List of Part Names', 'Offset Interval',
        'Part Combinations', 'Repeat Identical'
    ]
    # displayed fields
    filename = 0
    title = 1
    parts_list = 2
    offset_interval = 3
    parts_combinations = 4
    repeat_identical = 5

    # non-displayed fields
    quality = 100
    simple_ints = 101

    # instead of DisplayRole; used to tell data() to return the list of part names as a list
    ListRole = 4000

    # when a value hasn't been set, return a QVariant with this in it
    default_value = u'(unset)'

    def __init__(self, parent=QModelIndex()):
        """
        Create a new :class:`WorkflowWrapper` instance.
        """
        super(WorkflowWrapper, self).__init__()
        self._pathnames = [
        ]  # hold a list of pathnames for before the WorkflowManager
        self._workm = None  # hold the WorkflowManager
        self._settings_changed = False  # whether setData() was called (for settings_changed())
        self._when_done_import = None  # method to call when we're finished importing

    def rowCount(self, parent=QModelIndex()):
        """
        Return the number of pieces in this list. If the internal :class:`WorkflowManager` exists,
        this is the number of piece stored there; otherwise it is the number of places for
        pathnames.

        :returns: The number of pieces in this list.
        :rtype: ``int``
        """
        if self._workm is None:
            return len(self._pathnames)
        else:
            return len(self._workm)

    def columnCount(self, parent=QModelIndex()):
        "Return the number of columns in this WorkflowWrapper."
        return WorkflowWrapper._num_cols

    def data(self, index, role):
        """
        Get the data for the piece and metadatum or setting specified. Only the "parts_list" column
        responds to the WorkflowWrapper.ListRole, in which case a list of strings is returned
        instead of a comma-separated list of part names.

        :param index: The row-and-column index you wish to access. Either you can use a
            :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the
            index of the piece in the models, and the second element is one of the class properties
            described above in "Columns in the Data Model."
        :type index: :class:`QModelIndex` or 2-tuple of ``int``

        :param role: Either Qt.DisplayRole or WorkflowWrapper.ListRole
        :type role: ``int``

        :returns: The requested data or, if ``index`` or ``role`` is invalid, ``None``.
        :rtype: :class:`QVariant`

        .. note:: The method always returns a :class:`QVariant`. Access the Python object with the
            :meth:`toPyObject` method.

        .. note:: If the internal :class:`WorkflowManager` has not been instantiated, the return
            value is always ``None``.

        .. note:: This method never actually returns ``None``, but rather an empty :class:`QVariant`
            that will be ``None`` when you call :meth:`toPyObject` on it.
        """
        if self._workm is None or (Qt.DisplayRole != role
                                   and WorkflowWrapper.ListRole != role):
            return QVariant()

        # Set the row and column
        row = None
        column = None
        if isinstance(index, QModelIndex):
            # if the QModelIndex is invalid, we won't bother with it
            if not index.isValid():
                return QVariant()
            # otherwise, get the row and column from the QModelIndex
            row = index.row()
            column = index.column()
        else:
            row = index[0]
            column = index[1]

        # Verify the row and column
        if row >= self.rowCount() or column >= self._num_cols and \
        (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints):
            return QVariant()

        post = None
        if Qt.DisplayRole == role:
            # displayed fields
            if WorkflowWrapper.filename == column:
                post = self._workm.metadata(row, u'pathname')
            elif WorkflowWrapper.title == column:
                post = self._workm.metadata(row, u'title')
            elif WorkflowWrapper.parts_list == column:
                post = u', '.join(self._workm.metadata(row, u'parts'))
            elif WorkflowWrapper.offset_interval == column:
                post = self._workm.settings(row, u'offset interval')
            elif WorkflowWrapper.parts_combinations == column:
                post = self._workm.settings(row, u'voice combinations')
            elif WorkflowWrapper.repeat_identical == column:
                # the wording in the GUI and WorkflowManager has opposite meanings
                post = not self._workm.settings(row, u'filter repeats')
            # non-displayed fields
            elif WorkflowWrapper.quality == column:
                post = self._workm.settings(None, u'interval quality')
            elif WorkflowWrapper.simple_ints == column:
                post = self._workm.settings(None, u'simple intervals')
            else:
                post = QVariant()
        elif WorkflowWrapper.ListRole == role:
            if WorkflowWrapper.parts_list == column:
                post = self._workm.metadata(row, u'parts')
            else:
                post = QVariant()
        else:
            post = QVariant()

        if not isinstance(post, QVariant):
            if post is None:
                post = WorkflowWrapper.default_value
            post = QVariant(post)
        return post

    def headerData(self, section, orientation, role):
        """
        Return the column names for a WorkflowWrapper instance.

        Arguments:
        - section: the index of the column you want the name of
        - orientation: should be Qt.Horizontal; Qt.Vertical is ignored
        - role: should be Qt.DisplayRole; others are ignored

        If the section index is out of range, or the orientation or role is
        different than expected, an empty QVariant is returned.
        """
        # All of the column titles are stored as class variables. I decided to
        # use the class name here, rather than "self," just in case they were
        # accidentally changed in this instance. We do not want to allow that.
        if Qt.Horizontal == orientation and Qt.DisplayRole == role and \
        0 <= section < WorkflowWrapper._num_cols:
            return WorkflowWrapper._header_names[section]
        else:
            return QVariant()

    def setData(self, index, value, role):
        """
        Set the data for the piece and metadatum or setting specified. If the internal
            :class:`WorkflowManager` has not yet been created and this is the last pathname added,
            instantiate the :class:`WorkflowManager` and call :meth:`load`.

        :param index: The row-and-column index you wish to access. Either you can use a
            :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the
            index of the piece in the models, and the second element is one of the class properties
            described above in "Columns in the Data Model."
        :type index: :class:`QModelIndex` or 2-tuple of ``int``

        :param value: The desired value of the setting or metadatum. If you submit a
            :class:`QVariant`, we will call :meth:`toPyObject` before sending to the
            :class:`WorkflowManager`.
        :type value: :class:`QVariant` or any

        :param role: This should be Qt.EditRole.
        :type role: :class:`EditRole`

        :returns: Whether the data was successfully set.
        :rtype: ``True`` or ``False``

        .. note:: If the internal :class:`WorkflowManager` has not been instantiated, you can only
            set the ``pathname`` field. All other calls to :meth:`setData` will fail.
        """

        if Qt.EditRole != role:
            return False

        # Set the row and column
        row = None
        column = None
        if isinstance(index, QModelIndex):
            # if the QModelIndex is invalid, we won't bother with it
            if not index.isValid():
                return False
            # otherwise, get the row and column from the QModelIndex
            row = index.row()
            column = index.column()
        else:
            row = index[0]
            column = index[1]
            index = self.createIndex(row, column)

        # Verify the row and column
        if row >= self.rowCount() or column >= self._num_cols and \
        (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints):
            return False

        set_val = value.toPyObject() if isinstance(value, QVariant) else value

        # ensure we're trying to set a valid thing
        if self._workm is None:
            if WorkflowWrapper.filename != column:
                return False
            else:
                self._pathnames[row] = set_val
                ch_ind_1, ch_ind_2 = None, None
                if all(self._pathnames):
                    self._workm = WorkflowManager(self._pathnames)
                    self._workm.load(u'pieces')
                    # now that we imported, all the data's changed
                    ch_ind_1 = self.createIndex(0, 0)
                    ch_ind_2 = self.createIndex(len(self), self._num_cols - 1)
                    # let the GUI know
                    self._when_done_import()
                else:
                    # only one cell has changed
                    ch_ind_1 = ch_ind_2 = self.createIndex(row, column)
                self.dataChanged.emit(ch_ind_1, ch_ind_2)
                return True

        # displayed fields
        if WorkflowWrapper.filename == column:
            self._workm.metadata(row, u'pathname', set_val)
        elif WorkflowWrapper.title == column:
            self._workm.metadata(row, u'title', set_val)
        elif WorkflowWrapper.parts_list == column:
            self._workm.metadata(row, u'parts', set_val)
        elif WorkflowWrapper.offset_interval == column:
            self._workm.settings(row, u'offset interval', set_val)
        elif WorkflowWrapper.parts_combinations == column:
            self._workm.settings(row, u'voice combinations', set_val)
        elif WorkflowWrapper.repeat_identical == column:
            # the wording in the GUI and WorkflowManager has opposite meanings
            self._workm.settings(row, u'filter repeats', not set_val)
        # non-displayed fields
        elif WorkflowWrapper.quality == column:
            self._workm.settings(None, u'interval quality', set_val)
        elif WorkflowWrapper.simple_ints == column:
            self._workm.settings(None, u'simple intervals', set_val)
        else:
            return False
        self._settings_changed = True
        self.dataChanged.emit(index, index)
        return True

    def insertRows(self, row, count, parent=QModelIndex()):
        """
        Append new rows to the data model. Yes---append, not insert.

        :param row: An argument that will be ignored.
        :type row: any
        :param count: The number of rows you want to append.
        :type count: ``int``

        .. note:: If the internal :class:`WorkflowManager` already exists, it is destroyed and all
            metadata and settings are lost.

        .. note:: We recommend you add all the rows you will need before you call :meth:`setData`
            to set the pathnames of the newly-added rows.
        """
        if self._workm is not None:
            self._workm = None
        self.beginInsertRows(parent, len(self._pathnames),
                             len(self._pathnames) + count - 1)
        self._pathnames.extend([None for _ in xrange(count)])
        self.endInsertRows()

    def removeRows(self, row, count, parent=QModelIndex()):
        """
        This is the opposite of insertRows(), and the arguments work in the same
        way.
        """
        pass

    def __len__(self):
        "Alias for rowCount()."
        return self.rowCount()

    def __getitem__(self, index):
        "It's __getitem__(), what do you want?!"
        return self._workm[index]

    def connect_workflow_signals(self, finished_import):
        """
        Connect the WorkflowManager's PyQt signals to the methods or functions given here.

        "finished_import" is for the WorkM's "finished_import" signal.
        """
        print('--> type: ' + str(finished_import))  # DEBUG
        self._when_done_import = finished_import
        #self._workm.finished_import.connect(finished_import)
        #pass

    def get_workflow_manager(self):
        """
        Get the internal :class:`WorkflowManager` instance.
        """
        return self._workm

    def settings_changed(self):
        """
        Know whether there has been a call to :meth:`setData` since the last time this method was
        called.
        """
        ret = self._settings_changed
        self._settings_changed = False
        return ret