Skip to main content

Kids file management library

Project description

https://pypip.in/v/kids.file/badge.png https://secure.travis-ci.org/0k/kids.file.png?branch=master

This very small module is part of KIDS (Keep It Dead Simple), and propose some python coding shorcuts on very common tasks. Original tasks I’ve shortcuted often requires to know 2 to 10 lines of python or special cases or different modules location.

It is, for now, a very humble package.

Documentation

tmpfile, file_get_contents, file_put_contents

Let’s create a new temporary file containing the string ‘bonjour’:

>>> from kids.file import tmpfile, file_get_contents, file_put_contents
>>> f = tmpfile(content='bonjour')

f holds the file path of the temporary file. Let’s check what is the file content with file_get_contents:

>>> file_get_contents(f)
'bonjour'

Let’s now put some new content in this file, thanks to file_put_contents:

>>> file_put_contents(f, 'hello\nfriend')
>>> file_get_contents(f)
'hello\nfriend'

This is it.

chown, mkdir, touch

Let’s now create a small tree directory (using tmpdir, mkdir, touch):

>>> from kids.file import tmpdir, mkdir, touch
>>> from os.path import join

>>> d = tmpdir()
>>> base = join(d, 'base')

>>> mkdir(join(base, 'foo'), recursive=True)
>>> touch(join(base, 'plop'))

We test chown for root as it will be with uid and gid 0 for sure. There should be 3 chown issued.

We will mock the legacy ‘os.chown’ to monitor when it is called:

>>> import os
>>> import minimock
>>> m = minimock.mock('os.chown')

And call our chown on user ‘root’:

>>> from kids.file import chown
>>> chown(base, 'root', recursive=True)  ## doctest: +ELLIPSIS
Called os.chown('/.../base', 0, 0)
Called os.chown('/.../base/plop', 0, 0)
Called os.chown('/.../base/foo', 0, 0)

Let’s clean up our mess:

>>> minimock.restore()

>>> from kids.file import rmtree
>>> rmtree(d)

Additional Shortcuts

I’m not sure to keep these shortcuts. I’ll see if these are really used often.

Compressed file

You should now read this easily:

>>> f = tmpfile(content="foo")

Let’s zip this file:

>>> from kids.file import file_zip
>>> file_zip(f)

This created a new file along the previvous file. Let’s check its contents:

>>> file_get_contents(f + '.gz', uncompress="zlib")
'foo'

And now, we can clean up our mess:

>>> unlink(f)
>>> unlink(f + ".gz")

Tests

Well, this package is really small, and you’ve just read the tests.

To execute them, install nosetest, and run:

nosetests

Changelog

0.0.1 (2013-02-12)

  • First import. [Valentin Lab]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kids.file-0.0.1.tar.gz (5.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page