module filehelper.file_tree_node
¶
Short summary¶
module pyquickhelper.filehelper.file_tree_node
a node which contains a file or a folder
Classes¶
class |
truncated documentation |
---|---|
Defines a node for a folder or a tree. Example: |
Properties¶
property |
truncated documentation |
---|---|
Returns the modification date. |
|
Returns the full name. |
|
Returns the file name from the root. |
|
Returns the root directory, the one used as a root for a synchronization. |
|
Returns the size. |
|
Returns the file type ( |
Static Methods¶
staticmethod |
truncated documentation |
---|---|
Builds a regular expression validating a list of extension. |
Methods¶
method |
truncated documentation |
---|---|
returns the element i |
|
Defines a file, relative to a root. |
|
iterator on the element |
|
Returns the number of elements in this folder and in the subfolders. |
|
usual |
|
look for subfolders |
|
private: fill _type, _size |
|
Copies the file to path. |
|
Returns the differences with another folder. |
|
say if it does exist or not |
|
return a dictionary with some values which describe the file |
|
Returns the content of a text file. |
|
Returns a dictionary |
|
Computes a hash of a file. |
|
is it a folder? |
|
is it a file? |
|
return the more recent date |
|
return the number of children |
|
Removes the file. |
|
call ls of an instance of |
|
Returns |
Documentation¶
a node which contains a file or a folder
- 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)
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
- __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
- _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
- 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)
- 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.
- 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
- get_content(encoding='utf8')[source]¶
Returns the content of a text file.
- Parameters:
encoding – encoding
- Returns:
content as a string
- get_dict(lower=False)[source]¶
Returns a dictionary
{ self._file : node }
.- Parameters:
lower – if True, every filename is converted into lower case
- repo_ls(path)[source]¶
call ls of an instance of
SourceRepository
- 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.