<commit_before>import json
import logging

from lxml import etree
from time import time

from xmodule.editing_module import XMLEditingDescriptor
from xmodule.xml_module import XmlDescriptor
from xmodule.x_module import XModule
from xmodule.progress import Progress
from xmodule.exceptions import NotFoundError
from xblock.core import Float, String, Boolean, Scope


log = logging.getLogger(__name__)


class TimeLimitModule(XModule):
    '''
    Wrapper module which imposes a time constraint for the completion of its child.
    '''

    beginning_at = Float(help="The time this timer was started", scope=Scope.student_state)
    ending_at = Float(help="The time this timer will end", scope=Scope.student_state)
    accomodation_code = String(help="A code indicating accommodations to be given the student", scope=Scope.student_state)
    time_expired_redirect_url = String(help="Url to redirect users to after the timelimit has expired", scope=Scope.settings)
    duration = Float(help="The length of this timer", scope=Scope.settings)
    suppress_toplevel_navigation = Boolean(help="Whether the toplevel navigation should be suppressed when viewing this module", scope=Scope.settings)

    def __init__(self, *args, **kwargs):
        XModule.__init__(self, *args, **kwargs)

        self.rendered = False

    # For a timed activity, we are only interested here
    # in time-related accommodations, and these should be disjoint.
    # (For proctored exams, it is possible to have multiple accommodations
    # apply to an exam, so they require accommodating a multi-choice.)
    TIME_ACCOMMODATION_CODES = (('NONE', 'No Time Accommodation'),
                      ('ADDHALFTIME', 'Extra Time - 1 1/2 Time'),
                      ('ADD30MIN', 'Extra Time - 30 Minutes'),
                      ('DOUBLE', 'Extra Time - Double Time'),
                      ('TESTING', 'Extra Time -- Large amount for testing purposes')
                    )

    def _get_accommodated_duration(self, duration):
        '''
        Get duration for activity, as adjusted for accommodations.
        Input and output are expressed in seconds.
        '''
        if self.accommodation_code is None or self.accommodation_code == 'NONE':
            return duration
        elif self.accommodation_code == 'ADDHALFTIME':
            # TODO:  determine what type to return
            return int(duration * 1.5)
        elif self.accommodation_code == 'ADD30MIN':
            return (duration + (30 * 60))
        elif self.accommodation_code == 'DOUBLE':
            return (duration * 2)
        elif self.accommodation_code == 'TESTING':
            # when testing, set timer to run for a week at a time.
            return 3600 * 24 * 7

    @property
    def has_begun(self):
        return self.beginning_at is not None

    @property
    def has_ended(self):
        if not self.ending_at:
            return False
        return self.ending_at < time()

    def begin(self, duration):
        '''
        Sets the starting time and ending time for the activity,
        based on the duration provided (in seconds).
        '''
        self.beginning_at = time()
        modified_duration = self._get_accommodated_duration(duration)
        self.ending_at = self.beginning_at + modified_duration

    def get_remaining_time_in_ms(self):
        return int((self.ending_at - time()) * 1000)

    def get_html(self):
        self.render()
        return self.content

    def get_progress(self):
        ''' Return the total progress, adding total done and total available.
        (assumes that each submodule uses the same "units" for progress.)
        '''
        # TODO: Cache progress or children array?
        children = self.get_children()
        progresses = [child.get_progress() for child in children]
        progress = reduce(Progress.add_counts, progresses)
        return progress

    def handle_ajax(self, dispatch, get):
        raise NotFoundError('Unexpected dispatch type')

    def render(self):
        if self.rendered:
            return
        # assumes there is one and only one child, so it only renders the first child
        children = self.get_display_items()
        if children:
            child = children[0]
            self.content = child.get_html()
        self.rendered = True

    def get_icon_class(self):
        children = self.get_children()
        if children:
            return children[0].get_icon_class()
        else:
            return "other"

class TimeLimitDescriptor(XMLEditingDescriptor, XmlDescriptor):

    module_class = TimeLimitModule

    # For remembering when a student started, and when they should end
    stores_state = True

    @classmethod
    def definition_from_xml(cls, xml_object, system):
        children = []
        for child in xml_object:
            try:
                children.append(system.process_xml(etree.tostring(child, encoding='unicode')).location.url())
            except Exception as e:
                log.exception("Unable to load child when parsing TimeLimit wrapper. Continuing...")
                if system.error_tracker is not None:
                    system.error_tracker("ERROR: " + str(e))
                continue
        return {'children': children}
<commit_msg>Return children from timelimit module definition_from_xml in the new format (separate from definition)<commit_after>import json
import logging

from lxml import etree
from time import time

from xmodule.editing_module import XMLEditingDescriptor
from xmodule.xml_module import XmlDescriptor
from xmodule.x_module import XModule
from xmodule.progress import Progress
from xmodule.exceptions import NotFoundError
from xblock.core import Float, String, Boolean, Scope


log = logging.getLogger(__name__)


class TimeLimitModule(XModule):
    '''
    Wrapper module which imposes a time constraint for the completion of its child.
    '''

    beginning_at = Float(help="The time this timer was started", scope=Scope.student_state)
    ending_at = Float(help="The time this timer will end", scope=Scope.student_state)
    accomodation_code = String(help="A code indicating accommodations to be given the student", scope=Scope.student_state)
    time_expired_redirect_url = String(help="Url to redirect users to after the timelimit has expired", scope=Scope.settings)
    duration = Float(help="The length of this timer", scope=Scope.settings)
    suppress_toplevel_navigation = Boolean(help="Whether the toplevel navigation should be suppressed when viewing this module", scope=Scope.settings)

    def __init__(self, *args, **kwargs):
        XModule.__init__(self, *args, **kwargs)

        self.rendered = False

    # For a timed activity, we are only interested here
    # in time-related accommodations, and these should be disjoint.
    # (For proctored exams, it is possible to have multiple accommodations
    # apply to an exam, so they require accommodating a multi-choice.)
    TIME_ACCOMMODATION_CODES = (('NONE', 'No Time Accommodation'),
                      ('ADDHALFTIME', 'Extra Time - 1 1/2 Time'),
                      ('ADD30MIN', 'Extra Time - 30 Minutes'),
                      ('DOUBLE', 'Extra Time - Double Time'),
                      ('TESTING', 'Extra Time -- Large amount for testing purposes')
                    )

    def _get_accommodated_duration(self, duration):
        '''
        Get duration for activity, as adjusted for accommodations.
        Input and output are expressed in seconds.
        '''
        if self.accommodation_code is None or self.accommodation_code == 'NONE':
            return duration
        elif self.accommodation_code == 'ADDHALFTIME':
            # TODO:  determine what type to return
            return int(duration * 1.5)
        elif self.accommodation_code == 'ADD30MIN':
            return (duration + (30 * 60))
        elif self.accommodation_code == 'DOUBLE':
            return (duration * 2)
        elif self.accommodation_code == 'TESTING':
            # when testing, set timer to run for a week at a time.
            return 3600 * 24 * 7

    @property
    def has_begun(self):
        return self.beginning_at is not None

    @property
    def has_ended(self):
        if not self.ending_at:
            return False
        return self.ending_at < time()

    def begin(self, duration):
        '''
        Sets the starting time and ending time for the activity,
        based on the duration provided (in seconds).
        '''
        self.beginning_at = time()
        modified_duration = self._get_accommodated_duration(duration)
        self.ending_at = self.beginning_at + modified_duration

    def get_remaining_time_in_ms(self):
        return int((self.ending_at - time()) * 1000)

    def get_html(self):
        self.render()
        return self.content

    def get_progress(self):
        ''' Return the total progress, adding total done and total available.
        (assumes that each submodule uses the same "units" for progress.)
        '''
        # TODO: Cache progress or children array?
        children = self.get_children()
        progresses = [child.get_progress() for child in children]
        progress = reduce(Progress.add_counts, progresses)
        return progress

    def handle_ajax(self, dispatch, get):
        raise NotFoundError('Unexpected dispatch type')

    def render(self):
        if self.rendered:
            return
        # assumes there is one and only one child, so it only renders the first child
        children = self.get_display_items()
        if children:
            child = children[0]
            self.content = child.get_html()
        self.rendered = True

    def get_icon_class(self):
        children = self.get_children()
        if children:
            return children[0].get_icon_class()
        else:
            return "other"

class TimeLimitDescriptor(XMLEditingDescriptor, XmlDescriptor):

    module_class = TimeLimitModule

    # For remembering when a student started, and when they should end
    stores_state = True

    @classmethod
    def definition_from_xml(cls, xml_object, system):
        children = []
        for child in xml_object:
            try:
                children.append(system.process_xml(etree.tostring(child, encoding='unicode')).location.url())
            except Exception as e:
                log.exception("Unable to load child when parsing TimeLimit wrapper. Continuing...")
                if system.error_tracker is not None:
                    system.error_tracker("ERROR: " + str(e))
                continue
        return {}, children

{'fraction_non_alphanumeric': 0.08051286886018667, 'fraction_numerical': 0.005468087112284341, 'mean_word_length': 2.580155248059399, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4324146', 'n_tokens_mistral': 2859, 'n_tokens_neox': 2733, 'n_words': 937}