From 8603e0485333af6704e0cd0770f565fc2d716569 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 5 Dec 2009 16:29:33 -0500 Subject: Last touches for 3.2 final. --- doc/sample_html/_ned_py_path.html | 1711 +++++++++++++++++++------------------ 1 file changed, 858 insertions(+), 853 deletions(-) (limited to 'doc/sample_html/_ned_py_path.html') diff --git a/doc/sample_html/_ned_py_path.html b/doc/sample_html/_ned_py_path.html index 78637ee..0a60bbe 100644 --- a/doc/sample_html/_ned_py_path.html +++ b/doc/sample_html/_ned_py_path.html @@ -1,19 +1,21 @@ - + + Coverage for c:\ned\py\path - - + @@ -22,13 +24,16 @@ function toggle_lines(btn, cls) { @@ -68,34 +73,34 @@ function toggle_lines(btn, cls) {

29

30

31

-

32

+

32

33

-

34

+

34

35

-

36

-

37

+

36

+

37

38

39

-

40

-

41

-

42

-

43

+

40

+

41

+

42

+

43

44

45

46

47

-

48

-

49

+

48

+

49

50

51

52

53

-

54

-

55

-

56

+

54

+

55

+

56

57

58

-

59

+

59

60

61

62

@@ -104,18 +109,18 @@ function toggle_lines(btn, cls) {

65

66

67

-

68

+

68

69

70

71

-

72

+

72

73

74

-

75

+

75

76

77

78

-

79

+

79

80

81

82

@@ -124,26 +129,26 @@ function toggle_lines(btn, cls) {

85

86

87

-

88

+

88

89

-

90

+

90

91

92

-

93

+

93

94

95

96

97

-

98

-

99

-

100

-

101

-

102

-

103

-

104

-

105

+

98

+

99

+

100

+

101

+

102

+

103

+

104

+

105

106

-

107

+

107

108

109

110

@@ -152,33 +157,33 @@ function toggle_lines(btn, cls) {

113

114

115

-

116

+

116

117

118

119

-

120

+

120

121

122

123

-

124

+

124

125

126

127

-

128

+

128

129

130

131

132

133

134

-

135

+

135

136

137

138

139

140

141

-

142

+

142

143

144

145

@@ -186,22 +191,22 @@ function toggle_lines(btn, cls) {

147

148

149

-

150

+

150

151

152

153

-

154

+

154

155

156

157

158

159

-

160

+

160

161

162

163

164

-

165

+

165

166

167

168

@@ -211,7 +216,7 @@ function toggle_lines(btn, cls) {

172

173

174

-

175

+

175

176

177

178

@@ -228,7 +233,7 @@ function toggle_lines(btn, cls) {

189

190

191

-

192

+

192

193

194

195

@@ -236,28 +241,28 @@ function toggle_lines(btn, cls) {

197

198

199

-

200

-

201

+

200

+

201

202

203

204

-

205

+

205

206

207

208

-

209

+

209

210

211

212

213

-

214

+

214

215

216

217

218

219

220

-

221

+

221

222

223

224

@@ -279,14 +284,14 @@ function toggle_lines(btn, cls) {

240

241

242

-

243

+

243

244

245

246

247

248

249

-

250

+

250

251

252

253

@@ -326,7 +331,7 @@ function toggle_lines(btn, cls) {

287

288

289

-

290

+

290

291

292

293

@@ -342,7 +347,7 @@ function toggle_lines(btn, cls) {

303

304

305

-

306

+

306

307

308

309

@@ -355,7 +360,7 @@ function toggle_lines(btn, cls) {

316

317

318

-

319

+

319

320

321

322

@@ -368,7 +373,7 @@ function toggle_lines(btn, cls) {

329

330

331

-

332

+

332

333

334

335

@@ -385,7 +390,7 @@ function toggle_lines(btn, cls) {

346

347

348

-

349

+

349

350

351

352

@@ -399,7 +404,7 @@ function toggle_lines(btn, cls) {

360

361

362

-

363

+

363

364

365

366

@@ -415,7 +420,7 @@ function toggle_lines(btn, cls) {

376

377

378

-

379

+

379

380

381

382

@@ -423,7 +428,7 @@ function toggle_lines(btn, cls) {

384

385

386

-

387

+

387

388

389

390

@@ -436,11 +441,11 @@ function toggle_lines(btn, cls) {

397

398

399

-

400

+

400

401

402

403

-

404

+

404

405

406

407

@@ -448,7 +453,7 @@ function toggle_lines(btn, cls) {

409

410

411

-

412

+

412

413

414

415

@@ -464,7 +469,7 @@ function toggle_lines(btn, cls) {

425

426

427

-

428

+

428

429

430

431

@@ -497,7 +502,7 @@ function toggle_lines(btn, cls) {

458

459

460

-

461

+

461

462

463

464

@@ -548,7 +553,7 @@ function toggle_lines(btn, cls) {

509

510

511

-

512

+

512

513

514

515

@@ -574,7 +579,7 @@ function toggle_lines(btn, cls) {

535

536

537

-

538

+

538

539

540

541

@@ -602,101 +607,101 @@ function toggle_lines(btn, cls) {

563

564

565

-

566

-

567

-

568

-

569

-

570

-

571

+

566

+

567

+

568

+

569

+

570

+

571

572

-

573

+

573

574

575

-

576

-

577

+

576

+

577

578

579

580

-

581

-

582

+

581

+

582

583

584

585

-

586

-

587

-

588

+

586

+

587

+

588

589

590

591

-

592

-

593

+

592

+

593

594

595

596

-

597

-

598

+

597

+

598

599

600

601

602

603

604

-

605

+

605

606

607

608

-

609

+

609

610

611

612

-

613

+

613

614

615

616

617

-

618

+

618

619

620

621

622

623

624

-

625

+

625

626

627

628

-

629

+

629

630

631

-

632

+

632

633

634

635

-

636

+

636

637

638

-

639

+

639

640

641

642

643

644

-

645

+

645

646

647

-

648

+

648

649

650

-

651

+

651

652

653

-

654

+

654

655

656

657

658

659

-

660

+

660

661

662

663

@@ -704,26 +709,26 @@ function toggle_lines(btn, cls) {

665

666

667

-

668

+

668

669

670

-

671

+

671

672

673

674

675

676

-

677

+

677

678

679

680

681

-

682

+

682

683

684

685

686

-

687

+

687

688

689

690

@@ -745,759 +750,759 @@ function toggle_lines(btn, cls) {

706

707

708

-

709

-

710

-

711

-

712

-

713

-

714

-

715

-

716

-

717

+

709

+

710

+

711

+

712

+

713

+

714

+

715

+

716

+

717

718

719

720

721

-

722

+

722

723

724

725

-

726

-

727

+

726

+

727

728

729

-

""" path.py - An object representing a path to a file or directory. 

-

 

-

Example: 

-

 

-

from path import path 

-

d = path('/home/guido/bin') 

-

for f in d.files('*.py'): 

-

    f.chmod(0755) 

-

 

-

This module requires Python 2.2 or later. 

-

 

-

 

-

URL:     http://www.jorendorff.com/articles/python/path 

-

Author:  Jason Orendorff <jason@jorendorff.com> (and others - see the url!) 

-

Date:    29 Feb 2004 

-

""" 

-

 

-

 

-

# TODO 

-

#   - Bug in write_text().  It doesn't support Universal newline mode. 

-

#   - Better error message in listdir() when self isn't a 

-

#     directory. (On Windows, the error message really sucks.) 

-

#   - Make sure everything has a good docstring. 

-

#   - Add methods for regex find and replace. 

-

#   - guess_content_type() method? 

-

#   - Perhaps support arguments to touch(). 

-

#   - Could add split() and join() methods that generate warnings. 

-

#   - Note:  __add__() technically has a bug, I think, where 

-

#     it doesn't play nice with other types that implement 

-

#     __radd__().  Test this. 

-

 

-

from __future__ import generators 

-

 

-

import sys, os, fnmatch, glob, shutil, codecs 

-

 

-

__version__ = '2.0.2' 

-

__all__ = ['path'] 

-

 

-

# Pre-2.3 support.  Are unicode filenames supported? 

-

_base = str 

-

try: 

-

    if os.path.supports_unicode_filenames: 

-

        _base = unicode 

-

except AttributeError: 

-

    pass 

-

 

-

# Pre-2.3 workaround for basestring. 

-

try: 

-

    basestring 

-

except NameError: 

-

    basestring = (str, unicode) 

-

 

-

# Universal newline support 

-

_textmode = 'r' 

-

if hasattr(file, 'newlines'): 

-

    _textmode = 'U' 

-

 

-

 

-

class path(_base): 

-

    """ Represents a filesystem path. 

-

 

-

    For documentation on individual methods, consult their 

-

    counterparts in os.path. 

-

    """ 

-

 

-

    # --- Special Python methods. 

-

 

-

    def __repr__(self): 

-

        return 'path(%s)' % _base.__repr__(self) 

-

 

-

    # Adding a path and a string yields a path. 

-

    def __add__(self, more): 

-

        return path(_base(self) + more) 

-

 

-

    def __radd__(self, other): 

-

        return path(other + _base(self)) 

-

 

-

    # The / operator joins paths. 

-

    def __div__(self, rel): 

-

        """ fp.__div__(rel) == fp / rel == fp.joinpath(rel) 

-

 

-

        Join two path components, adding a separator character if 

-

        needed. 

-

        """ 

-

        return path(os.path.join(self, rel)) 

-

 

-

    # Make the / operator work even when true division is enabled. 

-

    __truediv__ = __div__ 

-

 

-

    def getcwd(): 

-

        """ Return the current working directory as a path object. """ 

-

        return path(os.getcwd()) 

-

    getcwd = staticmethod(getcwd) 

-

 

-

 

-

    # --- Operations on path strings. 

-

 

-

    def abspath(self):       return path(os.path.abspath(self)) 

-

    def normcase(self):      return path(os.path.normcase(self)) 

-

    def normpath(self):      return path(os.path.normpath(self)) 

-

    def realpath(self):      return path(os.path.realpath(self)) 

-

    def expanduser(self):    return path(os.path.expanduser(self)) 

-

    def expandvars(self):    return path(os.path.expandvars(self)) 

-

    def dirname(self):       return path(os.path.dirname(self)) 

-

    basename = os.path.basename 

-

 

-

    def expand(self): 

-

        """ Clean up a filename by calling expandvars(), 

-

        expanduser(), and normpath() on it. 

-

 

-

        This is commonly everything needed to clean up a filename 

-

        read from a configuration file, for example. 

-

        """ 

-

        return self.expandvars().expanduser().normpath() 

-

 

-

    def _get_namebase(self): 

-

        base, ext = os.path.splitext(self.name) 

-

        return base 

-

 

-

    def _get_ext(self): 

-

        f, ext = os.path.splitext(_base(self)) 

-

        return ext 

-

 

-

    def _get_drive(self): 

-

        drive, r = os.path.splitdrive(self) 

-

        return path(drive) 

-

 

-

    parent = property( 

-

        dirname, None, None, 

-

        """ This path's parent directory, as a new path object. 

-

 

-

        For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib') 

-

        """) 

-

 

-

    name = property( 

-

        basename, None, None, 

-

        """ The name of this file or directory without the full path. 

-

 

-

        For example, path('/usr/local/lib/libpython.so').name == 'libpython.so' 

-

        """) 

-

 

-

    namebase = property( 

-

        _get_namebase, None, None, 

-

        """ The same as path.name, but with one file extension stripped off. 

-

 

-

        For example, path('/home/guido/python.tar.gz').name     == 'python.tar.gz', 

-

        but          path('/home/guido/python.tar.gz').namebase == 'python.tar' 

-

        """) 

-

 

-

    ext = property( 

-

        _get_ext, None, None, 

-

        """ The file extension, for example '.py'. """) 

-

 

-

    drive = property( 

-

        _get_drive, None, None, 

-

        """ The drive specifier, for example 'C:'. 

-

        This is always empty on systems that don't use drive specifiers. 

-

        """) 

-

 

-

    def splitpath(self): 

-

        """ p.splitpath() -> Return (p.parent, p.name). """ 

-

        parent, child = os.path.split(self) 

-

        return path(parent), child 

-

 

-

    def splitdrive(self): 

-

        """ p.splitdrive() -> Return (p.drive, <the rest of p>). 

-

 

-

        Split the drive specifier from this path.  If there is 

-

        no drive specifier, p.drive is empty, so the return value 

-

        is simply (path(''), p).  This is always the case on Unix. 

-

        """ 

-

        drive, rel = os.path.splitdrive(self) 

-

        return path(drive), rel 

-

 

-

    def splitext(self): 

-

        """ p.splitext() -> Return (p.stripext(), p.ext). 

-

 

-

        Split the filename extension from this path and return 

-

        the two parts.  Either part may be empty. 

-

 

-

        The extension is everything from '.' to the end of the 

-

        last path segment.  This has the property that if 

-

        (a, b) == p.splitext(), then a + b == p. 

-

        """ 

-

        # Cast to plain string using _base because Python 2.2 

-

        # implementations of os.path.splitext use "for c in path:..." 

-

        # which means something different when applied to a path 

-

        # object. 

-

        filename, ext = os.path.splitext(_base(self)) 

-

        return path(filename), ext 

-

 

-

    def stripext(self): 

-

        """ p.stripext() -> Remove one file extension from the path. 

-

 

-

        For example, path('/home/guido/python.tar.gz').stripext() 

-

        returns path('/home/guido/python.tar'). 

-

        """ 

-

        return self.splitext()[0] 

-

 

-

    if hasattr(os.path, 'splitunc'): 

-

        def splitunc(self): 

-

            unc, rest = os.path.splitunc(self) 

-

            return path(unc), rest 

-

 

-

        def _get_uncshare(self): 

-

            unc, r = os.path.splitunc(self) 

-

            return path(unc) 

-

 

-

        uncshare = property( 

-

            _get_uncshare, None, None, 

-

            """ The UNC mount point for this path. 

-

            This is empty for paths on local drives. """) 

-

 

-

    def joinpath(self, *args): 

-

        """ Join two or more path components, adding a separator 

-

        character (os.sep) if needed.  Returns a new path 

-

        object. 

-

        """ 

-

        return path(os.path.join(self, *args)) 

-

 

-

    def splitall(self): 

-

        """ Return a list of the path components in this path. 

-

 

-

        The first item in the list will be a path.  Its value will be 

-

        either os.curdir, os.pardir, empty, or the root directory of 

-

        this path (for example, '/' or 'C:\\').  The other items in 

-

        the list will be strings. 

-

 

-

        path.path.joinpath(*result) will yield the original path. 

-

        """ 

-

        parts = [] 

-

        loc = self 

-

        while loc != os.curdir and loc != os.pardir: 

-

            prev = loc 

-

            loc, child = prev.splitpath() 

-

            if loc == prev: 

-

                break 

-

            parts.append(child) 

-

        parts.append(loc) 

-

        parts.reverse() 

-

        return parts 

-

 

-

    def relpath(self): 

-

        """ Return this path as a relative path, 

-

        based from the current working directory. 

-

        """ 

-

        cwd = path(os.getcwd()) 

-

        return cwd.relpathto(self) 

-

 

-

    def relpathto(self, dest): 

-

        """ Return a relative path from self to dest. 

-

 

-

        If there is no relative path from self to dest, for example if 

-

        they reside on different drives in Windows, then this returns 

-

        dest.abspath(). 

-

        """ 

-

        origin = self.abspath() 

-

        dest = path(dest).abspath() 

-

 

-

        orig_list = origin.normcase().splitall() 

-

        # Don't normcase dest!  We want to preserve the case. 

-

        dest_list = dest.splitall() 

-

 

-

        if orig_list[0] != os.path.normcase(dest_list[0]): 

-

            # Can't get here from there. 

-

            return dest 

-

 

-

        # Find the location where the two paths start to differ. 

-

        i = 0 

-

        for start_seg, dest_seg in zip(orig_list, dest_list): 

-

            if start_seg != os.path.normcase(dest_seg): 

-

                break 

-

            i += 1 

-

 

-

        # Now i is the point where the two paths diverge. 

-

        # Need a certain number of "os.pardir"s to work up 

-

        # from the origin to the point of divergence. 

-

        segments = [os.pardir] * (len(orig_list) - i) 

-

        # Need to add the diverging part of dest_list. 

-

        segments += dest_list[i:] 

-

        if len(segments) == 0: 

-

            # If they happen to be identical, use os.curdir. 

-

            return path(os.curdir) 

-

        else: 

-

            return path(os.path.join(*segments)) 

-

 

-

 

-

    # --- Listing, searching, walking, and matching 

-

 

-

    def listdir(self, pattern=None): 

-

        """ D.listdir() -> List of items in this directory. 

-

 

-

        Use D.files() or D.dirs() instead if you want a listing 

-

        of just files or just subdirectories. 

-

 

-

        The elements of the list are path objects. 

-

 

-

        With the optional 'pattern' argument, this only lists 

-

        items whose names match the given pattern. 

-

        """ 

-

        names = os.listdir(self) 

-

        if pattern is not None: 

-

            names = fnmatch.filter(names, pattern) 

-

        return [self / child for child in names] 

-

 

-

    def dirs(self, pattern=None): 

-

        """ D.dirs() -> List of this directory's subdirectories. 

-

 

-

        The elements of the list are path objects. 

-

        This does not walk recursively into subdirectories 

-

        (but see path.walkdirs). 

-

 

-

        With the optional 'pattern' argument, this only lists 

-

        directories whose names match the given pattern.  For 

-

        example, d.dirs('build-*'). 

-

        """ 

-

        return [p for p in self.listdir(pattern) if p.isdir()] 

-

 

-

    def files(self, pattern=None): 

-

        """ D.files() -> List of the files in this directory. 

-

 

-

        The elements of the list are path objects. 

-

        This does not walk into subdirectories (see path.walkfiles). 

-

 

-

        With the optional 'pattern' argument, this only lists files 

-

        whose names match the given pattern.  For example, 

-

        d.files('*.pyc'). 

-

        """ 

-

 

-

        return [p for p in self.listdir(pattern) if p.isfile()] 

-

 

-

    def walk(self, pattern=None): 

-

        """ D.walk() -> iterator over files and subdirs, recursively. 

-

 

-

        The iterator yields path objects naming each child item of 

-

        this directory and its descendants.  This requires that 

-

        D.isdir(). 

-

 

-

        This performs a depth-first traversal of the directory tree. 

-

        Each directory is returned just before all its children. 

-

        """ 

-

        for child in self.listdir(): 

-

            if pattern is None or child.fnmatch(pattern): 

-

                yield child 

-

            if child.isdir(): 

-

                for item in child.walk(pattern): 

-

                    yield item 

-

 

-

    def walkdirs(self, pattern=None): 

-

        """ D.walkdirs() -> iterator over subdirs, recursively. 

-

 

-

        With the optional 'pattern' argument, this yields only 

-

        directories whose names match the given pattern.  For 

-

        example, mydir.walkdirs('*test') yields only directories 

-

        with names ending in 'test'. 

-

        """ 

-

        for child in self.dirs(): 

-

            if pattern is None or child.fnmatch(pattern): 

-

                yield child 

-

            for subsubdir in child.walkdirs(pattern): 

-

                yield subsubdir 

-

 

-

    def walkfiles(self, pattern=None): 

-

        """ D.walkfiles() -> iterator over files in D, recursively. 

-

 

-

        The optional argument, pattern, limits the results to files 

-

        with names that match the pattern.  For example, 

-

        mydir.walkfiles('*.tmp') yields only files with the .tmp 

-

        extension. 

-

        """ 

-

        for child in self.listdir(): 

-

            if child.isfile(): 

-

                if pattern is None or child.fnmatch(pattern): 

-

                    yield child 

-

            elif child.isdir(): 

-

                for f in child.walkfiles(pattern): 

-

                    yield f 

-

 

-

    def fnmatch(self, pattern): 

-

        """ Return True if self.name matches the given pattern. 

-

 

-

        pattern - A filename pattern with wildcards, 

-

            for example '*.py'. 

-

        """ 

-

        return fnmatch.fnmatch(self.name, pattern) 

-

 

-

    def glob(self, pattern): 

-

        """ Return a list of path objects that match the pattern. 

-

 

-

        pattern - a path relative to this directory, with wildcards. 

-

 

-

        For example, path('/users').glob('*/bin/*') returns a list 

-

        of all the files users have in their bin directories. 

-

        """ 

-

        return map(path, glob.glob(_base(self / pattern))) 

-

 

-

 

-

    # --- Reading or writing an entire file at once. 

-

 

-

    def open(self, mode='r'): 

-

        """ Open this file.  Return a file object. """ 

-

        return file(self, mode) 

-

 

-

    def bytes(self): 

-

        """ Open this file, read all bytes, return them as a string. """ 

-

        f = self.open('rb') 

-

        try: 

-

            return f.read() 

-

        finally: 

-

            f.close() 

-

 

-

    def write_bytes(self, bytes, append=False): 

-

        """ Open this file and write the given bytes to it. 

-

 

-

        Default behavior is to overwrite any existing file. 

-

        Call this with write_bytes(bytes, append=True) to append instead. 

-

        """ 

-

        if append: 

-

            mode = 'ab' 

-

        else: 

-

            mode = 'wb' 

-

        f = self.open(mode) 

-

        try: 

-

            f.write(bytes) 

-

        finally: 

-

            f.close() 

-

 

-

    def text(self, encoding=None, errors='strict'): 

-

        """ Open this file, read it in, return the content as a string. 

-

 

-

        This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r' 

-

        are automatically translated to '\n'. 

-

 

-

        Optional arguments: 

-

 

-

        encoding - The Unicode encoding (or character set) of 

-

            the file.  If present, the content of the file is 

-

            decoded and returned as a unicode object; otherwise 

-

            it is returned as an 8-bit str. 

-

        errors - How to handle Unicode errors; see help(str.decode) 

-

            for the options.  Default is 'strict'. 

-

        """ 

-

        if encoding is None: 

-

            # 8-bit 

-

            f = self.open(_textmode) 

-

            try: 

-

                return f.read() 

-

            finally: 

-

                f.close() 

-

        else: 

-

            # Unicode 

-

            f = codecs.open(self, 'r', encoding, errors) 

-

            # (Note - Can't use 'U' mode here, since codecs.open 

-

            # doesn't support 'U' mode, even in Python 2.3.) 

-

            try: 

-

                t = f.read() 

-

            finally: 

-

                f.close() 

-

            return t.replace(u'\r\n', u'\n').replace(u'\r', u'\n') 

-

 

-

    def write_text(self, text, encoding=None, errors='strict', append=False): 

-

        """ Write the given text to this file. 

-

 

-

        The default behavior is to overwrite any existing file; 

-

        to append instead, use the 'append=True' keyword argument. 

-

 

-

        There are two differences between path.write_text() and 

-

        path.write_bytes(): Unicode handling and newline handling. 

-

 

-

        --- Unicode 

-

 

-

        If 'text' isn't Unicode, this essentially just does 

-

        open(self, 'w').write(text).  The 'encoding' and 'errors' 

-

        arguments are ignored. 

-

 

-

        If 'text' is Unicode, it is first converted to bytes using the 

-

        specified 'encoding' (or the default encoding if 'encoding' 

-

        isn't specified).  The 'errors' argument applies only to this 

-

        conversion. 

-

 

-

        --- Newlines 

-

 

-

        write_text() converts from programmer-friendly newlines 

-

        (always '\n') to platform-specific newlines (see os.linesep; 

-

        on Windows, for example, the end-of-line marker is '\r\n'). 

-

        This applies to Unicode text the same as to 8-bit text. 

-

 

-

        Because of this conversion, the text should only contain plain 

-

        newlines ('\n'), just like the return value of path.text(). 

-

        If the text contains the characters '\r\n', it may be written 

-

        as '\r\r\n' or '\r\r' depending on your platform.  (This is 

-

        exactly the same as when you open a file for writing with 

-

        fopen(filename, "w") in C or file(filename, 'w') in Python.) 

-

        """ 

-

        if isinstance(text, unicode): 

-

            text = text.replace(u'\n', os.linesep) 

-

            if encoding is None: 

-

                encoding = sys.getdefaultencoding() 

-

            bytes = text.encode(encoding, errors) 

-

            self.write_bytes(bytes, append) 

-

        else: 

-

            if append: 

-

                mode = 'a' 

-

            else: 

-

                mode = 'w' 

-

            f = self.open(mode) 

-

            try: 

-

                f.write(text) 

-

            finally: 

-

                f.close() 

-

 

-

    def lines(self, encoding=None, errors='strict', retain=True): 

-

        """ Open this file, read all lines, return them in a list. 

-

 

-

        Optional arguments: 

-

            encoding - The Unicode encoding (or character set) of 

-

                the file.  The default is None, meaning the content 

-

                of the file is read as 8-bit characters and returned 

-

                as a list of (non-Unicode) str objects. 

-

            errors - How to handle Unicode errors; see help(str.decode) 

-

                for the options.  Default is 'strict' 

-

            retain - If true, retain newline characters; but all newline 

-

                character combinations ('\r', '\n', '\r\n') are 

-

                translated to '\n'.  If false, newline characters are 

-

                stripped off.  Default is True. 

-

 

-

        This uses 'U' mode in Python 2.3 and later. 

-

        """ 

-

        if encoding is None and retain: 

-

            f = self.open(_textmode) 

-

            try: 

-

                return f.readlines() 

-

            finally: 

-

                f.close() 

-

        else: 

-

            return self.text(encoding, errors).splitlines(retain) 

-

 

-

    def write_lines(self, lines, encoding=None, errors='strict', 

-

                    linesep=os.linesep): 

-

        """ Overwrite this file with the given lines of text. 

-

 

-

        lines - A list of strings. 

-

        encoding - A Unicode encoding to use.  This applies only if 

-

            'lines' contains any Unicode strings. 

-

        errors - How to handle errors in Unicode encoding.  This 

-

            also applies only to Unicode strings. 

-

        linesep - A character sequence that will be added at the 

-

            end of every line that doesn't already have it. 

-

        """ 

-

        f = self.open('wb') 

-

        try: 

-

            for line in lines: 

-

                if not line.endswith(linesep): 

-

                    line += linesep 

-

                if isinstance(line, unicode): 

-

                    if encoding is None: 

-

                        encoding = sys.getdefaultencoding() 

-

                    line = line.encode(encoding, errors=errors) 

-

                f.write(line) 

-

        finally: 

-

            f.close() 

-

 

-

 

-

    # --- Methods for querying the filesystem. 

-

 

-

    exists = os.path.exists 

-

    isabs = os.path.isabs 

-

    isdir = os.path.isdir 

-

    isfile = os.path.isfile 

-

    islink = os.path.islink 

-

    ismount = os.path.ismount 

-

 

-

    if hasattr(os.path, 'samefile'): 

-

        samefile = os.path.samefile 

-

 

-

    getatime = os.path.getatime 

-

    atime = property( 

-

        getatime, None, None, 

-

        """ Last access time of the file. """) 

-

 

-

    getmtime = os.path.getmtime 

-

    mtime = property( 

-

        getmtime, None, None, 

-

        """ Last-modified time of the file. """) 

-

 

-

    if hasattr(os.path, 'getctime'): 

-

        getctime = os.path.getctime 

-

        ctime = property( 

-

            getctime, None, None, 

-

            """ Creation time of the file. """) 

-

 

-

    getsize = os.path.getsize 

-

    size = property( 

-

        getsize, None, None, 

-

        """ Size of the file, in bytes. """) 

-

 

-

    if hasattr(os, 'access'): 

-

        def access(self, mode): 

-

            """ Return true if current user has access to this path. 

-

 

-

            mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK 

-

            """ 

-

            return os.access(self, mode) 

-

 

-

    def stat(self): 

-

        """ Perform a stat() system call on this path. """ 

-

        return os.stat(self) 

-

 

-

    def lstat(self): 

-

        """ Like path.stat(), but do not follow symbolic links. """ 

-

        return os.lstat(self) 

-

 

-

    if hasattr(os, 'statvfs'): 

-

        def statvfs(self): 

-

            """ Perform a statvfs() system call on this path. """ 

-

            return os.statvfs(self) 

-

 

-

    if hasattr(os, 'pathconf'): 

-

        def pathconf(self, name): 

-

            return os.pathconf(self, name) 

-

 

-

 

-

    # --- Modifying operations on files and directories 

-

 

-

    def utime(self, times): 

-

        """ Set the access and modified times of this file. """ 

-

        os.utime(self, times) 

-

 

-

    def chmod(self, mode): 

-

        os.chmod(self, mode) 

-

 

-

    if hasattr(os, 'chown'): 

-

        def chown(self, uid, gid): 

-

            os.chown(self, uid, gid) 

-

 

-

    def rename(self, new): 

-

        os.rename(self, new) 

-

 

-

    def renames(self, new): 

-

        os.renames(self, new) 

-

 

-

 

-

    # --- Create/delete operations on directories 

-

 

-

    def mkdir(self, mode=0777): 

-

        os.mkdir(self, mode) 

-

 

-

    def makedirs(self, mode=0777): 

-

        os.makedirs(self, mode) 

-

 

-

    def rmdir(self): 

-

        os.rmdir(self) 

-

 

-

    def removedirs(self): 

-

        os.removedirs(self) 

-

 

-

 

-

    # --- Modifying operations on files 

-

 

-

    def touch(self): 

-

        """ Set the access/modified times of this file to the current time. 

-

        Create the file if it does not exist. 

-

        """ 

-

        fd = os.open(self, os.O_WRONLY | os.O_CREAT, 0666) 

-

        os.close(fd) 

-

        os.utime(self, None) 

-

 

-

    def remove(self): 

-

        os.remove(self) 

-

 

-

    def unlink(self): 

-

        os.unlink(self) 

-

 

-

 

-

    # --- Links 

-

 

-

    if hasattr(os, 'link'): 

-

        def link(self, newpath): 

-

            """ Create a hard link at 'newpath', pointing to this file. """ 

-

            os.link(self, newpath) 

-

 

-

    if hasattr(os, 'symlink'): 

-

        def symlink(self, newlink): 

-

            """ Create a symbolic link at 'newlink', pointing here. """ 

-

            os.symlink(self, newlink) 

-

 

-

    if hasattr(os, 'readlink'): 

-

        def readlink(self): 

-

            """ Return the path to which this symbolic link points. 

-

 

-

            The result may be an absolute or a relative path. 

-

            """ 

-

            return path(os.readlink(self)) 

-

 

-

        def readlinkabs(self): 

-

            """ Return the path to which this symbolic link points. 

-

 

-

            The result is always an absolute path. 

-

            """ 

-

            p = self.readlink() 

-

            if p.isabs(): 

-

                return p 

-

            else: 

-

                return (self.parent / p).abspath() 

-

 

-

 

-

    # --- High-level functions from shutil 

-

 

-

    copyfile = shutil.copyfile 

-

    copymode = shutil.copymode 

-

    copystat = shutil.copystat 

-

    copy = shutil.copy 

-

    copy2 = shutil.copy2 

-

    copytree = shutil.copytree 

-

    if hasattr(shutil, 'move'): 

-

        move = shutil.move 

-

    rmtree = shutil.rmtree 

-

 

-

 

-

    # --- Special stuff from os 

-

 

-

    if hasattr(os, 'chroot'): 

-

        def chroot(self): 

-

            os.chroot(self) 

-

 

-

    if hasattr(os, 'startfile'): 

-

        def startfile(self): 

-

            os.startfile(self) 

-

 

+

""" path.py - An object representing a path to a file or directory. 

+

 

+

Example: 

+

 

+

from path import path 

+

d = path('/home/guido/bin') 

+

for f in d.files('*.py'): 

+

    f.chmod(0755) 

+

 

+

This module requires Python 2.2 or later. 

+

 

+

 

+

URL:     http://www.jorendorff.com/articles/python/path 

+

Author:  Jason Orendorff <jason@jorendorff.com> (and others - see the url!) 

+

Date:    29 Feb 2004 

+

""" 

+

 

+

 

+

# TODO 

+

#   - Bug in write_text().  It doesn't support Universal newline mode. 

+

#   - Better error message in listdir() when self isn't a 

+

#     directory. (On Windows, the error message really sucks.) 

+

#   - Make sure everything has a good docstring. 

+

#   - Add methods for regex find and replace. 

+

#   - guess_content_type() method? 

+

#   - Perhaps support arguments to touch(). 

+

#   - Could add split() and join() methods that generate warnings. 

+

#   - Note:  __add__() technically has a bug, I think, where 

+

#     it doesn't play nice with other types that implement 

+

#     __radd__().  Test this. 

+

 

+

from __future__ import generators 

+

 

+

import sys, os, fnmatch, glob, shutil, codecs 

+

 

+

__version__ = '2.0.2' 

+

__all__ = ['path'] 

+

 

+

# Pre-2.3 support.  Are unicode filenames supported? 

+

_base = str 

+

try: 

+

48    if os.path.supports_unicode_filenames: 

+

        _base = unicode 

+

except AttributeError: 

+

    pass 

+

 

+

# Pre-2.3 workaround for basestring. 

+

try: 

+

    basestring 

+

except NameError: 

+

    basestring = (str, unicode) 

+

 

+

# Universal newline support 

+

_textmode = 'r' 

+

59if hasattr(file, 'newlines'): 

+

    _textmode = 'U' 

+

 

+

 

+

class path(_base): 

+

    """ Represents a filesystem path. 

+

 

+

    For documentation on individual methods, consult their 

+

    counterparts in os.path. 

+

    """ 

+

 

+

    # --- Special Python methods. 

+

 

+

    def __repr__(self): 

+

        return 'path(%s)' % _base.__repr__(self) 

+

 

+

    # Adding a path and a string yields a path. 

+

    def __add__(self, more): 

+

        return path(_base(self) + more) 

+

 

+

    def __radd__(self, other): 

+

        return path(other + _base(self)) 

+

 

+

    # The / operator joins paths. 

+

    def __div__(self, rel): 

+

        """ fp.__div__(rel) == fp / rel == fp.joinpath(rel) 

+

 

+

        Join two path components, adding a separator character if 

+

        needed. 

+

        """ 

+

        return path(os.path.join(self, rel)) 

+

 

+

    # Make the / operator work even when true division is enabled. 

+

    __truediv__ = __div__ 

+

 

+

    def getcwd(): 

+

        """ Return the current working directory as a path object. """ 

+

        return path(os.getcwd()) 

+

    getcwd = staticmethod(getcwd) 

+

 

+

 

+

    # --- Operations on path strings. 

+

 

+

exit    def abspath(self):       return path(os.path.abspath(self)) 

+

exit    def normcase(self):      return path(os.path.normcase(self)) 

+

exit    def normpath(self):      return path(os.path.normpath(self)) 

+

exit    def realpath(self):      return path(os.path.realpath(self)) 

+

exit    def expanduser(self):    return path(os.path.expanduser(self)) 

+

exit    def expandvars(self):    return path(os.path.expandvars(self)) 

+

exit    def dirname(self):       return path(os.path.dirname(self)) 

+

    basename = os.path.basename 

+

 

+

    def expand(self): 

+

        """ Clean up a filename by calling expandvars(), 

+

        expanduser(), and normpath() on it. 

+

 

+

        This is commonly everything needed to clean up a filename 

+

        read from a configuration file, for example. 

+

        """ 

+

        return self.expandvars().expanduser().normpath() 

+

 

+

    def _get_namebase(self): 

+

        base, ext = os.path.splitext(self.name) 

+

        return base 

+

 

+

    def _get_ext(self): 

+

        f, ext = os.path.splitext(_base(self)) 

+

        return ext 

+

 

+

    def _get_drive(self): 

+

        drive, r = os.path.splitdrive(self) 

+

        return path(drive) 

+

 

+

    parent = property( 

+

        dirname, None, None, 

+

        """ This path's parent directory, as a new path object. 

+

 

+

        For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib') 

+

        """) 

+

 

+

    name = property( 

+

        basename, None, None, 

+

        """ The name of this file or directory without the full path. 

+

 

+

        For example, path('/usr/local/lib/libpython.so').name == 'libpython.so' 

+

        """) 

+

 

+

    namebase = property( 

+

        _get_namebase, None, None, 

+

        """ The same as path.name, but with one file extension stripped off. 

+

 

+

        For example, path('/home/guido/python.tar.gz').name     == 'python.tar.gz', 

+

        but          path('/home/guido/python.tar.gz').namebase == 'python.tar' 

+

        """) 

+

 

+

    ext = property( 

+

        _get_ext, None, None, 

+

        """ The file extension, for example '.py'. """) 

+

 

+

    drive = property( 

+

        _get_drive, None, None, 

+

        """ The drive specifier, for example 'C:'. 

+

        This is always empty on systems that don't use drive specifiers. 

+

        """) 

+

 

+

    def splitpath(self): 

+

        """ p.splitpath() -> Return (p.parent, p.name). """ 

+

        parent, child = os.path.split(self) 

+

        return path(parent), child 

+

 

+

    def splitdrive(self): 

+

        """ p.splitdrive() -> Return (p.drive, <the rest of p>). 

+

 

+

        Split the drive specifier from this path.  If there is 

+

        no drive specifier, p.drive is empty, so the return value 

+

        is simply (path(''), p).  This is always the case on Unix. 

+

        """ 

+

        drive, rel = os.path.splitdrive(self) 

+

        return path(drive), rel 

+

 

+

    def splitext(self): 

+

        """ p.splitext() -> Return (p.stripext(), p.ext). 

+

 

+

        Split the filename extension from this path and return 

+

        the two parts.  Either part may be empty. 

+

 

+

        The extension is everything from '.' to the end of the 

+

        last path segment.  This has the property that if 

+

        (a, b) == p.splitext(), then a + b == p. 

+

        """ 

+

        # Cast to plain string using _base because Python 2.2 

+

        # implementations of os.path.splitext use "for c in path:..." 

+

        # which means something different when applied to a path 

+

        # object. 

+

        filename, ext = os.path.splitext(_base(self)) 

+

        return path(filename), ext 

+

 

+

    def stripext(self): 

+

        """ p.stripext() -> Remove one file extension from the path. 

+

 

+

        For example, path('/home/guido/python.tar.gz').stripext() 

+

        returns path('/home/guido/python.tar'). 

+

        """ 

+

        return self.splitext()[0] 

+

 

+

214    if hasattr(os.path, 'splitunc'): 

+

        def splitunc(self): 

+

            unc, rest = os.path.splitunc(self) 

+

            return path(unc), rest 

+

 

+

        def _get_uncshare(self): 

+

            unc, r = os.path.splitunc(self) 

+

            return path(unc) 

+

 

+

        uncshare = property( 

+

            _get_uncshare, None, None, 

+

            """ The UNC mount point for this path. 

+

            This is empty for paths on local drives. """) 

+

 

+

    def joinpath(self, *args): 

+

        """ Join two or more path components, adding a separator 

+

        character (os.sep) if needed.  Returns a new path 

+

        object. 

+

        """ 

+

        return path(os.path.join(self, *args)) 

+

 

+

    def splitall(self): 

+

        """ Return a list of the path components in this path. 

+

 

+

        The first item in the list will be a path.  Its value will be 

+

        either os.curdir, os.pardir, empty, or the root directory of 

+

        this path (for example, '/' or 'C:\\').  The other items in 

+

        the list will be strings. 

+

 

+

        path.path.joinpath(*result) will yield the original path. 

+

        """ 

+

        parts = [] 

+

        loc = self 

+

        while loc != os.curdir and loc != os.pardir: 

+

            prev = loc 

+

            loc, child = prev.splitpath() 

+

            if loc == prev: 

+

                break 

+

            parts.append(child) 

+

        parts.append(loc) 

+

        parts.reverse() 

+

        return parts 

+

 

+

    def relpath(self): 

+

        """ Return this path as a relative path, 

+

        based from the current working directory. 

+

        """ 

+

        cwd = path(os.getcwd()) 

+

        return cwd.relpathto(self) 

+

 

+

    def relpathto(self, dest): 

+

        """ Return a relative path from self to dest. 

+

 

+

        If there is no relative path from self to dest, for example if 

+

        they reside on different drives in Windows, then this returns 

+

        dest.abspath(). 

+

        """ 

+

        origin = self.abspath() 

+

        dest = path(dest).abspath() 

+

 

+

        orig_list = origin.normcase().splitall() 

+

        # Don't normcase dest!  We want to preserve the case. 

+

        dest_list = dest.splitall() 

+

 

+

        if orig_list[0] != os.path.normcase(dest_list[0]): 

+

            # Can't get here from there. 

+

            return dest 

+

 

+

        # Find the location where the two paths start to differ. 

+

        i = 0 

+

        for start_seg, dest_seg in zip(orig_list, dest_list): 

+

            if start_seg != os.path.normcase(dest_seg): 

+

                break 

+

            i += 1 

+

 

+

        # Now i is the point where the two paths diverge. 

+

        # Need a certain number of "os.pardir"s to work up 

+

        # from the origin to the point of divergence. 

+

        segments = [os.pardir] * (len(orig_list) - i) 

+

        # Need to add the diverging part of dest_list. 

+

        segments += dest_list[i:] 

+

        if len(segments) == 0: 

+

            # If they happen to be identical, use os.curdir. 

+

            return path(os.curdir) 

+

        else: 

+

            return path(os.path.join(*segments)) 

+

 

+

 

+

    # --- Listing, searching, walking, and matching 

+

 

+

    def listdir(self, pattern=None): 

+

        """ D.listdir() -> List of items in this directory. 

+

 

+

        Use D.files() or D.dirs() instead if you want a listing 

+

        of just files or just subdirectories. 

+

 

+

        The elements of the list are path objects. 

+

 

+

        With the optional 'pattern' argument, this only lists 

+

        items whose names match the given pattern. 

+

        """ 

+

        names = os.listdir(self) 

+

        if pattern is not None: 

+

            names = fnmatch.filter(names, pattern) 

+

        return [self / child for child in names] 

+

 

+

    def dirs(self, pattern=None): 

+

        """ D.dirs() -> List of this directory's subdirectories. 

+

 

+

        The elements of the list are path objects. 

+

        This does not walk recursively into subdirectories 

+

        (but see path.walkdirs). 

+

 

+

        With the optional 'pattern' argument, this only lists 

+

        directories whose names match the given pattern.  For 

+

        example, d.dirs('build-*'). 

+

        """ 

+

        return [p for p in self.listdir(pattern) if p.isdir()] 

+

 

+

    def files(self, pattern=None): 

+

        """ D.files() -> List of the files in this directory. 

+

 

+

        The elements of the list are path objects. 

+

        This does not walk into subdirectories (see path.walkfiles). 

+

 

+

        With the optional 'pattern' argument, this only lists files 

+

        whose names match the given pattern.  For example, 

+

        d.files('*.pyc'). 

+

        """ 

+

 

+

        return [p for p in self.listdir(pattern) if p.isfile()] 

+

 

+

    def walk(self, pattern=None): 

+

        """ D.walk() -> iterator over files and subdirs, recursively. 

+

 

+

        The iterator yields path objects naming each child item of 

+

        this directory and its descendants.  This requires that 

+

        D.isdir(). 

+

 

+

        This performs a depth-first traversal of the directory tree. 

+

        Each directory is returned just before all its children. 

+

        """ 

+

        for child in self.listdir(): 

+

            if pattern is None or child.fnmatch(pattern): 

+

                yield child 

+

            if child.isdir(): 

+

                for item in child.walk(pattern): 

+

                    yield item 

+

 

+

    def walkdirs(self, pattern=None): 

+

        """ D.walkdirs() -> iterator over subdirs, recursively. 

+

 

+

        With the optional 'pattern' argument, this yields only 

+

        directories whose names match the given pattern.  For 

+

        example, mydir.walkdirs('*test') yields only directories 

+

        with names ending in 'test'. 

+

        """ 

+

        for child in self.dirs(): 

+

            if pattern is None or child.fnmatch(pattern): 

+

                yield child 

+

            for subsubdir in child.walkdirs(pattern): 

+

                yield subsubdir 

+

 

+

    def walkfiles(self, pattern=None): 

+

        """ D.walkfiles() -> iterator over files in D, recursively. 

+

 

+

        The optional argument, pattern, limits the results to files 

+

        with names that match the pattern.  For example, 

+

        mydir.walkfiles('*.tmp') yields only files with the .tmp 

+

        extension. 

+

        """ 

+

        for child in self.listdir(): 

+

            if child.isfile(): 

+

                if pattern is None or child.fnmatch(pattern): 

+

                    yield child 

+

            elif child.isdir(): 

+

                for f in child.walkfiles(pattern): 

+

                    yield f 

+

 

+

    def fnmatch(self, pattern): 

+

        """ Return True if self.name matches the given pattern. 

+

 

+

        pattern - A filename pattern with wildcards, 

+

            for example '*.py'. 

+

        """ 

+

        return fnmatch.fnmatch(self.name, pattern) 

+

 

+

    def glob(self, pattern): 

+

        """ Return a list of path objects that match the pattern. 

+

 

+

        pattern - a path relative to this directory, with wildcards. 

+

 

+

        For example, path('/users').glob('*/bin/*') returns a list 

+

        of all the files users have in their bin directories. 

+

        """ 

+

        return map(path, glob.glob(_base(self / pattern))) 

+

 

+

 

+

    # --- Reading or writing an entire file at once. 

+

 

+

    def open(self, mode='r'): 

+

        """ Open this file.  Return a file object. """ 

+

        return file(self, mode) 

+

 

+

    def bytes(self): 

+

        """ Open this file, read all bytes, return them as a string. """ 

+

        f = self.open('rb') 

+

        try: 

+

            return f.read() 

+

        finally: 

+

            f.close() 

+

 

+

    def write_bytes(self, bytes, append=False): 

+

        """ Open this file and write the given bytes to it. 

+

 

+

        Default behavior is to overwrite any existing file. 

+

        Call this with write_bytes(bytes, append=True) to append instead. 

+

        """ 

+

        if append: 

+

            mode = 'ab' 

+

        else: 

+

            mode = 'wb' 

+

        f = self.open(mode) 

+

        try: 

+

            f.write(bytes) 

+

        finally: 

+

            f.close() 

+

 

+

    def text(self, encoding=None, errors='strict'): 

+

        """ Open this file, read it in, return the content as a string. 

+

 

+

        This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r' 

+

        are automatically translated to '\n'. 

+

 

+

        Optional arguments: 

+

 

+

        encoding - The Unicode encoding (or character set) of 

+

            the file.  If present, the content of the file is 

+

            decoded and returned as a unicode object; otherwise 

+

            it is returned as an 8-bit str. 

+

        errors - How to handle Unicode errors; see help(str.decode) 

+

            for the options.  Default is 'strict'. 

+

        """ 

+

        if encoding is None: 

+

            # 8-bit 

+

            f = self.open(_textmode) 

+

            try: 

+

                return f.read() 

+

            finally: 

+

                f.close() 

+

        else: 

+

            # Unicode 

+

            f = codecs.open(self, 'r', encoding, errors) 

+

            # (Note - Can't use 'U' mode here, since codecs.open 

+

            # doesn't support 'U' mode, even in Python 2.3.) 

+

            try: 

+

                t = f.read() 

+

            finally: 

+

                f.close() 

+

            return t.replace(u'\r\n', u'\n').replace(u'\r', u'\n') 

+

 

+

    def write_text(self, text, encoding=None, errors='strict', append=False): 

+

        """ Write the given text to this file. 

+

 

+

        The default behavior is to overwrite any existing file; 

+

        to append instead, use the 'append=True' keyword argument. 

+

 

+

        There are two differences between path.write_text() and 

+

        path.write_bytes(): Unicode handling and newline handling. 

+

 

+

        --- Unicode 

+

 

+

        If 'text' isn't Unicode, this essentially just does 

+

        open(self, 'w').write(text).  The 'encoding' and 'errors' 

+

        arguments are ignored. 

+

 

+

        If 'text' is Unicode, it is first converted to bytes using the 

+

        specified 'encoding' (or the default encoding if 'encoding' 

+

        isn't specified).  The 'errors' argument applies only to this 

+

        conversion. 

+

 

+

        --- Newlines 

+

 

+

        write_text() converts from programmer-friendly newlines 

+

        (always '\n') to platform-specific newlines (see os.linesep; 

+

        on Windows, for example, the end-of-line marker is '\r\n'). 

+

        This applies to Unicode text the same as to 8-bit text. 

+

 

+

        Because of this conversion, the text should only contain plain 

+

        newlines ('\n'), just like the return value of path.text(). 

+

        If the text contains the characters '\r\n', it may be written 

+

        as '\r\r\n' or '\r\r' depending on your platform.  (This is 

+

        exactly the same as when you open a file for writing with 

+

        fopen(filename, "w") in C or file(filename, 'w') in Python.) 

+

        """ 

+

        if isinstance(text, unicode): 

+

            text = text.replace(u'\n', os.linesep) 

+

            if encoding is None: 

+

                encoding = sys.getdefaultencoding() 

+

            bytes = text.encode(encoding, errors) 

+

            self.write_bytes(bytes, append) 

+

        else: 

+

            if append: 

+

                mode = 'a' 

+

            else: 

+

                mode = 'w' 

+

            f = self.open(mode) 

+

            try: 

+

                f.write(text) 

+

            finally: 

+

                f.close() 

+

 

+

    def lines(self, encoding=None, errors='strict', retain=True): 

+

        """ Open this file, read all lines, return them in a list. 

+

 

+

        Optional arguments: 

+

            encoding - The Unicode encoding (or character set) of 

+

                the file.  The default is None, meaning the content 

+

                of the file is read as 8-bit characters and returned 

+

                as a list of (non-Unicode) str objects. 

+

            errors - How to handle Unicode errors; see help(str.decode) 

+

                for the options.  Default is 'strict' 

+

            retain - If true, retain newline characters; but all newline 

+

                character combinations ('\r', '\n', '\r\n') are 

+

                translated to '\n'.  If false, newline characters are 

+

                stripped off.  Default is True. 

+

 

+

        This uses 'U' mode in Python 2.3 and later. 

+

        """ 

+

        if encoding is None and retain: 

+

            f = self.open(_textmode) 

+

            try: 

+

                return f.readlines() 

+

            finally: 

+

                f.close() 

+

        else: 

+

            return self.text(encoding, errors).splitlines(retain) 

+

 

+

    def write_lines(self, lines, encoding=None, errors='strict', 

+

                    linesep=os.linesep): 

+

        """ Overwrite this file with the given lines of text. 

+

 

+

        lines - A list of strings. 

+

        encoding - A Unicode encoding to use.  This applies only if 

+

            'lines' contains any Unicode strings. 

+

        errors - How to handle errors in Unicode encoding.  This 

+

            also applies only to Unicode strings. 

+

        linesep - A character sequence that will be added at the 

+

            end of every line that doesn't already have it. 

+

        """ 

+

        f = self.open('wb') 

+

        try: 

+

            for line in lines: 

+

                if not line.endswith(linesep): 

+

                    line += linesep 

+

                if isinstance(line, unicode): 

+

                    if encoding is None: 

+

                        encoding = sys.getdefaultencoding() 

+

                    line = line.encode(encoding, errors=errors) 

+

                f.write(line) 

+

        finally: 

+

            f.close() 

+

 

+

 

+

    # --- Methods for querying the filesystem. 

+

 

+

    exists = os.path.exists 

+

    isabs = os.path.isabs 

+

    isdir = os.path.isdir 

+

    isfile = os.path.isfile 

+

    islink = os.path.islink 

+

    ismount = os.path.ismount 

+

 

+

574    if hasattr(os.path, 'samefile'): 

+

        samefile = os.path.samefile 

+

 

+

    getatime = os.path.getatime 

+

    atime = property( 

+

        getatime, None, None, 

+

        """ Last access time of the file. """) 

+

 

+

    getmtime = os.path.getmtime 

+

    mtime = property( 

+

        getmtime, None, None, 

+

        """ Last-modified time of the file. """) 

+

 

+

592    if hasattr(os.path, 'getctime'): 

+

        getctime = os.path.getctime 

+

        ctime = property( 

+

            getctime, None, None, 

+

            """ Creation time of the file. """) 

+

 

+

    getsize = os.path.getsize 

+

    size = property( 

+

        getsize, None, None, 

+

        """ Size of the file, in bytes. """) 

+

 

+

605    if hasattr(os, 'access'): 

+

        def access(self, mode): 

+

            """ Return true if current user has access to this path. 

+

 

+

            mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK 

+

            """ 

+

            return os.access(self, mode) 

+

 

+

    def stat(self): 

+

        """ Perform a stat() system call on this path. """ 

+

        return os.stat(self) 

+

 

+

    def lstat(self): 

+

        """ Like path.stat(), but do not follow symbolic links. """ 

+

        return os.lstat(self) 

+

 

+

614    if hasattr(os, 'statvfs'): 

+

        def statvfs(self): 

+

            """ Perform a statvfs() system call on this path. """ 

+

            return os.statvfs(self) 

+

 

+

619    if hasattr(os, 'pathconf'): 

+

        def pathconf(self, name): 

+

            return os.pathconf(self, name) 

+

 

+

 

+

    # --- Modifying operations on files and directories 

+

 

+

    def utime(self, times): 

+

        """ Set the access and modified times of this file. """ 

+

        os.utime(self, times) 

+

 

+

    def chmod(self, mode): 

+

        os.chmod(self, mode) 

+

 

+

633    if hasattr(os, 'chown'): 

+

        def chown(self, uid, gid): 

+

            os.chown(self, uid, gid) 

+

 

+

    def rename(self, new): 

+

        os.rename(self, new) 

+

 

+

    def renames(self, new): 

+

        os.renames(self, new) 

+

 

+

 

+

    # --- Create/delete operations on directories 

+

 

+

    def mkdir(self, mode=0777): 

+

        os.mkdir(self, mode) 

+

 

+

    def makedirs(self, mode=0777): 

+

        os.makedirs(self, mode) 

+

 

+

    def rmdir(self): 

+

        os.rmdir(self) 

+

 

+

    def removedirs(self): 

+

        os.removedirs(self) 

+

 

+

 

+

    # --- Modifying operations on files 

+

 

+

    def touch(self): 

+

        """ Set the access/modified times of this file to the current time. 

+

        Create the file if it does not exist. 

+

        """ 

+

        fd = os.open(self, os.O_WRONLY | os.O_CREAT, 0666) 

+

        os.close(fd) 

+

        os.utime(self, None) 

+

 

+

    def remove(self): 

+

        os.remove(self) 

+

 

+

    def unlink(self): 

+

        os.unlink(self) 

+

 

+

 

+

    # --- Links 

+

 

+

678    if hasattr(os, 'link'): 

+

        def link(self, newpath): 

+

            """ Create a hard link at 'newpath', pointing to this file. """ 

+

            os.link(self, newpath) 

+

 

+

683    if hasattr(os, 'symlink'): 

+

        def symlink(self, newlink): 

+

            """ Create a symbolic link at 'newlink', pointing here. """ 

+

            os.symlink(self, newlink) 

+

 

+

688    if hasattr(os, 'readlink'): 

+

        def readlink(self): 

+

            """ Return the path to which this symbolic link points. 

+

 

+

            The result may be an absolute or a relative path. 

+

            """ 

+

            return path(os.readlink(self)) 

+

 

+

        def readlinkabs(self): 

+

            """ Return the path to which this symbolic link points. 

+

 

+

            The result is always an absolute path. 

+

            """ 

+

            p = self.readlink() 

+

            if p.isabs(): 

+

                return p 

+

            else: 

+

                return (self.parent / p).abspath() 

+

 

+

 

+

    # --- High-level functions from shutil 

+

 

+

    copyfile = shutil.copyfile 

+

    copymode = shutil.copymode 

+

    copystat = shutil.copystat 

+

    copy = shutil.copy 

+

    copy2 = shutil.copy2 

+

    copytree = shutil.copytree 

+

717    if hasattr(shutil, 'move'): 

+

        move = shutil.move 

+

    rmtree = shutil.rmtree 

+

 

+

 

+

    # --- Special stuff from os 

+

 

+

723    if hasattr(os, 'chroot'): 

+

        def chroot(self): 

+

            os.chroot(self) 

+

 

+

exit    if hasattr(os, 'startfile'): 

+

        def startfile(self): 

+

            os.startfile(self) 

+

 

-- cgit v1.2.1