xlwings-utils 0.0.0__py3-none-any.whl → 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of xlwings-utils might be problematic. Click here for more details.
- xlwings_utils/xlwings_utils.py +224 -1
- xlwings_utils-0.0.1.dist-info/METADATA +43 -0
- xlwings_utils-0.0.1.dist-info/RECORD +6 -0
- {xlwings_utils-0.0.0.dist-info → xlwings_utils-0.0.1.dist-info}/WHEEL +1 -1
- xlwings_utils-0.0.0.dist-info/METADATA +0 -23
- xlwings_utils-0.0.0.dist-info/RECORD +0 -6
- {xlwings_utils-0.0.0.dist-info → xlwings_utils-0.0.1.dist-info}/top_level.txt +0 -0
xlwings_utils/xlwings_utils.py
CHANGED
|
@@ -5,5 +5,228 @@
|
|
|
5
5
|
# /_/\_\|_| \_/\_/ |_||_| |_| \__, ||___/ _____ \__,_| \__||_||_||___/
|
|
6
6
|
# |___/ |_____|
|
|
7
7
|
|
|
8
|
-
__version__ = "0.0.
|
|
8
|
+
__version__ = "0.0.1"
|
|
9
9
|
|
|
10
|
+
|
|
11
|
+
import dropbox
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
_captured_stdout = []
|
|
16
|
+
dbx = None
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def dropbox_init(refresh_token, app_key, app_secret):
|
|
20
|
+
'''
|
|
21
|
+
dropbox initialize
|
|
22
|
+
|
|
23
|
+
This function has to be called prior to using any dropbox function
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
refresh_token : str
|
|
28
|
+
oauth2 refreshntoken
|
|
29
|
+
|
|
30
|
+
app_key : str
|
|
31
|
+
app key
|
|
32
|
+
|
|
33
|
+
app_secret : str
|
|
34
|
+
app secret
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
Returns
|
|
38
|
+
-------
|
|
39
|
+
-
|
|
40
|
+
'''
|
|
41
|
+
global dbx
|
|
42
|
+
dbx = dropbox.Dropbox(oauth2_refresh_token=refresh_token, app_key=app_key, app_secret=app_secret)
|
|
43
|
+
|
|
44
|
+
def _check_dbx():
|
|
45
|
+
if dbx is None:
|
|
46
|
+
raise ValueError('not initialized. Please call dropbox_init()')
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def list_dropbox(path="", recursive=False):
|
|
50
|
+
'''
|
|
51
|
+
list_dropbox
|
|
52
|
+
|
|
53
|
+
returns all dropbox files in path
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
path : str or Pathlib.Path
|
|
58
|
+
path from which to list all files (default: '')
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
recursive : bool
|
|
62
|
+
if True, recursively list files. if False (default) no recursion
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
files, relative to path : list
|
|
67
|
+
'''
|
|
68
|
+
_check_dbx()
|
|
69
|
+
out = []
|
|
70
|
+
result = dbx.files_list_folder(path, recursive=recursive)
|
|
71
|
+
|
|
72
|
+
for entry in result.entries:
|
|
73
|
+
if isinstance(entry, dropbox.files.FileMetadata):
|
|
74
|
+
out.append(entry.path_display)
|
|
75
|
+
|
|
76
|
+
while result.has_more:
|
|
77
|
+
result = dbx.files_list_folder_continue(result.cursor)
|
|
78
|
+
for entry in result.entries:
|
|
79
|
+
if isinstance(entry, dropbox.files.FileMetadata):
|
|
80
|
+
out.append(entry.path_display)
|
|
81
|
+
|
|
82
|
+
return out
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def read_dropbox(dropbox_path):
|
|
86
|
+
'''
|
|
87
|
+
read_dropbox
|
|
88
|
+
|
|
89
|
+
read from dopbox at given path
|
|
90
|
+
|
|
91
|
+
Parameters
|
|
92
|
+
----------
|
|
93
|
+
path : str or Pathlib.Path
|
|
94
|
+
path to read from
|
|
95
|
+
|
|
96
|
+
Returns
|
|
97
|
+
-------
|
|
98
|
+
contents of the dropbox file : bytes
|
|
99
|
+
'''
|
|
100
|
+
|
|
101
|
+
_check_dbx()
|
|
102
|
+
metadata, response = dbx.files_download(dropbox_path)
|
|
103
|
+
file_content = response.content
|
|
104
|
+
return file_content
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def write_dropbox(dropbox_path, contents):
|
|
108
|
+
_check_dbx()
|
|
109
|
+
dbx.files_upload(contents, dropbox_path, mode=dropbox.files.WriteMode.overwrite)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def list_pyodide(path):
|
|
113
|
+
path = Path(path)
|
|
114
|
+
return list(path.iterdir())
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def write_pyodide(path, contents):
|
|
118
|
+
path = Path(path)
|
|
119
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
120
|
+
with open(path, "wb") as f:
|
|
121
|
+
f.write(contents)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def read_pyodide(path):
|
|
125
|
+
path = Path(path)
|
|
126
|
+
with open(path, "rb") as f:
|
|
127
|
+
contents = f.read()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def clear_captured_stdout():
|
|
131
|
+
"""
|
|
132
|
+
empties the captured stdout
|
|
133
|
+
"""
|
|
134
|
+
_captured_stdout.clear()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def captured_stdout_as_str():
|
|
138
|
+
"""
|
|
139
|
+
returns the captured stdout as a list of strings
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
captured stdout : list
|
|
144
|
+
each line is an element of the list
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
return "".join(_captured_stdout)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def captured_stdout_as_range():
|
|
151
|
+
"""
|
|
152
|
+
returns the captured stdout as a list of strings
|
|
153
|
+
|
|
154
|
+
Returns
|
|
155
|
+
-------
|
|
156
|
+
captured stdout : list
|
|
157
|
+
each line is an element of the list
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
return [[line] for line in captured_stdout_as_str().splitlines()]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class capture_stdout:
|
|
164
|
+
"""
|
|
165
|
+
specifies how to capture stdout
|
|
166
|
+
|
|
167
|
+
Parameters
|
|
168
|
+
----------
|
|
169
|
+
include_print : bool
|
|
170
|
+
if True (default), the output is also printed out as normal
|
|
171
|
+
|
|
172
|
+
if False, no output is printed
|
|
173
|
+
|
|
174
|
+
Note
|
|
175
|
+
----
|
|
176
|
+
This function is normally used as a context manager, like ::
|
|
177
|
+
|
|
178
|
+
with capture_stdout():
|
|
179
|
+
...
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
def __init__(self, include_print: bool = True):
|
|
183
|
+
self.stdout = sys.stdout
|
|
184
|
+
self.include_print = include_print
|
|
185
|
+
|
|
186
|
+
def __enter__(self):
|
|
187
|
+
sys.stdout = self
|
|
188
|
+
|
|
189
|
+
def __exit__(self, exc_type, exc_value, tb):
|
|
190
|
+
sys.stdout = self.stdout
|
|
191
|
+
|
|
192
|
+
def write(self, data):
|
|
193
|
+
_captured_stdout.append(data)
|
|
194
|
+
if self.include_print:
|
|
195
|
+
self.stdout.write(data)
|
|
196
|
+
|
|
197
|
+
def flush(self):
|
|
198
|
+
if self.include_print:
|
|
199
|
+
self.stdout.flush()
|
|
200
|
+
_captured_stdout.append("\n")
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
if __name__ == "__main__":
|
|
204
|
+
...
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
# @xw.script
|
|
208
|
+
# def test_stdout(book: xw.Book):
|
|
209
|
+
# with capture_stdout():
|
|
210
|
+
# for i in range(10):
|
|
211
|
+
# print(10 * f'{i}')
|
|
212
|
+
# book.sheets.active.range("A1").value=captured_stdout_as_range()
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# @xw.script
|
|
216
|
+
# def test(book: xw.Book):
|
|
217
|
+
# dropbox_init(
|
|
218
|
+
# refresh_token=REFRESH_TOKEN,
|
|
219
|
+
# app_key=APP_KEY,
|
|
220
|
+
# app_secret=APP_SECRET,
|
|
221
|
+
# )
|
|
222
|
+
|
|
223
|
+
# files =list_dropbox("/Downloads",recursive=True)
|
|
224
|
+
# peek(files)
|
|
225
|
+
# write_pyodide("Test/x.x", b"abc")
|
|
226
|
+
# files = list_pyodide("Test")
|
|
227
|
+
|
|
228
|
+
# # files=list_dropbox_files("/Downloads")
|
|
229
|
+
# # peek(files)
|
|
230
|
+
# contents = read_dropbox("/Downloads/dropbox setup.py")
|
|
231
|
+
# write_dropbox("/Downloads/dropbox setup1.py", contents)
|
|
232
|
+
# peek("done")
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xlwings_utils
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: xlwings_utils
|
|
5
|
+
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/salabim/xlwings_utils
|
|
7
|
+
Project-URL: Repository, https://github.com/salabim/xlwings_utils
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
Requires-Dist: dropbox
|
|
13
|
+
|
|
14
|
+
# xlwings_utils
|
|
15
|
+
|
|
16
|
+
## Introduction
|
|
17
|
+
|
|
18
|
+
This module provides some useful functiona to be used in xlwings lite.
|
|
19
|
+
The xlwings lite system does not provide access to the local file system. With this
|
|
20
|
+
module, files can be copied between dropbox and the pyodide file systen. And
|
|
21
|
+
therefore, it is possible to indirectly use the local file system.
|
|
22
|
+
|
|
23
|
+
On top of that, this module makes it possible to capture stdout writes, which
|
|
24
|
+
can then be copied to a worksheet in a later stage.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Just add xlwings-utils to the requirement tab.
|
|
29
|
+
|
|
30
|
+
## Dropbox support
|
|
31
|
+
|
|
32
|
+
xlwings_lite only works with full access dropbox apps.
|
|
33
|
+
|
|
34
|
+
In order to use dropbox, is is necessary to initialize the module with credentials.
|
|
35
|
+
|
|
36
|
+
## Capture stdout support
|
|
37
|
+
|
|
38
|
+
Badges
|
|
39
|
+
|
|
40
|
+
  
|
|
41
|
+
 
|
|
42
|
+

|
|
43
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
xlwings_utils/__init__.py,sha256=FdaRztevSu5akGL7KBUBRzqwLMRTdvVUuS2Kfp2f1Uc,68
|
|
2
|
+
xlwings_utils/xlwings_utils.py,sha256=vc_w_NdyfQLX4vrq0PARMtxYoFYAn39UuUzi8FqlGCc,5234
|
|
3
|
+
xlwings_utils-0.0.1.dist-info/METADATA,sha256=3PoU8ESYGmgGQPCLj3ilC3TeeMxi8azkccGq_YEH3yU,1662
|
|
4
|
+
xlwings_utils-0.0.1.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
|
|
5
|
+
xlwings_utils-0.0.1.dist-info/top_level.txt,sha256=kf5SEv0gZiRObPhUoYcc1O_iX_wwTOPeUIYvzyYeAM4,14
|
|
6
|
+
xlwings_utils-0.0.1.dist-info/RECORD,,
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: xlwings_utils
|
|
3
|
-
Version: 0.0.0
|
|
4
|
-
Summary: xlwings_utils
|
|
5
|
-
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
|
-
Project-URL: Homepage, https://github.com/salabim/xlwings_utils
|
|
7
|
-
Project-URL: Repository, https://github.com/salabim/xlwings_utils
|
|
8
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
-
Requires-Python: >=3.9
|
|
11
|
-
Description-Content-Type: text/markdown
|
|
12
|
-
Requires-Dist: dropbox
|
|
13
|
-
|
|
14
|
-
# xlwings_utils
|
|
15
|
-
|
|
16
|
-
## Introduction
|
|
17
|
-
|
|
18
|
-
Badges
|
|
19
|
-
|
|
20
|
-
  
|
|
21
|
-
 
|
|
22
|
-

|
|
23
|
-
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
xlwings_utils/__init__.py,sha256=FdaRztevSu5akGL7KBUBRzqwLMRTdvVUuS2Kfp2f1Uc,68
|
|
2
|
-
xlwings_utils/xlwings_utils.py,sha256=KV-Hrm1YGQz-YJeirfna0ojfqj486sPZa_aXWGuhz4Q,444
|
|
3
|
-
xlwings_utils-0.0.0.dist-info/METADATA,sha256=2M20uewewm6G-pbiKXJEAhdbt-P5EUYNM0IxAtDKSrs,958
|
|
4
|
-
xlwings_utils-0.0.0.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
|
|
5
|
-
xlwings_utils-0.0.0.dist-info/top_level.txt,sha256=kf5SEv0gZiRObPhUoYcc1O_iX_wwTOPeUIYvzyYeAM4,14
|
|
6
|
-
xlwings_utils-0.0.0.dist-info/RECORD,,
|
|
File without changes
|