1
0
mirror of https://github.com/gryf/pygtktalog.git synced 2025-12-17 11:30:19 +01:00

Small refactoring, fixes for pep8/pylint findings

This commit is contained in:
2016-08-21 15:08:17 +02:00
parent efab8b4152
commit 35f01b1e9f

View File

@@ -34,6 +34,7 @@ PAT = re.compile("(\[[^\]]*\]"
class NoAccessError(Exception): class NoAccessError(Exception):
"""No access exception"""
pass pass
@@ -94,21 +95,23 @@ class Scan(object):
one query using WITH statement. For now on it has to be done in one query using WITH statement. For now on it has to be done in
application. application.
""" """
SQL = "select id from files where parent_id=? and type=1" query = "select id from files where parent_id=? and type=1"
SQL2 = "select id from files where parent_id in (%s)" query2 = "select id from files where parent_id in (%s)"
row = ((node_id,),) row = ((node_id,),)
all_ids = [] all_ids = []
def req(obj): def req(obj):
"""Requrisve function for gathering all child ids for given node"""
for line in obj: for line in obj:
all_ids.append(line[0]) all_ids.append(line[0])
res = engine.execute(SQL, (line[0],)).fetchall() res = engine.execute(query, (line[0],)).fetchall()
if res: if res:
req(res) req(res)
req(row) req(row)
sql = SQL2 % ",".join("?" * len(all_ids)) sql = query2 % ",".join("?" * len(all_ids))
all_ids = [row_[0] for row_ in engine all_ids = [row_[0] for row_ in engine
.execute(sql, tuple(all_ids)) .execute(sql, tuple(all_ids))
.fetchall()] .fetchall()]
@@ -116,13 +119,13 @@ class Scan(object):
all_obj = [] all_obj = []
# number of objects to retrieve at once. Limit is 999. Let's do a # number of objects to retrieve at once. Limit is 999. Let's do a
# little bit below. # little bit below.
no = 900 num = 900
steps = len(all_ids) / no + 1 steps = len(all_ids) / num + 1
for step in range(steps): for step in range(steps):
all_obj.extend(self._session all_obj.extend(self._session
.query(File) .query(File)
.filter(File.id .filter(File.id
.in_(all_ids[step * no:step * no + no])) .in_(all_ids[step * num:step * num + num]))
.all()) .all())
return all_obj return all_obj
@@ -172,7 +175,7 @@ class Scan(object):
if not os.access(update_path, os.R_OK | os.X_OK) \ if not os.access(update_path, os.R_OK | os.X_OK) \
or not os.path.isdir(update_path): or not os.path.isdir(update_path):
LOG.error("Access to %s is forbidden", update_path) LOG.error("Access to %s is forbidden", update_path)
raise NoAccessError("Access to %s is forbidden", update_path) raise NoAccessError("Access to %s is forbidden" % update_path)
directory = os.path.basename(update_path) directory = os.path.basename(update_path)
path = os.path.dirname(update_path) path = os.path.dirname(update_path)
@@ -181,7 +184,7 @@ class Scan(object):
return None return None
# update branch # update branch
#self._session.merge(self._files[0]) # self._session.merge(self._files[0])
LOG.debug("Deleting objects whitout parent: %s", LOG.debug("Deleting objects whitout parent: %s",
str(self._session.query(File) str(self._session.query(File)
.filter(File.parent==None).all())) .filter(File.parent==None).all()))
@@ -190,23 +193,6 @@ class Scan(object):
self._session.commit() self._session.commit()
return self._files return self._files
def _get_dirsize(self, path):
"""
Returns sum of all files under specified path (also in subdirs)
"""
size = 0
for root, dirs, files in os.walk(path):
for fname in files:
try:
size += os.lstat(os.path.join(root, fname)).st_size
except OSError:
LOG.warning("Cannot access file %s",
os.path.join(root, fname))
LOG.debug("_get_dirsize, %s: %d", path, size)
return size
def _gather_information(self, fobj): def _gather_information(self, fobj):
""" """
Try to guess type and gather information about File object if possible Try to guess type and gather information about File object if possible
@@ -237,11 +223,11 @@ class Scan(object):
pass pass
def _audio(self, fobj, filepath): def _audio(self, fobj, filepath):
#LOG.warning('audio') # LOG.warning('audio')
return return
def _image(self, fobj, filepath): def _image(self, fobj, filepath):
#LOG.warning('image') # LOG.warning('image')
return return
def _video(self, fobj, filepath): def _video(self, fobj, filepath):
@@ -331,7 +317,8 @@ class Scan(object):
if fobj: if fobj:
LOG.debug("found existing file in db: %s", str(fobj)) LOG.debug("found existing file in db: %s", str(fobj))
fobj.size = fob['size'] # TODO: update whole tree sizes (for directories/discs) # TODO: update whole tree sizes (for directories/discs)
fobj.size = fob['size']
fobj.filepath = fob['path'] fobj.filepath = fob['path']
fobj.type = fob['ftype'] fobj.type = fob['ftype']
else: else:
@@ -392,7 +379,7 @@ class Scan(object):
parent = self._mk_file(fname, path, parent, TYPE['dir']) parent = self._mk_file(fname, path, parent, TYPE['dir'])
parent.size = self._get_dirsize(fullpath) parent.size = _get_dirsize(fullpath)
parent.type = TYPE['dir'] parent.type = TYPE['dir']
LOG.info("Scanning `%s' [%s/%s]", fullpath, self.current_count, LOG.info("Scanning `%s' [%s/%s]", fullpath, self.current_count,
@@ -509,3 +496,22 @@ class Scan(object):
image_path = pygtktalog.misc.calculate_image_path(image_path.value) image_path = pygtktalog.misc.calculate_image_path(image_path.value)
self.img_path = image_path self.img_path = image_path
def _get_dirsize(path):
"""
Returns sum of all files under specified path (also in subdirs)
"""
size = 0
for root, _, files in os.walk(path):
for fname in files:
try:
size += os.lstat(os.path.join(root, fname)).st_size
except OSError:
LOG.warning("Cannot access file %s",
os.path.join(root, fname))
LOG.debug("_get_dirsize, %s: %d", path, size)
return size