pycrossword  0.3
Pure-Python implementation of a crossword puzzle generator and editor
Classes | Functions | Variables
pycross.utils.utils Namespace Reference

Classes

class  JsonHiliter
 Syntax highlighter class for JSON. More...
 
class  QThreadStump
 Customized thread class (based on QThread) that adds progress, error etc. More...
 

Functions

def is_iterable (obj)
 Checks if a given object is iterable (i.e. More...
 
def getosname ()
 Gets the current OS (platform) name. More...
 
def generate_uuid ()
 Generates a random UUID (alphanumeric string). More...
 
def copy_file (path_from, path_to)
 Copies a file into another location. More...
 
def walk_dir (root_path, abs_path=True, recurse=True, dir_process_function=None, file_process_function=None, file_types=None)
 Iterates the files and folder in a given folder, performing some operations on the found files / folders. More...
 
def run_exe (args, external=False, capture_output=True, stdout=subprocess.PIPE, encoding=ENCODING, timeout=None, shell=False, **kwargs)
 Runs an executable and optionally returns the result. More...
 
def datetime_to_str (dt=None, strformat='%Y-%m-%d %H-%M-%S')
 Converts a Python datetime object to a string. More...
 
def timestamp_to_str (ts=None, strformat='%Y-%m-%d %H-%M-%S')
 Converts a timestamp (Unix time) to a string. More...
 
def str_to_datetime (text, strformat='%Y-%m-%d %H-%M-%S')
 Converts a string to a Python datetime object. More...
 
def str_to_timestamp (text, strformat='%Y-%m-%d %H-%M-%S')
 Converts a string to a timestamp (Unix time). More...
 
def get_tempdir ()
 Gets the path to the Temp directory on the system. More...
 
def bytes_human (value, suffix='B')
 Returns a human-formatted file size as a string, e.g. More...
 
def restart_app (closefunction)
 Restarts this app. More...
 
def file_types_registered (filetypes=('xpf', 'ipuz', 'pxjson'))
 Checks if the given file type associations are registered in the OS. More...
 
def register_file_types (filetypes=('xpf', 'ipuz', 'pxjson'), register=True)
 Registers file associations in the current OS for the given file types and application. More...
 
def make_font (family, size=-1, weight=-1, italic=False, font_unit='pt')
 
def MsgBox (what, parent=None, title='pyCross', msgtype='info', btn=None, detailedText='', infoText='', execnow=True)
 
def UserInput (dialogtype='text', parent=None, title='pyCross', label='', value=None, textmode='normal', valrange=None, decimals=1, step=1, comboeditable=True, comboitems=[])
 
def clipboard_copy (value, valtype='text')
 
def clipboard_get (valtype='text')
 
def clipboard_clear ()
 
def stylesheet_load (style, dequote=True, strip_sz=True, units=('pt', 'px'))
 
def stylesheet_dump (d, quoted_keys=('font-family',), add_units={ 'font-size':'pt', 'border':'px', 'border-width':'px'})
 
def font_weight_css2qt (weight, default=0)
 
def font_weight_qt2css (weight, default=0)
 
def font_from_stylesheet (style, font_unit='pt', default_font=None)
 
def font_to_stylesheet (font, style, font_unit='pt')
 
def color_from_stylesheet (style, tag='background-color', default='black')
 
def color_to_stylesheet (color, style, tag='background-color')
 
def property_to_stylesheet (propname, propvalue, style)
 
def property_from_stylesheet (propname, style, default=None)
 

Variables

dictionary MSGBOX_BUTTONS
 
dictionary MSGBOX_TYPES
 

Function Documentation

◆ bytes_human()

def pycross.utils.utils.bytes_human (   value,
  suffix = 'B' 
)

Returns a human-formatted file size as a string, e.g.

"1Mi" (1 megabyte), "15GBi" (15 gigabytes) etc.

Parameters
valuefloat the file size value to convert
suffixstr the size suffix, default = 'B' (bytes)
Returns
str string representation of the file size

◆ clipboard_clear()

def pycross.utils.utils.clipboard_clear ( )

◆ clipboard_copy()

def pycross.utils.utils.clipboard_copy (   value,
  valtype = 'text' 
)

◆ clipboard_get()

def pycross.utils.utils.clipboard_get (   valtype = 'text')

◆ color_from_stylesheet()

def pycross.utils.utils.color_from_stylesheet (   style,
  tag = 'background-color',
  default = 'black' 
)

◆ color_to_stylesheet()

def pycross.utils.utils.color_to_stylesheet (   color,
  style,
  tag = 'background-color' 
)

◆ copy_file()

def pycross.utils.utils.copy_file (   path_from,
  path_to 
)

Copies a file into another location.

Parameters
path_fromstr the original file to copy
path_tostr the new file path or directory to copy the file to
Returns
str the path to the newly created (copied) file

◆ datetime_to_str()

def pycross.utils.utils.datetime_to_str (   dt = None,
  strformat = '%Y-%m-%d %H-%M-%S' 
)

Converts a Python datetime object to a string.

Parameters
dtdatetime Python datetime object representing a date and/or time; if None (default), the current date and time will be taken
strformatstr format string compliant to the Python datetime formatting
Returns
str string representation of the date / time

◆ file_types_registered()

def pycross.utils.utils.file_types_registered (   filetypes = ('xpf', 'ipuz', 'pxjson'))

Checks if the given file type associations are registered in the OS.

Parameters
filetypesiterable collection of file extensions to check (without leading dot)
Returns
bool True if the file types are associated with this app, False otherwise TODO: implement for OSX (Darwin)

◆ font_from_stylesheet()

def pycross.utils.utils.font_from_stylesheet (   style,
  font_unit = 'pt',
  default_font = None 
)

◆ font_to_stylesheet()

def pycross.utils.utils.font_to_stylesheet (   font,
  style,
  font_unit = 'pt' 
)

◆ font_weight_css2qt()

def pycross.utils.utils.font_weight_css2qt (   weight,
  default = 0 
)

◆ font_weight_qt2css()

def pycross.utils.utils.font_weight_qt2css (   weight,
  default = 0 
)

◆ generate_uuid()

def pycross.utils.utils.generate_uuid ( )

Generates a random UUID (alphanumeric string).

Returns
str UUID compliant to RFC 4122
See also
Python docs

◆ get_tempdir()

def pycross.utils.utils.get_tempdir ( )

Gets the path to the Temp directory on the system.

Returns
str full path to the system Temp directory

◆ getosname()

def pycross.utils.utils.getosname ( )

Gets the current OS (platform) name.

Returns
str platform name, e.g. 'Windows' or 'Linux'

◆ is_iterable()

def pycross.utils.utils.is_iterable (   obj)

Checks if a given object is iterable (i.e.

contains elements like an array).

Parameters
objthe object to check
Returns
bool True if the object is iterable (array-like) and False otherwise

◆ make_font()

def pycross.utils.utils.make_font (   family,
  size = -1,
  weight = -1,
  italic = False,
  font_unit = 'pt' 
)

◆ MsgBox()

def pycross.utils.utils.MsgBox (   what,
  parent = None,
  title = 'pyCross',
  msgtype = 'info',
  btn = None,
  detailedText = '',
  infoText = '',
  execnow = True 
)

◆ property_from_stylesheet()

def pycross.utils.utils.property_from_stylesheet (   propname,
  style,
  default = None 
)

◆ property_to_stylesheet()

def pycross.utils.utils.property_to_stylesheet (   propname,
  propvalue,
  style 
)

◆ register_file_types()

def pycross.utils.utils.register_file_types (   filetypes = ('xpf', 'ipuz', 'pxjson'),
  register = True 
)

Registers file associations in the current OS for the given file types and application.

After a call of this method succeeds, files with the indicated extensions can be launched directly with the 'open' verb, that is, by double-clicking or hitting Enter on them in the system file browser. These files will be opened with pycrossword thanks to the system-wide permanent file associations. The mechanism uses the System Registry on Windows and MIME types on Linux.

Parameters
filetypesiterable collection of file extensions to check (without leading dot)
registerbool set True to register the associations, False to unregister TODO: implement for OSX (Darwin)

◆ restart_app()

def pycross.utils.utils.restart_app (   closefunction)

Restarts this app.

Parameters
closefunctioncallable function to close down the app (e.g. gui::MainWindow::on_act_exit)

◆ run_exe()

def pycross.utils.utils.run_exe (   args,
  external = False,
  capture_output = True,
  stdout = subprocess.PIPE,
  encoding = ENCODING,
  timeout = None,
  shell = False,
**  kwargs 
)

Runs an executable and optionally returns the result.

Parameters
argslist | str arguments passed to the executable (a list of args or a single string)
externalbool whether the executable must be called as an external (detached) process; this basically means that the process will be created asynchronously, not blocking the main application process to wait for the result; if False (default), the executable will be called synchronously, waiting for the result and blocking the main process
capture_outputbool whether the console output of the executable must be captured
stdoutfile-like file / stream to channel the STDOUT and STDERR streams to; the default value is subprocess.PIPE, meaning that the output will be returned by the method
encodingstr the string encoding to use for the executable's output (default = UTF8)
timeoutfloat number of seconds to wait until timeout (default = None, i.e. wait infinitely)
shellbool whether the executable must be called via the system shell (default = False)
kwargskeyword arguments additional keyword arguments passed to subprocess.Popen
Returns
subprocess.CompletedProcess completed process results, see Python docs

◆ str_to_datetime()

def pycross.utils.utils.str_to_datetime (   text,
  strformat = '%Y-%m-%d %H-%M-%S' 
)

Converts a string to a Python datetime object.

Parameters
textstr datetime string to convert
strformatstr format string compliant to the Python datetime formatting
Returns
datetime Python datetime object

◆ str_to_timestamp()

def pycross.utils.utils.str_to_timestamp (   text,
  strformat = '%Y-%m-%d %H-%M-%S' 
)

Converts a string to a timestamp (Unix time).

Parameters
textstr datetime string to convert
strformatstr format string compliant to the Python datetime formatting
Returns
float timestamp, i.e. number of seconds since epoch (Unix time)

◆ stylesheet_dump()

def pycross.utils.utils.stylesheet_dump (   d,
  quoted_keys = ('font-family',),
  add_units = {'font-size': 'pt', 'border': 'px', 'border-width': 'px'} 
)

◆ stylesheet_load()

def pycross.utils.utils.stylesheet_load (   style,
  dequote = True,
  strip_sz = True,
  units = ('pt', 'px') 
)

◆ timestamp_to_str()

def pycross.utils.utils.timestamp_to_str (   ts = None,
  strformat = '%Y-%m-%d %H-%M-%S' 
)

Converts a timestamp (Unix time) to a string.

Parameters
tsfloat timestamp, i.e. number of seconds since epoch (Unix time) if None (default), the current timestamp will be taken
strformatstr format string compliant to the Python datetime formatting
Returns
str string representation of the timestamp

◆ UserInput()

def pycross.utils.utils.UserInput (   dialogtype = 'text',
  parent = None,
  title = 'pyCross',
  label = '',
  value = None,
  textmode = 'normal',
  valrange = None,
  decimals = 1,
  step = 1,
  comboeditable = True,
  comboitems = [] 
)

◆ walk_dir()

def pycross.utils.utils.walk_dir (   root_path,
  abs_path = True,
  recurse = True,
  dir_process_function = None,
  file_process_function = None,
  file_types = None 
)

Iterates the files and folder in a given folder, performing some operations on the found files / folders.

Parameters
root_pathstr the starting (root) directory path to start searching from
abs_pathbool if True (default), the given root path will be made absolute (relative to the current working directory); if False, it will be left as it is
recursebool whether to recurse into the found subdirectories (default = True)
dir_process_functioncallable callback function for found subdirectories. The callback takes a single argument - the full directory path.
file_process_functioncallable callback function for found files. The callback takes a single argument - the full file path.
file_typesiterable collection of file extensions (without the leading dot) that will be respected when a file is found; if None (default), no file type filtering will be done.

Variable Documentation

◆ MSGBOX_BUTTONS

dictionary pycross.utils.utils.MSGBOX_BUTTONS
Initial value:
1 = {'ok': (_('OK'), QtWidgets.QMessageBox.AcceptRole), 'yes': (_('Yes'), QtWidgets.QMessageBox.YesRole),
2  'no': (_('No'), QtWidgets.QMessageBox.NoRole), 'cancel': (_('Cancel'), QtWidgets.QMessageBox.RejectRole),
3  'yesall': (_('Yes to All'), QtWidgets.QMessageBox.YesRole), 'noall': (_('No to All'), QtWidgets.QMessageBox.NoRole),
4  'apply': (_('Apply'), QtWidgets.QMessageBox.ApplyRole), 'reset': (_('Reset'), QtWidgets.QMessageBox.ResetRole),
5  'open': (_('Open'), QtWidgets.QMessageBox.AcceptRole), 'save': (_('Save'), QtWidgets.QMessageBox.AcceptRole),
6  'close': (_('Close'), QtWidgets.QMessageBox.RejectRole), 'discard': (_('Discard'), QtWidgets.QMessageBox.DestructiveRole),
7  'restoredefaults': (_('Restore Defaults'), QtWidgets.QMessageBox.ResetRole), 'help': (_('Help'), QtWidgets.QMessageBox.HelpRole),
8  'saveall': (_('Save All'), QtWidgets.QMessageBox.AcceptRole), 'abort': (_('Abort'), QtWidgets.QMessageBox.RejectRole),
9  'retry': (_('Retry'), QtWidgets.QMessageBox.AcceptRole), 'ignore': (_('Ignore'), QtWidgets.QMessageBox.AcceptRole)}

◆ MSGBOX_TYPES

dictionary pycross.utils.utils.MSGBOX_TYPES
Initial value:
1 = {'error': (QtWidgets.QMessageBox.Critical, ['ok']), 'warn': (QtWidgets.QMessageBox.Warning, ['ok']), 'ask': (QtWidgets.QMessageBox.Question, ['yes', 'no']),
2  'info': (QtWidgets.QMessageBox.Information, ['ok']), '-': (QtWidgets.QMessageBox.NoIcon, ['ok'])}