module filehelper.file_tree_node

Inheritance diagram of pyquickhelper.filehelper.file_tree_node

Short summary

module pyquickhelper.filehelper.file_tree_node

a node which contains a file or a folder

source on GitHub

Classes

class

truncated documentation

FileTreeNode

Defines a node for a folder or a tree. Example:

Properties

property

truncated documentation

date

Returns the modification date.

fullname

Returns the full name.

name

Returns the file name from the root.

root

Returns the root directory, the one used as a root for a synchronization.

size

Returns the size.

type

Returns the file type (file or folder).

Static Methods

staticmethod

truncated documentation

build_expression

Builds a regular expression validating a list of extension.

Methods

method

truncated documentation

__getitem__

returns the element i

__init__

Defines a file, relative to a root.

__iter__

iterator on the element

__len__

Returns the number of elements in this folder and in the subfolders.

__str__

usual

_fill

look for subfolders

_fillstat

private: fill _type, _size

copy_to

Copies the file to path.

difference

Returns the differences with another folder.

exists

say if it does exist or not

get

return a dictionary with some values which describe the file

get_content

Returns the content of a text file.

get_dict

Returns a dictionary { self._file : node }.

get_fullname

hash_md5_readfile

Computes a hash of a file.

isdir

is it a folder?

isfile

is it a file?

max_date

return the more recent date

nb_children

return the number of children

remove

Removes the file.

repo_ls

call ls of an instance of SourceRepository

sign

Returns ==, < or > according the dates if the size is not too big, if the sign is < or >, …

Documentation

a node which contains a file or a folder

source on GitHub

class pyquickhelper.filehelper.file_tree_node.FileTreeNode(root, file=None, filter=None, level=0, parent=None, repository=False, log=False, log1=False, fLOG=<function noLOG>)[source]

Bases: object

Defines a node for a folder or a tree. Example:

def example (p1, p2, hash_size = 1024**2*2, svn1 = True, svn2 = False) :
    extout = re.compile (FileTreeNode.build_expression ("dvi bbl blg ilg ind old out pyc pyd " \
                                "bak idx obj log aux pdb sbr ncb res idb suo dep " \
                                "ogm manifest dsp dsz user ilk bsc exp eps".split ()))
    extfou = re.compile ("(exeinterpreter[/\\].*[.]dll)|([/\\]upgradereport)|" \
                        "(thumbs[.]db)|([.]svn)|(temp[_/\\].*)")

    def filter (root, path, f, d) :
        root = root.lower ()
        path = path.lower ()
        f    = f.lower ()
        if extout.search (f) :
            if not d and not f.endswith(".pyc"):
                print("rejected (o1)", path, f)
            return False
        fu = os.path.join (path, f)
        if extfou.search (fu) :
            if not d and not f.endswith(".pyc"):
                print("rejected (o2)", path, f)
            return False
        return True

    f1  = p1
    f2  = p2

    node1 = FileTreeNode(f1, filter = filter, repository = svn1)
    node2 = FileTreeNode(f2, filter = filter, repository = svn2)
    print(len(node1), node1.max_date())
    print(len(node2), node2.max_date())

    res = node1.difference(node2, hash_size=hash_size)
    return res

print(__file__, "synchro", OutputPrint = __name__ == "__main__")
res = example (p1, p2)

source on GitHub

Defines a file, relative to a root.

Parameters:
  • root – root (it must exist)

  • file – file, if None, fill _children

  • filter – function (root, path, f, dir) –> True or False if this is a string, it will be converted into a regular expression (using re), and it will look into subfolders

  • level – hierarchy level

  • parent – link to the parent

  • repository – use SVN or GIT if True

  • log – log every explored folder

  • log1 – intermediate logs (first level)

  • fLOG – logging function to use

source on GitHub

__getitem__(i)[source]

returns the element i

Parameters:

i – element

Returns:

element

source on GitHub

__init__(root, file=None, filter=None, level=0, parent=None, repository=False, log=False, log1=False, fLOG=<function noLOG>)[source]

Defines a file, relative to a root.

Parameters:
  • root – root (it must exist)

  • file – file, if None, fill _children

  • filter – function (root, path, f, dir) –> True or False if this is a string, it will be converted into a regular expression (using re), and it will look into subfolders

  • level – hierarchy level

  • parent – link to the parent

  • repository – use SVN or GIT if True

  • log – log every explored folder

  • log1 – intermediate logs (first level)

  • fLOG – logging function to use

source on GitHub

__iter__()[source]

iterator on the element

Returns:

iterator on all contained files

source on GitHub

__len__()[source]

Returns the number of elements in this folder and in the subfolders.

source on GitHub

__str__()[source]

usual

source on GitHub

_default_not_ext = ['bbl', 'out', 'pyc', 'log', 'lib', 'ind', 'pdb', 'opt'][source]
_default_out = re.compile('([.]svn)|(hal.*[.]((exe)|(dll)|(so)|(sln)|(vcproj)))(.*[.]bbl$)|(.*[.]out$)|(.*[.]pyc$)|(.*[.]log$)|(.*[.]lib$)|(.*[.]ind$)|(.*[.]pdb$)|(.*[.]opt$)')[source]
_fill(filter, repository)[source]

look for subfolders

Parameters:
  • filter – boolean function

  • repository – use svn or git

source on GitHub

_fillstat()[source]

private: fill _type, _size

source on GitHub

static build_expression(ext)[source]

Builds a regular expression validating a list of extension.

Parameters:

ext – list of extension (with no points)

Returns:

pattern (string)

source on GitHub

copy_to(path, exc=True)[source]

Copies the file to path.

Parameters:
  • path – path

  • exc – catch exception when possible, warning otherwise

If the new path doe nots exist, it will be created.

Warning

If a file already exists at the new location, it checks the dates. The file is copied only if the new file is older.

source on GitHub

property date[source]

Returns the modification date.

source on GitHub

difference(node, hash_size=2097152, lower=False)[source]

Returns the differences with another folder.

Parameters:
  • node – other node

  • hash_size – above this size, it does not compute the hash key

  • lower – if True, every filename is converted into lower case

Returns:

list of [ (?, self._file, node (in self), node (in node)) ], see below for the choice of ?

The question mark ? means:
  • == no change

  • > more recent in self

  • < more recent in node

  • >+ absent in node

  • <+ absent in self

source on GitHub

exists()[source]

say if it does exist or not

Returns:

boolean

source on GitHub

property fullname[source]

Returns the full name.

source on GitHub

get()[source]

return a dictionary with some values which describe the file

Returns:

dict

source on GitHub

get_content(encoding='utf8')[source]

Returns the content of a text file.

Parameters:

encoding – encoding

Returns:

content as a string

source on GitHub

get_dict(lower=False)[source]

Returns a dictionary { self._file : node }.

Parameters:

lower – if True, every filename is converted into lower case

source on GitHub

get_fullname()[source]
Returns:

the full name

source on GitHub

hash_md5_readfile()[source]

Computes a hash of a file.

Returns:

string

source on GitHub

isdir()[source]

is it a folder?

Returns:

boolean

source on GitHub

isfile()[source]

is it a file?

Returns:

boolean

source on GitHub

max_date()[source]

return the more recent date

source on GitHub

property name[source]

Returns the file name from the root.

source on GitHub

nb_children()[source]

return the number of children

Returns:

int

source on GitHub

remove()[source]

Removes the file.

source on GitHub

repo_ls(path)[source]

call ls of an instance of SourceRepository

source on GitHub

property root[source]

Returns the root directory, the one used as a root for a synchronization.

source on GitHub

sign(node, hash_size)[source]

Returns ==, < or > according the dates if the size is not too big, if the sign is < or >, applies the hash method.

source on GitHub

property size[source]

Returns the size.

source on GitHub

property type[source]

Returns the file type (file or folder).

source on GitHub