def perform_remote_action(i):
    """
    Input:  { See 'perform_action' function }
    Output: { See 'perform_action' function }
    """

    # Import modules compatible with Python 2.x and 3.x
    import urllib

    try:    import urllib.request as urllib2
    except: import urllib2 # pragma: no cover

    try:    from urllib.parse import urlencode
    except: from urllib import urlencode # pragma: no cover

    rr={'return':0}

    # Get action
    act=i.get('action','')

    # Check output
    o=i.get('out','')

    if o=='con':
#       out('Initiating remote access ...')
#       out('')
       i['out']='con'
       i['quiet']='yes'
       if act=='pull':
          i['out']='json'
    else:
       i['out']='json'

#    # Clean up input
#    if o!='json_file': 
#       rr['out']='json' # Decided to return json to show that it's remote ...

    if 'cid' in i: 
       del(i['cid']) # already processed

    # Get URL
    url=i.get('remote_server_url','')

    # Process i
    if 'remote_server_url' in i: del(i['remote_server_url'])

    # Pre process if push file ...
    if act=='push':
       # Check file
       fn=i.get('filename','')
       if fn=='':
          x=i.get('cids',[])
          if len(x)>0:
             fn=x[0]

       if fn=='':
          return {'return':1, 'error':'filename is empty'}

       if not os.path.isfile(fn):
          return {'return':1, 'error':'file '+fn+' not found'}

       rx=convert_file_to_upload_string({'filename':fn})
       if rx['return']>0: return rx

       i['file_content_base64']=rx['file_content_base64']

       # Leave only filename without path
       i['filename']=os.path.basename(fn)

    # Prepare post variables
    r=dumps_json({'dict':i, 'skip_indent':'yes'})
    if r['return']>0: return r
    s=r['string'].encode('utf8')

    post=urlencode({'ck_json':s})
    if sys.version_info[0]>2: post=post.encode('utf8')

    # If auth
    au=i.get('remote_server_user','')
    if au!='': 
       del(i['remote_server_user'])

       ap=i.get('remote_server_pass','')
       if ap!='': 
          del(i['remote_server_pass'])

       auth = urllib2.HTTPPasswordMgrWithDefaultRealm()
       auth.add_password(None, url, au, ap)
       urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(auth)))

    # Prepare request
    request = urllib2.Request(url, post)

    # Connect
    try:
       f=urllib2.urlopen(request)
    except Exception as e:
       return {'return':1, 'error':'Access to remote CK repository failed ('+format(e)+')'}

    # Read from Internet
    try:
       s=f.read()
       f.close()
    except Exception as e:
       return {'return':1, 'error':'Failed reading stream from remote CK web service ('+format(e)+')'}

    # Check output
    try: s=s.decode('utf8')
    except Exception as e: pass
    if o=='con' and act!='pull':
       out(s.rstrip())
    else:
       # Try to convert output to dictionary
       r=convert_json_str_to_dict({'str':s, 'skip_quote_replacement':'yes'})
       if r['return']>0: 
          return {'return':1, 'error':'can\'t parse output from remote CK server ('+r['error']+'):\n'+s[:256]+'\n\n...)'}

       d=r['dict']

       if 'return' in d: d['return']=int(d['return']) # Fix for some strange behavior when 'return' is not integer - should check why ...

       if d.get('return',0)>0:
          return d

       # Post process if pull file ...
       if act=='pull':
          if o!='json' and o!='json_file':
             # Convert encoded file to real file ...
             x=d.get('file_content_base64','')

             fn=d.get('filename','')
             if fn=='': fn=cfg['default_archive_name']

             r=convert_upload_string_to_file({'file_content_base64':x, 'filename':fn})
             if r['return']>0: return r

             if 'file_content_base64' in d: del(d['file_content_base64'])

       rr.update(d)

    # Restore original output
    i['out']=o

    return rr
def perform_remote_action ( i ) : import urllib try : import urllib . request as urllib2 except : import urllib2 try : from urllib . parse import urlencode except : from urllib import urlencode rr = { 'return' : 0 } act = i . get ( 'action' , '' ) o = i . get ( 'out' , '' ) if o == 'con' : i [ 'out' ] = 'con' i [ 'quiet' ] = 'yes' if act == 'pull' : i [ 'out' ] = 'json' else : i [ 'out' ] = 'json' if 'cid' in i : del ( i [ 'cid' ] ) url = i . get ( 'remote_server_url' , '' ) if 'remote_server_url' in i : del ( i [ 'remote_server_url' ] ) if act == 'push' : fn = i . get ( 'filename' , '' ) if fn == '' : x = i . get ( 'cids' , [ ] ) if len ( x ) > 0 : fn = x [ 0 ] if fn == '' : return { 'return' : 1 , 'error' : 'filename is empty' } if not os . path . isfile ( fn ) : return { 'return' : 1 , 'error' : 'file ' + fn + ' not found' } rx = convert_file_to_upload_string ( { 'filename' : fn } ) if rx [ 'return' ] > 0 : return rx i [ 'file_content_base64' ] = rx [ 'file_content_base64' ] i [ 'filename' ] = os . path . basename ( fn ) r = dumps_json ( { 'dict' : i , 'skip_indent' : 'yes' } ) if r [ 'return' ] > 0 : return r s = r [ 'string' ] . encode ( 'utf8' ) post = urlencode ( { 'ck_json' : s } ) if sys . version_info [ 0 ] > 2 : post = post . encode ( 'utf8' ) au = i . get ( 'remote_server_user' , '' ) if au != '' : del ( i [ 'remote_server_user' ] ) ap = i . get ( 'remote_server_pass' , '' ) if ap != '' : del ( i [ 'remote_server_pass' ] ) auth = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) auth . add_password ( None , url , au , ap ) urllib2 . install_opener ( urllib2 . build_opener ( urllib2 . HTTPBasicAuthHandler ( auth ) ) ) request = urllib2 . Request ( url , post ) try : f = urllib2 . urlopen ( request ) except Exception as e : return { 'return' : 1 , 'error' : 'Access to remote CK repository failed (' + format ( e ) + ')' } try : s = f . read ( ) f . close ( ) except Exception as e : return { 'return' : 1 , 'error' : 'Failed reading stream from remote CK web service (' + format ( e ) + ')' } try : s = s . decode ( 'utf8' ) except Exception as e : pass if o == 'con' and act != 'pull' : out ( s . rstrip ( ) ) else : r = convert_json_str_to_dict ( { 'str' : s , 'skip_quote_replacement' : 'yes' } ) if r [ 'return' ] > 0 : return { 'return' : 1 , 'error' : 'can\'t parse output from remote CK server (' + r [ 'error' ] + '):\n' + s [ : 256 ] + '\n\n...)' } d = r [ 'dict' ] if 'return' in d : d [ 'return' ] = int ( d [ 'return' ] ) if d . get ( 'return' , 0 ) > 0 : return d if act == 'pull' : if o != 'json' and o != 'json_file' : x = d . get ( 'file_content_base64' , '' ) fn = d . get ( 'filename' , '' ) if fn == '' : fn = cfg [ 'default_archive_name' ] r = convert_upload_string_to_file ( { 'file_content_base64' : x , 'filename' : fn } ) if r [ 'return' ] > 0 : return r if 'file_content_base64' in d : del ( d [ 'file_content_base64' ] ) rr . update ( d ) i [ 'out' ] = o return rr
Input:  { See 'perform_action' function }
    Output: { See 'perform_action' function }