Repository: DigitalSlideArchive/tifftools

Issue:
Readme example seems to be incorrect
Running the sample code from the readme file gives:

```plain
$ python3 sample.py
Traceback (most recent call last):
  File "sample2.py", line 8, in <module>
    exififd['tags'][tifftools.constants.EXIFTag.FNumber.value] = {
TypeError: list indices must be integers or slices, not str
```

it seems that we are missing level for exifs (SubIDF?).

The following does works:

```plain
$ diff -u sample.py.orig sample.py
--- sample.py.orig      2022-02-28 18:19:35.000000000 +0100
+++ sample.py   2022-02-28 18:19:52.000000000 +0100
@@ -4,7 +4,7 @@
     'data': 'A dog digging.',
     'datatype': tifftools.Datatype.ASCII
 }
-exififd = info['ifds'][0]['tags'][tifftools.Tag.EXIFIFD.value]['ifds'][0]
+exififd = info['ifds'][0]['tags'][tifftools.Tag.EXIFIFD.value]['ifds'][0][0]
 exififd['tags'][tifftools.constants.EXIFTag.FNumber.value] = {
     'data': [54, 10],
     'datatype': tifftools.Datatype.RATIONAL
$ python3 sample.py
$ tifftools dump photograph_tagged.tif | grep FNumber
      FNumber 33437 (0x829D) RATIONAL: 54 10 (5.4)
```

Side note: the above code will fail if the TIFF file doesn't already contains EXIFs.
Is the following snippet the correct way to add EXIF IFD?
```python
try:
    exif = info["ifds"][0]["tags"][tifftools.Tag.EXIFIFD.value]
except KeyError:
    exif = {
        "datatype": tifftools.Datatype.IFD,
        "ifds": [[{"tags": {}, "path_or_fobj": info["ifds"][0]["path_or_fobj"]}]],
    }
    info["ifds"][0]["tags"][tifftools.Tag.EXIFIFD.value] = exif
exififd = exif["ifds"][0][0]
# add tags herunder
```


Discussion:

Patch:
diff --git a/README.rst b/README.rst
index 8915cb6..b7a67cb 100644
--- a/README.rst
+++ b/README.rst
@@ -17,7 +17,7 @@ Example
         'data': 'A dog digging.',
         'datatype': tifftools.Datatype.ASCII
     }
-    exififd = info['ifds'][0]['tags'][tifftools.Tag.EXIFIFD.value]['ifds'][0]
+    exififd = info['ifds'][0]['tags'][tifftools.Tag.EXIFIFD.value]['ifds'][0][0]
     exififd['tags'][tifftools.constants.EXIFTag.FNumber.value] = {
         'data': [54, 10],
         'datatype': tifftools.Datatype.RATIONAL


Test patch:
diff --git a/tests/test_readme.py b/tests/test_readme.py
new file mode 100644
index 0000000..5f0e59e
--- /dev/null
+++ b/tests/test_readme.py
@@ -0,0 +1,22 @@
+import os
+import shutil
+
+from .datastore import datastore
+
+
+def test_readme_code(tmp_path):
+    imagePath = datastore.fetch('d043-200.tif')
+    readmePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../README.rst')
+    data = open(readmePath).read()
+    chunks = data.split('\n.. code-block:: python\n')[1:]
+    for chunk in chunks:
+        lines = chunk.split('\n')
+        keep = []
+        for line in lines:
+            if line.strip() and not line.startswith('    '):
+                break
+            keep.append(line[4:])
+        code = '\n'.join(keep).strip() + '\n'
+        os.chdir(tmp_path)
+        shutil.copy(imagePath, 'photograph.tif')
+        exec(code)