<s> from __future__ import unicode_literals <EOL> import json , inspect <EOL> import frappe <EOL> from frappe import _ <EOL> from frappe . utils import cint <EOL> @ frappe . whitelist ( ) <EOL> def runserverobj ( method , docs = None , dt = None , dn = None , arg = None , args = None ) : <EOL> """<STR_LIT>""" <EOL> if not args : args = arg or "<STR_LIT>" <EOL> if dt : <EOL> if not dn : dn = dt <EOL> doc = frappe . get_doc ( dt , dn ) <EOL> else : <EOL> doc = frappe . get_doc ( json . loads ( docs ) ) <EOL> doc . _original_modified = doc . modified <EOL> doc . check_if_latest ( ) <EOL> if not doc . has_permission ( "<STR_LIT>" ) : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) , raise_exception = True ) <EOL> if doc : <EOL> try : <EOL> args = json . loads ( args ) <EOL> except ValueError : <EOL> args = args <EOL> fnargs , varargs , varkw , defaults = inspect . getargspec ( getattr ( doc , method ) ) <EOL> if not fnargs or ( len ( fnargs ) == <NUM_LIT:1> and fnargs [ <NUM_LIT:0> ] == "<STR_LIT>" ) : <EOL> r = doc . run_method ( method ) <EOL> elif "<STR_LIT:args>" in fnargs or not isinstance ( args , dict ) : <EOL> r = doc . run_method ( method , args ) <EOL> else : <EOL> r = doc . run_method ( method , ** args ) <EOL> if r : <EOL> if cint ( frappe . form_dict . get ( '<STR_LIT>' ) ) : <EOL> make_csv_output ( r , doc . doctype ) <EOL> else : <EOL> frappe . response [ '<STR_LIT:message>' ] = r <EOL> frappe . response . docs . append ( doc ) <EOL> def make_csv_output ( res , dt ) : <EOL> """<STR_LIT>""" <EOL> import frappe <EOL> from cStringIO import StringIO <EOL> import csv <EOL> f = StringIO ( ) <EOL> writer = csv . writer ( f ) <EOL> for r in res : <EOL> row = [ ] <EOL> for v in r : <EOL> if isinstance ( v , basestring ) : <EOL> v = v . encode ( "<STR_LIT:utf-8>" ) <EOL> row . append ( v ) <EOL> writer . writerow ( row ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> frappe . response [ '<STR_LIT:result>' ] = unicode ( f . read ( ) , '<STR_LIT:utf-8>' ) <EOL> frappe . response [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> frappe . response [ '<STR_LIT>' ] = dt . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) </s>
