Skip to content

Commit 11e7d7f

Browse files
committed
bump version to 0.1.2 and add file renaming method in SharePoint service
1 parent d0c830e commit 11e7d7f

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "office365-service"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "Add your description here"
55
readme = "README.md"
66
authors = [

src/office365_service/sharepoint_service.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ def obter_pasta(self, caminho_pasta: str) -> Folder | None:
106106
def obter_arquivo(self, caminho_arquivo: str) -> File | None:
107107
"""Obtém um objeto File a partir do seu caminho relativo no servidor."""
108108
try:
109-
folder = self.ctx.web.get_file_by_server_relative_url(caminho_arquivo)
110-
folder.get().execute_query()
111-
return folder
109+
file = self.ctx.web.get_file_by_server_relative_url(caminho_arquivo)
110+
file.get().execute_query()
111+
return file
112112
except ClientRequestException as e:
113113
if e.response.status_code == 404:
114114
return None
@@ -122,8 +122,8 @@ def listar_arquivos(self, pasta_alvo: Folder | str):
122122
if pasta is None:
123123
raise FileNotFoundError(f"A pasta '{pasta_alvo}' não foi encontrada.")
124124
pasta_alvo = pasta
125-
126-
files = pasta_alvo.files.expand(["ModifiedBy"]).get().execute_query()
125+
files = pasta_alvo.files
126+
files.expand(["ModifiedBy"]).get().execute_query()
127127
return files
128128

129129
@handle_sharepoint_errors()
@@ -200,7 +200,6 @@ def mover_arquivo(self, arquivo_origem: File, pasta_destino: Folder | str):
200200
raise FileNotFoundError(f"A pasta de destino '{pasta_destino}' não foi encontrada.")
201201
pasta_destino = pasta
202202

203-
# Constrói a URL de destino
204203
url_destino = os.path.join(pasta_destino.properties['ServerRelativeUrl'], arquivo_origem.name)
205204

206205
print(f"Movendo '{arquivo_origem.name}' para '{pasta_destino.properties['ServerRelativeUrl']}'...")
@@ -223,7 +222,21 @@ def copiar_arquivo(self, arquivo_origem: File, pasta_destino: Folder | str):
223222
print("Arquivo copiado com sucesso.")
224223
return novo_arquivo
225224

225+
@handle_sharepoint_errors()
226+
def renomear_arquivo(self, arquivo: File, novo_nome: str) -> File:
227+
"""Renomeia um arquivo no SharePoint."""
228+
print(f"Renomeando '{arquivo.name}' para '{novo_nome}'...")
229+
novo_arquivo = arquivo.rename(novo_nome)
230+
novo_arquivo.execute_query()
231+
print("Arquivo renomeado com sucesso.")
232+
return novo_arquivo
233+
226234
def obter_pasta_por_nome(self, pasta_raiz: Folder, nome):
227235
pastas = list(self.listar_pastas(pasta_raiz))
228236
pasta_encontrada = next((pasta for pasta in pastas if nome in pasta.name), None)
229237
return pasta_encontrada
238+
239+
def obter_arquivo_por_nome(self, pasta: Folder, nome):
240+
arquivos = list(self.listar_arquivos(pasta))
241+
arquivo_encontrado = next((arquivo for arquivo in arquivos if nome in arquivo.name), None)
242+
return arquivo_encontrado

0 commit comments

Comments
 (0)