Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import wx
- import os
- import fileinput
- import sys
- def clearfile(path):
- file = open(path, 'w')
- file.write(u' ')
- file.close()
- def del_regexp(file, string):
- rst = []
- with open(file) as fd:
- t = fd.read()
- for line in t.splitlines():
- if line != string:
- rst.append(line)
- with open(file, 'w') as fd:
- fd.write(u'\n'.join(rst))
- fd.write(u'\n')
- def replaceAll(file, searchExp,replaceExp):
- for line in fileinput.input(file, inplace=1):
- if searchExp in line:
- line = line.replace(searchExp,replaceExp)
- sys.stdout.write(line)
- class StrangeFileWorker(dict):
- def load_file(self, file_name):
- with open(file_name) as fd:
- self.load_string(fd.read())
- def load_string(self, data):
- cur_key = ''
- for ln in filter(lambda a: a, data.split('\n')):
- if ln.endswith(':'):
- cur_key = ln.strip(':')
- self.setdefault(cur_key, [])
- elif cur_key:
- self[cur_key].append(ln)
- def __repr__(self):
- #return '\n'.join(map(lambda kv: '%s:\n%s\n' % (kv[0], '\n'.join(kv[1])), self.iteritems()))
- return '\n'.join(map(lambda kv: '\n%s:\n%s\n' % (kv[0], '\n'.join(kv[1])), self.iteritems()))
- def write_regexp(file, table, regexp):
- sfw = StrangeFileWorker()
- fd = open(file)
- data = fd.read()
- #data = data.decode('cp1251')
- sfw.load_string(data)
- sfw[table].append(regexp)
- data = repr(sfw) # получили представление объекта
- #data = data.encode('cp1251') # вспомнили про кодировку
- fd.write(data) # записали
- fd.close()
- #===============================================================================
- # def write_regexp(file, table, regexp):
- # result = list()
- # sfw = StrangeFileWorker()
- # store = False
- # with open(file) as fd:
- # for line in fd:
- # line = line.strip(' \n\r:')
- # if result and not line:
- # break
- # if line == table:
- # store = True
- # continue
- # if line == ' ':
- # file.write(regexp)
- #===============================================================================
- def get_regexp(file, table):
- sfw = StrangeFileWorker()
- list = []
- fd = open(file)
- data = fd.read()
- #data = data.decode('cp1251')
- sfw.load_string(data)
- if table in sfw:
- for reg_exp in sfw[table]:
- list.append(reg_exp)
- else:
- list = []
- return list
- data = repr(sfw) # получили представление объекта
- #data = data.encode('cp1251') # вспомнили про кодировку
- fd.write(data) # записали
- fd.close()
- #===============================================================================
- # def get_regexp(file, table):
- # sfw = StrangeFileWorker()
- # result = list()
- # store = False
- # fd = open(file) # Открыли файл
- # data = fd.read() #прочитали
- # data = data.decode('utf8') #преобразовали кодировку
- # sfw.load_string(data) #скормили в обьект
- # for line in data:
- # line = line.strip(' \n\r:')
- # if result and not line:
- # break
- # if line == table:
- # store = True
- # continue
- # if store:
- # result = repr(sfw) # получили представление объекта
- # result = result.encode('utf8') # вспомнили про кодировку
- # result.append(line)
- # return result
- #===============================================================================
- def readfile(path):
- file = open(path, 'r')
- file = file.readlines()
- for i in xrange(len(file) - 1): file[i] = file[i][0:-1]
- return file
- def writefile(table, regexp, path):
- file = open(path, 'a')
- file.write((u'\n%s:' % table))
- file.write((u'\n%s' % regexp))
- return file
- class dictpick_files(wx.Dialog):
- def __init__(self):
- wildcard = 'All files (*.*)|*.*'
- self.dialog = wx.FileDialog(None, 'Choose a file', os.getcwd(),
- '', wildcard, wx.OPEN)
- if self.dialog.ShowModal() == wx.ID_OK:
- self.Path = self.dialog.GetPath()
- self.dialog.Destroy()
- self.dialog.Destroy()
- def readfile_dlg(self, file, path):
- file = open(path, 'r')
- file = file.readlines()
- for i in xrange(len(file) - 1): file[i] = file[i][0:-1]
- return file
- def writefile_dlg(self, regexp):
- self.file = open(('%s' % (self.Path)), 'a')
- self.file.write((u'\n%s' % regexp))
- return self.file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement