<commit_before>import proxybase
import xmlrpclib
import mimetypes
import os

################################################################################
# returns an instance of a wpproxy object
def getInst(url, user, password):
   wp = WordpressProxy(url, user, password)
   return wp

################################################################################
# the following defines a blogproxy class that inherits methods from the 
# xmlrpclib.  To make this work, the __init__ method of the ancestor
# class(xmlrpclib.ServerProxy in this case) must be called explicitly as 
# part of the initialization.  From that point, the various server methods
# are "directly" accessible through my blogproxy class
#
class WordpressProxy(proxybase.BlogProxy):
    ############################################################################ 
    def getCategories(self):

        def _tryMethods(blogid):
            try:
                response = self.wp.getTerms(blogid, 
                                            self._username,
                                            self._password,
                                            'category',
                                            {})
            except xmlrpclib.Fault:
                pass
            except xmlrpclib.ProtocolError, error:
                raise proxybase.ProxyError("wp.getCategories", error)
            else:
                return [ { 'categoryName'        : cat['name'],
                           'parentId'            : cat['parent'],
                           'categoryId'          : cat['term_id'],
                           'categoryDescription' : cat['description'],} for cat in response ]

            # fallback to old method
            try:
                return self.metaWeblog.getCategories(blogid,
                                                     self._username,
                                                     self._password)
            except(xmlrpclib.Fault, xmlrpclib.ProtocolError), error:
                raise proxybase.ProxyError("wp.getCategories", error)

        ########################################################################
        # getCategories starts here...
        if self._categories == None:
            self._categories = _tryMethods(self._getBlogID)

        return self._categories

    ############################################################################ 
    def newCategory(self, newcat, parent, slug='', desc=''):
        blogid = self._getBlogID()
 
        newcStruct = {}

        newcStruct['name'] = newcat
        newcStruct['slug'] = slug
        newcStruct['description'] = desc
        newcStruct['parent_id'] = parent 

        try:
            id = self.wp.newCategory(blogid,
                                     self._username,
                                     self._password,
                                     newcStruct)
        except(xmlrpclib.Fault, xmlrpclib.ProtocolError), error:
            raise proxybase.ProxyError("wp.newCategory", error)

        return id

    ############################################################################ 
    def getRecentTitles(self, number):

        '''
            Function to maintain backwards compatibility with previous WP blog
            versions.
        '''
        def _tryMethods(blogid, number):
            # First, try the Wordpress XMLRPC API calls
            try:
                response = self.wp.getPosts(blogid,
                                            self._username,
                                            self._password,
                                            { # filter parameter
                                              'post_type'   : 'post',      # or 'page', 'attachment'<commit_msg>newCategory now uses Wordpress XMLRPC API<commit_after>import proxybase
import xmlrpclib
import mimetypes
import os

################################################################################
# returns an instance of a wpproxy object
def getInst(url, user, password):
   wp = WordpressProxy(url, user, password)
   return wp

################################################################################
# the following defines a blogproxy class that inherits methods from the 
# xmlrpclib.  To make this work, the __init__ method of the ancestor
# class(xmlrpclib.ServerProxy in this case) must be called explicitly as 
# part of the initialization.  From that point, the various server methods
# are "directly" accessible through my blogproxy class
#
class WordpressProxy(proxybase.BlogProxy):
    ############################################################################ 
    def getCategories(self):

        def _tryMethods(blogid):
            try:
                response = self.wp.getTerms(blogid, 
                                            self._username,
                                            self._password,
                                            'category',
                                            {})
            except xmlrpclib.Fault:
                pass
            except xmlrpclib.ProtocolError, error:
                raise proxybase.ProxyError("wp.getCategories", error)
            else:
                return [ { 'categoryName'        : cat['name'],
                           'parentId'            : cat['parent'],
                           'categoryId'          : cat['term_id'],
                           'categoryDescription' : cat['description'],} for cat in response ]

            # fallback to old method
            try:
                return self.metaWeblog.getCategories(blogid,
                                                     self._username,
                                                     self._password)
            except(xmlrpclib.Fault, xmlrpclib.ProtocolError), error:
                raise proxybase.ProxyError("wp.getCategories", error)

        ########################################################################
        # getCategories starts here...
        if self._categories == None:
            self._categories = _tryMethods(self._getBlogID)

        return self._categories

    ############################################################################ 
    def newCategory(self, newcat, parent, slug='', desc=''):
        blogid = self._getBlogID()

        # start by trying newer Wordpress API call
        try:
            return self.wp.newTerm(blogid, 
                                   self._username,
                                   self._password,
                                   { 'name'        : newcat,
                                     'taxonomy'    : 'category',
                                     'slug'        : slug,
                                     'description' : desc,
                                     'parent'      : parent })
        except xmlrpclib.Fault:
            pass
        except xmlrpclib.ProtocolError, error:
            raise proxybase.ProxyError("wp.newCategory", error)
 
        # fallback to old call
        try:
            return self.wp.newCategory(blogid,
                                       self._username,
                                       self._password,
                                       { 'name'        : newcat,
                                         'slug'        : slug,
                                         'description' : desc,
                                         'parent_id'   : parent})
        except(xmlrpclib.Fault, xmlrpclib.ProtocolError), error:
            raise proxybase.ProxyError("wp.newCategory", error)

    ############################################################################ 
    def getRecentTitles(self, number):

        '''
            Function to maintain backwards compatibility with previous WP blog
            versions.
        '''
        def _tryMethods(blogid, number):
            # First, try the Wordpress XMLRPC API calls
            try:
                response = self.wp.getPosts(blogid,
                                            self._username,
                                            self._password,
                                            { # filter parameter
                                              'post_type'   : 'post',      # or 'page', 'attachment'
{'fraction_non_alphanumeric': 0.17569687462290334, 'fraction_numerical': 0.0, 'mean_word_length': 1.4564315352697095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4527210', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1631, 'n_words': 435}