def __run_file_describer(filename=None): base_name = 'gm-describe_file' paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', base_name) candidates = [base_name, local_script] #, base_name + '.bat' found, binary = gmShellAPI.find_first_binary(binaries=candidates) if not found: _log.error('cannot find <%s(.bat)>', base_name) return (False, _('<%s(.bat)> not found') % base_name) cmd_line = [binary, filename] _log.debug('describing: %s', cmd_line) try: proc_result = subprocess.run( args=cmd_line, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, #timeout = timeout, encoding='utf8', errors='backslashreplace') except (subprocess.TimeoutExpired, FileNotFoundError): _log.exception('there was a problem running external process') return (False, _('problem with <%s>') % binary) _log.info('exit code [%s]', proc_result.returncode) if proc_result.returncode != 0: _log.error('[%s] failed', binary) _log.error('STDERR:\n%s', proc_result.stderr) _log.error('STDOUT:\n%s', proc_result.stdout) return (False, _('problem with <%s>') % binary) return (True, proc_result.stdout)
def __run_file_describer(filename=None): base_name = 'gm-describe_file' paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', base_name) candidates = [ base_name, local_script ] #, base_name + u'.bat' found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: _log.error('cannot find <%s(.bat)>', base_name) return (False, _('<%s(.bat)> not found') % base_name) cmd_line = [binary, filename] _log.debug('describing: %s', cmd_line) try: proc_result = subprocess.run ( args = cmd_line, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, #timeout = timeout, encoding = 'utf8' ) except (subprocess.TimeoutExpired, FileNotFoundError): _log.exception('there was a problem running external process') return (False, _('problem with <%s>') % binary) _log.info('exit code [%s]', proc_result.returncode) if proc_result.returncode != 0: _log.error('[%s] failed', binary) _log.error('STDERR:\n%s', proc_result.stderr) _log.error('STDOUT:\n%s', proc_result.stdout) return (False, _('problem with <%s>') % binary) return (True, proc_result.stdout)
def __detect_binary(self): if self.path_to_binary is not None: return True found, cmd = gmShellAPI.find_first_binary(binaries = [ r'/usr/local/bin/arriba', r'/usr/bin/arriba', r'arriba', r'arriba.exe' # r'c:\programs\freediams\freediams.exe', # r'freediams.exe' ]) if found: self.path_to_binary = cmd return True # try: # self.custom_path_to_binary # except AttributeError: # _log.error('cannot find FreeDiams binary, no custom path set') # return False # found, cmd = gmShellAPI.detect_external_binary(binary = self.custom_path_to_binary) # if found: # self.path_to_binary = cmd # return True _log.error('cannot find arriba binary') return False
def __detect_binary(self): if self.path_to_binary is not None: return True found, cmd = gmShellAPI.find_first_binary(binaries=[ r'/usr/local/bin/arriba', r'/usr/bin/arriba', r'arriba', r'arriba.exe' # r'c:\programs\freediams\freediams.exe', # r'freediams.exe' ]) if found: self.path_to_binary = cmd return True # try: # self.custom_path_to_binary # except AttributeError: # _log.error('cannot find FreeDiams binary, no custom path set') # return False # found, cmd = gmShellAPI.detect_external_binary(binary = self.custom_path_to_binary) # if found: # self.path_to_binary = cmd # return True _log.error('cannot find arriba binary') return False
def _print_files_by_shellscript(filenames=None, jobtype=None): paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') #candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat'] candidates = [u'gm-print_doc', local_script, u'gm-print_doc.bat'] found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: binary = r'gm-print_doc.bat' cmd_line = [ binary, jobtype ] cmd_line.extend(filenames) _log.debug('printing with %s', cmd_line) try: gm_print_doc = subprocess.Popen(cmd_line) except OSError: _log.debug('cannot run <gm_print_doc(.bat)>') return False gm_print_doc.communicate() if gm_print_doc.returncode != 0: _log.error('<gm_print_doc> returned [%s], failed to print', gm_print_doc.returncode) return False return True
def _print_files_by_shellscript(filenames=None, jobtype=None): paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') #candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat'] candidates = ['gm-print_doc', local_script, 'gm-print_doc.bat'] found, binary = gmShellAPI.find_first_binary(binaries=candidates) if not found: binary = r'gm-print_doc.bat' cmd_line = [binary, jobtype] cmd_line.extend(filenames) _log.debug('printing with %s', cmd_line) try: gm_print_doc = subprocess.Popen(cmd_line) except OSError: _log.debug('cannot run <gm_print_doc(.bat)>') return False gm_print_doc.communicate() if gm_print_doc.returncode != 0: _log.error('<gm_print_doc> returned [%s], failed to print', gm_print_doc.returncode) return False return True
def _print_files_by_shellscript(filenames=None, jobtype=None, verbose=False): paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') candidates = ['gm-print_doc', local_script, 'gm-print_doc.bat'] found, binary = gmShellAPI.find_first_binary(binaries=candidates) if not found: binary = r'gm-print_doc.bat' cmd_line = [binary, jobtype] cmd_line.extend(filenames) success, returncode, stdout = gmShellAPI.run_process(cmd_line=cmd_line, verbose=verbose) if not success: _log.debug( 'gm-print_doc(.bat) arguments: "DOCUMENT_TYPE LIST-OF-FILES-TO-PRINT"' ) _log.debug( 'gm-print_doc(.bat): call printing app, perhaps based on DOCUMENT_TYPE, and pass in LIST-OF-FILES-TO-PRINT' ) _log.debug('gm-print_doc(.bat): return 0 on success') _log.debug( 'gm-print_doc(.bat): DOCUMENT_TYPE - can be used to decide which way to process a particular print job (Example: medication_list)' ) _log.debug( 'gm-print_doc(.bat): LIST-OF-FILES-TO-PRINT - can be of any mimetype so the script needs to be able to process them, typically PDF though' ) return False return True
def __init__(self, *args, **kwargs): if not isinstance(self, (wx.TextCtrl, wx.stc.StyledTextCtrl)): raise TypeError('[%s]: can only be applied to wx.TextCtrl or wx.stc.StyledTextCtrl, not [%s]' % (cUnicodeInsertion_TextCtrlMixin, self.__class__.__name__)) if cUnicodeInsertion_TextCtrlMixin._unicode_selector is None: found, cUnicodeInsertion_TextCtrlMixin._unicode_selector = gmShellAPI.find_first_binary(binaries = _KNOWN_UNICODE_SELECTORS) if found: _log.debug('found [%s] for unicode character selection', cUnicodeInsertion_TextCtrlMixin._unicode_selector) else: _log.error('no unicode character selection tool found')
def convert_file(filename=None, target_mime=None, target_filename=None, target_extension=None, verbose=False): """Convert file from one format into another. target_mime: a mime type """ assert (target_mime is not None), '<target_mime> must not be None' assert (filename is not None), '<filename> must not be None' assert (filename != target_filename), '<target_filename> must be different from <filename>' source_mime = guess_mimetype(filename = filename) if source_mime.lower() == target_mime.lower(): _log.debug('source file [%s] already target mime type [%s]', filename, target_mime) if target_filename is None: return filename shutil.copyfile(filename, target_filename) return target_filename converted_ext = guess_ext_by_mimetype(target_mime) if converted_ext is None: if target_filename is not None: tmp, converted_ext = os.path.splitext(target_filename) if converted_ext is None: converted_ext = target_extension # can still stay None converted_fname = gmTools.get_unique_filename(suffix = converted_ext) _log.debug('attempting conversion: [%s] -> [<%s>:%s]', filename, target_mime, gmTools.coalesce(target_filename, converted_fname)) script_name = 'gm-convert_file' paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', script_name) candidates = [ script_name, local_script ] #, script_name + u'.bat' found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: # try anyway binary = script_name# + r'.bat' _log.debug('<%s> API: SOURCEFILE TARGET_MIMETYPE TARGET_EXTENSION TARGET_FILENAME' % binary) cmd_line = [ binary, filename, target_mime, converted_ext.lstrip('.'), converted_fname ] success, returncode, stdout = gmShellAPI.run_process(cmd_line = cmd_line, verbose = True) if not success: _log.error('conversion failed') return None if target_filename is None: return converted_fname shutil.copyfile(converted_fname, target_filename) return target_filename
def _print_files_by_shellscript(filenames=None, jobtype=None, verbose=False): paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') #candidates = [u'gm-print_doc', u'gm-print_doc.bat', local_script, u'gm-print_doc.bat'] candidates = ['gm-print_doc', local_script, 'gm-print_doc.bat'] found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: binary = r'gm-print_doc.bat' cmd_line = [binary, jobtype] cmd_line.extend(filenames) success, returncode, stdout = gmShellAPI.run_process(cmd_line = cmd_line, verbose = verbose) if not success: return False return True
def _print_files_by_shellscript(filenames=None, jobtype=None, verbose=False): paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', 'gm-print_doc') candidates = ['gm-print_doc', local_script, 'gm-print_doc.bat'] found, binary = gmShellAPI.find_first_binary(binaries=candidates) if not found: binary = r'gm-print_doc.bat' cmd_line = [binary, jobtype] cmd_line.extend(filenames) success, returncode, stdout = gmShellAPI.run_process(cmd_line=cmd_line, verbose=verbose) if not success: return False return True
def convert_file(filename=None, target_mime=None, target_filename=None, target_extension=None): """Convert file from one format into another. target_mime: a mime type """ source_mime = guess_mimetype(filename = filename) if source_mime.lower() == target_mime.lower(): _log.debug('source file [%s] already target mime type [%s]', filename, target_mime) shutil.copyfile(filename, target_filename) return True if target_extension is None: tmp, target_extension = os.path.splitext(target_filename) base_name = 'gm-convert_file' paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', base_name) candidates = [ base_name, local_script ] #, base_name + u'.bat' found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: binary = base_name# + r'.bat' cmd_line = [ binary, filename, target_mime, target_extension.strip('.'), target_filename ] _log.debug('converting: %s', cmd_line) try: gm_convert = subprocess.Popen(cmd_line) except OSError: _log.debug('cannot run <%s(.bat)>', base_name) return False gm_convert.communicate() if gm_convert.returncode != 0: _log.error('<%s(.bat)> returned [%s], failed to convert', base_name, gm_convert.returncode) return False return True
def __run_file_describer(filename=None): base_name = 'gm-describe_file' paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', base_name) candidates = [ base_name, local_script ] #, base_name + u'.bat' found, binary = gmShellAPI.find_first_binary(binaries = candidates) if not found: #binary = base_name# + r'.bat' _log.debug('cannot find <%s(.bat)>', base_name) return (False, _('<%s(.bat)> not found') % base_name) desc_fname = gmTools.get_unique_filename() cmd_line = [ binary, filename, desc_fname ] _log.debug('describing: %s', cmd_line) try: gm_describe = subprocess.Popen(cmd_line) except OSError: _log.debug('cannot run <%s>', binary) return (False, _('problem with <%s>') % binary) gm_describe.communicate() if gm_describe.returncode != 0: _log.error('<%s> returned [%s], failed to convert', binary, gm_describe.returncode) return (False, _('problem with <%s>') % binary) try: desc_file = io.open(desc_fname, mode = 'rt', encoding = 'utf8', errors = 'replace') except IOError: _log.exception('cannot open [%s]', desc_fname) return (False, _('problem with <%s>') % binary) desc = ''.join(desc_file) desc_file.close() return (True, desc)
def __detect_binary(self): if self.path_to_binary is not None: return True found, cmd = gmShellAPI.find_first_binary(binaries = [ r'/usr/bin/freediams', r'freediams', r'/Applications/FreeDiams.app/Contents/MacOs/FreeDiams', r'C:\Program Files (x86)\FreeDiams\freediams.exe', r'C:\Program Files\FreeDiams\freediams.exe', r'c:\programs\freediams\freediams.exe', r'freediams.exe' ]) if found: self.path_to_binary = cmd return True try: self.custom_path_to_binary except AttributeError: _log.error('cannot find FreeDiams binary, no custom path set') return False if self.custom_path_to_binary is None: _log.error('cannot find FreeDiams binary') return False found, cmd = gmShellAPI.detect_external_binary(binary = self.custom_path_to_binary) if found: self.path_to_binary = cmd return True _log.error('cannot find FreeDiams binary') return False
def convert_latex_to_pdf(filename: str = None, verbose: bool = False, is_sandboxed: bool = False) -> str: """Compile LaTeX code to PDF using pdflatex. Args: is_sandboxed: whether or not to create a sandbox for compiling Returns: Name of resulting PDF, or None on failure. """ global __LaTeX_version_checked global __pdflatex_executable if not __LaTeX_version_checked: __LaTeX_version_checked = True found, __pdflatex_executable = gmShellAPI.detect_external_binary( binary='pdflatex') if not found: _log.error('pdflatex not found') return None cmd_line = [__pdflatex_executable, '-version'] success, ret_code, stdout = gmShellAPI.run_process(cmd_line=cmd_line, encoding='utf8', verbose=True) if not success: _log.error('[%s] failed, LaTeX not usable', cmd_line) return None if is_sandboxed: sandbox_dir = os.path.split(filename)[0] else: sandbox_dir = gmTools.mk_sandbox_dir( prefix=gmTools.fname_stem(filename) + '_') shutil.copy(filename, sandbox_dir) filename = os.path.join(sandbox_dir, os.path.split(filename)[1]) _log.debug('LaTeX sandbox directory: [%s]', sandbox_dir) cmd_final = [ __pdflatex_executable, '-recorder', '-interaction=nonstopmode', "-output-directory=%s" % sandbox_dir ] cmd_draft = cmd_final + ['-draftmode'] # LaTeX can need up to three runs to get cross references et al right for cmd2run in [cmd_draft, cmd_draft, cmd_final]: success, ret_code, stdout = gmShellAPI.run_process( cmd_line=cmd2run + [filename], acceptable_return_codes=[0], encoding='utf8', verbose=True #_cfg.get(option = 'debug') ) if not success: _log.error( 'problem running pdflatex, cannot generate form output, trying diagnostics' ) found, binary = gmShellAPI.find_first_binary( binaries=['lacheck', 'miktex-lacheck.exe']) if not found: _log.debug('lacheck not found') else: cmd_line = [binary, filename] success, ret_code, stdout = gmShellAPI.run_process( cmd_line=cmd_line, encoding='utf8', verbose=True) found, binary = gmShellAPI.find_first_binary( binaries=['chktex', 'ChkTeX.exe']) if not found: _log.debug('chcktex not found') else: cmd_line = [binary, '--verbosity=2', '--headererr', filename] success, ret_code, stdout = gmShellAPI.run_process( cmd_line=cmd_line, encoding='utf8', verbose=True) return None return '%s.pdf' % os.path.splitext(filename)[0]
def convert_file(filename=None, target_mime=None, target_filename=None, target_extension=None, verbose=False): """Convert file from one format into another. target_mime: a mime type """ assert (target_mime is not None), '<target_mime> must not be None' assert (filename is not None), '<filename> must not be None' assert (filename != target_filename ), '<target_filename> must be different from <filename>' source_mime = guess_mimetype(filename=filename) if source_mime.casefold() == target_mime.casefold(): _log.debug('source file [%s] already target mime type [%s]', filename, target_mime) if target_filename is None: return filename shutil.copyfile(filename, target_filename) return target_filename converted_ext = guess_ext_by_mimetype(target_mime) if converted_ext is None: if target_filename is not None: tmp, converted_ext = os.path.splitext(target_filename) if converted_ext is None: converted_ext = target_extension # can still stay None converted_ext = gmTools.coalesce(converted_ext, '').strip().lstrip('.') converted_fname = gmTools.get_unique_filename(suffix=converted_ext) _log.debug('attempting conversion: [%s] -> [<%s>:%s]', filename, target_mime, gmTools.coalesce(target_filename, converted_fname)) # try user-local conversion script script_name = 'gm-convert_file' binary = os.path.join(gmTools.gmPaths().home_dir, 'bin', script_name) _log.debug('trying user-local script: %s', binary) _log.debug( '<%s> API: SOURCEFILE TARGET_MIMETYPE TARGET_EXTENSION TARGET_FILENAME', script_name) found, binary = gmShellAPI.detect_external_binary(binary=binary) if found: cmd_line = [ binary, filename, target_mime, converted_ext, converted_fname ] success, returncode, stdout = gmShellAPI.run_process(cmd_line=cmd_line, verbose=True) if success: if target_filename is None: return converted_fname shutil.copyfile(converted_fname, target_filename) return target_filename # try built-in conversions _log.debug('trying built-in conversion function') try: conversion_func = __CONVERSION_DELEGATES[source_mime][target_mime] except KeyError: conversion_func = None if conversion_func is not None: converted_fname = conversion_func(filename=filename, verbose=verbose) if converted_fname is not None: if target_filename is None: return converted_fname shutil.copyfile(converted_fname, target_filename) return target_filename # try system-wide conversion script paths = gmTools.gmPaths() local_script = os.path.join(paths.local_base_dir, '..', 'external-tools', script_name) candidates = [script_name, local_script] #, script_name + u'.bat' _log.debug('trying system-wide scripts: %s', candidates) found, binary = gmShellAPI.find_first_binary(binaries=candidates) if not found: # try anyway _log.debug('trying anyway as last-ditch resort') binary = script_name # + r'.bat' cmd_line = [binary, filename, target_mime, converted_ext, converted_fname] success, returncode, stdout = gmShellAPI.run_process(cmd_line=cmd_line, verbose=True) if success: if target_filename is None: return converted_fname shutil.copyfile(converted_fname, target_filename) return target_filename # seems to have failed but check for target file anyway _log.error( 'conversion script returned error exit code, checking target file anyway' ) if not os.path.exists(converted_fname): return None _log.info('conversion target file found') stats = os.stat(converted_fname) if stats.st_size == 0: return None _log.info('conversion target file size > 0') achieved_mime = guess_mimetype(filename=converted_fname) if achieved_mime != target_mime: _log.error('target: [%s], achieved: [%s]', target_mime, achieved_mime) return None # we may actually have something despite a non-0 exit code _log.info( 'conversion target file mime type [%s], as expected, might be usable', achieved_mime) if target_filename is None: return converted_fname shutil.copyfile(converted_fname, target_filename) return target_filename
raise ValueError('<enable> must be True or False') self.Refresh() #============================================================ _KNOWN_UNICODE_SELECTORS = [ 'kcharselect', # KDE 'gucharmap', # GNOME 'BabelMap.exe', # Windows, supposed to be better than charmap.exe 'charmap.exe', # Microsoft utility 'gm-unicode2clipboard' # generic GNUmed workaround # Mac OSX supposedly features built-in support ] found, __UNICODE_SELECTOR_APP = gmShellAPI.find_first_binary( binaries=_KNOWN_UNICODE_SELECTORS) if found: _log.debug('found [%s] for unicode character selection', __UNICODE_SELECTOR_APP) else: _log.error('no unicode character selection tool found') _log.debug('known tools: %s', _KNOWN_UNICODE_SELECTORS) _log.debug('gm-unicode2clipboard: no arguments passed in') _log.debug( 'gm-unicode2clipboard: if the clipboard contains new data upon return GNUmed will import the active text control' ) class cUnicodeInsertion_TextCtrlMixin(): """Insert unicode characters into text control via selection tool."""