# -*- encoding: utf-8 -*-
from abjad.tools import contexttools
from abjad.tools import durationtools


def scale_measure_denominator_and_adjust_measure_contents(measure, factor):
    r'''Scales power-of-two `measure` to non-power-of-two measure 
    with new denominator `factor`:

    ..  container:: example

        **Example.**

        ::

            >>> measure = Measure((2, 8), "c'8 d'8")
            >>> beam = spannertools.BeamSpanner()
            >>> beam.attach(measure.select_leaves())
            >>> show(measure) # doctest: +SKIP

        ..  doctest::

            >>> f(measure)
            {
                \time 2/8
                c'8 [
                d'8 ]
            }

        ::

            >>> measuretools.scale_measure_denominator_and_adjust_measure_contents(
            ...     measure, 3)
            Measure(3/12, [c'8., d'8.])
            >>> show(measure) # doctest: +SKIP

        ..  doctest::

            >>> f(measure)
            {
                \time 3/12
                \scaleDurations #'(2 . 3) {
                    c'8. [
                    d'8. ]
                }
            }

    Treats new denominator `factor` like clever form of ``1``:
    ``3/3`` or ``5/5`` or ``7/7``, etc.

    Preserves `measure` prolated duration.

    Derives new `measure` multiplier.

    Scales `measure` contents.

    Picks best new time signature.
    '''
    from abjad.tools import measuretools
    from abjad.tools import timesignaturetools

    # save old time signature duration
    old_time_signature_duration = measure.time_signature.duration

    # find new time signature
    new_time_signature = \
        timesignaturetools.duration_and_possible_denominators_to_time_signature(
        old_time_signature_duration, factor=factor)

    # scale contents of measures in expr
    multiplier = new_time_signature.implied_prolation.reciprocal
    measure._scale(multiplier)

    # assign new time signature
    for mark in measure._get_marks(contexttools.TimeSignatureMark):
        mark.detach()
    new_time_signature.attach(measure)

    # return measure
    return measure
