Esempio n. 1
0
 def test_assemble(self):
     parts = naming.explode_filename('namer.py', projects.DEFAULT_SCHEME)
     result = naming.assemble_filename(
         scheme=projects.DEFAULT_SCHEME,
         **parts
     )
     self.assertTrue(result.endswith('namer.py'))
Esempio n. 2
0
 def test_explode(self):
     name = 'S02-name.py'
     # spec: {location: S01.py | name: namer.py | type: py}
     result = naming.explode_filename(name, projects.DEFAULT_SCHEME)
     self.assertEqual(result['name'], 'name')
     self.assertEqual(result['index'], 1)
     self.assertEqual(result['extension'], 'py')
Esempio n. 3
0
 def test_explode(self):
     name = 'S02-name.py'
     # spec: {location: S01.py | name: namer.py | type: py}
     result = naming.explode_filename(name, projects.DEFAULT_SCHEME)
     self.assertEqual(result['name'], 'name')
     self.assertEqual(result['index'], 1)
     self.assertEqual(result['extension'], 'py')
Esempio n. 4
0
 def kernel_serialize(self):
     """ """
     status = self.status()
     out = dict(slug=self.definition.slug,
                index=self.index,
                source_path=self.source_path,
                status=status,
                exploded_name=naming.explode_filename(
                    self.definition.name, self.project.naming_scheme))
     out.update(status)
     return out
Esempio n. 5
0
def create_rename_entry(
        step: 'projects.ProjectStep',
        insertion_index: int = None,
        stash_path: str = None
) -> typing.Union[None, STEP_RENAME]:
    """
    Creates a STEP_RENAME for the given ProjectStep instance

    :param step:
        The ProjectStep instance for which the STEP_RENAME will be created
    :param insertion_index:
        An optional index where a step will be inserted as part of this
        renaming process. Allows files to be renamed prior to the insertion
        of the step to prevent conflicts.
    :param stash_path:
    :return:
    """

    project = step.project
    name = step.definition.name
    name_parts = naming.explode_filename(name, project.naming_scheme)
    index = project.index_of_step(name)
    name_index = index

    if insertion_index is not None and insertion_index <= index:
        # Adjusts indexing when renaming is for the purpose of
        # inserting a new step
        name_index += 1

    name_parts['index'] = name_index
    new_name = naming.assemble_filename(
        scheme=project.naming_scheme,
        **name_parts
    )

    if name == new_name:
        return None

    if not stash_path:
        fd, stash_path = tempfile.mkstemp(
            prefix='{}-{}--{}--'.format(step.reference_id, name, new_name)
        )
        os.close(fd)

    return STEP_RENAME(
        id=step.reference_id,
        index=index,
        old_name=name,
        new_name=new_name,
        old_path=step.source_path,
        stash_path=stash_path,
        new_path=os.path.join(step.project.source_directory, new_name)
    )
Esempio n. 6
0
 def kernel_serialize(self):
     """ """
     status = self.status()
     out = dict(
         slug=self.definition.slug,
         index=self.index,
         source_path=self.source_path,
         status=status,
         exploded_name=naming.explode_filename(
             self.definition.name,
             self.project.naming_scheme
         )
     )
     out.update(status)
     return out
Esempio n. 7
0
def create_rename_entry(
        step: 'projects.ProjectStep',
        insertion_index: int = None,
        stash_path: str = None) -> typing.Union[None, STEP_RENAME]:
    """
    Creates a STEP_RENAME for the given ProjectStep instance

    :param step:
        The ProjectStep instance for which the STEP_RENAME will be created
    :param insertion_index:
        An optional index where a step will be inserted as part of this
        renaming process. Allows files to be renamed prior to the insertion
        of the step to prevent conflicts.
    :param stash_path:
    :return:
    """

    project = step.project
    name = step.definition.name
    name_parts = naming.explode_filename(name, project.naming_scheme)
    index = project.index_of_step(name)
    name_index = index

    if insertion_index is not None and insertion_index <= index:
        # Adjusts indexing when renaming is for the purpose of
        # inserting a new step
        name_index += 1

    name_parts['index'] = name_index
    new_name = naming.assemble_filename(scheme=project.naming_scheme,
                                        **name_parts)

    if name == new_name:
        return None

    if not stash_path:
        fd, stash_path = tempfile.mkstemp(
            prefix='{}-{}--{}--'.format(step.reference_id, name, new_name))
        os.close(fd)

    return STEP_RENAME(id=step.reference_id,
                       index=index,
                       old_name=name,
                       new_name=new_name,
                       old_path=step.source_path,
                       stash_path=stash_path,
                       new_path=os.path.join(step.project.source_directory,
                                             new_name))
Esempio n. 8
0
 def test_explode_misaligned(self):
     # spec: {location: S01.py | name: namer.py | type: py}
     result = naming.explode_filename('namer.py', projects.DEFAULT_SCHEME)
     self.assertEqual(result['name'], 'namer')
     self.assertIsNone(result['index'], 0)
     self.assertEqual(result['extension'], 'py')
Esempio n. 9
0
def create_step(response: Response,
                project: Project,
                name: str,
                position: typing.Union[str, int],
                title: str = None) -> Response:
    """

    :param response:
    :param project:
    :param name:
    :param position:
    :param title:
    :return:
    """

    name = name.strip('"')
    title = title.strip('"') if title else title
    index = index_from_location(response, project, position)
    if index is None:
        index = len(project.steps)

    name_parts = naming.explode_filename(name, project.naming_scheme)

    if not project.naming_scheme and not name_parts['name']:
        name_parts['name'] = naming.find_default_filename(
            [s.definition.name for s in project.steps])

    name_parts['index'] = index
    name = naming.assemble_filename(scheme=project.naming_scheme, **name_parts)

    res = step_support.synchronize_step_names(project, index)
    response.consume(res)
    if response.failed:
        return response

    step_renames = res.returned
    step_data = {'name': name}

    if title:
        step_data['title'] = title

    result = project.add_step(step_data, index=index)

    if not os.path.exists(result.source_path):
        contents = ('import cauldron as cd\n\n'
                    if result.source_path.endswith('.py') else '')

        with open(result.source_path, 'w+') as f:
            f.write(contents)

    project.save()
    project.write()

    index = project.steps.index(result)

    step_changes = [
        dict(name=result.definition.name,
             filename=result.filename,
             action='added',
             step=writing.step_writer.serialize(result)._asdict(),
             after=None if index < 1 else project.steps[index -
                                                        1].definition.name)
    ]

    return response.update(
        project=project.kernel_serialize(),
        step_name=result.definition.name,
        step_path=result.source_path,
        step_changes=step_changes,
        step_renames=step_renames).notify(
            kind='CREATED',
            code='STEP_CREATED',
            message='"{}" step has been created'.format(
                result.definition.name)).console(whitespace=1).response
Esempio n. 10
0
def modify_step(response: Response,
                project: Project,
                name: str,
                new_name: str = None,
                position: typing.Union[str, int] = None,
                title: str = None):
    """

    :param response:
    :param project:
    :param name:
    :param new_name:
    :param position:
    :param title:
    :return:
    """

    new_name = new_name if new_name else name
    old_index = project.index_of_step(name)
    new_index = index_from_location(response, project, position, old_index)

    if new_index > old_index:
        # If the current position of the step occurs before the new position
        # of the step, the new index has to be shifted by one to account for
        # the fact that this step will no longer be in this position when it
        # get placed in the position within the project
        new_index -= 1

    old_step = project.remove_step(name)
    if not old_step:
        response.fail(
            code='NO_SUCH_STEP',
            message='Unable to modify unknown step "{}"'.format(name)).console(
                whitespace=1)
        return False

    source_path = old_step.source_path
    if os.path.exists(source_path):
        temp_path = '{}.cauldron_moving'.format(source_path)
        shutil.move(source_path, temp_path)
    else:
        temp_path = None

    res = step_support.synchronize_step_names(project, new_index)
    response.consume(res)
    step_renames = res.returned

    new_name_parts = naming.explode_filename(new_name, project.naming_scheme)
    new_name_parts['index'] = new_index

    if not project.naming_scheme and not new_name_parts['name']:
        new_name_parts['name'] = naming.find_default_filename(
            [s.definition.name for s in project.steps])

    new_name = naming.assemble_filename(scheme=project.naming_scheme,
                                        **new_name_parts)

    step_data = {'name': new_name}
    if title is None:
        if old_step.definition.get('title'):
            step_data['title'] = old_step.definition.title
    else:
        step_data['title'] = title.strip('"')

    new_step = project.add_step(step_data, new_index)

    project.save()

    if not os.path.exists(new_step.source_path):
        if temp_path and os.path.exists(temp_path):
            shutil.move(temp_path, new_step.source_path)
        else:
            with open(new_step.source_path, 'w+') as f:
                f.write('')

    if new_index > 0:
        before_step = project.steps[new_index - 1].definition.name
    else:
        before_step = None

    step_renames[old_step.definition.name] = {
        'name': new_step.definition.name,
        'title': new_step.definition.title
    }

    step_changes = [
        dict(name=new_step.definition.name,
             filename=new_step.filename,
             action='modified',
             after=before_step)
    ]

    response.update(
        project=project.kernel_serialize(),
        step_name=new_step.definition.name,
        step_changes=step_changes,
        step_renames=step_renames).notify(
            kind='SUCCESS',
            code='STEP_MODIFIED',
            message='Step modifications complete').console(whitespace=1)

    project.write()

    return True
Esempio n. 11
0
def create_step(
        response: Response,
        project: Project,
        name: str,
        position: typing.Union[str, int],
        title: str = None
) -> Response:
    """

    :param response:
    :param project:
    :param name:
    :param position:
    :param title:
    :return:
    """

    name = name.strip('"')
    title = title.strip('"') if title else title
    index = index_from_location(response, project, position)
    if index is None:
        index = len(project.steps)

    name_parts = naming.explode_filename(name, project.naming_scheme)

    if not project.naming_scheme and not name_parts['name']:
        name_parts['name'] = naming.find_default_filename(
            [s.definition.name for s in project.steps]
        )

    name_parts['index'] = index
    name = naming.assemble_filename(
        scheme=project.naming_scheme,
        **name_parts
    )

    res = step_support.synchronize_step_names(project, index)
    response.consume(res)
    if response.failed:
        return response

    step_renames = res.returned
    step_data = {'name': name}

    if title:
        step_data['title'] = title

    result = project.add_step(step_data, index=index)

    if not os.path.exists(result.source_path):
        contents = (
            'import cauldron as cd\n\n'
            if result.source_path.endswith('.py')
            else ''
        )

        with open(result.source_path, 'w+') as f:
            f.write(contents)

    project.save()
    project.write()

    index = project.steps.index(result)

    step_changes = [dict(
        name=result.definition.name,
        filename=result.filename,
        action='added',
        step=writing.step_writer.serialize(result)._asdict(),
        after=None if index < 1 else project.steps[index - 1].definition.name
    )]

    return response.update(
        project=project.kernel_serialize(),
        step_name=result.definition.name,
        step_path=result.source_path,
        step_changes=step_changes,
        step_renames=step_renames
    ).notify(
        kind='CREATED',
        code='STEP_CREATED',
        message='"{}" step has been created'.format(result.definition.name)
    ).console(
        whitespace=1
    ).response
Esempio n. 12
0
def modify_step(
        response: Response,
        project: Project,
        name: str,
        new_name: str = None,
        position: typing.Union[str, int] = None,
        title: str = None
):
    """

    :param response:
    :param project:
    :param name:
    :param new_name:
    :param position:
    :param title:
    :return:
    """

    new_name = new_name if new_name else name
    old_index = project.index_of_step(name)
    new_index = index_from_location(response, project, position, old_index)

    if new_index > old_index:
        # If the current position of the step occurs before the new position
        # of the step, the new index has to be shifted by one to account for
        # the fact that this step will no longer be in this position when it
        # get placed in the position within the project
        new_index -= 1

    old_step = project.remove_step(name)
    if not old_step:
        response.fail(
            code='NO_SUCH_STEP',
            message='Unable to modify unknown step "{}"'.format(name)
        ).console(
            whitespace=1
        )
        return False

    source_path = old_step.source_path
    if os.path.exists(source_path):
        temp_path = '{}.cauldron_moving'.format(source_path)
        shutil.move(source_path, temp_path)
    else:
        temp_path = None

    res = step_support.synchronize_step_names(project, new_index)
    response.consume(res)
    step_renames = res.returned

    new_name_parts = naming.explode_filename(new_name, project.naming_scheme)
    new_name_parts['index'] = new_index

    if not project.naming_scheme and not new_name_parts['name']:
        new_name_parts['name'] = naming.find_default_filename(
            [s.definition.name for s in project.steps]
        )

    new_name = naming.assemble_filename(
        scheme=project.naming_scheme,
        **new_name_parts
    )

    step_data = {'name': new_name}
    if title is None:
        if old_step.definition.title:
            step_data['title'] = old_step.definition.title
    else:
        step_data['title'] = title.strip('"')

    new_step = project.add_step(step_data, new_index)

    project.save()

    if not os.path.exists(new_step.source_path):
        if temp_path and os.path.exists(temp_path):
            shutil.move(temp_path, new_step.source_path)
        else:
            with open(new_step.source_path, 'w+') as f:
                f.write('')

    if new_index > 0:
        before_step = project.steps[new_index - 1].definition.name
    else:
        before_step = None

    step_renames[old_step.definition.name] = {
        'name': new_step.definition.name,
        'title': new_step.definition.title
    }

    step_changes = [dict(
        name=new_step.definition.name,
        filename=new_step.filename,
        action='modified',
        after=before_step
    )]

    response.update(
        project=project.kernel_serialize(),
        step_name=new_step.definition.name,
        step_changes=step_changes,
        step_renames=step_renames
    ).notify(
        kind='SUCCESS',
        code='STEP_MODIFIED',
        message='Step modifications complete'
    ).console(
        whitespace=1
    )

    project.write()

    return True
Esempio n. 13
0
 def test_assemble(self):
     parts = naming.explode_filename('namer.py', projects.DEFAULT_SCHEME)
     result = naming.assemble_filename(scheme=projects.DEFAULT_SCHEME,
                                       **parts)
     self.assertTrue(result.endswith('namer.py'))
Esempio n. 14
0
 def test_explode_misaligned(self):
     # spec: {location: S01.py | name: namer.py | type: py}
     result = naming.explode_filename('namer.py', projects.DEFAULT_SCHEME)
     self.assertEqual(result['name'], 'namer')
     self.assertIsNone(result['index'], 0)
     self.assertEqual(result['extension'], 'py')