Skip to content

Commit 0f4971f

Browse files
authored
added folders.create_path
* Add recursive folder creation and example * Corrections based on deepsource * New corrections based on deepsource second run * Correct typo in upload_file.py * Update Folders documentation missing parameters * Change method name to create_path Update code to be recursive * Updates based on comments * Remove HTTPError import from folders.py * Corrections based on comments * Update folders service based on latest comments --------- Co-authored-by: Xavier BIZOUX <xavier.bizouxësas.com>
1 parent 7a2a9a8 commit 0f4971f

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ doc/build
6767
doc/source/generated
6868
doc/_build
6969
doc/.doctrees
70+
71+
## VS CODE settings
72+
.vscode/settings.json

examples/upload_file.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import getpass
2+
import os
3+
4+
from sasctl import Session
5+
from sasctl.services import files, folders
6+
7+
server_name = "myServer.sas.com"
8+
user = "sasdemo"
9+
password = getpass.getpass()
10+
os_path = "/test/Downloads/pdfs"
11+
sas_path = "/Public/PDFs"
12+
13+
14+
def build_storage_lists(source_path, target_path):
15+
"""
16+
Function to create a list of files and directories
17+
from a source folder at OS level
18+
and mapped to target folder in SAS Content Server.
19+
20+
Parameters
21+
----------
22+
source_path : str
23+
Path to the folder which should be imported.
24+
target_path : str
25+
Path to the folder which will contain the files and folders in SAS Content Server.
26+
27+
Returns
28+
-------
29+
f_list
30+
A list of file information which can be used to map local and SAS files.
31+
d_list
32+
A list of directory information which can be user to map local and SAS folder structure.
33+
"""
34+
files_list = []
35+
folders_list = []
36+
37+
for root, _, src_files in os.walk(source_path):
38+
for src_file in src_files:
39+
if src_file.endswith(".pdf"):
40+
if root not in folders_list:
41+
folder_info = root.replace(source_path, target_path)
42+
if folder_info not in folders_list:
43+
folders_list.append(folder_info)
44+
file_info = {}
45+
file_info["source_file"] = os.path.join(root, src_file)
46+
file_info["target_folder"] = root.replace(
47+
source_path, target_path)
48+
files_list.append(file_info)
49+
return folders_list, files_list
50+
51+
52+
with Session(server_name, user, password):
53+
folders_list, files_list = build_storage_lists(os_path, sas_path)
54+
for folder in folders_list:
55+
folders.create_path(folder)
56+
for file in files_list:
57+
files.create_file(
58+
file["source_file"],
59+
file["target_folder"],
60+
os.path.basename(file["source_file"])
61+
)

src/sasctl/_services/folders.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def create_folder(cls, name, parent=None, description=None):
4949

5050
parent_uri = cls.get_link(parent_obj, "self")
5151
if parent_uri is None:
52-
raise ValueError("`parent` folder '%s' does not exist." % parent)
52+
raise ValueError(
53+
f"`parent` folder {parent} does not exist.")
5354
parent_uri = parent_uri["uri"]
5455
else:
5556
parent_uri = None
@@ -135,3 +136,35 @@ def get_folder(cls, folder, refresh=False):
135136
return cls.get(f"/folders/{folder}")
136137

137138
return cls._get_folder(folder, refresh=refresh)
139+
140+
@classmethod
141+
def create_path(cls, folder, description=None):
142+
"""Create a new folder recursively.
143+
144+
Parameters
145+
----------
146+
folder : str
147+
The folder to be created including the path.
148+
description: str, optional
149+
A description of the folder
150+
151+
Returns
152+
-------
153+
RestObj
154+
Details of newly-created folder
155+
156+
"""
157+
folder = str(folder)
158+
# Path must include a leading "/"
159+
if not folder.startswith("/"):
160+
folder = f"/{folder}"
161+
path = folder.split("/")
162+
for level in range(2, len(path)+1):
163+
164+
current_path = path[0:level]
165+
name = current_path[-1]
166+
parent = "/".join(current_path[0:-1]) or None
167+
new_folder = cls.get_folder("/".join(current_path))
168+
if not new_folder:
169+
new_folder = cls.create_folder(name, parent=parent, description=description)
170+
return new_folder

0 commit comments

Comments
 (0)