def get_max_ver(fmt, filelist): """有一堆字符串,文件名均包含 %d.%d.%d 形式版本号,返回其中版本号最大的那个。 我一般用它来检测一堆发行版中版本号最大的那个文件。 :param str fmt: 要检测测字符串形式,例如 rookout-%s.tar.gz ,其中 %s 会被正则替换。 :param list files: 字符串列表。 :returns: 版本号最大的字符串。 :rtype: str """ x, y, z = 0, 0, 0 verpat = fmt % '(\d+).(\d+).(\d+)' verre = re.compile(r'' + verpat + '', re.M) for f in filelist: match = verre.search(f) if match: x1 = int(match.group(1)) y1 = int(match.group(2)) z1 = int(match.group(3)) if x1 >= x and y1 >= y: x = x1 y = y1 z = z1 verfmt = fmt % ('%d.%d.%d') name = verfmt % (x, y, z) if x == 0 and y == 0 and z == 0: slog.info('Can not find the string "%s" !' % name) return None return name
def _new_draft(self): name = None if self.args.query: name = self.args.query[0] try: dfile, dname = self.conf.get_new_draft(name) except WPError as e: slog.critical(e) return dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') metatxt = [] tpl = OrderedDict(metatpl) tpl['date'] = dt tpl['modified'] = dt tpl['author'] = self.conf.get_user() tpl['posttype'] = self.args.type if self.args.type == 'post': tpl['tags'] = '' tpl['category'] = 'technology' for k,v in tpl.items(): metatxt.append('%s: %s'%(k, v)) write_file(dfile, '\n'.join(metatxt)) slog.info('The draft file "%s" has created.'%dfile)
def upload_dir(dir_name, upload_dir, ftp_conf): check_ftp_conf(ftp_conf) ftp, ftpStr = get_ftp(ftp_conf) if dir_name not in ftp.nlst(): ftp.mkd(dir_name) ftp.cwd(dir_name) slog.info('Uploading "%s" to "%s/%s" ......'%(upload_dir, ftpStr, dir_name)) rootLen = len(upload_dir) + 1 for r,d,fl in os.walk(upload_dir): if r.split('/')[-1].startswith('.'): continue for sdir in d: if not sdir.startswith('.'): dirPath = r[rootLen:] if sdir not in ftp.nlst(dirPath): dir_name = os.path.join(dirPath, sdir) ftp.mkd(dir_name) for sf in fl: filePath = os.path.join(r, sf) f = open(filePath, 'rb') ftpPath = filePath[rootLen:] slog.info('%s -> %s', filePath, ftpPath) ftp.storbinary('STOR %s'%ftpPath, f) f.close() ftp.quit()
def get_ftp(ftp_conf, debug=0): """得到一个 已经打开的FTP 实例,和一个 ftp 路径。 :param dict ftp_conf: ftp配置文件,格式如下: >>> { >>> 'server':'127.0.0.1', >>> 'start_path':None, >>> 'user':'******', >>> 'password':'******', >>> } :returns: ftp, ftpserverstr :rtype: :class:`ftplib.FTP` , str """ server = ftp_conf.get('server') user = ftp_conf.get('user') password = ftp_conf.get('password') start_path = ftp_conf.get('start_path') slog.info("Connecting FTP server %s ......", server) ftpStr = 'ftp://%s/' % server if start_path: ftpStr = ftpStr + start_path ftp = ftplib.FTP(server, user, password) ftp.set_debuglevel(debug) if start_path: ftp.cwd(start_path) serverFiles = ftp.nlst() slog.info('There are some files in %s:\n[%s]' % (ftpStr, ', '.join(serverFiles))) return ftp, ftpStr
def upload_file(file_path, remote_path, ftp_conf, remove_file=False): """上传第一个指定的文件到 FTP 服务器。 :param str file_path: 待上传文件的绝对路径。 :param str remote_path: 文件在 FTP 服务器上的相对路径(相对于 FTP 服务器的初始路径)。 :param dict ftp_conf: ftp配置文件,详见 :func:`get_ftp` 。 :param bool remove_file: 上传成功后是否删除本地文件。 :returns: FTP 服务器上的文件列表 :rtype: list """ check_ftp_conf(ftp_conf) ftp, ftpStr = get_ftp(ftp_conf) lf = open(file_path, 'rb') slog.info('Uploading "%s" to "%s/%s" ......' % (file_path, ftpStr, remote_path)) ftp.storbinary("STOR %s" % remote_path, lf) filelist = ftp.nlst() ftp.quit() lf.close() if remove_file: os.remove(file_path) slog.info('Upload done.') return filelist
def get_ftp(ftp_conf, debug=0): """得到一个 已经打开的FTP 实例,和一个 ftp 路径。 :param dict ftp_conf: ftp配置文件,格式如下: >>> { >>> 'server':'127.0.0.1', >>> 'start_path':None, >>> 'user':'******', >>> 'password':'******', >>> } :returns: ftp, ftpserverstr :rtype: :class:`ftplib.FTP` , str """ server = ftp_conf.get('server') user = ftp_conf.get('user') password = ftp_conf.get('password') start_path = ftp_conf.get('start_path') slog.info("Connecting FTP server %s ......", server) ftpStr = 'ftp://%s/'%server if start_path: ftpStr = ftpStr+start_path ftp = ftplib.FTP(server, user, password) ftp.set_debuglevel(debug) if start_path: ftp.cwd(start_path) serverFiles = ftp.nlst() slog.info('There are some files in %s:\n[%s]'%(ftpStr, ', '.join(serverFiles))) return ftp, ftpStr
def _new_draft(self): name = None if self.args.query: name = self.args.query[0] try: dfile, dname = self.conf.get_new_draft(name) except WPError as e: slog.critical(e) return dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') metatxt = [] tpl = OrderedDict(metatpl) tpl['date'] = dt tpl['modified'] = dt tpl['author'] = self.conf.get_user() tpl['posttype'] = self.args.type if self.args.type == 'post': tpl['tags'] = '' tpl['category'] = 'technology' for k, v in tpl.items(): metatxt.append('%s: %s' % (k, v)) write_file(dfile, '\n'.join(metatxt)) slog.info('The draft file "%s" has created.' % dfile)
def upload_dir(dir_name, upload_dir, ftp_conf): check_ftp_conf(ftp_conf) ftp, ftpStr = get_ftp(ftp_conf) if dir_name not in ftp.nlst(): ftp.mkd(dir_name) ftp.cwd(dir_name) slog.info('Uploading "%s" to "%s/%s" ......' % (upload_dir, ftpStr, dir_name)) rootLen = len(upload_dir) + 1 for r, d, fl in os.walk(upload_dir): if r.split('/')[-1].startswith('.'): continue for sdir in d: if not sdir.startswith('.'): dirPath = r[rootLen:] if sdir not in ftp.nlst(dirPath): dir_name = os.path.join(dirPath, sdir) ftp.mkd(dir_name) for sf in fl: filePath = os.path.join(r, sf) f = open(filePath, 'rb') ftpPath = filePath[rootLen:] slog.info('%s -> %s', filePath, ftpPath) ftp.storbinary('STOR %s' % ftpPath, f) f.close() ftp.quit()
def get_max_ver(fmt, filelist): """有一堆字符串,文件名均包含 %d.%d.%d 形式版本号,返回其中版本号最大的那个。 我一般用它来检测一堆发行版中版本号最大的那个文件。 :param str fmt: 要检测测字符串形式,例如 rookout-%s.tar.gz ,其中 %s 会被正则替换。 :param list files: 字符串列表。 :returns: 版本号最大的字符串。 :rtype: str """ x, y, z = 0,0,0 verpat = fmt%'(\d+).(\d+).(\d+)' verre = re.compile(r''+verpat+'', re.M) for f in filelist: match = verre.search(f) if match: x1 = int(match.group(1)) y1 = int(match.group(2)) z1 = int(match.group(3)) if x1 >= x and y1 >= y: x = x1 y = y1 z = z1 verfmt = fmt%('%d.%d.%d') name = verfmt%(x, y, z) if x == 0 and y == 0 and z == 0: slog.info('Can not find the string "%s" !'%name) return None return name
def print_results(self, results): if isinstance(results, list): for result in results: self.print_result(result) elif isinstance(results, dict): for k, v in results.items(): slog.info('%s %s' % (k, str(v))) else: self.print_result(results)
def _write_analytic(self): if self.args.dirname == 'post': postids = self.get_postid(as_list=True) filelist = [] for f in postids: if not os.path.exists(self.conf.get_work_path(self.args.dirname, f+'.md')): continue filelist.append(f) slog.info(filelist)
def _update_a_draft(self): postid = self.get_postid() if not postid: slog.warning('Please provide a post id!') return afile, aname = self.conf.get_draft(postid) # If output is provided, write the html to a file and abort. if self.args.output: self._write_html_file(afile) return html, meta, txt, medias = self._get_and_update_article_content(afile) if not html: return if meta.poststatus == 'draft': slog.warning( 'The post status of draft "%s" is "draft", ' 'please modify it to "publish".'%postid) return # Update all taxonomy before create a new article. self.get_terms_from_wp(['category']) self.get_terms_from_wp(['post_tag']) if meta.posttype == 'page': post = WordPressPage() else: post = WordPressPost() post.terms = self.cache.get_terms_from_meta(meta.category, meta.tags) if not post.terms: slog.warning('Please provide some terms.') return post.content= html post.title = meta.title post.slug = meta.nicename post.date = meta.date post.user = meta.author post.date_modified = meta.modified post.post_status = meta.poststatus post.comment_status = "open" post.ping_status = "open" postid = self.wpcall(NewPost(post)) if postid: write_by_templ(afile, afile, {'POSTID':postid, 'SLUG':postid}, True) else: return newfile, newname = None, None if meta.posttype == 'page': newfile, newname = self.conf.get_article(meta.nicename, meta.posttype) else: newfile, newname = self.conf.get_article(postid, meta.posttype) slog.info('Move "%s" to "%s".'%(afile, newfile)) shutil.move(afile, newfile)
def save_to_file(self, path, human=True): """将自身内容保存到文件。 :param str path: 保存的文件路径。 :param bool human: 参见 :func:`dump()` """ write_file(path, self.dump(human)) slog.info("Save %a done.", path)
def _show_medialib(self): field = {} field['number'] = self.args.number extra = self.get_dict_from_query(self.args.query) if extra: for k, v in extra.items(): field[k] = v slog.info("field:%s", field) return GetMediaLibrary(field)
def print_results(self, results): if isinstance(results, list): for result in results: self.print_result(result) elif isinstance(results, dict): for k,v in results.items(): slog.info('%s %s'%(k, str(v))) else: self.print_result(results)
def _show_medialib(self): field = {} field['number'] = self.args.number extra = self.get_dict_from_query(self.args.query) if extra: for k,v in extra.items(): field[k] = v slog.info("field:%s", field) return GetMediaLibrary(field)
def _write_analytic(self): if self.args.dirname == 'post': postids = self.get_postid(as_list=True) filelist = [] for f in postids: if not os.path.exists( self.conf.get_work_path(self.args.dirname, f + '.md')): continue filelist.append(f) slog.info(filelist)
def clone(giturl, gitpath): """clone 一个 git 库。 :param str giturl: git 仓库的 url 地址。 :param str gitpath: git 仓库保存路径。 """ gitArgs = ['git', 'clone', giturl, gitpath] slog.info(' '.join(gitArgs)) return subprocess.call(gitArgs)
def download_file(remote_path, file_path, ftp_conf): check_ftp_conf(ftp_conf) ftp, ftpstr = get_ftp(ftp_conf) lf = open(file_path, 'wb') slog.info('Downloading "%s/%s" to "%s" ......'%(ftpstr, remote_path, lf.name)) ftp.retrbinary('RETR %s'%remote_path, lf.write) ftp.quit() lf.close() slog.info('Download done.') return lf.name
def download_file(remote_path, file_path, ftp_conf): check_ftp_conf(ftp_conf) ftp, ftpstr = get_ftp(ftp_conf) lf = open(file_path, 'wb') slog.info('Downloading "%s/%s" to "%s" ......' % (ftpstr, remote_path, lf.name)) ftp.retrbinary('RETR %s' % remote_path, lf.write) ftp.quit() lf.close() slog.info('Download done.') return lf.name
def _check_posts(self): offset = 0 number = 20 results = self._check_post(offset, number) logfile = self.conf.get_work_path('output', 'check.txt') with open(logfile, 'w', encoding='utf-8', newline='\n') as f: while (results): for result in results: slog.info('id:%s, title:%s', result.id, result.title) if '\\"http' in result.content or '<pre lang="' in result.content: f.write(result.id + '|,|' + result.title + '\n') offset += number results = self._check_post(offset, number)
def _check_posts(self): offset = 0 number = 20 results = self._check_post(offset, number) logfile = self.conf.get_work_path('output', 'check.txt') with open(logfile, 'w', encoding='utf-8', newline='\n') as f: while(results): for result in results: slog.info('id:%s, title:%s', result.id, result.title) if '\\"http' in result.content or '<pre lang="' in result.content: f.write(result.id + '|,|' + result.title+'\n') offset += number results = self._check_post(offset, number)
def init(self, workdir): if os.path.exists(self.conffile): self.read_from_file() return True tplstr = read_file(resource_filename('wpcmd', Conf.TPL_FILE)) inistr = Template(tplstr).substitute({ 'CACHEFILE':Conf.CACHE_FILE, 'WORK':workdir, }) self.save_to_file(inistr) self.read_from_file() slog.info('Please modify %s !'%self.conffile) return False
def init(self, workdir): if os.path.exists(self.conffile): self.read_from_file() return True tplstr = read_file(resource_filename('wpcmd', Conf.TPL_FILE)) inistr = Template(tplstr).substitute({ 'CACHEFILE': Conf.CACHE_FILE, 'WORK': workdir, }) self.save_to_file(inistr) self.read_from_file() slog.info('Please modify %s !' % self.conffile) return False
def fmt(self, po_file, mo_file): """将 po 文件转换成 mo 文件。 :param string po_file: 待转换的 po 文件路径。 :param string mo_file: 目标 mo 文件的路径。 """ if not os.path.exists(po_file): slog.error('The PO file [%s] is non-existen!'%po_file) return txt = subprocess.check_output([self._msgfmt, '--check', "--strict", '--verbose', "--output-file", mo_file, po_file], stderr=subprocess.STDOUT, universal_newlines=True) slog.info(txt)
def fmt(self, po_file, mo_file): """将 po 文件转换成 mo 文件。 :param string po_file: 待转换的 po 文件路径。 :param string mo_file: 目标 mo 文件的路径。 """ if not os.path.exists(po_file): slog.error('The PO file [%s] is non-existen!' % po_file) return txt = subprocess.check_output([ self._msgfmt, '--check', "--strict", '--verbose', "--output-file", mo_file, po_file ], stderr=subprocess.STDOUT, universal_newlines=True) slog.info(txt)
def go(self): # print(self.args) if self.args.type == 'post': self._show_post() elif self.args.type == 'page': self._show_page() elif self.args.type == 'draft': for adir, aname, afile in self.conf.get_mdfiles('draft'): slog.info(afile) elif self.args.type == 'option': self._show_options() elif self.args.type == 'tax': self._show_tax() elif self.args.type in ('term', 'category', 'tag'): self._show_term() elif self.args.type == 'medialib': self._show_medialib() elif self.args.type == 'mediaitem': self._show_mediaitem()
def _new_term(self): if not self.args.query or len(self.args.query) < 1: slog.error('Provide 1 arguments at least please.') return query = self.get_term_query() # slog.info('self.args.query: %s', self.args.query) term = self.get_terms_from_wp(query, force=True) slog.info('Get term from WordPress, query: %s, result: %s', query, term) if term: slog.error('The term "%s" has been in wordpress.' % self.args.query[0]) return taxname = query[0] slug = self.args.query[0] name = self.args.query[1] if len(self.args.query) > 1 else slug term = WordPressTerm() term.slug = slug term.name = name term.taxonomy = taxname if len(self.args.query) > 2: term.description = self.args.query[2] termid = self.wpcall(NewTerm(term)) if not termid: return term = self.wpcall(GetTerm(taxname, termid)) if not term: return slog.info('The term %s(%s) has created.' % (name, termid)) self.cache.save_term(term, taxname) self.cache.save_to_file() slog.info('The term %s has saved.' % name)
def merge(self, po_file, source_files): """从源码中获取所有条目,合并到 po_file 中。 :param string po_file: 待写入的 po 文件路径。 :param list source_files : 所有待处理的原文件路径 list。 """ # Create a temporary file to write pot file pot_file = tempfile.NamedTemporaryFile(mode='wb', prefix='rookout_', delete=False) pot_filename = pot_file.name slog.info('Create POT file [%s].', pot_filename) xargs = [ self._xgettext, "--package-name=main", "--package-version=0.1", "--default-domain=main", "--from-code=UTF-8", "-C", "-k_", "--output", pot_filename ] txt = subprocess.check_output(xargs + source_files, stderr=subprocess.STDOUT, universal_newlines=True) if len(txt) > 0: raise (ChildProcessError(txt)) slog.info('Start merge [%s] to [%s].', pot_filename, po_file) xargs = [self._msgmerge, "-U", po_file, pot_filename] txt = subprocess.check_output(xargs, universal_newlines=True) slog.info(txt) pot_file.close() os.remove(pot_filename)
def _new_term(self): if not self.args.query or len(self.args.query)<1: slog.error('Provide 1 arguments at least please.') return query = self.get_term_query() # slog.info('self.args.query: %s', self.args.query) term = self.get_terms_from_wp(query, force=True) slog.info('Get term from WordPress, query: %s, result: %s', query, term) if term: slog.error('The term "%s" has been in wordpress.'%self.args.query[0]) return taxname = query[0] slug = self.args.query[0] name = self.args.query[1] if len(self.args.query)>1 else slug term = WordPressTerm() term.slug = slug term.name = name term.taxonomy = taxname if len(self.args.query)>2: term.description = self.args.query[2] termid = self.wpcall(NewTerm(term)) if not termid: return term = self.wpcall(GetTerm(taxname, termid)) if not term: return slog.info('The term %s(%s) has created.'%(name, termid)) self.cache.save_term(term, taxname) self.cache.save_to_file() slog.info('The term %s has saved.'%name)
def merge(self, po_file, source_files): """从源码中获取所有条目,合并到 po_file 中。 :param string po_file: 待写入的 po 文件路径。 :param list source_files : 所有待处理的原文件路径 list。 """ # Create a temporary file to write pot file pot_file = tempfile.NamedTemporaryFile(mode='wb', prefix='rookout_', delete=False) pot_filename = pot_file.name slog.info('Create POT file [%s].', pot_filename) xargs = [self._xgettext, "--package-name=main", "--package-version=0.1", "--default-domain=main", "--from-code=UTF-8", "-C", "-k_", "--output", pot_filename] txt = subprocess.check_output(xargs+source_files, stderr=subprocess.STDOUT, universal_newlines=True) if len(txt) > 0: raise(ChildProcessError(txt)) slog.info('Start merge [%s] to [%s].', pot_filename, po_file) xargs = [self._msgmerge, "-U", po_file, pot_filename] txt = subprocess.check_output(xargs, universal_newlines=True) slog.info(txt) pot_file.close() os.remove(pot_filename)
def create_zip(files, trim_arcname=None, target_file=None, **zipfile_args): """创建一个 zip 文件。 :param list files: 要创建zip 的文件列表。 :param int trim_arcname: 若提供这个值,则使用 ZipFile.write(filename, filename[trim_arcname:]) 进行调用。 :returns: zip 文件的路径。 :rtype: str """ zipname = None azip = None if not target_file: azip = tempfile.NamedTemporaryFile(mode='wb', delete=False) zipname = azip.name else: azip = target_file zipname = target_file.name if hasattr(azip, 'read') else azip slog.info('Package %d files to "%s"'%(len(files), azip.name)) fileNum = len(files) curFile = 0 zipfile_args['mode'] = 'w' if not zipfile_args.get('compression'): zipfile_args['compression'] = zipfile.ZIP_DEFLATED with zipfile.ZipFile(azip, **zipfile_args) as zipf: for f in files: percent = round(curFile/fileNum*100) sys.stdout.write('\r%d%%'%(percent)) sys.stdout.flush() zipf.write(f, f[trim_arcname:] if trim_arcname else None ) curFile = curFile+1 sys.stdout.write('\r100%\n') sys.stdout.flush() if hasattr(azip, 'close'): azip.close() return zipname
def create_zip(files, trim_arcname=None, target_file=None, **zipfile_args): """创建一个 zip 文件。 :param list files: 要创建zip 的文件列表。 :param int trim_arcname: 若提供这个值,则使用 ZipFile.write(filename, filename[trim_arcname:]) 进行调用。 :returns: zip 文件的路径。 :rtype: str """ zipname = None azip = None if not target_file: azip = tempfile.NamedTemporaryFile(mode='wb', delete=False) zipname = azip.name else: azip = target_file zipname = target_file.name if hasattr(azip, 'read') else azip slog.info('Package %d files to "%s"' % (len(files), azip.name)) fileNum = len(files) curFile = 0 zipfile_args['mode'] = 'w' if not zipfile_args.get('compression'): zipfile_args['compression'] = zipfile.ZIP_DEFLATED with zipfile.ZipFile(azip, **zipfile_args) as zipf: for f in files: percent = round(curFile / fileNum * 100) sys.stdout.write('\r%d%%' % (percent)) sys.stdout.flush() zipf.write(f, f[trim_arcname:] if trim_arcname else None) curFile = curFile + 1 sys.stdout.write('\r100%\n') sys.stdout.flush() if hasattr(azip, 'close'): azip.close() return zipname
def upload_file(file_path, remote_path, ftp_conf, remove_file=False): """上传第一个指定的文件到 FTP 服务器。 :param str file_path: 待上传文件的绝对路径。 :param str remote_path: 文件在 FTP 服务器上的相对路径(相对于 FTP 服务器的初始路径)。 :param dict ftp_conf: ftp配置文件,详见 :func:`get_ftp` 。 :param bool remove_file: 上传成功后是否删除本地文件。 :returns: FTP 服务器上的文件列表 :rtype: list """ check_ftp_conf(ftp_conf) ftp, ftpStr = get_ftp(ftp_conf) lf = open(file_path, 'rb') slog.info('Uploading "%s" to "%s/%s" ......'%(file_path, ftpStr, remote_path)) ftp.storbinary("STOR %s"%remote_path, lf) filelist = ftp.nlst() ftp.quit() lf.close() if remove_file: os.remove(file_path) slog.info('Upload done.') return filelist
def _update_medias(self, medias): slog.info('Ready for upload some medias to WordPress.') attach_ids = [] urls = [] for path, name in medias: bits = None mediafile = self.conf.get_path(path) slog.info('Upload media file:%s', mediafile) with open(mediafile, 'rb') as m: bits = Binary(m.read()).data amedia = {} amedia['name'] = name amedia['type'] = mimetypes.guess_type(path)[0] amedia['bits'] = bits upd = self.wpcall(UploadFile(amedia)) url = upd['url'] urls.append(url) match = self.media_url_re.search(url) targetdir = self.conf.get_work_path('media', match.group(1)) if not os.path.exists(targetdir): os.makedirs(targetdir) attach_ids.append(upd['id']) shutil.move(mediafile, os.path.join(targetdir, name)) return urls, attach_ids
def update_submodules(path, init=True, update=True): """更新子模块。 :param str path: git 仓库文件夹路径。 :param bool init: 是否初始化子模块。 :param bool update: 是否更新子模块。 """ succ = None if init: arg = get_args(path, 'submodule', 'init', work_tree=False) slog.info(' '.join(arg)) succ = subprocess.call(arg) if succ > 0: slog.error('git execute error!') return succ if update: arg = get_args(path, "submodule", "update", work_tree=False) slog.info(' '.join(arg)) succ = subprocess.call(arg) if succ > 0: slog.error('git execute error!') return succ return succ
def _get_and_update_article_content(self, afile, istxt=False): html, meta, txt, medias = self._get_article_content(afile) if not html: return None, None, None, None # Update ATTACHMENTS in metadata. attach = 0 if not meta.attachments: attach = 1 elif meta.attachments[0] == '$ATTACHMENTS': attach = 2 elif len(meta.attachments)>0: attach = 3 if medias and attach>0: try: urls,attachids = self._update_medias(medias) except OSError as e: slog.error(e) return None, None, None, None idstxt = ','.join(attachids) if attach == 1: # Add attachments to the TOF. txt = 'Attachments: %s\n%s'%(idstxt, txt) slog.info('Fill attachments: %s.'%idstxt) elif attach == 2: txt = Template(txt).safe_substitute(ATTACHMENTS=idstxt) slog.info('Fill attachments: %s.'%idstxt) elif attach == 3: slog.info('Add new attachments: %s.'%idstxt) meta.attachments += attachids txt = re.sub(r'^Attachments: .*$', 'Attachments: '+ ','.join(meta.attachments), txt, 0, re.M) # Replace draft url to true url. i = 0 for path, name in medias: txt = txt.replace(path, urls[i]) i = i+1 # Rewrite the text with modified metadata. write_file(afile, txt, newline='\n') medias = self._get_medias(txt) if medias: slog.error('Medias in the article are maybe wrong!') return None, None, None, None outputdir, baseurl, namepre = self._get_output_arg(afile) html, md, txt = wpcmd.md.convert(txt, outputdir, baseurl, namepre) meta = self._get_article_metadata(md.metadata) # medias is must be None return html, meta, txt, None
def _update_term(self): """update term's info to wordpress. """ q = self.args.query term = None query = self.get_term_query() typ = query[0] if q and len(q) > 1: term = self.get_terms_from_wp(query, force=True) if not term: slog.error('The term %s is not existend.'%str(q)) return term = self.wpcall(GetTerm(typ, term.id)) if term: term.slug = q[0] term.name = q[1] if len(q)>2: term.description = q[2] # post_tag can not support parent. if term.taxonomy == 'post_tag': term.parent = None issucc = self.wpcall(EditTerm(term.id, term)) if issucc: self.cache.save_term(term, typ) self.cache.save_to_file() slog.info('The term %s(%s) has saved.'%(term.slug, term.id)) else: slog.info('The term %s(%s) saves unsuccessfully.'%(term.slug, term.id)) else: slog.info('Can not get term "%s".'%typ) else: term = self.get_terms_from_wp(query, force=True) if term: slog.info('Update terms done.') else: slog.warning('No terms.')
def print_result(self, result): if isinstance(result, WordPressTerm): slog.info('id=%s, group=%s, ' 'taxnomy_id=%s, name=%s, slug=%s, ' 'parent=%s, count=%d', result.id, result.group, result.taxonomy_id, result.name, result.slug, result.parent, result.count) elif isinstance(result, WordPressPost): slog.info('id=%s, date=%s, date_modified=%s, ' 'slug=%s, title=%s, post_status=%s, post_type=%s', result.id, str(result.date), str(result.date_modified), result.slug, result.title, result.post_status, result.post_type) elif isinstance(result, WordPressMedia): slog.info('id=%s, parent=%s, title=%s, ' 'description=%s, caption=%s, date_created=%s, link=%s, ' 'thumbnail=%s, metadata=%s', result.id, result.parent, result.title, result.description, result.caption, str(result.date_created), result.link, result.thumbnail, result.metadata) else: slog.info(result)
def print_result(self, result): if isinstance(result, WordPressTerm): slog.info( 'id=%s, group=%s, ' 'taxnomy_id=%s, name=%s, slug=%s, ' 'parent=%s, count=%d', result.id, result.group, result.taxonomy_id, result.name, result.slug, result.parent, result.count) elif isinstance(result, WordPressPost): slog.info( 'id=%s, date=%s, date_modified=%s, ' 'slug=%s, title=%s, post_status=%s, post_type=%s', result.id, str(result.date), str(result.date_modified), result.slug, result.title, result.post_status, result.post_type) elif isinstance(result, WordPressMedia): slog.info( 'id=%s, parent=%s, title=%s, ' 'description=%s, caption=%s, date_created=%s, link=%s, ' 'thumbnail=%s, metadata=%s', result.id, result.parent, result.title, result.description, result.caption, str(result.date_created), result.link, result.thumbnail, result.metadata) else: slog.info(result)
def _update_an_article(self, postid): afile, aname = self.conf.get_article(postid, self.args.type) # If output is provided, write the html to a file and abort. if self.args.output: self._write_html_file(afile) return html, meta, txt, medias = self._get_and_update_article_content(afile) if not html: return resultclass = WordPressPost if self.args.type == 'page': postid = meta.postid resultclass = WordPressPage post = self.wpcall(GetPost(postid, result_class=resultclass)) if not post: slog.warning('No post "%s"!'%postid) return slog.info('Old article:') self.print_results(post) post.title = meta.title post.user = meta.author post.slug = meta.nicename post.date = meta.date post.content = html post.post_status = meta.poststatus if meta.modified: post.date_modified = meta.modified terms = self.cache.get_terms_from_meta(meta.category, meta.tags) if terms: post.terms = terms elif self.args.type == 'post': slog.warning('Please provide some terms.') return succ = self.wpcall(EditPost(postid, post)) if succ == None: return if succ: slog.info('Update %s successfully!'%postid) else: slog.info('Update %s fail!'%postid)
def check_args(argv=None): parser = argparse.ArgumentParser(prog='wpcmd') parser.add_argument('-v', '--version', action='store_true', help='Show version of wpcmd.') subParsers = parser.add_subparsers(dest='sub_name', help='sub-commands') put = subParsers.add_parser('util', help='Some utils.') put.add_argument('-r', '--readme', action='store_true', help='Build README.md.') put.add_argument('-u', '--url', action='store_true', help='Rewrite url.') put.add_argument('-c', '--category', action='store_true', help='Rewrite category.') put.add_argument('-d', '--dirname', type=str, default='post', choices = ['post', 'page', 'draft', 'all'], help='Rewrite articles by type. The value is [post|page|draft|all].') put.add_argument('-a', '--analytic', action='store_true', help='Analytic the articles.') put.add_argument('--check-posts', action='store_true', help='Check articles.') put.add_argument('--check-mds', action='store_true', help='Check markdown files.') put.add_argument('-q', '--query', nargs='*', help='The options for query.') pn = subParsers.add_parser('new', help='Create some new content.') pn.add_argument('-t', '--type', type=str, choices=['post', 'page', 'tag', 'category'], help='Create a new content in wordpress.') pn.add_argument('-q', '--query', nargs='*', help='The options for query.') ps = subParsers.add_parser('show', help='Show wordpress contents.') ps.add_argument('-t', '--type', type=str, choices=['post', 'page', 'draft', 'option','tax','term', 'category','tag', 'medialib', 'mediaitem'], default='option', help='Content type of wordpress.') ps.add_argument('-n', '--number', type=int, default=10, help='The amount for GetPosts.') ps.add_argument('-o', '--orderby', choices=['post_modified', 'post_id'], default='post_id', help='To sort the result-set by one column.') ps.add_argument('-d', '--order', choices=['ASC', 'DESC'], default='DESC', help='To sort the records in a descending or a ascending order.') ps.add_argument('-q', '--query', nargs='*', help='The options for query.') pu = subParsers.add_parser('update', help='Update wordpress contents.') pu.add_argument('-t', '--type', type=str, choices=['post', 'page', 'draft', 'option', 'tag', 'category'], default='post', help='Content type of wordpress.') pu.add_argument('-q', '--query', nargs='*', help='The options for query.') pu.add_argument('-o', '--output', type=str, help='Write output text to a file.') # Add site argument to new/update/show. for subp in (pn, ps, pu, put): subp.add_argument('-s', '--site', type=str, default='site', help='Set the site section in ini config files. Default value is "site".') args = parser.parse_args(args=argv) if args.sub_name: return args, subParsers.choices[args.sub_name] if args.version: slog.info(__version__) return None, None parser.print_help() return None, None