17.01.2015 Views

py Documentation - Read the Docs

py Documentation - Read the Docs

py Documentation - Read the Docs

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>py</strong> <strong>Documentation</strong>, Release 1.4.27.dev1<br />

ignore is an Exception class that is ignoredwhen calling dirlist() on any of <strong>the</strong> paths (by default, all exceptions<br />

are reported)<br />

bf if True will cause a breadthfirst search instead of <strong>the</strong> default depthfirst. Default: False<br />

sort if True will sort entries within each directory level.<br />

class <strong>py</strong>._path.svnwc.SvnAuth(username, password, cache_auth=True, interactive=True)<br />

container for auth information for Subversion<br />

2.3 Common vs. specific API, Examples<br />

All Path objects support a common set of operations, suitable for many use cases and allowing to transparently switch<br />

<strong>the</strong> path object within an application (e.g. from “local” to “svnwc”). The common set includes functions such as<br />

path.read() to read all data from a file, path.write() to write data, path.listdir() to get a list of directory entries,<br />

path.check() to check if a node exists and is of a particular type, path.join() to get to a (grand)child, path.visit() to<br />

recursively walk through a node’s children, etc. Only things that are not common on ‘normal’ filesystems (yet), such<br />

as handling metadata (e.g. <strong>the</strong> Subversion “properties”) require using specific APIs.<br />

A quick ‘cookbook’ of small examples that will be useful ‘in real life’, which also presents parts of <strong>the</strong> ‘common’<br />

API, and shows some non-common methods:<br />

2.3.1 Searching .txt files<br />

Search for a particular string inside all files with a .txt extension in a specific directory.<br />

>>> dirpath = temppath.ensure(’testdir’, dir=True)<br />

>>> dirpath.join(’textfile1.txt’).write(’foo bar baz’)<br />

>>> dirpath.join(’textfile2.txt’).write(’frob bar spam eggs’)<br />

>>> subdir = dirpath.ensure(’subdir’, dir=True)<br />

>>> subdir.join(’textfile1.txt’).write(’foo baz’)<br />

>>> subdir.join(’textfile2.txt’).write(’spam eggs spam foo bar spam’)<br />

>>> results = []<br />

>>> for fpath in dirpath.visit(’*.txt’):<br />

... if ’bar’ in fpath.read():<br />

... results.append(fpath.basename)<br />

>>> results.sort()<br />

>>> results<br />

[’textfile1.txt’, ’textfile2.txt’, ’textfile2.txt’]<br />

2.3.2 Working with Paths<br />

This example shows <strong>the</strong> <strong>py</strong>.path features to deal with filesystem paths Note that <strong>the</strong> filesystem is never touched, all<br />

operations are performed on a string level (so <strong>the</strong> paths don’t have to exist, ei<strong>the</strong>r):<br />

>>> p1 = <strong>py</strong>.path.local(’/foo/bar’)<br />

>>> p2 = p1.join(’baz/qux’)<br />

>>> p2 == <strong>py</strong>.path.local(’/foo/bar/baz/qux’)<br />

True<br />

>>> sep = <strong>py</strong>.path.local.sep<br />

>>> p2.relto(p1).replace(sep, ’/’) # os-specific path sep in <strong>the</strong> string<br />

’baz/qux’<br />

>>> p2.bestrelpath(p1).replace(sep, ’/’)<br />

’../..’<br />

>>> p2.join(p2.bestrelpath(p1)) == p1<br />

16 Chapter 2. <strong>py</strong>.path

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!