def get_user_opt(self): args = Base.get_user_input('hcf:r:s:t:') if r'-h' in args: Base.print_help(self.help_menu) if r'-c' in args: self._combine_files = True if r'-f' in args: fmt = args['-f'].lower() if fmt == TYPE_TXT or fmt == TYPE_CSV: self._fmt = fmt else: Base.print_exit("Error, unsupport format!") if r'-r' in args: if args['-r'] == r'True': self._sort_reverse = True else: self._sort_reverse = False if r'-s' in args: # set _src to list if self._src == None: self._src = list() # get src files fs = self.get_src_files(os.path.abspath(args['-s'])) if fs: # add fs to _src for f in fs: self._src.append(f) if r'-t' in args: ftype = File.get_filetype(os.path.abspath(args['-t'])) if ftype in [TYPE_TXT, TYPE_CSV]: self._tgt = os.path.abspath(args['-t']) self._combine_files = True self._fmt = ftype else: self._tgt = os.path.abspath(args['-t'])
def get_output_name(self, fmt): if File.get_filetype(self._tgt) == fmt: name = self._tgt elif self._combine_files == True: name = r'%s/%s.%s' % (self._tgt, u'日历', fmt.lower()) else: name = r'%s/%s.%s' % (self._tgt, self._cal_name, fmt.lower()) # return name of file. return name
def get_src_files(cls, path): fs = None if os.path.exists(path) == True: if os.path.isfile(path) == True and File.get_filetype(path) == TYPE_ICS: fs = list() fs.append(path) elif os.path.isdir(path) == True: fs = ICSCalendar.get_ics_files(path) # return result. return fs
def get_ics_files(cls, path): fs = None for root, dirs, files in os.walk(path): if len(files) != 0: for f in files: if File.get_filetype(f) == TYPE_ICS: if fs == None: fs = list() fs.append(os.path.join(root, f)) # return all of files. return fs
def clean_pyc(path=os.getenv('MYPY'), show=False): for rt, dr, fs in os.walk(path): if fs: for f in fs: f = os.path.join(rt, f) if File.get_exname(f) == '.pyc': os.remove(f) if show: print('remove: %s' % f) if dr: for d in dr: if d == '__pycache__': os.remove(d)
def get_url_content(cls, url, retry_times=3, view=True, path=None): if cls.url_is_https(url): content = cls.get_html(url=url, context=cls.CONTEXT_UNVERIFIED, retry_times=retry_times, view=view) else: content = cls.get_html(url=url, retry_times=retry_times, view=view) # save content to path. if all((content, path)): Path.make_path(path) f = '%s/%s' % (path, cls.convert_url_to_title(url)) if File.get_exname(f) != '.html': f = f + '.html' with open(f, 'w') as fd: fd.write(content) return content
def unzip_wiz(self): for f in self._fs: path = os.path.join( os.path.dirname(f).replace(self._src, self._dst), File.get_fname(f)) path = os.path.splitext(path)[0] Path.make_path(path) self.unzip_file(f, path) # remove small image. Image.remove_small_image(path) # move image. if os.path.exists('%s/index_files' % path): for ff in os.listdir('%s/index_files' % path): if Image.image_file('%s/index_files/%s' % (path, ff)): shutil.copyfile('%s/index_files/%s' % (path, ff), '%s/%s' % (path, ff)) # remove invalid files and dirs. shutil.rmtree('%s/index_files' % path) if os.path.exists('%s/index.html' % path): os.remove('%s/index.html' % path)
def reclaim_image(cls, f, obj=None, xfunc=None): fname = f if obj: img = obj else: img = cls.image_file(f) if img: fmt = img.format.lower() if fmt == 'jpeg': fmt = 'jpg' ftype = File.get_filetype(f) if not ftype: # no ext name fname = '%s.%s' % (f, fmt) elif fmt != ftype: fname = re.sub(ftype, fmt, f) try: os.rename(f, fname) except OSError as e: print('%s, failed to rename %s.' % (str(e), f)) # run xfunc if xfunc: xfunc(fname)
def convert_url_to_title(cls, url): return File.reclaim_name(re.sub('/$', '', url))
def image_file2(cls, f): exname = File.get_exname(f) if exname in ['.jpg', '.png', '.gif', '.jpeg', '.bmp']: return True else: return False
def get_all_of_wiz(self): for root, dirs, fs in os.walk(self._src): if len(fs) != 0: for f in fs: if File.get_exname(f) == '.ziw': self._fs.append(os.path.join(root, f))