fairdatanow 0.2.0__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.
@@ -0,0 +1,3 @@
1
+ __version__ = "0.2.0"
2
+
3
+ from .webdav import *
fairdatanow/_modidx.py ADDED
@@ -0,0 +1,15 @@
1
+ # Autogenerated by nbdev
2
+
3
+ d = { 'settings': { 'branch': 'master',
4
+ 'doc_baseurl': '/fairdatanow',
5
+ 'doc_host': 'https://fligt.github.io',
6
+ 'git_url': 'https://github.com/fligt/fairdatanow',
7
+ 'lib_path': 'fairdatanow'},
8
+ 'syms': { 'fairdatanow.webdav': { 'fairdatanow.webdav.RemoteData': ( 'exploring-your-remote-data.html#remotedata',
9
+ 'fairdatanow/webdav.py'),
10
+ 'fairdatanow.webdav.RemoteData.__init__': ( 'exploring-your-remote-data.html#remotedata.__init__',
11
+ 'fairdatanow/webdav.py'),
12
+ 'fairdatanow.webdav.RemoteData.download_selected': ( 'exploring-your-remote-data.html#remotedata.download_selected',
13
+ 'fairdatanow/webdav.py'),
14
+ 'fairdatanow.webdav._node_to_dataframe': ( 'exploring-your-remote-data.html#_node_to_dataframe',
15
+ 'fairdatanow/webdav.py')}}}
fairdatanow/webdav.py ADDED
@@ -0,0 +1,139 @@
1
+ """Instantly find and access all your Nextcloud data files"""
2
+
3
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../notebooks/10_exploring-your-remote-data.ipynb.
4
+
5
+ # %% auto 0
6
+ __all__ = ['RemoteData']
7
+
8
+ # %% ../notebooks/10_exploring-your-remote-data.ipynb 19
9
+ import nc_py_api
10
+ from nc_py_api import Nextcloud
11
+
12
+ import polars as pl
13
+ import itables
14
+ from itables.widget import ITable
15
+ import os
16
+ from pathlib import Path
17
+ import time
18
+ import re
19
+ from IPython.display import HTML, display
20
+
21
+ # %% ../notebooks/10_exploring-your-remote-data.ipynb 20
22
+ def _node_to_dataframe(fsnode):
23
+ '''Convert `fsnode` object to polars a single row polars dataframe.'''
24
+
25
+ df = pl.DataFrame({'path': fsnode.user_path, 'size': fsnode.info.size, 'mimetype': fsnode.info.mimetype, 'modified': fsnode.info.last_modified,
26
+ 'isdir': fsnode.is_dir, 'ext': os.path.splitext(fsnode.user_path)[1]})
27
+
28
+ return df
29
+
30
+
31
+ class RemoteData(object):
32
+
33
+ # See: https://help.nextcloud.com/t/using-nc-py-api-i-cant-download-any-file-due-to-ssl-certificte-verify-failed/194019
34
+ nc_py_api.options.NPA_NC_CERT = False
35
+
36
+ # keep full dataframe
37
+ itables.options.maxBytes = 0
38
+ itables.init_notebook_mode()
39
+
40
+ def __init__(self, configuration, searchBuilder={}):
41
+ '''Recursively scan the contents of a remote webdav server as specified by `configuration`.
42
+ '''
43
+
44
+ # parse configuration
45
+ m = re.match('(^https://[^/]+/)(.*)', configuration['url'])
46
+ nextcloud_url, self.cache_dir = m.groups()
47
+ nc_auth_user = configuration['user']
48
+ nc_auth_pass = configuration['password']
49
+
50
+ print(f'Please wait while scanning all file paths in remote folder...')
51
+
52
+ # Instantiate Nextcloud client
53
+ self.nc = Nextcloud(nextcloud_url=nextcloud_url, nc_auth_user=nc_auth_user, nc_auth_pass=nc_auth_pass)
54
+
55
+ # query webdav server to obtain file listing
56
+ fs_nodes_list = self.nc.files.listdir(self.cache_dir, depth=-1, exclude_self=False)
57
+
58
+ n_paths = len(fs_nodes_list)
59
+
60
+ # initialize polars dataframe with first row to fix schema
61
+ self.df = _node_to_dataframe(fs_nodes_list[0])
62
+
63
+ for fsnode in fs_nodes_list[1:]:
64
+ self.df.extend(_node_to_dataframe(fsnode))
65
+
66
+ # create interactive table
67
+ self.itable = ITable(
68
+ self.df,
69
+ layout={"top1": "searchBuilder"},
70
+ select=True,
71
+ searchBuilder=searchBuilder,
72
+ scrollY="500px", scrollCollapse=True, paging=False,
73
+ )
74
+
75
+ print(f"Ready building file table for '{self.cache_dir}', Total number of files and directories: {n_paths} ")
76
+
77
+
78
+ def download_selected(self, cache_dir=None):
79
+ '''Download selected files (blue rows) from `table` to default local cache directory.
80
+
81
+ A custom `cache_dir` can be specified. '''
82
+
83
+ # create cache path
84
+ if cache_dir is None:
85
+ cache_path = Path.home().joinpath('.cache', 'fairdatanow')
86
+ else:
87
+ cache_path = Path.home().joinpath('.cache', cache_dir)
88
+
89
+ os.makedirs(cache_path, exist_ok=True)
90
+
91
+ # obtain remote paths and remote timestamps
92
+ remote_path_list = [self.itable.df['path'][n] for n in self.itable.selected_rows]
93
+ remote_modified_list = [self.itable.df['modified'][n] for n in self.itable.selected_rows]
94
+ remote_isdir_list = [self.itable.df['isdir'][n] for n in self.itable.selected_rows]
95
+
96
+ n_files = len(remote_path_list)
97
+
98
+ for i, [remote_path, remote_modified, remote_isdir] in enumerate(zip(remote_path_list, remote_modified_list, remote_isdir_list)):
99
+
100
+ # only download actual files
101
+ if not remote_isdir:
102
+ remote_directory = os.path.dirname(remote_path)
103
+ local_directory = cache_path.joinpath(remote_directory) # I guess this will not yet work for Windows
104
+
105
+ # create directory structure inside cache
106
+ os.makedirs(local_directory, exist_ok=True)
107
+
108
+ # get remote epoch time
109
+ remote_modified_epoch_time = remote_modified.timestamp()
110
+
111
+ # construct corresponding local path
112
+ local_path = cache_path.joinpath(remote_path)
113
+
114
+ # check if local file exists and if modification times are similar
115
+ is_local = local_path.exists()
116
+
117
+ is_similar = False
118
+ local_modified_epoch_time = None
119
+ if is_local:
120
+ local_modified_epoch_time = os.stat(local_path).st_mtime
121
+ if local_modified_epoch_time == remote_modified_epoch_time:
122
+ is_similar = True
123
+
124
+ # download from nextcloud
125
+ if not is_similar:
126
+ print(f'[{i}/{n_files - 1}] Timestamps do no match: {remote_modified_epoch_time} vs {local_modified_epoch_time}', end='\r')
127
+ print(f'[{i}/{n_files - 1}] Downloading to: {local_path} ' , end='\r')
128
+
129
+ # write to cache
130
+ with open(local_path, 'bw') as fh:
131
+ self.nc.files.download2stream(remote_path, fh)
132
+
133
+ # adjust last modified timestamp
134
+ now = int(time.time())
135
+ os.utime(local_path, (now, remote_modified_epoch_time))
136
+
137
+ print(f"Ready with downloading {n_files} selected remote files to local cache: {cache_path} ")
138
+
139
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Frank Ligterink
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,66 @@
1
+ Metadata-Version: 2.2
2
+ Name: fairdatanow
3
+ Version: 0.2.0
4
+ Summary: Experimental Python package to support working with data from (y)our Nextcloud server
5
+ Home-page: https://github.com/fligt/fairdatanow
6
+ Author: Frank Ligterink
7
+ Author-email: frank.ligterink@gmail.com
8
+ License: MIT License
9
+ Keywords: nbdev jupyter notebook python
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Natural Language :: English
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Requires-Python: >=3.9
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: nc-py-api
22
+ Requires-Dist: polars[all]
23
+ Requires-Dist: itables
24
+ Requires-Dist: anywidget
25
+ Provides-Extra: dev
26
+ Dynamic: author
27
+ Dynamic: author-email
28
+ Dynamic: classifier
29
+ Dynamic: description
30
+ Dynamic: description-content-type
31
+ Dynamic: home-page
32
+ Dynamic: keywords
33
+ Dynamic: license
34
+ Dynamic: provides-extra
35
+ Dynamic: requires-dist
36
+ Dynamic: requires-python
37
+ Dynamic: summary
38
+
39
+ # Welcome to fairdatanow
40
+
41
+
42
+ <!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
43
+
44
+ Effective interdisciplinary cooperation in data science projects
45
+ requires smooth access to both data and code. And with the recent
46
+ development of two amazing open source frameworks (self hosted Nextcloud
47
+ for data storage and Jupyter notebooks for computing), we are now in a
48
+ position to actually make this happen. Recently our lab (the
49
+ Rijkserfgoedlaboratorium in Amsterdam) has installed a Nextcloud server
50
+ that gives our lab the possibility to share data files organized in
51
+ project folders with research partners. These can be accessed manually
52
+ via a web interface, but also programmatically with the webdav protocol!
53
+ With `fairdatanow` we want to learn how to easily find en download data
54
+ files from a Nextcloud storage directly in Jupyter notebooks in order to
55
+ explore them and create visualizations.
56
+
57
+ Under the hood the `fairdatanow` makes use of the Nextcloud Python
58
+ Framework [nc-py-api](https://pypi.org/project/nc-py-api) for
59
+ communication with the cloud server, and the powerful Python packages
60
+ [polars](https://pypi.org/project/polars/),
61
+ [itables](https://pypi.org/project/itables/) and
62
+ [anywidget](https://pypi.org/project/anywidget/) and for creating
63
+ interactive tables.
64
+
65
+ Although the package is still **under construction** it is already
66
+ available for installation from the python package index.
@@ -0,0 +1,9 @@
1
+ fairdatanow/__init__.py,sha256=ck9MDNa13_UNZFFApaxdA-9ySmsdjfwm_QCtnJQWgUk,45
2
+ fairdatanow/_modidx.py,sha256=0qQSdqRZmE87iEA-niRDh_LUAk5BzlU3EH6Xg00B_yo,1242
3
+ fairdatanow/webdav.py,sha256=JcLYfZoc3WpNSXPUCUEX2UmrKRzHXNxUAdW1GR9H-K0,5852
4
+ fairdatanow-0.2.0.dist-info/LICENSE,sha256=j-59fryWPhEIb36jnuX7LMesZyQnDOftm4PbTqf0Vj8,1072
5
+ fairdatanow-0.2.0.dist-info/METADATA,sha256=R3tCdxn4lCFUoCsvoYNim7fWXU9a5oy4ybQhaWOPr5Q,2564
6
+ fairdatanow-0.2.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
+ fairdatanow-0.2.0.dist-info/entry_points.txt,sha256=Ssb-NQaPCHs9cNvXjv0u9YaHXQiz1YrM_tiQo7p_v-E,44
8
+ fairdatanow-0.2.0.dist-info/top_level.txt,sha256=1UDSldUj8-hHz6ejbIcoGulNtowBRLKrMqyA_cECAsg,12
9
+ fairdatanow-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.8.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [nbdev]
2
+ fairdatanow = fairdatanow._modidx:d
@@ -0,0 +1 @@
1
+ fairdatanow