Package Gnumed :: Package pycommon :: Module gmTools
[frames] | no frames]

Module gmTools

source code

GNUmed general tools.


Author: K. Hilbert <Karsten.Hilbert@gmx.net>

License: GPL v2 or later (details at http://www.gnu.org)

Classes
  gmPaths
This class provides the following paths:
Functions
 
handle_uncaught_exception_console(t, v, tb) source code
 
mkdir(directory=None, mode=None)
Create directory.
source code
 
rmdir(directory) source code
 
rm_dir_content(directory) source code
 
mk_sandbox_dir(prefix=None, base_dir=None) source code
 
parent_dir(directory) source code
 
dirname_stem(directory) source code
 
dir_is_empty(directory=None) source code
 
copy_tree_content(directory, target_directory)
Copy the *content* of <directory> *into* <target_directory> which is created if need be.
source code
 
recode_file(source_file=None, target_file=None, source_encoding='utf8', target_encoding=None, base_dir=None, error_mode='replace') source code
 
unzip_archive(archive_name, target_dir=None, remove_archive=False) source code
 
remove_file(filename, log_error=True, force=False) source code
 
file2md5(filename=None, return_hex=True) source code
 
file2chunked_md5(filename=None, chunk_size=524288000) source code
 
unicode_csv_reader(unicode_csv_data, dialect=??, encoding='utf-8', **kwargs) source code
 
old_unicode2charset_encoder(unicode_csv_data, encoding='utf-8') source code
 
old_unicode_csv_reader(unicode_csv_data, dialect=??, encoding='utf-8', **kwargs) source code
 
fname_sanitize(filename)
Normalizes unicode, removes non-alpha characters, converts spaces to underscores.
source code
 
fname_stem(filename)
/home/user/dir/filename.ext -> filename
source code
 
fname_stem_with_path(filename)
/home/user/dir/filename.ext -> /home/user/dir/filename
source code
 
fname_extension(filename=None, fallback=None)
/home/user/dir/filename.ext -> .ext '' or '.' -> fallback if any else ''
source code
 
fname_dir(filename) source code
 
fname_from_path(filename) source code
 
get_unique_filename(prefix=None, suffix=None, tmp_dir=None, include_timestamp=False)
This function has a race condition between its file.close() and actually using the filename in callers.
source code
 
mklink(physical_name, link_name, overwrite=False) source code
 
import_module_from_directory(module_path=None, module_name=None, always_remove_path=False)
Import a module from any location.
source code
 
size2str(size=0, template='%s') source code
 
bool2subst(boolean=None, true_return=True, false_return=False, none_return=None) source code
 
bool2str(boolean=None, true_str='True', false_str='False') source code
 
none_if(value=None, none_equivalent=None, strip_string=False)
Modelled after the SQL NULLIF function.
source code
 
coalesce(value2test=None, return_instead=None, template4value=None, template4instead=None, none_equivalents=None, function4value=None, value2return=None)
Modelled after the SQL coalesce function.
source code
 
capitalize(text=None, mode=4)
Capitalize the first character but leave the rest alone.
source code
 
input2decimal(initial=None) source code
 
input2int(initial=None, minval=None, maxval=None) source code
 
strip_prefix(text, prefix, remove_repeats=False, remove_whitespace=False) source code
 
decorate_window_title(title) source code
 
undecorate_window_title(title) source code
 
strip_suffix(text, suffix, remove_repeats=False, remove_whitespace=False) source code
 
strip_leading_empty_lines(lines=None, text=None, eol='\n', return_list=True) source code
 
strip_trailing_empty_lines(lines=None, text=None, eol='\n', return_list=True) source code
 
strip_empty_lines(lines=None, text=None, eol='\n', return_list=True) source code
 
list2text(lines, initial_indent='', subsequent_indent='', eol='\n', strip_leading_empty_lines=True, strip_trailing_empty_lines=True, strip_trailing_whitespace=True, max_line_width=None) source code
 
wrap(text=None, width=None, initial_indent='', subsequent_indent='', eol='\n')
A word-wrap function that preserves existing line breaks and most spaces in the text.
source code
 
unwrap(text=None, max_length=None, strip_whitespace=True, remove_empty_lines=True, line_separator=' // ') source code
 
shorten_text(text=None, max_length=None) source code
 
shorten_words_in_line(text=None, max_length=None, min_word_length=None, ignore_numbers=True, ellipsis='\\u2026') source code
 
xml_escape_string(text=None)
check for special XML characters and transform them
source code
 
tex_escape_string(text=None, replace_known_unicode=True, replace_eol=False, keep_visual_eol=False)
Check for special TeX characters and transform them.
source code
 
rst2latex_snippet(rst_text) source code
 
rst2html(rst_text, replace_eol=False, keep_visual_eol=False) source code
 
xetex_escape_string(text=None) source code
 
html_escape_string(text=None, replace_eol=False, keep_visual_eol=False) source code
 
dict2json(obj) source code
 
json_serialize(obj) source code
 
compare_dict_likes(d1, d2, title1=None, title2=None) source code
 
format_dict_likes_comparison(d1, d2, title_left=None, title_right=None, left_margin=0, key_delim=' || ', data_delim=' | ', missing_string='=/=', difference_indicator='! ', ignore_diff_in_keys=None) source code
 
format_dict_like(d, relevant_keys=None, template=None, missing_key_template='<[%(key)s] MISSING>', left_margin=0, tabular=False, value_delimiters=('>>>', '<<<'), eol='\n', values2ignore=None) source code
 
dicts2table(dict_list, left_margin=0, eol='\n', keys2ignore=None, headers=None, show_only_changes=False, equality_value='<=>', date_format=None) source code
 
normalize_dict_like(d, required_keys, missing_key_template='<[%(key)s] MISSING>') source code
 
enumerate_removable_partitions() source code
 
enumerate_optical_writers() source code
 
prompted_input(prompt=None, default=None)
Obtains entry from standard input.
source code
 
get_icon(wx=None) source code
 
create_qrcode(text=None, filename=None, qr_filename=None, verbose=False) source code
Variables
  __doc__ = """GNUmed general tools."""
  du_core = None
  u_currency_pound = '\\u00A3'
  u_currency_sign = '\\u00A4'
  u_currency_yen = '\\u00A5'
  u_right_double_angle_quote = '\\u00AB'
  u_registered_trademark = '\\u00AE'
  u_plus_minus = '\\u00B1'
  u_superscript_one = '\\u00B9'
  u_left_double_angle_quote = '\\u00BB'
  u_one_quarter = '\\u00BC'
  u_one_half = '\\u00BD'
  u_three_quarters = '\\u00BE'
  u_multiply = '\\u00D7'
  u_greek_ALPHA = '\\u0391'
  u_greek_alpha = '\\u03b1'
  u_greek_OMEGA = '\\u03A9'
  u_greek_omega = '\\u03c9'
  u_dagger = '\\u2020'
  u_triangular_bullet = '\\u2023'
  u_ellipsis = '\\u2026'
  u_euro = '\\u20AC'
  u_numero = '\\u2116'
  u_down_left_arrow = '\\u21B5'
  u_left_arrow = '\\u2190'
  u_up_arrow = '\\u2191'
  u_arrow2right = '\\u2192'
  u_down_arrow = '\\u2193'
  u_left_arrow_with_tail = '\\u21a2'
  u_arrow2right_from_bar = '\\u21a6'
  u_arrow2right_until_vertical_bar = '\\u21e5'
  u_sum = '\\u2211'
  u_almost_equal_to = '\\u2248'
  u_corresponds_to = '\\u2258'
  u_infinity = '\\u221E'
  u_arrow2right_until_vertical_bar2 = '\\u2b72'
  u_diameter = '\\u2300'
  u_checkmark_crossed_out = '\\u237B'
  u_box_horiz_high = '\\u23ba'
  u_box_vert_left = '\\u23b8'
  u_box_vert_right = '\\u23b9'
  u_space_as_open_box = '\\u2423'
  u_box_horiz_single = '\\u2500'
  u_box_vert_light = '\\u2502'
  u_box_horiz_light_3dashes = '\\u2504'
  u_box_vert_light_4dashes = '\\u2506'
  u_box_horiz_4dashes = '\\u2508'
  u_box_T_right = '\\u251c'
  u_box_T_left = '\\u2524'
  u_box_T_down = '\\u252c'
  u_box_T_up = '\\u2534'
  u_box_plus = '\\u253c'
  u_box_top_double = '\\u2550'
  u_box_top_left_double_single = '\\u2552'
  u_box_top_right_double_single = '\\u2555'
  u_box_top_left_arc = '\\u256d'
  u_box_top_right_arc = '\\u256e'
  u_box_bottom_right_arc = '\\u256f'
  u_box_bottom_left_arc = '\\u2570'
  u_box_horiz_light_heavy = '\\u257c'
  u_box_horiz_heavy_light = '\\u257e'
  u_skull_and_crossbones = '\\u2620'
  u_caduceus = '\\u2624'
  u_frowning_face = '\\u2639'
  u_smiling_face = '\\u263a'
  u_black_heart = '\\u2665'
  u_female = '\\u2640'
  u_male = '\\u2642'
  u_male_female = '\\u26a5'
  u_chain = '\\u26d3'
  u_checkmark_thin = '\\u2713'
  u_checkmark_thick = '\\u2714'
  u_heavy_greek_cross = '\\u271a'
  u_arrow2right_thick = '\\u2794'
  u_writing_hand = '\\u270d'
  u_pencil_1 = '\\u270e'
  u_pencil_2 = '\\u270f'
  u_pencil_3 = '\\u2710'
  u_latin_cross = '\\u271d'
  u_arrow2right_until_black_diamond = '\\u291e'
  u_kanji_yen = '\\u5186'
  u_replacement_character = '\\ufffd'
  u_link_symbol = '\\u1f517'
  default_csv_reader_rest_key = 'list_of_values_of_unknown_fields'
  CAPS_ALLCAPS = 2
  CAPS_FIRST = 1
  CAPS_FIRST_ONLY = 5
  CAPS_NAMES = 4
  CAPS_NONE = 0
  CAPS_WORDS = 3
  __package__ = 'Gnumed.pycommon'

Imports: sys, os, csv, tempfile, logging, hashlib, platform, subprocess, decimal, getpass, io, functools, json, shutil, zipfile, pydt, regex, xml_tools, pickle, zlib, gmBorg


Function Details

mkdir(directory=None, mode=None)

source code 
Create directory.

- creates parent dirs if necessary
- does not fail if directory exists
<mode>: numeric, say 0o0700  for "-rwx------"

get_unique_filename(prefix=None, suffix=None, tmp_dir=None, include_timestamp=False)

source code 
This function has a race condition between
                its file.close()
   and actually
                using the filename in callers.

The file will NOT exist after calling this function.

coalesce(value2test=None, return_instead=None, template4value=None, template4instead=None, none_equivalents=None, function4value=None, value2return=None)

source code 
Modelled after the SQL coalesce function.

To be used to simplify constructs like:

        if value2test is None (or in none_equivalents):
                value = (template4instead % return_instead) or return_instead
        else:
                value = (template4value % value2test) or value2test
        print value

@param value2test: the value to be tested for <None>
@type value2test: any Python type, must have a __str__ method if template4value is not None
@param return_instead: the value to be returned if <initial> is None
@type return_instead: any Python type, must have a __str__ method if template4instead is not None
@param template4value: if <initial> is returned replace the value into this template, must contain one <%s> 
@type template4value: string or None
@param template4instead: if <return_instead> is returned replace the value into this template, must contain one <%s> 
@type template4instead: string or None

example:
        function4value = ('strftime', '%Y-%m-%d')

Ideas:
        - list of return_insteads: initial, [return_instead, template], [return_instead, template], [return_instead, template], template4value, ...

capitalize(text=None, mode=4)

source code 

Capitalize the first character but leave the rest alone.

Note that we must be careful about the locale, this may have issues ! However, for UTF strings it should just work.

wrap(text=None, width=None, initial_indent='', subsequent_indent='', eol='\n')

source code 
A word-wrap function that preserves existing line breaks
        and most spaces in the text. Expects that existing line
        breaks are posix newlines (
).
        

tex_escape_string(text=None, replace_known_unicode=True, replace_eol=False, keep_visual_eol=False)

source code 
Check for special TeX characters and transform them.

                replace_eol:
                        replaces "
" with "\newline"
                keep_visual_eol:
                        replaces "
" with "\newline 
" such that
                        both LaTeX will know to place a line break
                        at this point as well as the visual formatting
                        is preserved in the LaTeX source (think multi-
                        row table cells)
        

prompted_input(prompt=None, default=None)

source code 

Obtains entry from standard input.

prompt: Prompt text to display in standard output default: Default value (for user to press enter only) CTRL-C: aborts and returns None