ConDict

Check-in [8ed6cdeb7b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:start add-function
Timelines: family | ancestors | descendants | both | testing
Files: files | file ages | folders
SHA1:8ed6cdeb7b8ee944de6751c6c076e6ecfbdbf689
User & Date: zorro 2012-09-06 18:08:49
Context
2012-09-08
11:23
finish add pattern check-in: f13acd25c6 user: zorro tags: testing
2012-09-06
18:08
start add-function check-in: 8ed6cdeb7b user: zorro tags: testing
2012-09-05
18:18
create func pattern for edit/add/del check-in: d3d59fd363 user: zorro tags: testing
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to condt.py.

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
..
61
62
63
64
65
66
67

68
69
70
71
72
73
74
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
232



233
234
235
236
237















































238
239
240
241
    """Condt - class for ConDict"""
    COMMANDS = {'.help': {'desc': 'list commands', 'command': None}, 
        '.chname': {'desc': 'change current user name', 'command': None},
        '.chpassword': {'desc': 'change current password', 'command': None},
        '.list': {'desc': 'list users words', 'command': None},
        '.en': {'desc': 'dictionary mode English to Russian', 'command': None},
        '.ru': {'desc': 'dictionary mode Russian to English', 'command': None},
        # '.add': {'desc': 'add new words', 'command': None},
        # '.edit': {'desc': 'edit words', 'command': None},
        # '.del': {'desc': 'delete words', 'command': None},
        '.exit': {'desc': 'quit from program', 'command': None},
        }
    def __init__(self, name, dbfile):
        super().__init__(name, dbfile)       
        self.__pcounter = 3
................................................................................
        self.COMMANDS['.help']['command'] = self.command_help
        self.COMMANDS['.exit']['command'] = self.command_exit
        self.COMMANDS['.chname']['command'] = self.command_chname
        self.COMMANDS['.chpassword']['command'] = self.command_chpassword
        self.COMMANDS['.list']['command'] = self.command_list
        self.COMMANDS['.en']['command'] = self.command_en
        self.COMMANDS['.ru']['command'] = self.command_ru


    def hash_pass(self, password):
        result = bytes(password.strip() + SALT, 'utf-8')
        result = bytes(hashlib.md5(result).hexdigest(), 'utf-8')
        return hashlib.sha1(result).hexdigest()

    def check_name(self, cur):
................................................................................
                self.password = password1
                print("You password updated successfully")
                break
        cur.close()
        return 'chpassword'

    def command_en(self, text):
        self.command_enru(text, 'en')
        return 'en'
    def command_ru(self, text):
        self.command_enru(text, 'ru')
        return 'ru'
    def command_enru(self, text, tr_type):
        result = get_translate(text, tr_type)
        if not result or result['code'] != 200:
            return "Error, not foud translate"
        print(result['text'])
        return 0

    def command_list(self, pattern=None):
        cur = self.connect.cursor()
        sql_list = "SELECT `translate`.`id`, `term`.`en`, `translate`.`rus`, `progress`.`all`, `progress`.`error` FROM `translate` LEFT JOIN `term` ON (`translate`.`term`=`term`.`token`) LEFT JOIN `progress` ON (`progress`.`translate_id`=`translate`.`id`) WHERE `translate`.`user_id`=(?) "
        params = (self.user_id,)
        result_text, result_param = "Get {0} rows", [0]
        if pattern:
            sql_list += " AND `term`.`en` LIKE (?)"
            params = (self.user_id, pattern + '%')
            result_text = "Get {0} rows for pattern '{1}%'"
            result_param.append(pattern)
        sql_list += " ORDER BY `translate`.`created` DESC"

        cur.execute(sql_list, params)
        i = 1
        for row in cur.fetchall():
            print("{0}. ID={1} all {2}, error {3}".format(i, row[0], row[3], row[4]))
            print("\t(en) {0}\n\t(ru) {1}".format(row[1], row[2]))
            i +=1
        result_param[0] = i - 1
        print(result_text.format(*result_param))



        cur.close()
        return 'list'
    
    def command_add(self, en_words=None):
        pass















































    def command_edit(self, translate_id):
        pass
    def command_delete(self, id_or_pattern):
        pass







|







 







>







 







|


|





|
<












>
|
|
|
|
|
|
|
|
>
>
>




<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
..
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    """Condt - class for ConDict"""
    COMMANDS = {'.help': {'desc': 'list commands', 'command': None}, 
        '.chname': {'desc': 'change current user name', 'command': None},
        '.chpassword': {'desc': 'change current password', 'command': None},
        '.list': {'desc': 'list users words', 'command': None},
        '.en': {'desc': 'dictionary mode English to Russian', 'command': None},
        '.ru': {'desc': 'dictionary mode Russian to English', 'command': None},
        '.add': {'desc': 'add new words', 'command': None},
        # '.edit': {'desc': 'edit words', 'command': None},
        # '.del': {'desc': 'delete words', 'command': None},
        '.exit': {'desc': 'quit from program', 'command': None},
        }
    def __init__(self, name, dbfile):
        super().__init__(name, dbfile)       
        self.__pcounter = 3
................................................................................
        self.COMMANDS['.help']['command'] = self.command_help
        self.COMMANDS['.exit']['command'] = self.command_exit
        self.COMMANDS['.chname']['command'] = self.command_chname
        self.COMMANDS['.chpassword']['command'] = self.command_chpassword
        self.COMMANDS['.list']['command'] = self.command_list
        self.COMMANDS['.en']['command'] = self.command_en
        self.COMMANDS['.ru']['command'] = self.command_ru
        self.COMMANDS['.add']['command'] = self.command_add

    def hash_pass(self, password):
        result = bytes(password.strip() + SALT, 'utf-8')
        result = bytes(hashlib.md5(result).hexdigest(), 'utf-8')
        return hashlib.sha1(result).hexdigest()

    def check_name(self, cur):
................................................................................
                self.password = password1
                print("You password updated successfully")
                break
        cur.close()
        return 'chpassword'

    def command_en(self, text):
        print(self.command_enru(text, 'en'))
        return 'en'
    def command_ru(self, text):
        print(self.command_enru(text, 'ru'))
        return 'ru'
    def command_enru(self, text, tr_type):
        result = get_translate(text, tr_type)
        if not result or result['code'] != 200:
            return "Error, not foud translate"
        return result['text']


    def command_list(self, pattern=None):
        cur = self.connect.cursor()
        sql_list = "SELECT `translate`.`id`, `term`.`en`, `translate`.`rus`, `progress`.`all`, `progress`.`error` FROM `translate` LEFT JOIN `term` ON (`translate`.`term`=`term`.`token`) LEFT JOIN `progress` ON (`progress`.`translate_id`=`translate`.`id`) WHERE `translate`.`user_id`=(?) "
        params = (self.user_id,)
        result_text, result_param = "Get {0} rows", [0]
        if pattern:
            sql_list += " AND `term`.`en` LIKE (?)"
            params = (self.user_id, pattern + '%')
            result_text = "Get {0} rows for pattern '{1}%'"
            result_param.append(pattern)
        sql_list += " ORDER BY `translate`.`created` DESC"
        try:
            cur.execute(sql_list, params)
            i = 1
            for row in cur.fetchall():
                print("{0}. ID={1} all {2}, error {3}".format(i, row[0], row[3], row[4]))
                print("\t(en) {0}\n\t(ru) {1}".format(row[1], row[2]))
                i +=1
            result_param[0] = i - 1
            print(result_text.format(*result_param))
        except (sqlite3.DatabaseError, IncorrectDbData) as er:
            print('Sorry, error')
            return 'list'
        cur.close()
        return 'list'
    
    def command_add(self, en_words=None):

        cur = self.connect.cursor()
        print('Please enter your patterns:')
        while True:
            try:
                en = input('En [' + en_words + ']:') if en_words else input('En: ')
                if not en:
                    if not en_words:
                        raise IncorrectDbData()
                    else:
                        en = en_words
                try:
                    ru_words = self.command_enru(en, 'en')[0]
                except Exception as e:
                    if DEBUG: print(e)
                    ru_words = None
                ru = input('Ru [' + ru_words + ']:') if ru_words else input('Ru: ')
                if not ru:
                    if not ru_words:
                        raise IncorrectDbData()
                    else:
                        ru = ru_words
                ru = ru.lower().strip()
                en = en.lower().strip()
                token = hashlib.md5(bytes(en, 'utf-8')).hexdigest()
                # search token
                sql_list = "SELECT `token` FROM `term` WHERE `token`=(?)"
                cur.execute(sql_list, (token,))
                if cur.fetchone():
                    print('Words add already.')
                    break
                sql_list1 = "INSERT INTO `term` (`token`, `en`) VALUES ((?), (?))"
                cur.execute(sql_list1, (token, en))
                sql_list2 = "INSERT INTO `translate` (`term`, `user_id`, `rus`) VALUES (?, ?, ?)"
                cur.execute(sql_list2, (token, self.user_id, ru))
            except (sqlite3.DatabaseError, IncorrectDbData) as er:
                if DEBUG: print(er)
                print('Incorrect information, change data')
                ent = input('Do you wand enter new words [Y/n]?')
                if ent in ('n', 'N'):
                    break
                continue
            else:
                self.connect.commit()
                break
        cur.close()
        return 'add'

    def command_edit(self, translate_id):
        pass
    def command_delete(self, id_or_pattern):
        pass

Changes to db.sqlite.

cannot compute difference between binary files