The path Module

This module helps in interacting with the filesystem. The main focus is on abstracting files and directories and paths.

Path loads Files, Directories, etc. Files have a path associated with them. Directories are a collection of Paths (that link to other files and directories).

There is a circular dependency with these objects, so they need to be kept in one file.

class moments.path.Directory(path='', **kwargs)[source]

This object holds a summary of a single directory. (no recursion. one level only)

A Directory is a collection of Path objects. They can be sortable based on types, dates, etc. Each of the Paths can handle loading and types.

Directories on the filesystem share many properties with Files, so the Directory class is a subclass of the File class.

adjust_time(hours=0)[source]

adjust the modified time of all files in the directory by the number of hours specified.

auto_rotate_images(update_thumbs=True)[source]

#it’s best to just use: jhead -autorot *.JPG

this resets the last modified timestamp to now() not what we want, so go through and reset all timestamps to original times

http://www.sentex.net/~mwandel/jhead/

check_size(recurse=False)[source]

Go through all files and add up the size.

It is possible to recursively add up sizes of subdirectories, but this can be a resource intensive operation. Be careful when setting recurse=True.

create_journal(journal='action.txt', items='Images', full_path=False)[source]

if we don’t have a journal create one using the items of type items

adapted from moments/scripts/images_to_journal.py

default_file()[source]

usually just want an image but if there are no images, we may want to look for other file types

default_image(pick_by='random')[source]

if we have an action log, use that regardless of pick_by

Not currently configured to work with RemoteJournal

file_date_range()[source]

generate a name based on the range of dates for files in this directory

files_to_journal(filetype='Image', journal_file='action.txt', full_path=False)[source]

*2010.12.22 06:49:41 seems similar in function to create_journal this is a bit easier to understand from the name though

this is used by /c/moments/scripts/import_usb.py

make_thumbs()[source]

generate thumbnails for all images in this directory

reset()[source]

if we’ve already scanned something once, and a subsequent scan is called, we’ll want to reset ourself so duplicates are not added (common mistake) this is the same thing that happens during initialization, so breaking it out here

scan_directory(recurse=False)[source]

only load paths

this will clear out any previous scans to avoid duplication reset includes filetypes and sorts

scan_filetypes()[source]

look in the directory’s list of files for different types of files put them in the right list type in the directory

should have already scanned the directory for files

we will look through the list of files for files that are likely images then populate that list

not sure if this should always happen at scan time what if we don’t need to use images, sounds, movies? extra step maybe only create special node types if they’re needed.

depending on the file extension, should create an object with the appropriate type and add it to the correct list in the Directory

summary()[source]

standard way of representing the directory concisely?

class moments.path.File(path)[source]

files are Nodes with sizes also leafs in tree structure

could be a file or a directory

one thing connected to other things on the filesystem

structure to hold the meta data of a node on a filesystem should hold the common attributes of files and directories

Node paths are the paths on the local system... i.e. how python would find them

operations common to both files and directories

adjust_time(hours=0)[source]

wrap change stats in a more user friendly function

change_stats(accessed=None, modified=None)[source]

take new values for the accessed and modified times and update the file’s properties should only accept Timestamp values. Timestamp can be used for conversions as needed. then use Timestamp.epoch() to get right values here:

check_size()[source]

Wraps os.path.getsize() to return the file’s size.

check_stats()[source]

check and see what the operating system is reporting for this node’s stats update our copy of the stats

day()[source]

print creation time in a specific format

make_md5()[source]

calculate the md5 hash for ourself

could store this in metadata at some point

http://docs.python.org/library/hashlib.html#module-hashlib

move(rel_destination)[source]

this utilizes the os.rename function

reset_stats()[source]

some actions (like image rotate) may update the file’s modified times but we might want to keep the original time this resets them to what they were when originally initialized

timestamp()[source]

return a corresponding moments Timestamp object for the file’s mtime

class moments.path.Image(path)[source]

object to hold Image specific meta data for an image locally available

and rendering thumbnails

copy(destination, relative=True)[source]

copy the original image, along with all thumbs

dimensions()[source]

return the dimensions of this image

make_thumb_dirs(base=None)[source]

if they don’t already exist, create them

make_thumbs()[source]

regenerate all thumbnails from original

move(destination, relative=False)[source]

this utilizes the os.rename function but should also move thumbnails

if relative is true, will expect a relative path that is joined with the local path otherwise destination is assumed to be full local path

very similar functionality as minstream.import_media._move_image_and_thumbs() import_media uses subprocess system level move commands which is not as cross platform

rotate(degrees=90)[source]

rotate image by number of degrees (clockwise!!)

need to reset file timestamp to be original especially if not keeping track of that elsewhere

see also Directory.auto_rotate_images()

but if you need to tune individually, better to call jpegtrans here

jhead -cmd “jpegtran -progressive -rotate 90 &i > &o” IMG_4965.JPG

http://www.sentex.net/~mwandel/jhead/usage.html

rotate_pil(degrees=90)[source]

rotate image by number of degrees (clockwise!!)

use Python Image Library

PIL is very LOSSY!!

will also lose original EXIF data

(but it does work if you don’t have access to jhead/jpegtran)

size_path(size, square=True)[source]

take a size and create the corresponding thumbnail (local) path

*2012.08.18 11:28:13 also, decide if a squared version of the image is requested

seems like this is something that should be done here (and maybe both should be available)

class moments.path.Path(path=None, parts=None, relative=False, relative_prefix='')[source]

a path to a specific destination

represented as a string with separators

very similar in purpose to os.path

is not involved with what is contained at the path destination

this is a collection of common operations needed for manipulating paths, in some cases wrapping the standard library os.path module

copy(destination)[source]

wrapping os call seems like there was trouble doing this if it crossed devices needed a system call in that case http://docs.python.org/library/shutil.html

create(mode=None)[source]

see if we have an extension create a blank file if so

otherwise make a new directory

created()[source]

wrapper shortcut for getting the modified timestamp of the path

custom_relative_path(prefix=None, path=None)[source]

method to change system path to viewer path

if path on file system is different than path displayed by viewer generate it here

ideally would just use routes here... heavy overlap

distance(path)[source]

the shortest number of nodes between self and path

find common prefix then count from there

exists()[source]

check if the actual path exists.

expand()[source]

check if we start with a ‘.’ something expand and reparse

load(node_type=None, create=True)[source]

return a storage.Node of the destination

can look at types here and return the appropriate type

looks at the path, determines the right kind of storage object to associate with that path returns the storage object

load_journal(add_tags=[], subtract_tags=[], include_path_tags=True, create=False)[source]

walk the given path and create a journal object from all logs encountered in the path

create a temporary, in memory, journal from logs

this works for both directories and log files

*2009.06.18 12:38:45

this was started to be abstracted from osbrowser in player.py. By moving here, we minimize dependencies outside of Moments module

load_journal cannot guarantee that the returned Journal item will have a filename (self.path) associated with it for later saving.

in that case should use:

j = Journal()
j.load(path, add_tags=these_tags)

-or-

j = load_journal(path)
j.path = destination

of course you can always pass the path in explicitly to save: save(filename=path)

log_action(actions=['view'])[source]

this is closely related to journal.log_action (don’t forget to use that if it’s easier)

but sometimes it is inconvenient to think in terms of a journal when you are working with paths

this assumes the journal to log into is “action.txt” if the path is a directory, look for it in the directory if the path is a file (more common) look for action.txt in the parent directory

if logs need to be added anywhere else, use this concept, or journal.log_action

make_tree()[source]

go through all parts and make a node for each of them return the root node

parent()[source]

return a Path object to our parent don’t want to do this on initialization, since it would recursively call

Similar in concept to: os.path.dirname(self.path)

parts()[source]

return a list of all parts of the path (os.path.split only splits into two parts, this does all)

relative_path_parts()[source]

split the pieces up so that they can be navigated

to_relative(path='', extension=None)[source]

should work either way... returns the difference between the two paths (self.path and path) return value is just a string representation

accept a path (either Path or path... will get resolved down to str) return the relative part by removing the path sent from our prefix

convert a local file path into one acceptable for use as a relative path in a URL

if node is a file, this will include the filename at the end

to_tags(include_name=True, include_parent=True)[source]

looks at the specified path to generate a list of tags based on the file name and location

check if the last item in the path is a file with an extension get rid of the extension if so

type()[source]

determine the subclass that should be associated with this Path this gives us a central place to track this

moments.path.check_ignore(item, ignores=[])[source]

take a string (item) and see if any of the strings in ignores list are in the item if so ignore it.

moments.path.extension(name)[source]

turns out that this is similar to os.path.splitext() but will only return the extension (not both parts)

find a file’s file extension (part of filename after last ‘.’)

splitting into a list with two items: prefix, extension = f.name.split(”.”) will not work with file names with multiple ‘.’s in them

moments.path.load_instance(instances='/c/instances.txt', tag=None)[source]

load instances.txt journal look for the newest entry with tag return the data of the entry as a list of each file/line

moments.path.load_journal(path, **kwargs)[source]

helper to simplify call

moments.path.name_only(name)[source]

opposite of extension() return the filename without any extension

This Page