File: example-using-facade.md

package info (click to toggle)
python-pycdlib 1.12.0%2Bds1-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,748 kB
  • sloc: python: 36,118; makefile: 63
file content (86 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Example: Using a facade
This example will show how to access various aspects of an ISO with a [facade](examples.md#facades).  Here's the complete code for the example:

```
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib

iso = pycdlib.PyCdlib()
iso.new(joliet=3)

joliet = iso.get_joliet_facade()

foostr = b'foo\n'
joliet.add_fp(BytesIO(foostr), len(foostr), joliet_path='/foo')

joliet.add_directory(joliet_path='/dir1')

iso.write('new.iso')

iso.close()
```

Let's take a closer look at the code.

```
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib
```

As in earlier examples, import the relevant libraries, including pycdlib itself.

```
iso = pycdlib.PyCdlib()
iso.new(joliet=3)
```

Create a new PyCdlib object, and then create a new ISO with that object.  As in previous examples, make it a Joliet ISO by passing `joliet=3` to the [new](pycdlib-api.html#PyCdlib-new) method.

```
joliet = iso.get_joliet_facade()
```

Fetch a Joliet facade for the PyCdlib object.  We'll use this for manipulation of the object below.

```
foostr = b'foo\n'
joliet.add_fp(BytesIO(foostr), len(foostr), joliet_path='/foo')
```

As in earlier examples, create a new file on the ISO from a string.  Note that we call the `add_fp` method on the facade, and that we *only* pass the joliet_path to the facade.  This simplifies things considerably for us.  However, this also means that the file will only show up in the Joliet context.  If we want it to show up in other contexts (like Rock Ridge or UDF), we have two options.  We can either use the more complicated form of the `add_fp` method on the original `PyCdlib` object, or we can call `add_hard_link` on the original `PyCdlib` object to make the file appear in other contexts.

```
joliet.add_directory(joliet_path='/dir1')
```

Create a new directory on the ISO.  Again we use the facade to create the directory, so we only have to pass the `joliet_path`.

```
iso.write('new.iso')
iso.close()
```

Write the new ISO out to a file, then close out the ISO.  Note that we use the original `PyCdlib` object, and not the facades for this.  Since there is nothing context-specific about these calls, the original object works just fine.

---

<div style="width: 100%; display: table;">
  <div style="display: table-row;">
    <div style="width: 33%; display: table-cell; text-align: left;">
      <a href="example-creating-udf-iso.html"><-- Example: Creating an ISO with UDF</a>
    </div>
    <div style="width: 33%; display: table-cell; text-align: center;">
      <a href="https://clalancette.github.io/pycdlib/">Top</a>
    </div>
    <div style="width: 33%; display: table-cell; text-align: right;">
      <a href="example-modifying-file-in-place.html">Example: Modifying a file in place --></a>
    </div>
</div>