fileops¶
This modules provides casting features, that is to force the output type
bzcat¶
- class
textops.
bzcat
(context={})¶Uncompress the bz2 file(s) with the name(s) given in input text
If a context dict is specified, the path is formatted with that context (str.format) The gzipped file must have a textual content.
Parameters: context (dict) – The context to format the file path (Optionnal) Examples
>>> '/var/log/dmesg' | cat() | grep('error') | togzfile('/tmp/errors.log.gz')Note
A list of filename can be given as input text : all specified files will be uncompressed
cat¶
- class
textops.
cat
(context={})¶Return the content of the file with the path given in the input text
If a context dict is specified, the path is formatted with that context (str.format) The file must have a textual content.
Parameters: Yields: str – the file content lines
Examples
>>> open('/tmp/testfile.txt','w').write('here is the file content') 24 >>> '/tmp/testfile.txt' | cat() #doctest: +ELLIPSIS <generator object extend_type_gen at ...> >>> '/tmp/testfile.txt' | cat().tostr() 'here is the file content' >>> '/tmp/testfile.txt' >> cat() ['here is the file content'] >>> '/tmp/testfile.txt' | cat().upper().tostr() 'HERE IS THE FILE CONTENT' >>> context = {'path':'/tmp/'} >>> '{path}testfile.txt' | cat(context) #doctest: +ELLIPSIS <generator object extend_type_gen at ...> >>> '{path}testfile.txt' | cat(context).tostr() 'here is the file content' >>> cat('/tmp/testfile.txt').s 'here is the file content' >>> cat('/tmp/testfile.txt').upper().s 'HERE IS THE FILE CONTENT' >>> cat('/tmp/testfile.txt').l ['here is the file content'] >>> cat('/tmp/testfile.txt').g #doctest: +ELLIPSIS <generator object extend_type_gen at ...> >>> for line in cat('/tmp/testfile.txt'): ... print(line) ... here is the file content >>> for bits in cat('/tmp/testfile.txt').grep('content').cut(): ... print(bits) ... ['here', 'is', 'the', 'file', 'content'] >>> open('/tmp/testfile.txt','w').write('here is the file content\nanother line') 37 >>> '/tmp/testfile.txt' | cat().tostr() 'here is the file content\nanother line' >>> '/tmp/testfile.txt' | cat().tolist() ['here is the file content', 'another line'] >>> cat('/tmp/testfile.txt').s 'here is the file content\nanother line' >>> cat('/tmp/testfile.txt').l ['here is the file content', 'another line'] >>> context = {'path': '/tmp/'} >>> cat('/{path}/testfile.txt',context).l ['here is the file content', 'another line'] >>> for bits in cat('/tmp/testfile.txt').grep('content').cut(): ... print(bits) ... ['here', 'is', 'the', 'file', 'content']
find¶
- class
textops.
find
(pattern='*', context={}, only_files=False, only_dirs=False)¶Return a list of files/dirs matching a pattern
find recursively files/dirs matching a pattern. The pattern is a unix-like pattern, it searches only against the last part of the path (basename)
Parameters: Yields: str – file name matching the pattern
Examples
To come …
findre¶
- class
textops.
findre
(pattern='', context={}, only_files=False, only_dirs=False)¶Return a list of files/dirs matching a pattern
find recursively files/dirs matching a pattern. The pattern is a python regex, it searches against the whole file path
Parameters: Yields: str – file name matching the pattern
Examples
To come …
gzcat¶
- class
textops.
gzcat
(context={})¶Uncompress the gzfile(s) with the name(s) given in input text
If a context dict is specified, the path is formatted with that context (str.format) The gzipped file must have a textual content.
Parameters: context (dict) – The context to format the file path (Optionnal) Note
A list of filename can be given as input text : all specified files will be uncompressed
Note
Password encrypted zip creation is not supported.
ls¶
- class
textops.
ls
(pattern='*', context={}, only_files=False, only_dirs=False)¶Return a list of files/dirs
it uses the python
glob.glob()
so it will do a Unix style pathname pattern expansion
Parameters: Yields: str – file name matching the pattern
Examples
To come …
replacefile¶
- class
textops.
replacefile
(filename, mode='w', newline='n')¶send input to file
Works like
textops.tofile
except it takes care to consume input text generators before writing the file. This is mandatory when doing some in-file textops. The drawback is that the data to write to file is stored temporarily in memory.This does not work:
cat('myfile').sed('from_patter','to_pattern').tofile('myfile').nThis works:
cat('myfile').sed('from_patter','to_pattern').replacefile('myfile').n
Parameters: Examples
>>> cat('myfile').sed('from_patter','to_pattern').replacefile('myfile').n
stats¶
teefile¶
- class
textops.
teefile
(filename, mode='w', newline='n')¶send input to file AND yield the same input text
Parameters: Yields: str, list or dict – the same input text
Examples
>>> '/var/log/dmesg' | cat() | teefile('/tmp/dmesg_before') | grep('error') | tofile('/tmp/dmesg_after')
tobz2file¶
- class
textops.
tobz2file
(filename, mode='w', newline='n')¶send input to gz file
tobz2file()
must be the last text operation
Parameters: Examples
>>> '/var/log/dmesg' | cat() | grep('error') | togzfile('/tmp/errors.log.gz')Note
Password encrypted zip creation is not supported.
tofile¶
- class
textops.
tofile
(filename, mode='w', newline='n')¶send input to file
tofile()
must be the last text operation, if you want to write to file AND continue some text operations, usetextops.teefile
instead. if you want to write the same file than the one opened, please usetextops.replacefile
instead.
Parameters: Examples
>>> '/var/log/dmesg' | cat() | grep('error') | tofile('/tmp/errors.log')
togzfile¶
- class
textops.
togzfile
(filename, mode='wb', newline='n')¶send input to gz file
togzfile()
must be the last text operation
Parameters: Examples
>>> '/var/log/dmesg' | cat() | grep('error') | togzfile('/tmp/errors.log.gz')Note
Password encrypted zip creation is not supported.
tozipfile¶
- class
textops.
tozipfile
(filename, member, mode='w', newline='n')¶send input to zip file
tozipfile()
must be the last text operation.
Parameters:
- filename (str) – The zip file to send COMPRESSED output to
- member (str) – The name of the file INSIDE the zip file to send UNCOMPRESSED output to
- mode (str) – File open mode (Default : ‘w’, use ‘a’ to append an existing zip or create it if not present)
- newline (str) – The newline string to add for each line (default: ‘n’)
Examples
>>> '/var/log/dmesg' | cat() | grep('error') | tozipfile('/tmp/errors.log.zip','/tmp/errors.log')Note
Password encrypted zip creation is not supported.
unzip¶
- class
textops.
unzip
(member, topath=None, password=None, context={}, ignore=False)¶Extract ONE file from a zip archive
The zip file name is taken from text input.
Parameters:
- member (str) – the file name to extract from the zip archive
- topath (str) – the directory path to extract to (Default : current directory)
- password (str) – The password to open zip if it is encrypted (Optionnal)
- context (dict) – The context to format the file path and topath argument (Optionnal)
- ignore (bool) – If True do not raise exception when member does not exist (Default : False)
Yields: str – the member file path
Examples
To come …
unzipre¶
- class
textops.
unzipre
(member_regex, topath=None, password=None, context={}, ignore=False)¶Extract files having a specified name pattern from a zip archive
The zip file name is taken from text input.
Parameters:
- member_regex (str or regex) – the regex to find the first file inside the zip to read
- topath (str) – the directory path to extract to (Default : current directory)
- password (str) – The password to open zip if it is encrypted (Optionnal)
- context (dict) – The context to format the file path and topath argument (Optionnal)
Yields: str – the extracted files path
Examples
To come …
zipcat¶
- class
textops.
zipcat
(member, context={}, password=None)¶Return the content of the zip file with the path given in the input text
If a context dict is specified, the path is formatted with that context (str.format)
Parameters: Yields: str – the file content lines
Examples
To come …
zipcatre¶
- class
textops.
zipcatre
(member_regex, context={}, password=None)¶Return the content of the zip file with the path given in the input text
If a context dict is specified, the path is formatted with that context (str.format) Works like
textops.zipcat
except that the member name is a regular expression : it will cat all member matching the regex
Parameters: Yields: str – the file content lines
Examples
To come …