mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-17 11:30:19 +01:00
Imports modules over objects/functions.
This commit is contained in:
@@ -4,47 +4,16 @@ Fast and ugly CLI interface
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
|
|
||||||
from pycatalog import scan
|
from pycatalog import scan
|
||||||
from pycatalog import misc
|
from pycatalog import misc
|
||||||
from pycatalog import dbobjects as dbo
|
from pycatalog import dbobjects as dbo
|
||||||
from pycatalog.dbcommon import connect, Session
|
from pycatalog import dbcommon
|
||||||
from pycatalog import logger
|
from pycatalog import logger
|
||||||
|
|
||||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)
|
|
||||||
|
|
||||||
RESET_SEQ = '\033[0m'
|
|
||||||
COLOR_SEQ = '\033[1;%dm'
|
|
||||||
BOLD_SEQ = '\033[1m'
|
|
||||||
|
|
||||||
LOG = logger.get_logger()
|
LOG = logger.get_logger()
|
||||||
|
|
||||||
|
|
||||||
def colorize(txt, color):
|
|
||||||
"""Pretty print with colors to console."""
|
|
||||||
color_map = {'black': BLACK,
|
|
||||||
'red': RED,
|
|
||||||
'green': GREEN,
|
|
||||||
'yellow': YELLOW,
|
|
||||||
'blue': BLUE,
|
|
||||||
'magenta': MAGENTA,
|
|
||||||
'cyan': CYAN,
|
|
||||||
'white': WHITE}
|
|
||||||
return COLOR_SEQ % color_map[color] + txt + RESET_SEQ
|
|
||||||
|
|
||||||
|
|
||||||
def asserdb(func):
|
|
||||||
def wrapper(args):
|
|
||||||
if not os.path.exists(args.db):
|
|
||||||
print(colorize("File `%s' does not exists!" % args.db, 'red'))
|
|
||||||
sys.exit(1)
|
|
||||||
func(args)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
TYPE_MAP = {0: 'd', 1: 'd', 2: 'f', 3: 'l'}
|
TYPE_MAP = {0: 'd', 1: 'd', 2: 'f', 3: 'l'}
|
||||||
|
|
||||||
|
|
||||||
@@ -52,8 +21,8 @@ class Iface(object):
|
|||||||
"""Main class which interacts with the pyGTKtalog modules"""
|
"""Main class which interacts with the pyGTKtalog modules"""
|
||||||
def __init__(self, dbname, pretend=False, debug=False):
|
def __init__(self, dbname, pretend=False, debug=False):
|
||||||
"""Init"""
|
"""Init"""
|
||||||
self.engine = connect(dbname)
|
self.engine = dbcommon.connect(dbname)
|
||||||
self.sess = Session()
|
self.sess = dbcommon.Session()
|
||||||
self.dry_run = pretend
|
self.dry_run = pretend
|
||||||
self.root = None
|
self.root = None
|
||||||
self._dbname = dbname
|
self._dbname = dbname
|
||||||
@@ -99,7 +68,7 @@ class Iface(object):
|
|||||||
|
|
||||||
ext = ''
|
ext = ''
|
||||||
if node.parent.type == dbo.TYPE['root']:
|
if node.parent.type == dbo.TYPE['root']:
|
||||||
ext = colorize(' (%s)' % node.filepath, 'white')
|
ext = misc.colorize(' (%s)' % node.filepath, 'white')
|
||||||
|
|
||||||
path = []
|
path = []
|
||||||
path.append(node.filename)
|
path.append(node.filename)
|
||||||
@@ -150,7 +119,7 @@ class Iface(object):
|
|||||||
node = self.root
|
node = self.root
|
||||||
msg = "Content of path `/':"
|
msg = "Content of path `/':"
|
||||||
|
|
||||||
print(colorize(msg, 'white'))
|
print(misc.colorize(msg, 'white'))
|
||||||
|
|
||||||
if recursive:
|
if recursive:
|
||||||
items = self._walk(node)
|
items = self._walk(node)
|
||||||
@@ -178,8 +147,8 @@ class Iface(object):
|
|||||||
self.root = self.root.filter(dbo.File.type == dbo.TYPE['root']).first()
|
self.root = self.root.filter(dbo.File.type == dbo.TYPE['root']).first()
|
||||||
node = self._resolve_path(path)
|
node = self._resolve_path(path)
|
||||||
if node == self.root:
|
if node == self.root:
|
||||||
print(colorize('Cannot update entire db, since root was provided '
|
print(misc.colorize('Cannot update entire db, since root was '
|
||||||
'as path.', 'red'))
|
'provided as path.', 'red'))
|
||||||
return
|
return
|
||||||
|
|
||||||
if not dir_to_update:
|
if not dir_to_update:
|
||||||
@@ -188,8 +157,8 @@ class Iface(object):
|
|||||||
if not os.path.exists(dir_to_update):
|
if not os.path.exists(dir_to_update):
|
||||||
raise OSError("Path to updtate doesn't exists: %s", dir_to_update)
|
raise OSError("Path to updtate doesn't exists: %s", dir_to_update)
|
||||||
|
|
||||||
print(colorize("Updating node `%s' against directory "
|
print(misc.colorize("Updating node `%s' against directory "
|
||||||
"`%s'" % (path, dir_to_update), 'white'))
|
"`%s'" % (path, dir_to_update), 'white'))
|
||||||
if not self.dry_run:
|
if not self.dry_run:
|
||||||
scanob = scan.Scan(dir_to_update)
|
scanob = scan.Scan(dir_to_update)
|
||||||
# scanob.update_files(node.id)
|
# scanob.update_files(node.id)
|
||||||
@@ -211,8 +180,8 @@ class Iface(object):
|
|||||||
self.sess.add(config)
|
self.sess.add(config)
|
||||||
self.sess.commit()
|
self.sess.commit()
|
||||||
|
|
||||||
print(colorize("Creating new db against directory `%s'" % dir_to_add,
|
print(misc.colorize("Creating new db against directory `%s'" %
|
||||||
'white'))
|
dir_to_add, 'white'))
|
||||||
if not self.dry_run:
|
if not self.dry_run:
|
||||||
scanob = scan.Scan(dir_to_add)
|
scanob = scan.Scan(dir_to_add)
|
||||||
scanob.add_files(self.engine)
|
scanob.add_files(self.engine)
|
||||||
@@ -225,7 +194,7 @@ class Iface(object):
|
|||||||
if not os.path.exists(dir_to_add):
|
if not os.path.exists(dir_to_add):
|
||||||
raise OSError("Path to add doesn't exists: %s", dir_to_add)
|
raise OSError("Path to add doesn't exists: %s", dir_to_add)
|
||||||
|
|
||||||
print(colorize("Adding directory `%s'" % dir_to_add, 'white'))
|
print(misc.colorize("Adding directory `%s'" % dir_to_add, 'white'))
|
||||||
if not self.dry_run:
|
if not self.dry_run:
|
||||||
scanob = scan.Scan(dir_to_add)
|
scanob = scan.Scan(dir_to_add)
|
||||||
scanob.add_files()
|
scanob.add_files()
|
||||||
@@ -248,12 +217,12 @@ class Iface(object):
|
|||||||
if idx in indexes:
|
if idx in indexes:
|
||||||
if not highlight:
|
if not highlight:
|
||||||
highlight = True
|
highlight = True
|
||||||
result.append(COLOR_SEQ % WHITE)
|
result.append(misc.COLOR_SEQ % misc.WHITE)
|
||||||
result.append(char)
|
result.append(char)
|
||||||
else:
|
else:
|
||||||
if highlight:
|
if highlight:
|
||||||
highlight = False
|
highlight = False
|
||||||
result.append(RESET_SEQ)
|
result.append(misc.RESET_SEQ)
|
||||||
result.append(char)
|
result.append(char)
|
||||||
|
|
||||||
return "".join(result)
|
return "".join(result)
|
||||||
@@ -284,7 +253,7 @@ def _get_highest_size_length(item_dict):
|
|||||||
return highest + highest / 3
|
return highest + highest / 3
|
||||||
|
|
||||||
|
|
||||||
@asserdb
|
@misc.asserdb
|
||||||
def list_db(args):
|
def list_db(args):
|
||||||
"""List"""
|
"""List"""
|
||||||
obj = Iface(args.db, False, args.debug)
|
obj = Iface(args.db, False, args.debug)
|
||||||
@@ -292,7 +261,7 @@ def list_db(args):
|
|||||||
obj.close()
|
obj.close()
|
||||||
|
|
||||||
|
|
||||||
@asserdb
|
@misc.asserdb
|
||||||
def update_db(args):
|
def update_db(args):
|
||||||
"""Update"""
|
"""Update"""
|
||||||
obj = Iface(args.db, args.pretend, args.debug)
|
obj = Iface(args.db, args.pretend, args.debug)
|
||||||
@@ -300,7 +269,7 @@ def update_db(args):
|
|||||||
obj.close()
|
obj.close()
|
||||||
|
|
||||||
|
|
||||||
@asserdb
|
@misc.asserdb
|
||||||
def add_dir(args):
|
def add_dir(args):
|
||||||
"""Add"""
|
"""Add"""
|
||||||
obj = Iface(args.db, args.pretend, args.debug)
|
obj = Iface(args.db, args.pretend, args.debug)
|
||||||
@@ -315,7 +284,7 @@ def create_db(args):
|
|||||||
obj.close()
|
obj.close()
|
||||||
|
|
||||||
|
|
||||||
@asserdb
|
@misc.asserdb
|
||||||
def search(args):
|
def search(args):
|
||||||
"""Find"""
|
"""Find"""
|
||||||
obj = Iface(args.db, False, args.debug)
|
obj = Iface(args.db, False, args.debug)
|
||||||
|
|||||||
@@ -5,11 +5,33 @@
|
|||||||
Author: Roman 'gryf' Dobosz, gryf73@gmail.com
|
Author: Roman 'gryf' Dobosz, gryf73@gmail.com
|
||||||
Created: 2009-04-05
|
Created: 2009-04-05
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from pycatalog import logger
|
from pycatalog import logger
|
||||||
|
|
||||||
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)
|
||||||
|
|
||||||
|
RESET_SEQ = '\033[0m'
|
||||||
|
COLOR_SEQ = '\033[1;%dm'
|
||||||
|
BOLD_SEQ = '\033[1m'
|
||||||
|
|
||||||
LOG = logger.get_logger()
|
LOG = logger.get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
def colorize(txt, color):
|
||||||
|
"""Pretty print with colors to console."""
|
||||||
|
color_map = {'black': BLACK,
|
||||||
|
'red': RED,
|
||||||
|
'green': GREEN,
|
||||||
|
'yellow': YELLOW,
|
||||||
|
'blue': BLUE,
|
||||||
|
'magenta': MAGENTA,
|
||||||
|
'cyan': CYAN,
|
||||||
|
'white': WHITE}
|
||||||
|
return COLOR_SEQ % color_map[color] + txt + RESET_SEQ
|
||||||
|
|
||||||
|
|
||||||
def float_to_string(float_length):
|
def float_to_string(float_length):
|
||||||
"""
|
"""
|
||||||
Parse float digit into time string
|
Parse float digit into time string
|
||||||
@@ -23,3 +45,12 @@ def float_to_string(float_length):
|
|||||||
float_length -= minutes * 60
|
float_length -= minutes * 60
|
||||||
sec = int(float_length)
|
sec = int(float_length)
|
||||||
return f"{hour:02}:{minutes:02}:{sec:02}"
|
return f"{hour:02}:{minutes:02}:{sec:02}"
|
||||||
|
|
||||||
|
|
||||||
|
def asserdb(func):
|
||||||
|
def wrapper(args):
|
||||||
|
if not os.path.exists(args.db):
|
||||||
|
print(colorize("File `%s' does not exists!" % args.db, 'red'))
|
||||||
|
sys.exit(1)
|
||||||
|
func(args)
|
||||||
|
return wrapper
|
||||||
|
|||||||
@@ -10,9 +10,8 @@ import re
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
|
||||||
import pycatalog.misc
|
from pycatalog.dbobjects import File, TYPE
|
||||||
from pycatalog.dbobjects import File, Config, TYPE
|
from pycatalog import dbcommon
|
||||||
from pycatalog.dbcommon import Session
|
|
||||||
from pycatalog.logger import get_logger
|
from pycatalog.logger import get_logger
|
||||||
from pycatalog.video import Video
|
from pycatalog.video import Video
|
||||||
|
|
||||||
@@ -27,7 +26,6 @@ RE_FN_START = re.compile(r'(?P<fname_start>'
|
|||||||
|
|
||||||
class NoAccessError(Exception):
|
class NoAccessError(Exception):
|
||||||
"""No access exception"""
|
"""No access exception"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Scan(object):
|
class Scan(object):
|
||||||
@@ -45,7 +43,7 @@ class Scan(object):
|
|||||||
self._files = []
|
self._files = []
|
||||||
self._existing_files = [] # for re-use purpose in adding
|
self._existing_files = [] # for re-use purpose in adding
|
||||||
self._existing_branch = [] # for branch storage, mainly for updating
|
self._existing_branch = [] # for branch storage, mainly for updating
|
||||||
self._session = Session()
|
self._session = dbcommon.Session()
|
||||||
self.files_count = self._get_files_count()
|
self.files_count = self._get_files_count()
|
||||||
self.current_count = 0
|
self.current_count = 0
|
||||||
|
|
||||||
@@ -73,6 +71,8 @@ class Scan(object):
|
|||||||
|
|
||||||
# add only first item from _files, because it is a root of the other,
|
# add only first item from _files, because it is a root of the other,
|
||||||
# so other will be automatically added aswell.
|
# so other will be automatically added aswell.
|
||||||
|
if label:
|
||||||
|
self._files[0].filename = label
|
||||||
self._session.add(self._files[0])
|
self._session.add(self._files[0])
|
||||||
self._session.commit()
|
self._session.commit()
|
||||||
return self._files
|
return self._files
|
||||||
@@ -212,11 +212,11 @@ class Scan(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def _audio(self, fobj, filepath):
|
def _audio(self, fobj, filepath):
|
||||||
# LOG.warning('audio')
|
# tags, depending on the format?
|
||||||
return
|
return
|
||||||
|
|
||||||
def _image(self, fobj, filepath):
|
def _image(self, fobj, filepath):
|
||||||
# LOG.warning('image')
|
# exif?
|
||||||
return
|
return
|
||||||
|
|
||||||
def _video(self, fobj, filepath):
|
def _video(self, fobj, filepath):
|
||||||
|
|||||||
Reference in New Issue
Block a user