<s> import os <EOL> import re <EOL> import mimetypes <EOL> import shutil <EOL> from string import Template <EOL> from xmlrpc . client import Binary <EOL> from rookout import slog <EOL> from rookout . conf import PYConf <EOL> from rookout . base import ( read_file , write_file , write_by_templ ) <EOL> from wpcmd . base import Action <EOL> from wordpress_xmlrpc import ( WordPressPost , WordPressPage ) <EOL> from wordpress_xmlrpc . methods . posts import ( GetPost , EditPost , NewPost ) <EOL> from wordpress_xmlrpc . methods . media import ( UploadFile ) <EOL> from wordpress_xmlrpc . methods . taxonomies import ( GetTerm , EditTerm ) <EOL> import wpcmd . md <EOL> class UpdateAction ( Action ) : <EOL> def __init__ ( self , gconf , gtermcache , gargs , gparser ) : <EOL> super ( UpdateAction , self ) . __init__ ( gconf , gtermcache , gargs , gparser ) <EOL> self . media_url_re = re . compile ( r'<STR_LIT>' ) <EOL> self . media_draft_re = re . compile ( r'<STR_LIT>' % <EOL> self . conf . get_site ( '<STR_LIT>' ) , re . M ) <EOL> def _get_article_metadata ( self , meta ) : <EOL> adict = PYConf ( ) <EOL> adict . title = meta [ '<STR_LIT:title>' ] [ <NUM_LIT:0> ] <EOL> adict . postid = meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> adict . nicename = meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> adict . slug = meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> adict . date = self . get_datetime ( meta [ '<STR_LIT:date>' ] [ <NUM_LIT:0> ] ) <EOL> adict . author = meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> tags = meta . get ( '<STR_LIT>' ) <EOL> if tags : <EOL> adict . tags = [ tag . strip ( ) for tag in tags [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> category = meta . get ( '<STR_LIT>' ) <EOL> if category : <EOL> adict . category = [ cat . strip ( ) for cat in category [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> modified = meta . get ( '<STR_LIT>' ) <EOL> if modified : <EOL> adict . modified = self . get_datetime ( modified [ <NUM_LIT:0> ] ) <EOL> posttype = meta . get ( '<STR_LIT>' ) <EOL> if posttype : <EOL> adict . posttype = posttype [ <NUM_LIT:0> ] <EOL> else : <EOL> adict . posttype = '<STR_LIT>' <EOL> poststatus = meta . get ( '<STR_LIT>' ) <EOL> if poststatus : <EOL> adict . poststatus = poststatus [ <NUM_LIT:0> ] <EOL> else : <EOL> adict . poststatus = '<STR_LIT>' <EOL> attachments = meta . get ( '<STR_LIT>' ) <EOL> if attachments : <EOL> adict . attachments = [ att . strip ( ) for att in attachments [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> return adict <EOL> def _get_output_arg ( self , afile , output = None ) : <EOL> def _get_mainname ( afile ) : <EOL> return os . path . splitext ( os . path . split ( afile ) [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> namepre = _get_mainname ( afile ) <EOL> media = self . conf . get_site ( '<STR_LIT>' ) <EOL> outputdir = self . conf . get_path ( media , '<STR_LIT>' ) <EOL> baseurl = '<STR_LIT>' % media <EOL> if output : <EOL> namepre = _get_mainname ( output ) <EOL> outputdir = os . path . join ( os . path . split ( output ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> baseurl = '<STR_LIT>' <EOL> if not os . path . exists ( outputdir ) : <EOL> os . makedirs ( outputdir ) <EOL> return outputdir , baseurl , namepre <EOL> def _get_article_content ( self , afile , output = None ) : <EOL> if not os . path . exists ( afile ) : <EOL> slog . error ( '<STR_LIT>' % afile ) <EOL> return None , None , None , None <EOL> txt = read_file ( afile ) <EOL> outputdir , baseurl , namepre = self . _get_output_arg ( afile , output ) <EOL> html , md , txt = wpcmd . md . convert ( txt , outputdir , baseurl , namepre ) <EOL> medias = self . _get_medias ( txt ) <EOL> meta = self . _get_article_metadata ( md . metadata ) <EOL> return html , meta , txt , medias <EOL> def _get_and_update_article_content ( self , afile , istxt = False ) : <EOL> html , meta , txt , medias = self . _get_article_content ( afile ) <EOL> if not html : <EOL> return None , None , None , None <EOL> attach = <NUM_LIT:0> <EOL> if not meta . attachments : <EOL> attach = <NUM_LIT:1> <EOL> elif meta . attachments [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> attach = <NUM_LIT:2> <EOL> elif len ( meta . attachments ) > <NUM_LIT:0> : <EOL> attach = <NUM_LIT:3> <EOL> if medias and attach > <NUM_LIT:0> : <EOL> try : <EOL> urls , attachids = self . _update_medias ( medias ) <EOL> except OSError as e : <EOL> slog . error ( e ) <EOL> return None , None , None , None <EOL> idstxt = '<STR_LIT:U+002C>' . join ( attachids ) <EOL> if attach == <NUM_LIT:1> : <EOL> txt = '<STR_LIT>' % ( idstxt , txt ) <EOL> slog . info ( '<STR_LIT>' % idstxt ) <EOL> elif attach == <NUM_LIT:2> : <EOL> txt = Template ( txt ) . safe_substitute ( ATTACHMENTS = idstxt ) <EOL> slog . info ( '<STR_LIT>' % idstxt ) <EOL> elif attach == <NUM_LIT:3> : <EOL> slog . info ( '<STR_LIT>' % idstxt ) <EOL> meta . attachments += attachids <EOL> txt = re . sub ( r'<STR_LIT>' , <EOL> '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( meta . attachments ) , <EOL> txt , <NUM_LIT:0> , re . M ) <EOL> i = <NUM_LIT:0> <EOL> for path , name in medias : <EOL> txt = txt . replace ( path , urls [ i ] ) <EOL> i = i + <NUM_LIT:1> <EOL> write_file ( afile , txt , newline = '<STR_LIT:\n>' ) <EOL> medias = self . _get_medias ( txt ) <EOL> if medias : <EOL> slog . error ( '<STR_LIT>' ) <EOL> return None , None , None , None <EOL> outputdir , baseurl , namepre = self . _get_output_arg ( afile ) <EOL> html , md , txt = wpcmd . md . convert ( txt , outputdir , baseurl , namepre ) <EOL> meta = self . _get_article_metadata ( md . metadata ) <EOL> return html , meta , txt , None <EOL> def _get_medias ( self , txt ) : <EOL> """<STR_LIT>""" <EOL> return [ ( item , item . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) for item in self . media_draft_re . findall ( txt ) ] <EOL> def _write_html_file ( self , afile ) : <EOL> out = self . args . output if os . path . isabs ( self . args . output ) else self . conf . get_work_path ( '<STR_LIT>' , self . args . output ) <EOL> html , meta , txt , medias = self . _get_article_content ( afile , output = out ) <EOL> if html : <EOL> write_file ( out , html , newline = '<STR_LIT:\n>' ) <EOL> def _update_a_draft ( self ) : <EOL> postid = self . get_postid ( ) <EOL> if not postid : <EOL> slog . warning ( '<STR_LIT>' ) <EOL> return <EOL> afile , aname = self . conf . get_draft ( postid ) <EOL> if self . args . output : <EOL> self . _write_html_file ( afile ) <EOL> return <EOL> html , meta , txt , medias = self . _get_and_update_article_content ( afile ) <EOL> if not html : <EOL> return <EOL> if meta . poststatus == '<STR_LIT>' : <EOL> slog . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % postid ) <EOL> return <EOL> self . get_terms_from_wp ( [ '<STR_LIT>' ] ) <EOL> self . get_terms_from_wp ( [ '<STR_LIT>' ] ) <EOL> if meta . posttype == '<STR_LIT>' : <EOL> post = WordPressPage ( ) <EOL> else : <EOL> post = WordPressPost ( ) <EOL> post . terms = self . cache . get_terms_from_meta ( meta . category , meta . tags ) <EOL> if not post . terms : <EOL> slog . warning ( '<STR_LIT>' ) <EOL> return <EOL> post . content = html <EOL> post . title = meta . title <EOL> post . slug = meta . nicename <EOL> post . date = meta . date <EOL> post . user = meta . author <EOL> post . date_modified = meta . modified <EOL> post . post_status = meta . poststatus <EOL> post . comment_status = "<STR_LIT>" <EOL> post . ping_status = "<STR_LIT>" <EOL> postid = self . wpcall ( NewPost ( post ) ) <EOL> if postid : <EOL> write_by_templ ( afile , afile , { '<STR_LIT>' : postid , '<STR_LIT>' : postid } , True ) <EOL> else : <EOL> return <EOL> newfile , newname = None , None <EOL> if meta . posttype == '<STR_LIT>' : <EOL> newfile , newname = self . conf . get_article ( meta . nicename , meta . posttype ) <EOL> else : <EOL> newfile , newname = self . conf . get_article ( postid , meta . posttype ) <EOL> slog . info ( '<STR_LIT>' % ( afile , newfile ) ) <EOL> shutil . move ( afile , newfile ) <EOL> def _update_articles ( self ) : <EOL> postids = self . get_postid ( as_list = True ) <EOL> if not postids : <EOL> slog . warning ( '<STR_LIT>' ) <EOL> return <EOL> self . get_terms_from_wp ( [ '<STR_LIT>' ] ) <EOL> self . get_terms_from_wp ( [ '<STR_LIT>' ] ) <EOL> for postid in postids : <EOL> self . _update_an_article ( postid ) <EOL> def _update_an_article ( self , postid ) : <EOL> afile , aname = self . conf . get_article ( postid , self . args . type ) <EOL> if self . args . output : <EOL> self . _write_html_file ( afile ) <EOL> return <EOL> html , meta , txt , medias = self . _get_and_update_article_content ( afile ) <EOL> if not html : <EOL> return <EOL> resultclass = WordPressPost <EOL> if self . args . type == '<STR_LIT>' : <EOL> postid = meta . postid <EOL> resultclass = WordPressPage <EOL> post = self . wpcall ( GetPost ( postid , result_class = resultclass ) ) <EOL> if not post : <EOL> slog . warning ( '<STR_LIT>' % postid ) <EOL> return <EOL> slog . info ( '<STR_LIT>' ) <EOL> self . print_results ( post ) <EOL> post . title = meta . title <EOL> post . user = meta . author <EOL> post . slug = meta . nicename <EOL> post . date = meta . date <EOL> post . content = html <EOL> post . post_status = meta . poststatus <EOL> if meta . modified : <EOL> post . date_modified = meta . modified <EOL> terms = self . cache . get_terms_from_meta ( meta . category , meta . tags ) <EOL> if terms : <EOL> post . terms = terms <EOL> elif self . args . type == '<STR_LIT>' : <EOL> slog . warning ( '<STR_LIT>' ) <EOL> return <EOL> succ = self . wpcall ( EditPost ( postid , post ) ) <EOL> if succ == None : <EOL> return <EOL> if succ : <EOL> slog . info ( '<STR_LIT>' % postid ) <EOL> else : <EOL> slog . info ( '<STR_LIT>' % postid ) <EOL> def _update_medias ( self , medias ) : <EOL> slog . info ( '<STR_LIT>' ) <EOL> attach_ids = [ ] <EOL> urls = [ ] <EOL> for path , name in medias : <EOL> bits = None <EOL> mediafile = self . conf . get_path ( path ) <EOL> slog . info ( '<STR_LIT>' , mediafile ) <EOL> with open ( mediafile , '<STR_LIT:rb>' ) as m : <EOL> bits = Binary ( m . read ( ) ) . data <EOL> amedia = { } <EOL> amedia [ '<STR_LIT:name>' ] = name <EOL> amedia [ '<STR_LIT:type>' ] = mimetypes . guess_type ( path ) [ <NUM_LIT:0> ] <EOL> amedia [ '<STR_LIT>' ] = bits <EOL> upd = self . wpcall ( UploadFile ( amedia ) ) <EOL> url = upd [ '<STR_LIT:url>' ] <EOL> urls . append ( url ) <EOL> match = self . media_url_re . search ( url ) <EOL> targetdir = self . conf . get_work_path ( '<STR_LIT>' , match . group ( <NUM_LIT:1> ) ) <EOL> if not os . path . exists ( targetdir ) : <EOL> os . makedirs ( targetdir ) <EOL> attach_ids . append ( upd [ '<STR_LIT:id>' ] ) <EOL> shutil . move ( mediafile , os . path . join ( targetdir , name ) ) <EOL> return urls , attach_ids <EOL> def _update_term ( self ) : <EOL> """<STR_LIT>""" <EOL> q = self . args . query <EOL> term = None <EOL> query = self . get_term_query ( ) <EOL> typ = query [ <NUM_LIT:0> ] <EOL> if q and len ( q ) > <NUM_LIT:1> : <EOL> term = self . get_terms_from_wp ( query , force = True ) <EOL> if not term : <EOL> slog . error ( '<STR_LIT>' % str ( q ) ) <EOL> return <EOL> term = self . wpcall ( GetTerm ( typ , term . id ) ) <EOL> if term : <EOL> term . slug = q [ <NUM_LIT:0> ] <EOL> term . name = q [ <NUM_LIT:1> ] <EOL> if len ( q ) > <NUM_LIT:2> : <EOL> term . description = q [ <NUM_LIT:2> ] <EOL> if term . taxonomy == '<STR_LIT>' : <EOL> term . parent = None <EOL> issucc = self . wpcall ( EditTerm ( term . id , term ) ) <EOL> if issucc : <EOL> self . cache . save_term ( term , typ ) <EOL> self . cache . save_to_file ( ) <EOL> slog . info ( '<STR_LIT>' % ( term . slug , term . id ) ) <EOL> else : <EOL> slog . info ( '<STR_LIT>' % ( term . slug , <EOL> term . id ) ) <EOL> else : <EOL> slog . info ( '<STR_LIT>' % typ ) <EOL> else : <EOL> term = self . get_terms_from_wp ( query , force = True ) <EOL> if term : <EOL> slog . info ( '<STR_LIT>' ) <EOL> else : <EOL> slog . warning ( '<STR_LIT>' ) <EOL> def go ( self ) : <EOL> if self . args . type == '<STR_LIT>' : <EOL> self . _update_a_draft ( ) <EOL> elif self . args . type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _update_articles ( ) <EOL> elif self . args . type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _update_term ( ) <EOL> def build ( gconf , gcache , gargs , parser = None ) : <EOL> action = UpdateAction ( gconf , gcache , gargs , parser ) <EOL> action . build ( ) </s>
