io_helpers – Helpers for IO activities

pyhetdex.tools.io_helpers.count_lines(ios)[source]

Count the lines in a open file. After counting resets the file position to the original one

Parameters:
ios: file object
Returns:
lines: int

number of lines

pyhetdex.tools.io_helpers.eat_to_char(ios, c)[source]

Advance the file position one character at a time until the desired one is found.

Parameters:
ios: file object
c : character

single character to find

Returns:
ch : character

character found or last character if not found

pyhetdex.tools.io_helpers.eat_to_blockstart(ios)[source]

Advance the file position to the first non empty character after a [

Parameters:
ios: file object
Returns:
ch : character

first non empty character

pyhetdex.tools.io_helpers.read_to_char(ios, c, skipnewline=True)[source]

Read the file until the desired character is found.

Parameters:
ios: file object
c : character

single character to find

skipnewline : bool, optional

if True converts new lines to empty spaces

Returns:
result : string

all the read content until c excluded

pyhetdex.tools.io_helpers.skip_commentlines(ios)[source]

Reads one line at a time and returns the first non-empty or non-comment

Parameters:
ios: file object
Returns:
line : string
pyhetdex.tools.io_helpers.duplicates(l)[source]

Search for duplicates

Parameters:
l : iterable

iterable and sortable object in which search for duplicates

Returns:
list

sorted list of duplicate items in l

pyhetdex.tools.io_helpers._duplicates(l)[source]
pyhetdex.tools.io_helpers.unique(seq, idfun=None)[source]

Order preserving unique algorithm

Todo

see if it makes sense to use numpy.unique()

Parameters:
seq : iterable

sequence to order

idfun : callable

function to call for the ordering (??)

Returns:
result : list

unique elements

pyhetdex.tools.io_helpers.ask_yes_no(message)[source]

Ask the user message and expect y or n as answer.

The string `` (y/[n])`` is appended to the message. EOF (ctrl+D) and empty string are interpreted as n

Parameters:
message : string

message to print to screen

Returns:
is_yes : bool

whether the answer is y

pyhetdex.tools.io_helpers.decode(bytes_)[source]

In python 3 decodes bytes into string, in python 2 returns the input

Parameters:
bytes_ : byte type

string to encode

Returns:
string
pyhetdex.tools.io_helpers.get_resource_file(name, filename)[source]

Get the file from the package using setuptools resource access and decode it.

Parameters:
name : string

name of the package. Typically is the __name__ variable of the module calling the function

filename : string

name of the file relative to name

Returns:
file_content : string

content of the file

class pyhetdex.tools.io_helpers.CopyResource(name, backup=False, force=False, verbose=False)[source]

Copy the given list of resource files.

Parameters:
name : string

name of the package. Typically is the __name__ variable of the module calling the function

backup : bool, optional

existing files are backed-up before copying the new ones

force : bool, optional

force overwriting if the file already exists; override backup

verbose : bool, optional

activate verbose printing

Attributes:
name : string

same as input

backup, force, verbose : bool

same as input

written_files, non_written_files, backed_up_files : list of strings

name of the files that has been written, not written or backed-up

filename : string

name of the file to copy; set in __call__() for each element in flist

target_dir, reldir : strings

same as in __call__()

__call__(flist, target_dir, reldir='.')[source]

Copy the given list of resource files.

If one of the files already exists on the destination one of the following cases happens:

  • the user is asked whether she/he wants to replace the file;
  • if force is True: the file is replaced without asking;
  • if backup is True: the destination file is copied to a new file, to which name ".bkp" is appended, and then replaced; overridden by force
Parameters:
flist : list of strings

list of files to copy relative to name

target_dir : string

directory where to copy the files

reldir : string, optional

if the files to copy are into a reldir directory with respect to name, this will path will be removed form the destination path. E.g. if reldir = "static" directory and flist = ["static/my_file.cfg", ] and the file will be copied into "target_dir/my_file.cfg". If reldir is not given the file will be copied into "target_dir/static/my_file.cfg". reldir must be at the beginning of the file names or will be ignored.

report(header_written='Copied files: ', header_non_written='Skipped files: ', header_backedup='Backed-up files: ')[source]

Print the result of the files copied by __call__().

Parameters:
header_written, header_non_written, header_backedup : strings

header to use when printing the results stored in written_files, non_written_files and backed_up_files.

_rel_filename(filename, reldir)[source]

Create the relative target file name

Parameters:
filename : string

file name

reldir : string

directory name

Returns:
rel_filename : string

relative file name

_target_file(filename, target_dir)[source]

Split the file name, create the target directory and return the name of the target file.

Parameters:
filename : string

name of the file name

target_dir : string

directory where to copy the files

Returns:
string

name of the target file

manipulate_resource(resource)[source]

Manipulate the resource string and return it.

The default implementation does nothing. Override this method in derived classes to operate on resource

Parameters:
resource : string

string to manipulate

Returns:
string

same as input

pyhetdex.tools.io_helpers.copy_resources(name, flist, target_dir, reldir='.', backup=False, force=False, replace_func=None, verbose=False)[source]

Copy the given list of resource files.

This function is a wrapper around CopyResource and CopyResource.__call__()

Parameters:
name, backup, force, verbose

see CopyResource

flist, target_dir, reldir

see CopyResource.__call__()

Returns:
written_files, non_written_files, backed_up_files : list of strings

name of the files that has been written, not written or backed-up

pyhetdex.tools.io_helpers.print_list(header, list_)[source]

If list_ is not empty, print it after header and indented accordingly.

>>> printed = print_list('test header: ', ['entry1', 'entry2', 'entry3',
...                                        'entry4', 'entry5', 'entry6',
...                                        'entry7', 'entry8'])
test header: entry1, entry2, entry3, entry4, entry5, entry6, entry7,
             entry8
>>> printed
True
Parameters:
header : string

header to put in the first line and to use to indent the subsequent lines. It can contain ANSI escape characters for, e.g. coloring

list_ : list of strings

the list is joined with commas and printed after header

Returns:
printed : bool

whether something has been printed