Repository: Pythagora-io/gpt-pilot
File: pilot/helpers/Project.py (Python)

Code before:
                "path": file_path,
                "content": file_content
            })
        return files_with_content

    def save_file(self, data):
        """
        Save a file.

        Args:
            data: { name: 'hello.py', path: 'path/to/hello.py', content: 'print("Hello!")' }
        """
        name = data['name'] if 'name' in data and data['name'] != '' else os.path.basename(data['path'])
        path = data['path'] if 'path' in data else name

        path, full_path = self.get_full_file_path(path, name)
        update_file(full_path, data['content'])

        (File.insert(app=self.app, path=path, name=name, full_path=full_path)
         .on_conflict(
            conflict_target=[File.app, File.name, File.path],
            preserve=[],
            update={'name': name, 'path': path, 'full_path': full_path})
         .execute())

    def get_full_file_path(self, file_path: str, file_name: str) -> Tuple[str, str]:
        file_name = os.path.basename(file_name)

        if file_path.startswith(self.root_path):
            file_path = file_path.replace(self.root_path, '')

        are_windows_paths = '\\' in file_path or '\\' in file_name or '\\' in self.root_path
        if are_windows_paths:
            file_path = file_path.replace('\\', '/')

        # Force all paths to be relative to the workspace
        file_path = re.sub(r'^(\w+:/|[/~.]+)', '', file_path, 1)

        # file_path should not include the file name
        if file_path == file_name:
            file_path = ''
        elif file_path.endswith('/' + file_name):
            file_path = file_path.replace('/' + file_name, '')
        elif file_path.endswith('/'):
            file_path = file_path[:-1]

        absolute_path = self.root_path + '/' + file_name if file_path == '' \
            else self.root_path + '/' + file_path + '/' + file_name

        return file_path, absolute_path


Review comment (refactor):
this function is not working properly for many test cases on mac, lets remove changes made to this function from this PR and do it in separate PR just for this function refactoring once it is finished

Code after:
            })
        return files_with_content

    def save_file(self, data):
        """
        Save a file.

        Args:
            data: { name: 'hello.py', path: 'path/to/hello.py', content: 'print("Hello!")' }
        """
        # TODO fix this in prompts
        if 'path' not in data:
            data['path'] = data['name']

        if 'name' not in data or data['name'] == '':
            data['name'] = os.path.basename(data['path'])
        elif not data['path'].endswith(data['name']):
            if data['path'] == '':
                data['path'] = data['name']
            else:
                data['path'] = data['path'] + '/' + data['name']
        # TODO END

        data['path'], data['full_path'] = self.get_full_file_path(data['path'], data['name'])
        update_file(data['full_path'], data['content'])

        (File.insert(app=self.app, path=data['path'], name=data['name'], full_path=data['full_path'])
         .on_conflict(
            conflict_target=[File.app, File.name, File.path],
            preserve=[],
            update={'name': data['name'], 'path': data['path'], 'full_path': data['full_path']})
         .execute())

    def get_full_file_path(self, file_path: str, file_name: str) -> Tuple[str, str]:

        # WINDOWS
        are_windows_paths = '\\' in file_path or '\\' in file_name or '\\' in self.root_path
        if are_windows_paths:
            file_name = file_name.replace('\\', '/')
            file_path = file_path.replace('\\', '/')
        # END WINDOWS

        # Universal modifications
        file_path = file_path.replace('~', '')
        file_name = file_name.replace('~', '')

        file_path = file_path.replace(self.root_path, '')
        file_name = file_name.replace(self.root_path, '')

        if '.' not in file_path and not file_path.endswith('/'):
            file_path += '/'