feedwarrior

Slim, portable tooling for creating and distributing decentralized append logs
git clone git://git.defalsify.org/logwarrior.git
Log | Files | Refs | README | LICENSE

fileadapter.py (2831B)


      1 # standard imports
      2 import os
      3 import gzip
      4 import logging
      5 import json
      6 
      7 logg = logging.getLogger()
      8 
      9 
     10 class fileadapter:
     11 
     12     def __init__(self, db_directory, uu):
     13         try:
     14             os.mkdir(os.path.join(db_directory, 'feeds'))
     15         except FileExistsError:
     16             pass
     17 
     18         try:
     19             os.mkdir(os.path.join(db_directory, 'feeds', str(uu)))
     20         except FileExistsError:
     21             pass
     22 
     23         try:
     24             os.mkdir(os.path.join(db_directory, 'entries'))
     25         except FileExistsError:
     26             pass
     27 
     28         try:
     29             os.mkdir(os.path.join(db_directory,  'feeds', str(uu), 'entries'))
     30         except FileExistsError:
     31             pass
     32 
     33         self.src = db_directory
     34         self.feeds_uuid = uu
     35 
     36 
     37     def get_raw_fp(self, fp):
     38         return open(fp, 'r')
     39 
     40 
     41     def get_gz_fp(self, fp):
     42         fp += '.gz'
     43         logg.debug('uncompressing {}'.format(fp))
     44         return gzip.open(fp, 'rb')
     45         #return open(fp, 'r')
     46 
     47     
     48     def get_with_type(self, uu, **kwargs):
     49         entry_path = os.path.join(self.src, 'entries', str(uu))
     50         f = None
     51         typ = 'plain'
     52         if entry_path[-3:] == '.gz':
     53             entry_path = entry_path[:-3]
     54         logg.debug('etnry {}'.format(entry_path))
     55         try:
     56             f = self.get_raw_fp(entry_path)
     57         except FileNotFoundError:
     58             f = self.get_gz_fp(entry_path)
     59             typ = 'gzip'
     60         c = f.read()
     61         f.close()
     62         return (c, typ,)
     63 
     64 
     65     def get(self, uu, **kwargs):
     66         r = self.get_with_type(uu, **kwargs)
     67         return r[0]
     68 
     69 #        entry_path = os.path.join(self.src, 'entries', str(uu))
     70 #        f = None
     71 #        try:
     72 #            f = self.get_raw_fp(entry_path)
     73 #        except FileNotFoundError:
     74 #            f = self.get_gz_fp(entry_path)
     75 #        c = f.read()
     76 #        f.close()
     77 #        return c
     78 
     79 
     80     def put(self, uu, entry, replace=False, **kwargs):
     81         fm = 'xb'
     82         if replace:
     83             fm = 'wb'
     84 
     85         entry_serialized = entry.serialize()
     86         entry_json = json.dumps(entry_serialized)
     87         contents_bytes = entry_json.encode('utf-8')
     88 
     89         entry_path = os.path.join(self.src, 'entries', str(uu))
     90         if not replace and (os.path.exists(entry_path) or os.path.exists(entry_path + '.gz')):
     91             raise FileExistsError('record {} already exists'.format(str(uu)))
     92         f = None
     93         if kwargs.get('compress') != None:
     94             entry_path += '.gz'
     95             f = gzip.open(entry_path, fm)
     96         else:
     97             f = open(entry_path, fm)
     98             
     99         f.write(contents_bytes)
    100         f.close()
    101 
    102         feeds_entry_path = os.path.join(self.src, 'feeds', str(self.feeds_uuid), 'entries', str(uu))
    103         if kwargs.get('compress') != None:
    104             feeds_entry_path += '.gz'
    105         os.symlink(entry_path, feeds_entry_path)