core.pathlib #
pathlib.
Is a generic way how to work with paths.
import freeflowuniverse.crystallib.core.pathlib
mut p:=pathlib.get('adir')!
p.is_dir()
pathlib
a flexible way how to find files.
import freeflowuniverse.crystallib.core.pathlib
mut p:=pathlib.get('/tmp/mysourcefiles')!
mut pathlist:=p.list(regex:[r'.*.md$'])! //this gets all files ending on .md
pathlist.copy(dest:'/tmp/mydest')!
Library:
fn find_common_ancestor #
fn find_common_ancestor(paths_ []string) string
recursively finds the least common ancestor of array of paths . will always return the absolute path (relative gets changed to absolute).
fn find_simple_common_ancestor #
fn find_simple_common_ancestor(paths_ []string) string
same as above but will treat symlinks as if normal links allowing finding relative paths between links as well QUESTION: should we merge with above?
fn get #
fn get(path_ string) Path
gets Path object, will check if it exists, is dir_file, ...
fn get_dir #
fn get_dir(args_ GetArgs) !Path
get a directory, or needs to be created if the dir doesn't exist and is not created, then there will be an error
fn get_file #
fn get_file(args_ GetArgs) !Path
fn get_link #
fn get_link(args_ GetArgs) !Path
fn get_no_check #
fn get_no_check(path_ string) Path
fn is_image #
fn is_image(path string) bool
fn path_equal #
fn path_equal(a_ string, b_ string) bool
case insentive check on paths
fn path_relative #
fn path_relative(source_ string, linkpath_ string) !string
recalc path between target & source . we only support if source_ is an existing dir, links will not be supported . a0 := pathlib.path_relative('$testpath/a/b/c', '$testpath/a/d.txt') or { panic(err) } . assert a0 == '../../d.txt' . a2 := pathlib.path_relative('$testpath/a/b/c', '$testpath/d.txt') or { panic(err) } . assert a2 == '../../../d.txt' . a8 := pathlib.path_relative('$testpath/a/b/c', '$testpath/a/b/c/d/e/e.txt') or { panic(err) } . assert a8 == 'd/e/e.txt' . symlinks will not be resolved, as it leads to unexpected behaviour
fn rsync #
fn rsync(args_ RsyncArgs) !
flexible tool to sync files from to, does even support ssh . args: .
source string
dest string
delete bool //do we want to delete the destination
ipaddr_src string //e.g. root@192.168.5.5:33 (can be without root@ or :port)
ipaddr_dst string //can only use src or dst, not both
ignore []string //arguments to ignore
ignore_default bool = true //if set will ignore a common set
stdout bool = true
.
fn rsync_cmd_options #
fn rsync_cmd_options(args_ RsyncArgs) !string
return the cmd with all rsync arguments . see rsync for usage of args
fn temp_write #
fn temp_write(args_ TMPWriteArgs) !string
write temp file and return path
fn template_write #
fn template_write(template_ string, dest string, overwrite bool) !
template is the text coming from template engine.
enum Category #
enum Category {
unknown
file
dir
linkdir
linkfile
}
enum JobErrorType #
enum JobErrorType {
error
nodir
notfound
wrongtype // asked for dir or file, but found other type
islink
}
enum UYN #
enum UYN {
unknown
yes
no
}
struct BackupArgs #
struct BackupArgs {
pub mut:
root string
dest string
overwrite bool
restore bool // if we want to find the latest one, if we can't find one then its error
}
struct CopyArgs #
struct CopyArgs {
pub mut:
dest string // path
delete bool // if true will remove files which are on dest which are not on source
rsync bool = true // we use rsync as default
ssh_target string // e.g. root@195.192.213.2:999
ignore []string // arguments to ignore e.g. ['*.pyc','*.bak']
ignore_default bool = true // if set will ignore a common set
}
struct GetArgs #
struct GetArgs {
pub mut:
path string
create bool
check bool = true // means will check the dir, link or file exists
empty bool // will empty the dir or the file
delete bool
}
struct ListArgs #
struct ListArgs {
pub mut:
regex []string
recursive bool = true
ignoredefault bool = true // ignore files starting with . and _
include_links bool // wether to include links in list
dirs_only bool
files_only bool
}
struct ListArgsInternal #
struct ListArgsInternal {
mut:
regex []regex.RE // only put files in which follow one of the regexes
recursive bool = true
ignoredefault bool = true // ignore files starting with . and _
dirs_only bool
files_only bool
include_links bool
}
struct MoveArgs #
struct MoveArgs {
pub mut:
dest string // path
delete bool // if true will remove files which are on dest which are not on source
chmod_execute bool
}
struct Path #
struct Path {
pub mut:
path string
cat Category
exist UYN
}
fn (Path) absolute #
fn (path Path) absolute() string
return absolute path . careful symlinks will not be resolved
fn (Path) backup #
fn (mut path Path) backup(args BackupArgs) !Path
create a backup, will maintain the extension
fn (Path) backup_path #
fn (mut path Path) backup_path(args BackupArgs) !Path
start from existing name and look for name.$nr.$ext, nr need to be unique, ideal for backups if dest "" then will use the directory of the fileitself + "/.backup" e.g. /code/myaccount/despiegk/somedir/test.v if would be backed up to /code/myaccount/despiegk/somedir/.backup/test.1.v root is the start of the dir we process e.g. /code/myaccount/despiegk/somedir/test.v if if source = /code/myaccount/despiegk and dest = /backup then the file will be backed up to /backup/somedir/test.1.v
struct BackupArgs{ root string dest string overwrite bool restore bool //if we want to find the latest one, if we can't find one then its error } if overwrite this means will overwrite the last one in the directory
fn (Path) backups_remove #
fn (mut path Path) backups_remove(args BackupArgs) !
fn (Path) check #
fn (mut path Path) check()
check the inside of pathobject, is like an init function
fn (Path) chmod #
fn (mut path Path) chmod(mode int) !
chmod change file access attributes of path to mode. Octals like 0o600 can be used.
fn (Path) chown #
fn (mut path Path) chown(owner int, group int) !
chown changes the owner and group attributes of path to owner and group.
fn (Path) copy #
fn (mut path Path) copy(args_ CopyArgs) !
copy file,dir is always recursive if ssh_target used then will copy over ssh e.g. . dest needs to be a directory or file . return Path of the destination file or dir .
fn (Path) delete #
fn (mut path Path) delete() !
delete
fn (Path) dir_exists #
fn (mut path Path) dir_exists(tofind string) bool
find dir underneith path, if exists return True
fn (Path) dir_get #
fn (mut path Path) dir_get(tofind string) !Path
find dir underneith path, return as Path
fn (Path) dir_get_new #
fn (mut path Path) dir_get_new(tofind string) !Path
get file, if not exist make new one
fn (Path) empty #
fn (mut path Path) empty() !
remove all content but if dir let the dir exist
fn (Path) exists #
fn (mut path Path) exists() bool
check path exists
fn (Path) expand #
fn (mut path Path) expand(dest string) !Path
uncompress to specified directory . if copy then will keep the original
fn (Path) extend #
fn (mut path Path) extend(parts ...string) !
extend the path, path stays same, no return if dir, needs to stay dir anything else fails
fn (Path) extend_dir_create #
fn (mut p Path) extend_dir_create(parts ...string) !Path
join parts to a path and return path, returns a new path, create if needed
fn (Path) extend_file #
fn (mut p Path) extend_file(name string) !Path
only works for a dir
fn (Path) extension #
fn (path Path) extension() string
returns extension without .
fn (Path) extension_lower #
fn (path Path) extension_lower() string
returns extension without and all lower case
fn (Path) file_exists #
fn (path Path) file_exists(tofind string) bool
find file underneith dir path, if exists return True
fn (Path) file_exists_ignorecase #
fn (mut path Path) file_exists_ignorecase(tofind string) bool
is case insensitive
fn (Path) file_get #
fn (mut path Path) file_get(tofind string) !Path
find file underneith path, if exists return as Path, otherwise error .
fn (Path) file_get_ignorecase #
fn (mut path Path) file_get_ignorecase(tofind string) !Path
fn (Path) file_get_new #
fn (mut path Path) file_get_new(tofind string) !Path
get file, if not exist make new one
fn (Path) getlink #
fn (mut path Path) getlink() !Path
return path object which is the result of the link (path link points too)
fn (Path) is_dir #
fn (mut path Path) is_dir() bool
fn (Path) is_dir_link #
fn (mut path Path) is_dir_link() bool
check is dir and a link
fn (Path) is_file #
fn (mut path Path) is_file() bool
is a file but no link
fn (Path) is_image #
fn (path Path) is_image() bool
fn (Path) is_image_jpg_png #
fn (path Path) is_image_jpg_png() bool
fn (Path) is_link #
fn (path Path) is_link() bool
fn (Path) link #
fn (mut path Path) link(linkpath string, delete_exists bool) !Path
path needs to be existing linkpath is where the link will be (the symlink who points to path)
fn (Path) link_exists #
fn (mut path Path) link_exists(tofind string) bool
find link underneith path, if exists return True is case insensitive
fn (Path) link_exists_ignorecase #
fn (mut path Path) link_exists_ignorecase(tofind string) bool
find link underneith path, if exists return True is case insensitive
fn (Path) link_get #
fn (mut path Path) link_get(tofind string) !Path
find link underneith path, return as Path, can only be one tofind is part of link name
fn (Path) list #
fn (mut path Path) list(args_ ListArgs) !PathList
list all files & dirs, follow symlinks . will sort all items . return as list of Paths . . params: .
regex []string
recursive bool // std off, means we recursive not over dirs by default
ignoredefault bool = true // ignore files starting with . and _
dirs_only bool
example see https://github.com/freeflowuniverse/crystallib/blob/development/examples/core/pathlib/examples/list/path_list.v
e.g. p.list(regex:[r'.*\.v$'])! //notice the r in front of string, this is regex for all files ending with .v
please note links are ignored for walking over dirstructure (for files and dirs)
fn (Path) md5 #
fn (mut path Path) md5() ![]u8
calculate md5 in reproducable way for directory as well as large file
fn (Path) md5hex #
fn (mut path Path) md5hex() !string
return in hex format
fn (Path) move #
fn (mut path Path) move(args MoveArgs) !
move to other location
dest string // path
delete bool // if true will remove files which are on dest which are not on source
fn (Path) moveup #
fn (mut path Path) moveup() !
the path will move itself up 1 level . the e.g. /tmp/rclone/rclone-v1.64.2-linux-amd64/ -> /tmp/rclone
fn (Path) moveup_single_subdir #
fn (mut path Path) moveup_single_subdir() !
the path will move itself up 1 level . e.g. path is /tmp/rclone and there is /tmp/rclone/rclone-v1.64.2-linux-amd64 . that last dir needs to move 1 up
fn (Path) name #
fn (path Path) name() string
returns name with extension
fn (Path) name_ends_with_underscore #
fn (mut path Path) name_ends_with_underscore() bool
fn (Path) name_fix_keepext #
fn (mut path Path) name_fix_keepext() string
return name with all lowercase_special chars done but keep extension
fn (Path) name_fix_no_ext #
fn (mut path Path) name_fix_no_ext() string
fn (Path) name_fix_no_underscore_no_ext #
fn (mut path Path) name_fix_no_underscore_no_ext() string
return name with all lowercase_special chars done and also no extension
fn (Path) name_no_ext #
fn (mut path Path) name_no_ext() string
QUESTION: should this mutate path's name, probably not?
fn (Path) parent #
fn (path Path) parent() !Path
find parent of path
fn (Path) parent_find #
fn (path Path) parent_find(tofind string) !Path
walk upwards starting from path untill dir or file tofind is found works recursive
fn (Path) path_dir #
fn (mut path Path) path_dir() string
full path of dir
fn (Path) path_get_name_with_underscore #
fn (mut path Path) path_get_name_with_underscore() string
return a path which has name ending with _
fn (Path) path_no_ext #
fn (mut path Path) path_no_ext() string
fn (Path) path_normalize #
fn (mut path Path) path_normalize() !bool
will rewrite the path to lower_case if not the case yet will also remove weird chars if changed will return true the file will be moved to the new location
fn (Path) path_relative #
fn (path Path) path_relative(destpath string) !string
get relative path in relation to destpath . will not resolve symlinks
fn (Path) read #
fn (mut path Path) read() !string
read content from file
fn (Path) readb #
fn (mut path Path) readb() ![]u8
read bytes from file
fn (Path) readlink #
fn (mut path Path) readlink() !string
return string
fn (Path) realpath #
fn (path Path) realpath() string
return absolute path . careful the symlinks will be followed !!!
fn (Path) recursive_text #
fn (mut path Path) recursive_text() ![]string
get all text for path and underneith (works for dir & file)
fn (Path) relink #
fn (mut path Path) relink() !
will make sure that the link goes from file with largest path to smallest good to make sure we have links always done in same way
fn (Path) rename #
fn (mut path Path) rename(name string) !
rename the file or directory
fn (Path) restore #
fn (mut path Path) restore(args BackupArgs) !
fn (Path) rm #
fn (mut path Path) rm() !
delete
fn (Path) scan #
fn (mut path Path) scan(mut parameters paramsparser.Params, filters []Filter0, executors []Executor0) !paramsparser.Params
the filters are function which needs to return true if to process with alle executors . see https://github.com/freeflowuniverse/crystallib/blob/development/examples/core/pathlib/examples/scanner/path_scanner.v . if any of the filters returns false then we don't continue . if we return True then it means the dir or file is processed . . type Filter0 = fn (mut Path, mut paramsparser.Params) bool type Executor0 = fn (mut Path, mut paramsparser.Params) !paramsparser.Params
fn (Path) sha256 #
fn (mut path Path) sha256() !string
return sha256 hash of a file
fn (Path) shortpath #
fn (path Path) shortpath() string
fn (Path) size #
fn (mut path Path) size() !f64
fn (Path) size_kb #
fn (mut path Path) size_kb() !int
fn (Path) sub_exists #
fn (mut path Path) sub_exists(args_ SubGetParams) !bool
will check if dir exists params: .- name
- name_fix_find bool :means we will also find if name is same as the name_fix .
- name_fix bool :if file found and name fix was different than file on filesystem, will rename .
- dir_ensure bool :if dir_ensure on will fail if its not a dir .
- file_ensure bool :if file_ensure on will fail if its not a dir .
fn (Path) sub_get #
fn (mut path Path) sub_get(args_ SubGetParams) !Path
will get dir or file underneith a dir . e.g. mypath.sub_get(name:"mysub_file.md",name_fix_find:true,name_fix:true)! . this will find Mysubfile.md as well as mysub_File.md and rename to mysub_file.md and open . params: .- name .
- name_fix_find bool :means we will also find if name is same as the name_fix.
- name_fix bool :if file found and name fix was different than file on filesystem, will rename .
- dir_ensure bool :if dir_ensure on will fail if its not a dir .
- file_ensure bool :if file_ensure on will fail if its not a dir .. will return SubGetError if error .
returns a path
fn (Path) template_write #
fn (mut path Path) template_write(template_ string, overwrite bool) !
fn (Path) unlink #
fn (mut path Path) unlink() !
resolve link to the real content copy the target of the link to the link
fn (Path) write #
fn (mut path Path) write(content string) !
write content to the file, check is file if the path is a link to a file then will change the content of the file represented by the link
fn (Path) writeb #
fn (mut path Path) writeb(content []u8) !
write bytes to file
struct PathList #
struct PathList {
pub mut:
// is the root under which all paths are, think about it like a changeroot environment
root string
paths []Path
}
the result of pathlist
fn (PathList) copy #
fn (mut pathlist PathList) copy(dest string) !
copy all
fn (PathList) delete #
fn (mut pathlist PathList) delete() !
delete all
struct RsyncArgs #
struct RsyncArgs {
pub mut:
source string
dest string
ipaddr_src string // e.g. root@192.168.5.5:33 (can be without root@ or :port)
ipaddr_dst string
delete bool // do we want to delete the destination
ignore []string // arguments to ignore e.g. ['*.pyc','*.bak']
ignore_default bool = true // if set will ignore a common set
debug bool = true
fast_rsync bool
}
struct SubGetError #
struct SubGetError {
Error
pub mut:
msg string
path string
error_type JobErrorType
}
An internal struct for representing failed jobs.
fn (SubGetError) msg #
fn (err SubGetError) msg() string
fn (SubGetError) code #
fn (err SubGetError) code() int
struct SubGetParams #
struct SubGetParams {
pub mut:
name string
name_fix_find bool // means we will also find if name is same as the name_fix
name_fix bool // if file found and name fix was different than file on filesystem, will rename
dir_ensure bool // if dir_ensure on will fail if its not a dir
file_ensure bool // if file_ensure on will fail if its not a dir
}
struct TMPWriteArgs #
struct TMPWriteArgs {
pub mut:
name string // optional name to remember it more easily
tmpdir string
text string // text to put in file
path string // to overrule the path where script will be stored
ext string = 'sh'
}
- README
- fn find_common_ancestor
- fn find_simple_common_ancestor
- fn get
- fn get_dir
- fn get_file
- fn get_link
- fn get_no_check
- fn is_image
- fn path_equal
- fn path_relative
- fn rsync
- fn rsync_cmd_options
- fn temp_write
- fn template_write
- enum Category
- enum JobErrorType
- enum UYN
- struct BackupArgs
- struct CopyArgs
- struct GetArgs
- struct ListArgs
- struct ListArgsInternal
- struct MoveArgs
- struct Path
- fn absolute
- fn backup
- fn backup_path
- fn backups_remove
- fn check
- fn chmod
- fn chown
- fn copy
- fn delete
- fn dir_exists
- fn dir_get
- fn dir_get_new
- fn empty
- fn exists
- fn expand
- fn extend
- fn extend_dir_create
- fn extend_file
- fn extension
- fn extension_lower
- fn file_exists
- fn file_exists_ignorecase
- fn file_get
- fn file_get_ignorecase
- fn file_get_new
- fn getlink
- fn is_dir
- fn is_dir_link
- fn is_file
- fn is_image
- fn is_image_jpg_png
- fn is_link
- fn link
- fn link_exists
- fn link_exists_ignorecase
- fn link_get
- fn list
- fn md5
- fn md5hex
- fn move
- fn moveup
- fn moveup_single_subdir
- fn name
- fn name_ends_with_underscore
- fn name_fix_keepext
- fn name_fix_no_ext
- fn name_fix_no_underscore_no_ext
- fn name_no_ext
- fn parent
- fn parent_find
- fn path_dir
- fn path_get_name_with_underscore
- fn path_no_ext
- fn path_normalize
- fn path_relative
- fn read
- fn readb
- fn readlink
- fn realpath
- fn recursive_text
- fn relink
- fn rename
- fn restore
- fn rm
- fn scan
- fn sha256
- fn shortpath
- fn size
- fn size_kb
- fn sub_exists
- fn sub_get
- fn template_write
- fn unlink
- fn write
- fn writeb
- struct PathList
- struct RsyncArgs
- struct SubGetError
- struct SubGetParams
- struct TMPWriteArgs