88
99logging .basicConfig (filename = 'copy.log' , level = logging .INFO )
1010
11- def _get_file_details (src_file_path ):
12- modification_time = os .path .getmtime (src_file_path )
13- modification_date = datetime .fromtimestamp (modification_time )
14- ad_year = modification_date .year
15- ad_month = modification_date .month
16- ad_day = modification_date .day
17- return modification_date , ad_year , ad_month , ad_day
18-
19- def _copy_machine (filename , src_file_path , dest_folder ):
20- log_message = f"{ datetime .now ()} ^ Copying|||{ filename } |||to|||{ dest_folder } "
21- print (log_message )
22- logging .info (log_message )
23- try :
24- shutil .copy2 (src_file_path , dest_folder )
25- return True
26- except PermissionError :
27- logging .error (f"{ datetime .now ()} ^ Your|||system|||does|||not|||allow|||to|||use|||this|||folder" )
28- return False
2911
3012
3113class CopyModel :
@@ -35,6 +17,25 @@ def __init__(self, controller):
3517 def _update_progress_bar (self , items_count , items_copied ):
3618 percentage = items_copied / items_count
3719 self .controller .update_progress_bar (percentage )
20+
21+ def _get_file_details (self , src_file_path ):
22+ modification_time = os .path .getmtime (src_file_path )
23+ modification_date = datetime .fromtimestamp (modification_time )
24+ ad_year = modification_date .year
25+ ad_month = modification_date .month
26+ ad_day = modification_date .day
27+ return modification_date , ad_year , ad_month , ad_day
28+
29+ def _copy_machine (self , filename , src_file_path , dest_folder ):
30+ log_message = f"{ datetime .now ()} ^ Copying|||{ filename } |||to|||{ dest_folder } "
31+ print (log_message )
32+ logging .info (log_message )
33+ try :
34+ shutil .copy2 (src_file_path , dest_folder )
35+ return True
36+ except PermissionError :
37+ logging .error (f"{ datetime .now ()} ^ Your|||system|||does|||not|||allow|||to|||use|||this|||folder" )
38+ return False
3839
3940 def _valid_dir (self , src_folder ):
4041 try :
@@ -45,17 +46,17 @@ def _valid_dir(self, src_folder):
4546 return False
4647
4748 def _create_folders (self , dest_folder , mode , month_name , ad_year , persian_year = 0 ):
48- if mode == "georgian" or mode == "georgian_persian" :
49+ if mode == COPY_MODES [ "gg" ] or mode == COPY_MODES [ "gp" ] :
4950 parent_year_folder = os .path .join (dest_folder , str (ad_year ))
5051 else :
5152 parent_year_folder = os .path .join (dest_folder , str (persian_year ))
5253
5354 if not os .path .exists (parent_year_folder ):
5455 os .makedirs (parent_year_folder )
5556
56- if mode == "georgian_persian" :
57+ if mode == COPY_MODES [ "gp" ] :
5758 sub_folder_format = f"{ PERSIAN_MONTHS_DICT .get (month_name , 0 )} - { month_name } { str (persian_year )} "
58- elif mode == "persian_persian" :
59+ elif mode == COPY_MODES [ "pp" ] :
5960 sub_folder_format = f"{ PERSIAN_MONTHS_DICT .get (month_name , 0 )} - { month_name } "
6061 else :
6162 sub_folder_format = f"{ GEORGIAN_MONTHS_DICT .get (month_name , 0 )} - { month_name } "
@@ -77,16 +78,16 @@ def copy_and_organize_files_shamsi_order_with_georgian_years(self, src_folder, d
7778 items_copied_count = 0
7879 for filename in listdir :
7980 src_file_path = os .path .join (src_folder , filename )
80- modification_date , ad_year , ad_month , ad_day = _get_file_details (src_file_path )
81+ modification_date , ad_year , ad_month , ad_day = self . _get_file_details (src_file_path )
8182
8283 persian_date = jdatetime .date .fromgregorian (year = ad_year , month = ad_month , day = ad_day )
8384 persian_year = persian_date .year
8485 persian_month = persian_date .month
8586 persian_month_name = persian_date .strftime ("%B" )
8687
87- destination_folder = self ._create_folders (dest_folder , "georgian_persian" , persian_month_name , ad_year , persian_year )
88+ destination_folder = self ._create_folders (dest_folder , COPY_MODES [ "gp" ] , persian_month_name , ad_year , persian_year )
8889
89- result = _copy_machine (filename , src_file_path , destination_folder )
90+ result = self . _copy_machine (filename , src_file_path , destination_folder )
9091 if not result :
9192 return False
9293 items_copied_count += 1
@@ -103,16 +104,16 @@ def copy_and_organize_files_shamsi_order(self, src_folder, dest_folder):
103104 items_copied_count = 0
104105 for filename in listdir :
105106 src_file_path = os .path .join (src_folder , filename )
106- modification_date , ad_year , ad_month , ad_day = _get_file_details (src_file_path )
107+ modification_date , ad_year , ad_month , ad_day = self . _get_file_details (src_file_path )
107108
108109 persian_date = jdatetime .date .fromgregorian (year = ad_year , month = ad_month , day = ad_day )
109110 persian_year = persian_date .year
110111 persian_month = persian_date .month
111112 persian_month_name = persian_date .strftime ("%B" )
112113
113- destination_folder = self ._create_folders (dest_folder , "persian_persian" , persian_month_name , ad_year , persian_year )
114+ destination_folder = self ._create_folders (dest_folder , COPY_MODES [ "pp" ] , persian_month_name , ad_year , persian_year )
114115
115- result = _copy_machine (filename , src_file_path , destination_folder )
116+ result = self . _copy_machine (filename , src_file_path , destination_folder )
116117 if not result :
117118 return False
118119 items_copied_count += 1
@@ -129,13 +130,13 @@ def copy_and_organize_files_georgian_order(self, src_folder, dest_folder):
129130 items_copied_count = 0
130131 for filename in listdir :
131132 src_file_path = os .path .join (src_folder , filename )
132- modification_date , ad_year , * other = _get_file_details (src_file_path )
133+ modification_date , ad_year , * other = self . _get_file_details (src_file_path )
133134
134135 ad_month_name = modification_date .strftime ("%B" )
135136
136- destination_folder = self ._create_folders (dest_folder , "georgian" , ad_month_name , ad_year )
137+ destination_folder = self ._create_folders (dest_folder , COPY_MODES [ "gg" ] , ad_month_name , ad_year )
137138
138- result = _copy_machine (filename , src_file_path , destination_folder )
139+ result = self . _copy_machine (filename , src_file_path , destination_folder )
139140 if not result :
140141 return False
141142 items_copied_count += 1
@@ -153,7 +154,7 @@ def simple_bulk_copy(self, src_folder, dest_folder):
153154 items_copied_count = 0
154155 for filename in listdir :
155156 src_file_path = os .path .join (src_folder , filename )
156- result = _copy_machine (filename , src_file_path , dest_folder )
157+ result = self . _copy_machine (filename , src_file_path , dest_folder )
157158 if not result :
158159 return False
159160 items_copied_count += 1
0 commit comments