Skip to content

Commit 63aa11f

Browse files
committed
Media file paths can now be Path objects
1 parent 17dd2b1 commit 63aa11f

File tree

5 files changed

+59
-45
lines changed

5 files changed

+59
-45
lines changed

api.ipynb

+31-21
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@
427427
" * a list of paths to the image/media files (filename is used as title)\n",
428428
" * a list of dicts, each containing `title`, and `path` values\n",
429429
" \n",
430+
" The path values can either be strings or pathlib Paths.\n",
431+
" \n",
430432
" Returns: \n",
431433
" * the modified payload dict\n",
432434
" '''\n",
@@ -435,12 +437,12 @@
435437
" for index, media_file in enumerate(media_files):\n",
436438
" if isinstance(media_file, dict):\n",
437439
" title = media_file['title']\n",
438-
" path = media_file['path']\n",
440+
" path = Path(media_file['path'])\n",
439441
" else:\n",
440-
" title = media_file[:-4]\n",
441-
" path = media_file\n",
442+
" path = Path(media_file)\n",
443+
" title = path.name\n",
442444
" payload['o:media'].append({'o:ingester': 'upload', 'file_index': str(index), 'o:item': {}, 'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]})\n",
443-
" files[f'file[{index}]'] = Path(path).read_bytes()\n",
445+
" files[f'file[{index}]'] = path.read_bytes()\n",
444446
" files['data'] = (None, json.dumps(payload), 'application/json')\n",
445447
" return files"
446448
]
@@ -1846,9 +1848,9 @@
18461848
"data": {
18471849
"text/plain": [
18481850
"{'@context': 'http://timsherratt.org/collections/api-context',\n",
1849-
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
1851+
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
18501852
" '@type': 'o:Item',\n",
1851-
" 'o:id': 730,\n",
1853+
" 'o:id': 763,\n",
18521854
" 'o:is_public': True,\n",
18531855
" 'o:owner': {'@id': 'http://timsherratt.org/collections/api/users/1',\n",
18541856
" 'o:id': 1},\n",
@@ -1858,9 +1860,9 @@
18581860
" 'o:thumbnail': None,\n",
18591861
" 'o:title': 'The Bendigo Independent (Vic. : 1891 - 1918)',\n",
18601862
" 'thumbnail_display_urls': {'large': None, 'medium': None, 'square': None},\n",
1861-
" 'o:created': {'@value': '2022-01-26T04:46:55+00:00',\n",
1863+
" 'o:created': {'@value': '2022-01-27T05:35:36+00:00',\n",
18621864
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
1863-
" 'o:modified': {'@value': '2022-01-26T04:46:55+00:00',\n",
1865+
" 'o:modified': {'@value': '2022-01-27T05:35:36+00:00',\n",
18641866
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
18651867
" 'o:media': [],\n",
18661868
" 'o:item_set': [],\n",
@@ -2071,8 +2073,8 @@
20712073
" '@value': \"MR WRAGGE'S PREDICTION. RENEWAL OF CYCLONE FORETOLD.\"}],\n",
20722074
" 'schema:isPartOf': [{'property_id': 736,\n",
20732075
" 'type': 'resource:item',\n",
2074-
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
2075-
" 'value_resource_id': 730,\n",
2076+
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
2077+
" 'value_resource_id': 763,\n",
20762078
" 'value_resource_name': 'items'}],\n",
20772079
" 'schema:datePublished': [{'property_id': 928,\n",
20782080
" 'type': 'numeric:timestamp',\n",
@@ -2105,6 +2107,7 @@
21052107
"outputs": [],
21062108
"source": [
21072109
"# Create a list of paths pointing to media files\n",
2110+
"# The paths can be strings or pathlib Paths\n",
21082111
"media_files = ['media/nla.news-article226799674-24144902.jpg']\n",
21092112
"\n",
21102113
"# Include the media files when we upload the payload\n",
@@ -2127,9 +2130,9 @@
21272130
"data": {
21282131
"text/plain": [
21292132
"{'@context': 'http://timsherratt.org/collections/api-context',\n",
2130-
" '@id': 'http://timsherratt.org/collections/api/items/731',\n",
2133+
" '@id': 'http://timsherratt.org/collections/api/items/766',\n",
21312134
" '@type': 'o:Item',\n",
2132-
" 'o:id': 731,\n",
2135+
" 'o:id': 766,\n",
21332136
" 'o:is_public': True,\n",
21342137
" 'o:owner': {'@id': 'http://timsherratt.org/collections/api/users/1',\n",
21352138
" 'o:id': 1},\n",
@@ -2138,15 +2141,15 @@
21382141
" 'o:id': 4},\n",
21392142
" 'o:thumbnail': None,\n",
21402143
" 'o:title': \"MR WRAGGE'S PREDICTION. RENEWAL OF CYCLONE FORETOLD.\",\n",
2141-
" 'thumbnail_display_urls': {'large': 'http://timsherratt.org/collections/files/large/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg',\n",
2142-
" 'medium': 'http://timsherratt.org/collections/files/medium/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg',\n",
2143-
" 'square': 'http://timsherratt.org/collections/files/square/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg'},\n",
2144-
" 'o:created': {'@value': '2022-01-26T04:47:02+00:00',\n",
2144+
" 'thumbnail_display_urls': {'large': 'http://timsherratt.org/collections/files/large/370fd463d1743fc81fa00d71448f294e7aec7643.jpg',\n",
2145+
" 'medium': 'http://timsherratt.org/collections/files/medium/370fd463d1743fc81fa00d71448f294e7aec7643.jpg',\n",
2146+
" 'square': 'http://timsherratt.org/collections/files/square/370fd463d1743fc81fa00d71448f294e7aec7643.jpg'},\n",
2147+
" 'o:created': {'@value': '2022-01-27T05:37:15+00:00',\n",
21452148
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
2146-
" 'o:modified': {'@value': '2022-01-26T04:47:02+00:00',\n",
2149+
" 'o:modified': {'@value': '2022-01-27T05:37:15+00:00',\n",
21472150
" '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},\n",
2148-
" 'o:media': [{'@id': 'http://timsherratt.org/collections/api/media/732',\n",
2149-
" 'o:id': 732}],\n",
2151+
" 'o:media': [{'@id': 'http://timsherratt.org/collections/api/media/767',\n",
2152+
" 'o:id': 767}],\n",
21502153
" 'o:item_set': [],\n",
21512154
" 'o:site': [],\n",
21522155
" 'schema:name': [{'type': 'literal',\n",
@@ -2164,8 +2167,8 @@
21642167
" 'property_id': 736,\n",
21652168
" 'property_label': 'isPartOf',\n",
21662169
" 'is_public': True,\n",
2167-
" '@id': 'http://timsherratt.org/collections/api/items/730',\n",
2168-
" 'value_resource_id': 730,\n",
2170+
" '@id': 'http://timsherratt.org/collections/api/items/763',\n",
2171+
" 'value_resource_id': 763,\n",
21692172
" 'value_resource_name': 'items',\n",
21702173
" 'url': None,\n",
21712174
" 'display_title': 'The Bendigo Independent (Vic. : 1891 - 1918)'}],\n",
@@ -2193,6 +2196,13 @@
21932196
"\n",
21942197
"* update and delete methods"
21952198
]
2199+
},
2200+
{
2201+
"cell_type": "code",
2202+
"execution_count": null,
2203+
"metadata": {},
2204+
"outputs": [],
2205+
"source": []
21962206
}
21972207
],
21982208
"metadata": {

docs/api.html

+20-18
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ <h4 id="OmekaAPIClient.add_media_to_payload" class="doc_header"><code>OmekaAPICl
11331133
<li>a list of paths to the image/media files (filename is used as title)</li>
11341134
<li>a list of dicts, each containing <code>title</code>, and <code>path</code> values</li>
11351135
</ul>
1136+
<p>The path values can either be strings or pathlib Paths.</p>
11361137
<p>Returns:</p>
11371138
<ul>
11381139
<li>the modified payload dict</li>
@@ -1865,9 +1866,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
18651866

18661867
<div class="output_text output_subarea output_execute_result">
18671868
<pre>{&#39;@context&#39;: &#39;http://timsherratt.org/collections/api-context&#39;,
1868-
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
1869+
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
18691870
&#39;@type&#39;: &#39;o:Item&#39;,
1870-
&#39;o:id&#39;: 730,
1871+
&#39;o:id&#39;: 763,
18711872
&#39;o:is_public&#39;: True,
18721873
&#39;o:owner&#39;: {&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/users/1&#39;,
18731874
&#39;o:id&#39;: 1},
@@ -1877,9 +1878,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
18771878
&#39;o:thumbnail&#39;: None,
18781879
&#39;o:title&#39;: &#39;The Bendigo Independent (Vic. : 1891 - 1918)&#39;,
18791880
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: None, &#39;medium&#39;: None, &#39;square&#39;: None},
1880-
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:46:55+00:00&#39;,
1881+
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:35:36+00:00&#39;,
18811882
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
1882-
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:46:55+00:00&#39;,
1883+
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:35:36+00:00&#39;,
18831884
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
18841885
&#39;o:media&#39;: [],
18851886
&#39;o:item_set&#39;: [],
@@ -2145,8 +2146,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
21452146
&#39;@value&#39;: &#34;MR WRAGGE&#39;S PREDICTION. RENEWAL OF CYCLONE FORETOLD.&#34;}],
21462147
&#39;schema:isPartOf&#39;: [{&#39;property_id&#39;: 736,
21472148
&#39;type&#39;: &#39;resource:item&#39;,
2148-
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
2149-
&#39;value_resource_id&#39;: 730,
2149+
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
2150+
&#39;value_resource_id&#39;: 763,
21502151
&#39;value_resource_name&#39;: &#39;items&#39;}],
21512152
&#39;schema:datePublished&#39;: [{&#39;property_id&#39;: 928,
21522153
&#39;type&#39;: &#39;numeric:timestamp&#39;,
@@ -2178,7 +2179,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
21782179

21792180
<div class="inner_cell">
21802181
<div class="input_area">
2181-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">media_files</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;media/nla.news-article226799674-24144902.jpg&#39;</span><span class="p">]</span>
2182+
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># The paths can be strings or pathlib Paths</span>
2183+
<span class="n">media_files</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;media/nla.news-article226799674-24144902.jpg&#39;</span><span class="p">]</span>
21822184

21832185
<span class="c1"># Include the media files when we upload the payload</span>
21842186
<span class="n">article_item</span> <span class="o">=</span> <span class="n">omeka_auth</span><span class="o">.</span><span class="n">add_item</span><span class="p">(</span><span class="n">article_payload</span><span class="p">,</span> <span class="n">media_files</span><span class="o">=</span><span class="n">media_files</span><span class="p">,</span> <span class="n">template_id</span><span class="o">=</span><span class="n">article_template_id</span><span class="p">)</span>
@@ -2221,9 +2223,9 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
22212223

22222224
<div class="output_text output_subarea output_execute_result">
22232225
<pre>{&#39;@context&#39;: &#39;http://timsherratt.org/collections/api-context&#39;,
2224-
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/731&#39;,
2226+
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/766&#39;,
22252227
&#39;@type&#39;: &#39;o:Item&#39;,
2226-
&#39;o:id&#39;: 731,
2228+
&#39;o:id&#39;: 766,
22272229
&#39;o:is_public&#39;: True,
22282230
&#39;o:owner&#39;: {&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/users/1&#39;,
22292231
&#39;o:id&#39;: 1},
@@ -2232,15 +2234,15 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
22322234
&#39;o:id&#39;: 4},
22332235
&#39;o:thumbnail&#39;: None,
22342236
&#39;o:title&#39;: &#34;MR WRAGGE&#39;S PREDICTION. RENEWAL OF CYCLONE FORETOLD.&#34;,
2235-
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: &#39;http://timsherratt.org/collections/files/large/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;,
2236-
&#39;medium&#39;: &#39;http://timsherratt.org/collections/files/medium/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;,
2237-
&#39;square&#39;: &#39;http://timsherratt.org/collections/files/square/aa9ef3fe881ee92c46bc8f1500d7f9fa9f3d6bb4.jpg&#39;},
2238-
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:47:02+00:00&#39;,
2237+
&#39;thumbnail_display_urls&#39;: {&#39;large&#39;: &#39;http://timsherratt.org/collections/files/large/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;,
2238+
&#39;medium&#39;: &#39;http://timsherratt.org/collections/files/medium/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;,
2239+
&#39;square&#39;: &#39;http://timsherratt.org/collections/files/square/370fd463d1743fc81fa00d71448f294e7aec7643.jpg&#39;},
2240+
&#39;o:created&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:37:15+00:00&#39;,
22392241
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
2240-
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-26T04:47:02+00:00&#39;,
2242+
&#39;o:modified&#39;: {&#39;@value&#39;: &#39;2022-01-27T05:37:15+00:00&#39;,
22412243
&#39;@type&#39;: &#39;http://www.w3.org/2001/XMLSchema#dateTime&#39;},
2242-
&#39;o:media&#39;: [{&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/media/732&#39;,
2243-
&#39;o:id&#39;: 732}],
2244+
&#39;o:media&#39;: [{&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/media/767&#39;,
2245+
&#39;o:id&#39;: 767}],
22442246
&#39;o:item_set&#39;: [],
22452247
&#39;o:site&#39;: [],
22462248
&#39;schema:name&#39;: [{&#39;type&#39;: &#39;literal&#39;,
@@ -2258,8 +2260,8 @@ <h2 id="Example:-adding-a-newspaper-article-from-Trove">Example: adding a newspa
22582260
&#39;property_id&#39;: 736,
22592261
&#39;property_label&#39;: &#39;isPartOf&#39;,
22602262
&#39;is_public&#39;: True,
2261-
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/730&#39;,
2262-
&#39;value_resource_id&#39;: 730,
2263+
&#39;@id&#39;: &#39;http://timsherratt.org/collections/api/items/763&#39;,
2264+
&#39;value_resource_id&#39;: 763,
22632265
&#39;value_resource_name&#39;: &#39;items&#39;,
22642266
&#39;url&#39;: None,
22652267
&#39;display_title&#39;: &#39;The Bendigo Independent (Vic. : 1891 - 1918)&#39;}],

omeka_s_tools/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.3"
1+
__version__ = "0.0.4"

omeka_s_tools/api.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ def add_media_to_payload(self, payload, media_files):
391391
* a list of paths to the image/media files (filename is used as title)
392392
* a list of dicts, each containing `title`, and `path` values
393393
394+
The path values can either be strings or pathlib Paths.
395+
394396
Returns:
395397
* the modified payload dict
396398
'''
@@ -399,11 +401,11 @@ def add_media_to_payload(self, payload, media_files):
399401
for index, media_file in enumerate(media_files):
400402
if isinstance(media_file, dict):
401403
title = media_file['title']
402-
path = media_file['path']
404+
path = Path(media_file['path'])
403405
else:
404-
title = media_file[:-4]
405-
path = media_file
406+
path = Path(media_file)
407+
title = path.name
406408
payload['o:media'].append({'o:ingester': 'upload', 'file_index': str(index), 'o:item': {}, 'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]})
407-
files[f'file[{index}]'] = Path(path).read_bytes()
409+
files[f'file[{index}]'] = path.read_bytes()
408410
files['data'] = (None, json.dumps(payload), 'application/json')
409411
return files

settings.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ author = Tim Sherratt
1313
author_email = [email protected]
1414
copyright = Tim Sherratt
1515
branch = master
16-
version = 0.0.3
16+
version = 0.0.4
1717
min_python = 3.6
1818
audience = Developers
1919
language = English

0 commit comments

Comments
 (0)