Skip to content

Commit

Permalink
option for hidden files
Browse files Browse the repository at this point in the history
more translation strings
  • Loading branch information
xy committed Dec 22, 2024
1 parent 342265a commit 2eef1e1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 83 deletions.
144 changes: 77 additions & 67 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

from time import sleep, perf_counter,time,strftime,localtime,mktime
from threading import Thread
from stat import FILE_ATTRIBUTE_HIDDEN as stat_FILE_ATTRIBUTE_HIDDEN
from os import cpu_count,scandir,stat,sep,name as os_name,remove as os_remove,rename
from os.path import abspath,normpath,basename,dirname,join as path_join

Expand All @@ -53,6 +54,12 @@

windows = bool(os_name=='nt')

def is_hidden(filepath):
if windows:
return bool(stat(filepath).st_file_attributes & stat_FILE_ATTRIBUTE_HIDDEN)
else:
return basename(abspath(filepath)).startswith('.')

if windows:
from subprocess import CREATE_NO_WINDOW
else:
Expand Down Expand Up @@ -385,7 +392,7 @@ def compress_with_header_update_wrapp(data,datalabel):

print_func(('save','finished'),True)

def scan_rec(self,print_func,abort_list,path, scan_like_data,filenames_set,check_dev=True,dev_call=None) :
def scan_rec(self,print_func,abort_list,path, scan_like_data,filenames_set,check_dev=True,dev_call=None,include_hidden=False) :
if any(abort_list) :
return True

Expand All @@ -400,96 +407,100 @@ def scan_rec(self,print_func,abort_list,path, scan_like_data,filenames_set,check
filenames_set_add = filenames_set.add
self_header_ext_stats = self.header.ext_stats
self_header_ext_stats_size = self.header.ext_stats_size

try:

with scandir(path) as res:

local_folder_files_count = 0
local_folder_folders_count = 0

for entry in res:
subitems+=1
if any(abort_list) :
break

entry_name = entry.name
filenames_set_add(entry_name)
if include_hidden or (not is_hidden(entry)):

is_dir,is_file,is_symlink = entry.is_dir(),entry.is_file(),entry.is_symlink()
subitems+=1
if any(abort_list) :
break

ext=pathlib_Path(entry).suffix
entry_name = entry.name
filenames_set_add(entry_name)

if is_file:
self_header_ext_stats[ext]+=1
is_dir,is_file,is_symlink = entry.is_dir(),entry.is_file(),entry.is_symlink()

try:
stat_res = stat(entry)
mtime = int(stat_res.st_mtime)
dev=stat_res.st_dev
except Exception as e:
print_func( ('error',f'stat {entry_name} error:{e}'),True )
#size -1 <=> error, dev,in ==0
is_bind = False
size=-1
mtime=0
has_files = False
scan_like_data[entry_name] = [size,is_dir,is_file,is_symlink,is_bind,has_files,mtime]
else:
dict_entry={}
is_bind=False
if check_dev:
if dev_call:
if dev_call!=dev:
#self.log.info('devices mismatch:%s %s %s %s' % (path,entry_name,dev_call,dev) )
print_func( ('info',f'devices mismatch:{path},{entry_name},{dev_call},{dev}'),True )
is_bind=True
else:
dev_call=dev

if is_dir:
if is_symlink :
has_files = False
size = 0
elif is_bind:
has_files = False
size = 0
else:
size,sub_sub_items = self_scan_rec(print_func,abort_list,path_join_loc(path,entry_name),dict_entry,filenames_set,check_dev,dev)
has_files = bool(sub_sub_items)
ext=pathlib_Path(entry).suffix

local_folder_size_with_subtree += size
if is_file:
self_header_ext_stats[ext]+=1

local_folder_folders_count += 1
try:
stat_res = stat(entry)
mtime = int(stat_res.st_mtime)
dev=stat_res.st_dev
except Exception as e:
print_func( ('error',f'stat {entry_name} error:{e}'),True )
#size -1 <=> error, dev,in ==0
is_bind = False
size=-1
mtime=0
has_files = False
scan_like_data[entry_name] = [size,is_dir,is_file,is_symlink,is_bind,has_files,mtime]
else:
if is_symlink :
has_files = False
size = 0
dict_entry={}
is_bind=False
if check_dev:
if dev_call:
if dev_call!=dev:
#self.log.info('devices mismatch:%s %s %s %s' % (path,entry_name,dev_call,dev) )
print_func( ('info',f'devices mismatch:{path},{entry_name},{dev_call},{dev}'),True )
is_bind=True
else:
dev_call=dev

if is_dir:
if is_symlink :
has_files = False
size = 0
elif is_bind:
has_files = False
size = 0
else:
size,sub_sub_items = self_scan_rec(print_func,abort_list,path_join_loc(path,entry_name),dict_entry,filenames_set,check_dev,dev,include_hidden)
has_files = bool(sub_sub_items)

local_folder_size_with_subtree += size

local_folder_folders_count += 1
else:
has_files = False
size = int(stat_res.st_size)
self_header_ext_stats_size[ext]+=size
if is_symlink :
has_files = False
size = 0
else:
has_files = False
size = int(stat_res.st_size)
self_header_ext_stats_size[ext]+=size

local_folder_size += size
local_folder_size += size

local_folder_files_count += 1
local_folder_files_count += 1

temp_list_ref = scan_like_data[entry_name]=[size,is_dir,is_file,is_symlink,is_bind,has_files,mtime]
temp_list_ref = scan_like_data[entry_name]=[size,is_dir,is_file,is_symlink,is_bind,has_files,mtime]

if dict_entry:
temp_list_ref.append(dict_entry)
if dict_entry:
temp_list_ref.append(dict_entry)

self_header = self.header
self_header.sum_size += local_folder_size
self_header.quant_files += local_folder_files_count
self_header.quant_folders += local_folder_folders_count
self_header = self.header
self_header.sum_size += local_folder_size
self_header.quant_files += local_folder_files_count
self_header.quant_folders += local_folder_folders_count

print_func( ('scan',self_header.sum_size,self_header.quant_files,self_header.quant_folders,path) )
print_func( ('scan',self_header.sum_size,self_header.quant_files,self_header.quant_folders,path) )

except Exception as e:
print_func( ('error', f'scandir {path} error:{e}'),True )

return (local_folder_size_with_subtree+local_folder_size,subitems)

def scan(self,print_func,abort_list,cde_list,check_dev=True):
def scan(self,print_func,abort_list,cde_list,check_dev=True,include_hidden=False):
self.header.sum_size = 0

self.header.ext_stats=defaultdict(int)
Expand All @@ -499,7 +510,8 @@ def scan(self,print_func,abort_list,cde_list,check_dev=True):
#########################
time_start = perf_counter()
filenames_set=set()
self.scan_rec(print_func,abort_list,self.header.scan_path,self.scan_data,filenames_set,check_dev=check_dev)

self.scan_rec(print_func,abort_list,self.header.scan_path,self.scan_data,filenames_set,check_dev=check_dev,include_hidden=include_hidden)
time_end = perf_counter()

self.header.scanning_time = time_end-time_start
Expand Down Expand Up @@ -1335,8 +1347,6 @@ def prepare_info(self):
self.txtinfo_basic = self.txtinfo_basic + f'\n\n{loaded_fs_info}\n{loaded_cd_info}'

try:
longest = max({len(ext) for ext in self.header.ext_stats})+2

sublist=[]
for ext,ext_stat in sorted(self.header.ext_stats.items(),key = lambda x : x[1],reverse=True):
sublist.append(f'{bytes_to_str(self.header.ext_stats_size[ext]).rjust(12)} {fnumber(ext_stat).rjust(12)} {ext}')
Expand Down
40 changes: 28 additions & 12 deletions src/librer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
CFG_SORTING = 'sorting'
CFG_KEY_show_popups = 'show_popups'
CFG_KEY_groups_collapse = 'groups_collapse'
CFG_KEY_include_hidden = 'include_hidden'


cfg_defaults={
CFG_KEY_SINGLE_DEVICE:True,
Expand Down Expand Up @@ -148,7 +150,8 @@
CFG_last_dir:'.',
CFG_geometry:'',
CFG_KEY_show_popups:True,
CFG_KEY_groups_collapse:True
CFG_KEY_groups_collapse:True,
CFG_KEY_include_hidden:False
}

HOMEPAGE='https://github.com/PJDude/librer'
Expand Down Expand Up @@ -1749,16 +1752,9 @@ def get_settings_dialog(self):

sfdma = self.settings_dialog.area_main

self.show_popups_var = BooleanVar()
self.popups_cb = Checkbutton(sfdma,text=STR('Show tooltips'),variable=self.show_popups_var,command=self.popups_show_mod)
self.popups_cb.grid(row=0, column=0, sticky='news',padx=4,pady=4)

self.groups_collapsed_var = BooleanVar()
self.popups_cb = Checkbutton(sfdma,text=STR('Groups collapsed at startup'),variable=self.groups_collapsed_var,command=self.groups_collapse_mod)
self.popups_cb.grid(row=1, column=0, sticky='news',padx=4,pady=4)

lang_frame = Frame(sfdma)
lang_frame.grid(row=2, column=0, sticky='news',padx=4,pady=4)
lang_frame.grid(row=0, column=0, sticky='news',padx=4,pady=4)

Label(lang_frame,text=STR('Language:'),anchor='w').grid(row=2, column=0, sticky='wens',padx=8,pady=4)

Expand All @@ -1769,6 +1765,18 @@ def get_settings_dialog(self):

self.lang_cb.bind('<<ComboboxSelected>>', self.lang_change)

self.show_popups_var = BooleanVar()
self.popups_cb = Checkbutton(sfdma,text=STR('Show tooltips'),variable=self.show_popups_var,command=self.popups_show_mod)
self.popups_cb.grid(row=1, column=0, sticky='news',padx=4,pady=4)

self.groups_collapsed_var = BooleanVar()
self.popups_cb = Checkbutton(sfdma,text=STR('Groups collapsed at startup'),variable=self.groups_collapsed_var,command=self.groups_collapse_mod)
self.popups_cb.grid(row=2, column=0, sticky='news',padx=4,pady=4)

self.scan_hidden_var = BooleanVar()
self.scan_hidden_cb = Checkbutton(sfdma,text=STR('Include hidden files/folders in scan.'),variable=self.scan_hidden_var,command=self.scan_hidden_var_mod)
self.scan_hidden_cb.grid(row=3, column=0, sticky='news',padx=4,pady=4)

sfdma.grid_columnconfigure( 0, weight=1)

Button(self.settings_dialog.area_buttons, text=STR('Close'), width=14, command=self.settings_close ).pack(side='right', anchor='n',padx=5,pady=5)
Expand All @@ -1777,6 +1785,8 @@ def get_settings_dialog(self):

self.show_popups_var.set(self.cfg.get(CFG_KEY_show_popups))
self.groups_collapsed_var.set(self.cfg.get(CFG_KEY_groups_collapse))
self.scan_hidden_var.set(self.cfg.get(CFG_KEY_include_hidden))
self.lang_var.set(self.cfg_get(CFG_lang))

return self.settings_dialog

Expand All @@ -1790,6 +1800,9 @@ def popups_show_mod(self):
def groups_collapse_mod(self):
self.cfg.set(CFG_KEY_groups_collapse,self.groups_collapsed_var.get())

def scan_hidden_var_mod(self):
self.cfg.set(CFG_KEY_include_hidden,self.scan_hidden_var.get())

def settings_close(self):
self.settings_dialog.hide()

Expand Down Expand Up @@ -2665,7 +2678,6 @@ def exit(self):
@block
def settings_show(self):
dialog = self.get_settings_dialog()
self.lang_var.set(self.cfg_get(CFG_lang))

dialog.show(STR('Settings'))

Expand Down Expand Up @@ -3840,6 +3852,8 @@ def scan_wrapper(self):
compression_level = self.scan_compr_var_int.get()
threads = self.scan_threads_var_int.get()

include_hidden=self.cfg.get(CFG_KEY_include_hidden)

try:
if self.scan(compression_level,threads,group):
self.scan_dialog_hide_wrapper()
Expand Down Expand Up @@ -4025,9 +4039,11 @@ def scan(self,compression_level,threads,group=None):

#################################################################################################################################################

include_hidden=self.cfg.get(CFG_KEY_include_hidden)

try:
with open(sep.join([self.temp_dir,SCAN_DAT_FILE]), "wb") as f:
f.write(ZstdCompressor(level=8,threads=1).compress(dumps([new_label,path_to_scan_from_entry,check_dev,compression_level,threads,cde_list])))
f.write(ZstdCompressor(level=8,threads=1).compress(dumps([new_label,path_to_scan_from_entry,check_dev,compression_level,threads,cde_list,include_hidden])))

except Exception as e:
print(e)
Expand Down Expand Up @@ -4714,7 +4730,7 @@ def single_record_show(self,record,expand_groups=True):
self.tree_on_select()

records_len=len(librer_core.records)
self.status_records_all_configure(f'Records:{records_len}')
self.status_records_all_configure(STR('Records') + f':{records_len}')

self.record_filename_to_record[record.file_name] = record

Expand Down
4 changes: 2 additions & 2 deletions src/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def proper_exit(code):
with open(sep.join([comm_dir,SCAN_DAT_FILE]),"rb") as f:
create_list = loads(ZstdDecompressor().decompress(f.read()))

label,path_to_scan,check_dev,compression_level,threads,cde_list = create_list
label,path_to_scan,check_dev,compression_level,threads,cde_list,include_hidden = create_list
except Exception as e:
print_info(f'create error:{e}')
proper_exit(2)
Expand All @@ -345,7 +345,7 @@ def proper_exit(code):

try:
print_func(('stage',0),True)
new_record.scan(print_func,abort_list,tuple(cde_list),check_dev)
new_record.scan(print_func,abort_list,tuple(cde_list),check_dev,include_hidden)
except Exception as fe:
print_info(f'scan error:{fe}')
else:
Expand Down
7 changes: 5 additions & 2 deletions src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class LANGUAGES:
'If you abort at this stage,\nData record will not be created.':{
"pl":'Jeżeli przerwiesz na tym etapie,\nRekord danych nie zostanie utworzony.'},
'CDE Total space:':{
"pl":'Całkowita przestrzeń danych CDE'},
"pl":'Ilość danych CDE'},
'CDE Files number:':{
"pl":'Liczba plików CDE'},
'Creating new data record (scanning)':{
Expand Down Expand Up @@ -341,7 +341,10 @@ class LANGUAGES:
"pl":'Wymagany restart aplikacji.'},
'Language:':{
"pl":'Język:'},

'Records':{
"pl":'Rekordy'},
'Include hidden files/folders in scan.':{
"pl":'Uwzględniaj pliki/foldery ukryte.'},
}

def __init__(self):
Expand Down

0 comments on commit 2eef1e1

Please sign in to comment.