forked from crftwr/cfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfiler_misc.py
144 lines (107 loc) · 4.33 KB
/
cfiler_misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sys
import os
import shutil
import datetime
import pyauto
import ckit
from ckit.ckit_const import *
import cfiler_native
import cfiler_resource
import cfiler_debug
## @addtogroup misc
## @{
#--------------------------------------------------------------------
ignore_1second = True
#--------------------------------------------------------------------
class candidate_Filename():
def __init__( self, basedir, fixed_items=[] ):
self.basedir = basedir
self.fixed_items = fixed_items
def __call__( self, update_info ):
left = update_info.text[ : update_info.selection[0] ]
pos_dir = max(left.rfind("/")+1,left.rfind("\\")+1)
directory = left[:pos_dir]
directory_lower = directory.lower()
name_prefix = left[pos_dir:].lower()
dirname_list = []
filename_list = []
for item in self.fixed_items:
item_lower = item.lower()
if item_lower.startswith(directory_lower):
item_lower = item_lower[ len(directory_lower) : ]
if item_lower.startswith(name_prefix) and len(item_lower)!=len(name_prefix):
filename_list.append( item[ len(directory_lower) : ] )
path = ckit.joinPath( self.basedir, directory )
drive, tail = os.path.splitdrive(path)
unc = ( drive.startswith("\\\\") or drive.startswith("//") )
if unc:
checkNetConnection(path)
if unc and not tail:
servername = drive.replace('/','\\')
try:
infolist = cfiler_native.enumShare(servername)
except WindowsError:
infolist = []
for info in infolist:
if info[1]==0:
if info[0].lower().startswith(name_prefix):
if ckit.pathSlash():
dirname_list.append( info[0] + "/" )
else:
dirname_list.append( info[0] + "\\" )
else:
try:
infolist = cfiler_native.findFile( ckit.joinPath(path,'*'), use_cache=True )
except WindowsError:
infolist = []
for info in infolist:
if info[0].lower().startswith(name_prefix):
if info[3] & ckit.FILE_ATTRIBUTE_DIRECTORY:
if ckit.pathSlash():
dirname_list.append( info[0] + "/" )
else:
dirname_list.append( info[0] + "\\" )
else:
filename_list.append( info[0] )
return dirname_list + filename_list, len(directory)
#--------------------------------------------------------------------
def getFileSizeString(size):
if size < 1000:
return "%dB" % ( size, )
if size < 1000*1024:
s = "%.1fK" % ( size / float(1024), )
if len(s)<=6 : return s
if size < 1000*1024*1024:
s = "%.1fM" % ( size / float(1024*1024), )
if len(s)<=6 : return s
if size < 1000*1024*1024*1024:
s = "%.1fG" % ( size / float(1024*1024*1024), )
if len(s)<=6 : return s
return "%.1fT" % ( size / float(1024*1024*1024*1024), )
#--------------------------------------------------------------------
_net_connection_handler = None
def registerNetConnectionHandler(handler):
global _net_connection_handler
_net_connection_handler = handler
def checkNetConnection(path):
drive, tail = os.path.splitdrive(path)
unc = ( drive.startswith("\\\\") or drive.startswith("//") )
if unc:
remote_resource_name = drive.replace('/','\\').rstrip('\\')
try:
_net_connection_handler(remote_resource_name)
except Exception as e:
cfiler_debug.printErrorInfo()
print( e )
#--------------------------------------------------------------------
def compareTime( t1, t2 ):
if type(t1)!=type(t2):
raise TypeError("two arguments are not compatible.")
if ignore_1second:
delta = abs( datetime.datetime(*t1) - datetime.datetime(*t2) )
if delta.days==0 and delta.seconds<=1 : return 0
if t1<t2: return -1
elif t1>t2: return 1
else: return 0
#--------------------------------------------------------------------
## @} misc