Gnumed.business.gmExportTray

Package Gnumed :: Package business :: Module gmExportTray |
[frames] | no frames]

# Source Code for Module Gnumed.business.gmExportTray

      1  """GNUmed export tray / filing cabinet.
      2
      3  Think shopping cart in a web shop.
      4  """
      5  #============================================================
      6  __license__ = "GPL"
      7  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
      8
      9
     10  import sys
     11  import logging
     12  import shutil
     13  import os
     14
     15
     16  if __name__ == '__main__':
     17          sys.path.insert(0, '../../')
     18  from Gnumed.pycommon import gmTools
     19  #from Gnumed.pycommon import gmPG2
     20  #from Gnumed.pycommon import gmDateTime
     21
     22  #from Gnumed.business import gmStaff
     23
     24  _log = logging.getLogger('gm.tray')
     25  #============================================================
     26 -class cExportTray(object):
     27
     28 -        def __init__(self, directory):
     29                  if not gmTools.mkdir(directory = directory):
     30                          raise ValueError('[%s.__init__()]: path [%s] is not valid' % (self.__class__.__name__, directory))
     31                  self.__dir = directory
     32                  _log.debug('export tray [%s]', self.__dir)
     33                  self.__tray_items = {}
     34          #--------------------------------------------------------
     35 -        def add_file(self, filename=None, description=None, remove_source_file=False):
     36
     37                  src_filename = filename
     38                  if description is None:
     39                          description = src_filename
     40
     41                  # check for dupes
     42                  try:
     43                          item_fname = self.__tray_items[description]
     44                          item_md5 = gmTools.file2md5(filename = item_fname, return_hex = False)
     45                          src_md5 = gmTools.file2md5(filename = src_filename, return_hex = False)
     46                          if item_md5 == src_md5:
     47                                  _log.debug('md5 match: [%s] (%s) already in tray as [%s]', description, src_filename, item_fname)
     48                                  return True
     49                  except KeyError:
     50                          pass
     51                  except StandardError:
     52                          _log.exception('cannot check [%s] for dupes in export tray [%s]', src_filename, self.__dir)
     53                          return False
     54
     55                  # move into tray
     56                  src_dir, src_name = os.path.split(src_filename)
     57                  target_filename = os.path.join(self.__dir, src_name)
     58                  try:
     59                          shutil.copy2(src_filename, target_filename)
     60                  except StandardError:
     61                          _log.exception('cannot copy [%s] into export tray as [%s]', src_filename, target_filename)
     62                          return False
     63                  self.__tray_items[description] = target_filename
     64
     65                  # remove source
     66                  if remove_source_file:
     67                          try:
     68                                  os.remove(src_filename)
     69                          except StandardError:
     70                                  _log.exception('cannot remove [%s]', src_filename)
     71
     72                  return True
     73          #--------------------------------------------------------
     74 -        def _get_tray_items(self):
     75                  return self.__tray_items
     76
     77          items = property(_get_tray_items, lambda x:x)
     78          #--------------------------------------------------------
     79 -        def _get_filenames(self):
     80                  return self.__tray_items.values()
     81
     82          filenames = property(_get_filenames, lambda x:x)
     83  #============================================================
     84  if __name__ == '__main__':
     85
     86          if len(sys.argv) < 2:
     87                  sys.exit()
     88
     89          if sys.argv[1] != 'test':
     90                  sys.exit()
     91
     92          from Gnumed.pycommon import gmI18N
     93
     94          gmI18N.activate_locale()
     95          gmI18N.install_domain()
     96
     97          #---------------------------------------
     98 -        def test_tray():
     99                  tray = cExportTray(os.path.expanduser('~/tmp/'))
    100                  print tray
    101                  print tray.items
    102                  print tray.filenames
    103          #---------------------------------------
    104          test_tray()
    105