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 _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 _get_article_content(self, afile, output=None): if not os.path.exists(afile): slog.error('The file "%s" is inexistance!'%afile) return None, None, None, None txt = read_file(afile) outputdir, baseurl, namepre = self._get_output_arg(afile, output) html, md, txt = wpcmd.md.convert(txt, outputdir, baseurl, namepre) medias = self._get_medias(txt) meta = self._get_article_metadata(md.metadata) return html,meta,txt,medias
def wpcall(self, method): if not self._wp: self._wp = Client(self.conf.get_url(), self.conf.get_user(), self.conf.get_password()) try: results = self._wp.call(method) except InvalidCredentialsError as e: slog.error(e) return None except Fault as e: slog.error(e) return None return results
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 get_terms_from_wp(self, query, force=False): if not query or len(query)== 0: slog.error('Please provide a taxonomy name! You can use ' '"show -t tax" to get one.') return None taxname = query[0] slug = query[1] if len(query)>1 else None terms = self.cache[taxname] if not terms or force: results = self.wpcall(GetTerms(taxname)) if results: self.cache.save_terms(results, taxname) if terms and slug: return terms[slug] return terms
def get_terms_from_wp(self, query, force=False): if not query or len(query) == 0: slog.error('Please provide a taxonomy name! You can use ' '"show -t tax" to get one.') return None taxname = query[0] slug = query[1] if len(query) > 1 else None terms = self.cache[taxname] if not terms or force: results = self.wpcall(GetTerms(taxname)) if results: self.cache.save_terms(results, taxname) if terms and slug: return terms[slug] return terms
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 _get_title_and_date(self, path): title = None time = None with open(path, 'r', encoding='utf-8') as f: for line in f: if line.lower().startswith('title:'): title = line[6:].strip() title = title.replace('_', r'\_') continue if line.lower().startswith('date:'): time = line[6:16] continue if time and title: break if not time or not title: slog.error('There is NOT title or date in this article: [%s]!'%path) return None, None return title, time
def _get_title_and_date(self, path): title = None time = None with open(path, 'r', encoding='utf-8') as f: for line in f: if line.lower().startswith('title:'): title = line[6:].strip() title = title.replace('_', r'\_') continue if line.lower().startswith('date:'): time = line[6:16] continue if time and title: break if not time or not title: slog.error('There is NOT title or date in this article: [%s]!' % path) return None, None return title, time
def get_terms_from_meta(self, categories, tags): terms = [] if categories: for cat in categories: term = self.get_term('category', cat) if not term: slog.error('The category "%s" is not in wordpress.' ' Please create it first.' % cat) return None terms.append(term) if tags: for tag in tags: term = self.get_term('post_tag', tag) if not term: slog.error('The tag "%s" is not in wordpress.' 'Please create it first' % tag) return None terms.append(term) return terms
def get_terms_from_meta(self, categories, tags): terms = [] if categories: for cat in categories: term = self.get_term('category', cat) if not term: slog.error('The category "%s" is not in wordpress.' ' Please create it first.'%cat) return None terms.append(term) if tags: for tag in tags: term = self.get_term('post_tag', tag) if not term: slog.error('The tag "%s" is not in wordpress.' 'Please create it first'%tag) return None terms.append(term) return terms
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 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 _show_mediaitem(self): if not self.args.query or len(self.args.query) == 0: slog.error('Please provide a attachment_id!') return None return GetMediaItem(self.args.query[0])