sources for path.html [rev. unknown]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>[path] </title>
    <meta content="text/html;charset=ISO-8859-1" name="Content-Type"/>
    <link href="style.css" media="screen" rel="stylesheet" type="text/css"/></head>
  <body>
    <div><a href="http://codespeak.net"><img alt="py lib" height="114" id="pyimg" src="http://codespeak.net/img/pylib.png" width="154"/></a></div>
    <div id="metaspace">
      <div class="project_title">[path] </div>
      <div id="menubar"><a class="menu" href="index.html">index</a> <a class="menu" href="../../apigen/api/index.html">api</a> <a class="menu" href="../../apigen/source/index.html">source</a> <a class="menu" href="contact.html">contact</a> <a class="menu" href="download.html">download</a></div></div>
    <div id="contentspace">
      <div id="docinfoline">
        <div style="float: right; font-style: italic;"> </div></div>
<div class="document" id="py-path">
<h1 class="title">py.path</h1>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#path-implementations-provided-by-py-path" id="id1" name="id1">1&nbsp;&nbsp;&nbsp;Path implementations provided by py.path</a><ul class="auto-toc">
<li><a class="reference" href="#py-path-local" id="id2" name="id2">1.1&nbsp;&nbsp;&nbsp;py.path.local</a></li>
<li><a class="reference" href="#py-path-svnurl-and-py-path-svnwc" id="id3" name="id3">1.2&nbsp;&nbsp;&nbsp;py.path.svnurl and py.path.svnwc</a></li>
</ul>
</li>
<li><a class="reference" href="#common-vs-specific-api" id="id4" name="id4">2&nbsp;&nbsp;&nbsp;Common vs. specific API</a><ul class="auto-toc">
<li><a class="reference" href="#examples" id="id5" name="id5">2.1&nbsp;&nbsp;&nbsp;Examples</a><ul class="auto-toc">
<li><a class="reference" href="#searching-txt-files" id="id6" name="id6">2.1.1&nbsp;&nbsp;&nbsp;Searching <span class="incremental">.txt</span> files</a></li>
<li><a class="reference" href="#working-with-paths" id="id7" name="id7">2.1.2&nbsp;&nbsp;&nbsp;Working with Paths</a></li>
<li><a class="reference" href="#checking-path-types" id="id8" name="id8">2.1.3&nbsp;&nbsp;&nbsp;Checking path types</a></li>
<li><a class="reference" href="#setting-svn-properties" id="id9" name="id9">2.1.4&nbsp;&nbsp;&nbsp;Setting svn-properties</a></li>
<li><a class="reference" href="#svn-authentication" id="id10" name="id10">2.1.5&nbsp;&nbsp;&nbsp;SVN authentication</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference" href="#known-problems-limitations" id="id11" name="id11">3&nbsp;&nbsp;&nbsp;Known problems / limitations</a></li>
<li><a class="reference" href="#future-plans" id="id12" name="id12">4&nbsp;&nbsp;&nbsp;Future plans</a></li>
</ul>
</div>
<p>The 'py' lib provides a uniform high-level api to deal with filesystems
and filesystem-like interfaces: <a class="reference" href="../../apigen/api/path.html">py.path</a>.  It aims to offer a central
object to fs-like object trees (reading from and writing to files, adding
files/directories, examining the types and structure, etc.), and out-of-the-box
provides a number of implementations of this API.</p>
<div class="section">
<h1><a id="path-implementations-provided-by-py-path" name="path-implementations-provided-by-py-path">1&nbsp;&nbsp;&nbsp;Path implementations provided by <a class="reference" href="../../apigen/api/path.html">py.path</a></a></h1>
<div class="section">
<h2><a id="py-path-local" name="py-path-local">1.1&nbsp;&nbsp;&nbsp;<a class="reference" href="../../apigen/api/path.local.html">py.path.local</a></a></h2>
<p>The first and most obvious of the implementations is a wrapper around a local
filesystem. It's just a bit nicer in usage than the regular Python APIs, and
of course all the functionality is bundled together rather than spread over a
number of modules.</p>
<p>Example usage, here we use the <a class="reference" href="../../apigen/api/test.ensuretemp.html">py.test.ensuretemp()</a> function to create
a <a class="reference" href="../../apigen/api/path.local.html">py.path.local</a> object for us (which wraps a directory):</p>
<pre class="literal-block">
&gt;&gt;&gt; import py
&gt;&gt;&gt; temppath = py.test.ensuretemp('py.path_documentation')
&gt;&gt;&gt; foopath = temppath.join('foo') # get child 'foo' (lazily)
&gt;&gt;&gt; foopath.check() # check if child 'foo' exists
False
&gt;&gt;&gt; foopath.write('bar') # write some data to it
&gt;&gt;&gt; foopath.check()
True
&gt;&gt;&gt; foopath.read()
'bar'
&gt;&gt;&gt; foofile = foopath.open() # return a 'real' file object
&gt;&gt;&gt; foofile.read(1)
'b'
</pre>
</div>
<div class="section">
<h2><a id="py-path-svnurl-and-py-path-svnwc" name="py-path-svnurl-and-py-path-svnwc">1.2&nbsp;&nbsp;&nbsp;<a class="reference" href="../../apigen/api/path.svnurl.html">py.path.svnurl</a> and <a class="reference" href="../../apigen/api/path.svnwc.html">py.path.svnwc</a></a></h2>
<p>Two other <a class="reference" href="../../apigen/api/path.html">py.path</a> implementations that the py lib provides wrap the
popular <a class="reference" href="http://subversion.tigris.org/">Subversion</a> revision control system: the first (called 'svnurl')
by interfacing with a remote server, the second by wrapping a local checkout.
Both allow you to access relatively advanced features such as metadata and
versioning, and both in a way more user-friendly manner than existing other
solutions.</p>
<p>Some example usage of <a class="reference" href="../../apigen/api/path.svnurl.html">py.path.svnurl</a>:</p>
<pre class="literal-block">
.. &gt;&gt;&gt; import py
.. &gt;&gt;&gt; if not getattr(py.test.config.option, 'checkremote', 0): py.test.skip(&quot;use --checkremote to enable svn remote doctests&quot;)
&gt;&gt;&gt; url = py.path.svnurl('http://codespeak.net/svn/py')
&gt;&gt;&gt; info = url.info()
&gt;&gt;&gt; info.kind
'dir'
&gt;&gt;&gt; firstentry = url.log()[-1]
&gt;&gt;&gt; import time
&gt;&gt;&gt; time.strftime('%Y-%m-%d', time.gmtime(firstentry.date))
'2004-10-02'
</pre>
<p>Example usage of <a class="reference" href="../../apigen/api/path.svnwc.html">py.path.svnwc</a>:</p>
<pre class="literal-block">
&gt;&gt;&gt; temp = py.test.ensuretemp('py.path_documentation')
&gt;&gt;&gt; wc = py.path.svnwc(temp.join('svnwc'))
&gt;&gt;&gt; wc.checkout('http://codespeak.net/svn/py/dist/py/path/local')
&gt;&gt;&gt; wc.join('local.py').check()
True
</pre>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id4" id="common-vs-specific-api" name="common-vs-specific-api">2&nbsp;&nbsp;&nbsp;Common vs. specific API</a></h1>
<p>All Path objects support a common set of operations, suitable
for many use cases and allowing to transparently switch the
path object within an application (e.g. from &quot;local&quot; to &quot;svnwc&quot;).
The common set includes functions such as <span class="incremental">path.read()</span> to read all data
from a file, <span class="incremental">path.write()</span> to write data, <span class="incremental">path.listdir()</span> to get a list
of directory entries, <span class="incremental">path.check()</span> to check if a node exists
and is of a particular type, <span class="incremental">path.join()</span> to get
to a (grand)child, <span class="incremental">path.visit()</span> to recursively walk through a node's
children, etc.  Only things that are not common on 'normal' filesystems (yet),
such as handling metadata (e.g. the Subversion &quot;properties&quot;) require
using specific APIs.</p>
<div class="section">
<h2><a class="toc-backref" href="#id5" id="examples" name="examples">2.1&nbsp;&nbsp;&nbsp;Examples</a></h2>
<p>A quick 'cookbook' of small examples that will be useful 'in real life',
which also presents parts of the 'common' API, and shows some non-common
methods:</p>
<div class="section">
<h3><a class="toc-backref" href="#id6" id="searching-txt-files" name="searching-txt-files">2.1.1&nbsp;&nbsp;&nbsp;Searching <span class="incremental">.txt</span> files</a></h3>
<p>Search for a particular string inside all files with a .txt extension in a
specific directory.</p>
<pre class="literal-block">
&gt;&gt;&gt; dirpath = temppath.ensure('testdir', dir=True)
&gt;&gt;&gt; dirpath.join('textfile1.txt').write('foo bar baz')
&gt;&gt;&gt; dirpath.join('textfile2.txt').write('frob bar spam eggs')
&gt;&gt;&gt; subdir = dirpath.ensure('subdir', dir=True)
&gt;&gt;&gt; subdir.join('textfile1.txt').write('foo baz')
&gt;&gt;&gt; subdir.join('textfile2.txt').write('spam eggs spam foo bar spam')
&gt;&gt;&gt; results = []
&gt;&gt;&gt; for fpath in dirpath.visit('*.txt'):
...     if 'bar' in fpath.read():
...         results.append(fpath.basename)
&gt;&gt;&gt; results
['textfile1.txt', 'textfile2.txt', 'textfile2.txt']
</pre>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id7" id="working-with-paths" name="working-with-paths">2.1.2&nbsp;&nbsp;&nbsp;Working with Paths</a></h3>
<p>This example shows the <a class="reference" href="../../apigen/api/path.html">py.path</a> features to deal with
filesystem paths Note that the filesystem is never touched,
all operations are performed on a string level (so the paths
don't have to exist, either):</p>
<pre class="literal-block">
&gt;&gt;&gt; p1 = py.path.local('/foo/bar')
&gt;&gt;&gt; p2 = p1.join('baz/qux')
&gt;&gt;&gt; p2 == py.path.local('/foo/bar/baz/qux')
True
&gt;&gt;&gt; sep = py.path.local.sep
&gt;&gt;&gt; p2.relto(p1).replace(sep, '/') # os-specific path sep in the string
'baz/qux'
&gt;&gt;&gt; p3 = p1 / 'baz/qux' # the / operator allows joining, too
&gt;&gt;&gt; p2 == p3
True
&gt;&gt;&gt; p4 = p1 + &quot;.py&quot;
&gt;&gt;&gt; p4.basename == &quot;bar.py&quot;
True
&gt;&gt;&gt; p4.ext == &quot;.py&quot;
True
&gt;&gt;&gt; p4.purebasename == &quot;bar&quot;
True
</pre>
<p>This should be possible on every implementation of <a class="reference" href="../../apigen/api/path.html">py.path</a>, so
regardless of whether the implementation wraps a UNIX filesystem, a Windows
one, or a database or object tree, these functions should be available (each
with their own notion of path seperators and dealing with conversions, etc.).</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id8" id="checking-path-types" name="checking-path-types">2.1.3&nbsp;&nbsp;&nbsp;Checking path types</a></h3>
<p>Now we will show a bit about the powerful 'check()' method on paths, which
allows you to check whether a file exists, what type it is, etc.:</p>
<pre class="literal-block">
&gt;&gt;&gt; file1 = temppath.join('file1')
&gt;&gt;&gt; file1.check() # does it exist?
False
&gt;&gt;&gt; file1 = file1.ensure(file=True) # 'touch' the file
&gt;&gt;&gt; file1.check()
True
&gt;&gt;&gt; file1.check(dir=True) # is it a dir?
False
&gt;&gt;&gt; file1.check(file=True) # or a file?
True
&gt;&gt;&gt; file1.check(ext='.txt') # check the extension
False
&gt;&gt;&gt; textfile = temppath.ensure('text.txt', file=True)
&gt;&gt;&gt; textfile.check(ext='.txt')
True
&gt;&gt;&gt; file1.check(basename='file1') # we can use all the path's properties here
True
</pre>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id9" id="setting-svn-properties" name="setting-svn-properties">2.1.4&nbsp;&nbsp;&nbsp;Setting svn-properties</a></h3>
<p>As an example of 'uncommon' methods, we'll show how to read and write
properties in an <a class="reference" href="../../apigen/api/path.svnwc.html">py.path.svnwc</a> instance:</p>
<pre class="literal-block">
&gt;&gt;&gt; wc.propget('foo')
''
&gt;&gt;&gt; wc.propset('foo', 'bar')
&gt;&gt;&gt; wc.propget('foo')
'bar'
&gt;&gt;&gt; len(wc.status().prop_modified) # our own props
1
&gt;&gt;&gt; msg = wc.revert() # roll back our changes
&gt;&gt;&gt; len(wc.status().prop_modified)
0
</pre>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id10" id="svn-authentication" name="svn-authentication">2.1.5&nbsp;&nbsp;&nbsp;SVN authentication</a></h3>
<p>Some uncommon functionality can also be provided as extensions, such as SVN
authentication:</p>
<pre class="literal-block">
&gt;&gt;&gt; auth = py.path.SvnAuth('anonymous', 'user', cache_auth=False,
...             interactive=False)
&gt;&gt;&gt; wc.auth = auth
&gt;&gt;&gt; wc.update() # this should work
&gt;&gt;&gt; path = wc.ensure('thisshouldnotexist.txt')
&gt;&gt;&gt; try:
...     path.commit('testing')
... except py.process.cmdexec.Error, e:
...     pass
&gt;&gt;&gt; 'authorization failed' in str(e)
True
</pre>
</div>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id11" id="known-problems-limitations" name="known-problems-limitations">3&nbsp;&nbsp;&nbsp;Known problems / limitations</a></h1>
<ul class="simple">
<li>The SVN path objects require the &quot;svn&quot; command line,
there is currently no support for python bindings.
Parsing the svn output can lead to problems, particularly
regarding if you have a non-english &quot;locales&quot; setting.</li>
<li>While the path objects basically work on windows,
there is no attention yet on making unicode paths
work or deal with the famous &quot;8.3&quot; filename issues.</li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id12" id="future-plans" name="future-plans">4&nbsp;&nbsp;&nbsp;Future plans</a></h1>
<p>The Subversion path implementations are based
on the <span class="incremental">svn</span> command line, not on the bindings.
It makes sense now to directly use the bindings.</p>
<p>Moreover, it would be good, also considering
<a class="reference" href="execnet.html">py.execnet</a> distribution of programs, to
be able to manipulate Windows Paths on Linux
and vice versa.  So we'd like to consider
refactoring the path implementations
to provide this choice (and getting rid
of platform-dependencies as much as possible).</p>
<p>There is some experimental small approach
(<a class="reference" href="../../apigen/source/path/gateway/index.html">py/path/gateway/</a>) aiming at having
a convenient Remote Path implementation
and some considerations about future
works in the according <a class="reference" href="../../apigen/source/path/gateway/TODO.txt.html">py/path/gateway/TODO.txt</a></p>
<p>There are various hacks out there to have
Memory-Filesystems and even path objects
being directly mountable under Linux (via <span class="incremental">fuse</span>).
However, the Path object implementations
do not internally have a clean abstraction
of going to the filesystem - so with some
refactoring it should become easier to
have very custom Path objects, still offering
the quite full interface without requiring
to know about all details of the full path
implementation.</p>
</div>
</div>
</div></body></html>