utils.py
5.23 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
import os
import re
import wx
import threading
import time
import i18n
_ = i18n.language.ugettext #use ugettext instead of getttext to avoid unicode errors
import wx.lib.mixins.listctrl as listmix
from ConfigDialog import ConfigDialog
import const
def match_ext(name, exts):
return any(name.endswith(ext) for ext in exts)
def cut_ext(name, exts):
return re.sub("(%s)$" % "|".join(exts), "", name)
def normal_files(work_dir, exts):
names = set(cut_ext(name, exts)
for name in os.listdir(work_dir)
if match_ext(name, exts))
return sorted(names)
def super_files(work_dir, exts):
names = []
for name in os.listdir(work_dir):
path = os.path.join(work_dir, name)
if os.path.isdir(path):
local = set(cut_ext(name, exts)
for name in os.listdir(path)
if match_ext(name, exts))
names.extend(os.path.join(name, tail) for tail in local)
return sorted(names)
def all_files(work_dir, exts):
return normal_files(work_dir, exts) + super_files(work_dir, exts)
def get_entries(get_names, work_dir, exts, ready, footprints):
names = get_names(work_dir, exts)
result = []
for file_name in names:
status = _("Modified") if footprints.modified(file_name) else "--"
is_ready = _("Yes") if file_name in ready else _("No")
stage = "--"
result.append((file_name, status, is_ready, stage))
return result
def normal_entries(work_dir, exts, ready, footprints):
return get_entries(normal_files, work_dir, exts, ready, footprints)
def super_entries(work_dir, exts, ready, footprints):
return get_entries(super_files, work_dir, exts, ready, footprints)
def differ(data1, data2):
if len(data1) != len(data2):
return True
d1={}
for i in range(len(data1)):
d1[data1[i][0]]=data1[i][1:]
for i in range(len(data2)):
if not d1.has_key(data2[i][0]):
return True
if d1[data2[i][0]] != data2[i][1:]:
return True
return False
def change_ready(ready, file_list, pos):
file_name = file_list.GetFilename(pos)
if file_list.IsReady(pos):
file_list.SetReadyNo(pos)
ready.remove_file(file_name)
else:
file_list.SetReadyYes(pos)
ready.save_file(file_name)
def change_ready_prim(ready, super_file_list, pos):
def set_no(dirname):
for i in range(super_file_list.GetItemCount()):
other_name = super_file_list.GetFilename(i)
if os.path.dirname(other_name) == dirname:
super_file_list.SetReadyNo(i)
if other_name in ready.get_entries():
ready.remove_file(other_name)
file_name = super_file_list.GetFilename(pos)
if super_file_list.IsReady(pos):
super_file_list.SetReadyNo(pos)
ready.remove_file(file_name)
else:
set_no(os.path.dirname(file_name))
super_file_list.SetReadyYes(pos)
ready.save_file(file_name)
def create_dir_config(path, parent, config=None):
dialog = ConfigDialog(parent, -1)
if config:
dialog.login_ctrl.SetValue(config["login"])
dialog.passwd_ctrl.SetValue(config["passwd"])
dialog.exts_ctrl.SetValue(config["extensions"])
else:
dialog.exts_ctrl.SetValue(" ".join(const.default_exts))
try:
if dialog.ShowModal() == wx.ID_OK:
login = dialog.login_ctrl.GetValue()
password = dialog.passwd_ctrl.GetValue()
file_exts = dialog.exts_ctrl.GetValue()
with open(path, "w") as conf:
print >> conf, "login =", login
print >> conf, "passwd =", password
print >> conf, "extensions =", file_exts
return True
finally:
dialog.Destroy()
return False
def fill_dir_menu(main_frame, cache):
dir_menu = main_frame.dir_menu
last_dir_set=False
for entry in cache:
if not os.path.isdir(entry):
continue
wx_id = wx.NewId()
dir_menu.Append(wx_id, entry, kind=wx.ITEM_NORMAL)
main_frame.Bind(wx.EVT_MENU, main_frame.open_directory, id=wx_id)
if not last_dir_set:
cmd = wx.CommandEvent(wx.EVT_MENU.evtType[0])
cmd.SetEventObject(main_frame)
cmd.SetId(wx_id)
main_frame.GetEventHandler().ProcessEvent(cmd)
last_dir_set = True
# Watek odswieza informacje o zawartosci pliku to-checkin.txt
# (moze sie zmienic za posrednictwem TrEda lub wyedytowany recznie).
class RefreshThread (threading.Thread):
def __init__(self, manager, delay=2):
self.exitFlag = False
self.delay = delay
self.manager = manager
threading.Thread.__init__(self)
def run(self):
while self.exitFlag != True:
wx.CallAfter(self.manager.refresh)
time.sleep(self.delay)
# self.mtime = None # message file 'modified time'
# while self.exitFlag != True:
# new_mtime = os.stat(self.ready_path).st_mtime
# if new_mtime != self.mtime:
# wx.CallAfter(self.manager.refresh)
# self.mtime = new_mtime
# time.sleep(self.delay)