<s> import subprocess <EOL> class Printer ( object ) : <EOL> def __init__ ( self , name , flags = None , options = None ) : <EOL> self . name = name <EOL> if flags : <EOL> self . flags = flags <EOL> else : <EOL> self . options = { } <EOL> if options : <EOL> self . options = options <EOL> else : <EOL> self . options = [ ] <EOL> def __str__ ( self ) : <EOL> ret = '<STR_LIT>' + self . name + '<STR_LIT:\n>' <EOL> ret += '<STR_LIT>' <EOL> for flag in self . flags . keys ( ) : <EOL> ret += '<STR_LIT>' . format ( flag , self . flags [ flag ] ) <EOL> for op in self . options : <EOL> o = str ( op ) <EOL> if o != '<STR_LIT>' : <EOL> ret += o + '<STR_LIT:U+0020>' <EOL> return ret <EOL> def setFlag ( self , flag , value ) : <EOL> if flag == '<STR_LIT:d>' : <EOL> return False <EOL> try : <EOL> self . flags [ flag ] = value <EOL> except : <EOL> return False <EOL> return True <EOL> def getFlag ( self , flag ) : <EOL> try : <EOL> return self . flags [ flag ] <EOL> except : <EOL> return False <EOL> def addOption ( self , new_op ) : <EOL> for i , op in enumerate ( self . options ) : <EOL> if op . name == new_op . name : <EOL> self . options [ i ] = new_op <EOL> return True <EOL> self . options . append ( op ) <EOL> def getOption ( self , name ) : <EOL> for op in self . options : <EOL> if op . name == name : <EOL> return op <EOL> return False <EOL> def __call__ ( self , item ) : <EOL> self . sendPrint ( item ) <EOL> def sendPrint ( self , item ) : <EOL> command = [ '<STR_LIT>' ] <EOL> for flag in self . flags . keys ( ) : <EOL> command . append ( '<STR_LIT>' . format ( flag , self . flags [ flag ] ) ) <EOL> for op in self . options : <EOL> o = str ( op ) <EOL> if o != '<STR_LIT>' : <EOL> command . append ( str ( op ) ) <EOL> print command <EOL> p = subprocess . Popen ( command , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> p . stdin . write ( item ) <EOL> outs = p . communicate ( ) <EOL> print outs <EOL> class Option ( object ) : <EOL> def __init__ ( self , name , options , default = None , human_name = None ) : <EOL> self . name = name <EOL> self . options = options <EOL> self . human_name = human_name <EOL> if default : <EOL> self . default = default <EOL> else : <EOL> self . default = self . options [ <NUM_LIT:0> ] <EOL> def __str__ ( self ) : <EOL> if self . default : <EOL> return '<STR_LIT>' . format ( self . name , self . default ) <EOL> return '<STR_LIT>' <EOL> def setDefault ( self , op ) : <EOL> self . default = op <EOL> return True <EOL> def listPrinters ( ) : <EOL> lpsc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , stdout = subprocess . PIPE ) <EOL> lpstats = lpsc . communicate ( ) [ <NUM_LIT:0> ] <EOL> lpsplit = lpstats . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> printers = [ ] <EOL> for p in lpsplit : <EOL> printers . append ( p . split ( ) [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> return printers <EOL> def listOptions ( printer ) : <EOL> lpop = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , printer , '<STR_LIT>' ] , stdout = subprocess . PIPE ) <EOL> lpout = lpop . communicate ( ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) [ : - <NUM_LIT:1> ] <EOL> ops = [ ] <EOL> for line in lpout : <EOL> name , values = line . split ( '<STR_LIT::>' ) <EOL> human_name = name [ name . index ( '<STR_LIT:/>' ) + <NUM_LIT:1> : ] <EOL> name = name [ : name . index ( '<STR_LIT:/>' ) ] <EOL> valuelist = values . split ( '<STR_LIT:U+0020>' ) <EOL> for i , v in enumerate ( valuelist ) : <EOL> if '<STR_LIT:*>' in v : <EOL> valuelist [ i ] = valuelist [ i ] . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> ops . append ( Option ( name , valuelist , None , human_name ) ) <EOL> return ops <EOL> def getRicoh ( ) : <EOL> ops = listOptions ( '<STR_LIT>' ) <EOL> prin = Printer ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:t>' : '<STR_LIT>' } , ops ) <EOL> op = prin . getOption ( '<STR_LIT>' ) <EOL> op . setDefault ( '<STR_LIT>' ) <EOL> prin . addOption ( op ) <EOL> op = prin . getOption ( '<STR_LIT>' ) <EOL> op . setDefault ( '<STR_LIT>' ) <EOL> prin . addOption ( op ) <EOL> op = prin . getOption ( '<STR_LIT>' ) <EOL> op . setDefault ( '<STR_LIT>' ) <EOL> prin . addOption ( op ) <EOL> op = prin . getOption ( '<STR_LIT>' ) <EOL> op . setDefault ( '<STR_LIT>' ) <EOL> prin . addOption ( op ) <EOL> return prin <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> r = getRicoh ( ) <EOL> print r <EOL> r ( open ( '<STR_LIT>' , '<STR_LIT:r>' ) . read ( ) ) </s>
