file operations
This commit is contained in:
parent
e9259dc7ba
commit
bd9103d362
6 changed files with 74 additions and 51 deletions
102
orvani.py
102
orvani.py
|
|
@ -1,9 +1,11 @@
|
|||
import os
|
||||
import yaml
|
||||
import shutil
|
||||
|
||||
class OrvaniTag:
|
||||
|
||||
DB = dict()
|
||||
FILENAME = ''
|
||||
FILE = ''
|
||||
CTX = {
|
||||
'contexts' : '@',
|
||||
|
|
@ -12,11 +14,12 @@ class OrvaniTag:
|
|||
}
|
||||
|
||||
def __init__(self, path: str='context.yaml', contexts=CTX):
|
||||
self.FILE = path
|
||||
self.__cd()
|
||||
self.FILENAME = os.path.basename(path)
|
||||
self.__cd(path)
|
||||
|
||||
def __cd(self):
|
||||
def __cd(self, path: str):
|
||||
"""change file context to subdirectory"""
|
||||
self.FILE = os.path.join(os.path.dirname(path), self.FILENAME)
|
||||
if os.path.isfile(self.FILE):
|
||||
try:
|
||||
with open(self.FILE, 'r') as data:
|
||||
|
|
@ -49,41 +52,78 @@ class OrvaniTag:
|
|||
return context
|
||||
|
||||
def add(self, file: str, *argv: str):
|
||||
if os.path.dirname(file):
|
||||
self.FILE = os.path.join(os.path.dirname(file), self.FILE)
|
||||
self.__cd()
|
||||
file = os.path.basename(file)
|
||||
context = self.__context(argv)
|
||||
if file in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
self.DB[file][ctx] = list(set( self.DB[file][ctx] + context[ctx]))
|
||||
if 'note' in context.keys():
|
||||
self.DB[file]['note'] = context['note']
|
||||
else:
|
||||
self.DB.update( { file : context } )
|
||||
self.__save()
|
||||
if os.path.isfile(file):
|
||||
if os.path.dirname(file):
|
||||
self.__cd(file)
|
||||
file = os.path.basename(file)
|
||||
context = self.__context(argv)
|
||||
if file in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
self.DB[file][ctx] = list(set( self.DB[file][ctx] + context[ctx]))
|
||||
if 'note' in context.keys():
|
||||
self.DB[file]['note'] = context['note']
|
||||
else:
|
||||
self.DB.update( { file : context } )
|
||||
self.__save()
|
||||
|
||||
def remove(self, file: str, *argv: str):
|
||||
if os.path.dirname(file):
|
||||
self.FILE = os.path.join(os.path.dirname(file), self.FILE)
|
||||
self.__cd()
|
||||
file = os.path.basename(file)
|
||||
context = self.__context(argv)
|
||||
if file in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
for item in context[ctx]:
|
||||
if item in self.DB[file][ctx]:
|
||||
self.DB[file][ctx].remove(item)
|
||||
self.__save()
|
||||
if os.path.isfile(file):
|
||||
if os.path.dirname(file):
|
||||
self.__cd(file)
|
||||
file = os.path.basename(file)
|
||||
context = self.__context(argv)
|
||||
if file in self.DB:
|
||||
for ctx in self.CTX.keys():
|
||||
for item in context[ctx]:
|
||||
if item in self.DB[file][ctx]:
|
||||
self.DB[file][ctx].remove(item)
|
||||
self.__save()
|
||||
|
||||
def purge(self, file: str):
|
||||
def delete(self, file: str, fop=False):
|
||||
if os.path.dirname(file):
|
||||
self.FILE = os.path.join(os.path.dirname(file), self.FILE)
|
||||
self.__cd()
|
||||
self.__cd(file)
|
||||
if fop:
|
||||
os.remove(file)
|
||||
file = os.path.basename(file)
|
||||
if file in self.DB:
|
||||
del self.DB[file]
|
||||
self.__save()
|
||||
|
||||
def move(self, source: str, dest: str, retain=False, fop=True):
|
||||
if os.path.isfile(source) and os.path.isdir(os.path.dirname(dest)):
|
||||
sourcefolder = os.path.dirname(source)
|
||||
sourcefile = os.path.basename(source)
|
||||
destfolder = os.path.dirname(dest)
|
||||
destfile = os.path.basename(dest)
|
||||
if not destfile:
|
||||
destfile = sourcefile
|
||||
sourcedata = {}
|
||||
self.__cd(source)
|
||||
if self.DB[sourcefile]:
|
||||
sourcedata = self.DB[sourcefile]
|
||||
if not retain:
|
||||
self.purge(source)
|
||||
self.__cd(dest)
|
||||
self.DB.update( { destfile : sourcedata } )
|
||||
self.__save()
|
||||
if fop:
|
||||
if retain:
|
||||
shutil.copy2(source, dest)
|
||||
else:
|
||||
shutil.move(source, dest, copy_function=shutil.copy2)
|
||||
|
||||
def purge(self, path: str=''):
|
||||
if os.path.dirname(path):
|
||||
self.__cd(path)
|
||||
for file in self.DB.keys():
|
||||
if not os.path.isfile(file):
|
||||
if file in self.DB:
|
||||
del self.DB[file]
|
||||
self.__save()
|
||||
if not self.DB:
|
||||
os.remove(self.FILE)
|
||||
|
||||
|
||||
o = OrvaniTag()
|
||||
o.add('dir/file.txt', 'weird')
|
||||
#o.add('TODO.md', '#tagx', '+projectX')
|
||||
o.purge()
|
||||
Loading…
Add table
Add a link
Reference in a new issue