<s> """<STR_LIT>""" <EOL> from security_monkey . monitors import all_monitors , get_monitor <EOL> from security_monkey . datastore import Item , ItemRevision , Account , Technology <EOL> import json <EOL> import os <EOL> def __prep_accounts__ ( accounts ) : <EOL> if accounts == '<STR_LIT:all>' : <EOL> accounts = Account . query . filter ( Account . third_party == False ) . filter ( Account . active == True ) . all ( ) <EOL> accounts = [ account . name for account in accounts ] <EOL> return accounts <EOL> else : <EOL> return accounts . split ( '<STR_LIT:U+002C>' ) <EOL> def __prep_monitor_names__ ( monitor_names ) : <EOL> if monitor_names == '<STR_LIT:all>' : <EOL> return [ monitor . index for monitor in all_monitors ( ) ] <EOL> else : <EOL> return monitor_names . split ( '<STR_LIT:U+002C>' ) <EOL> def backup_config_to_json ( accounts , monitor_names , output_folder ) : <EOL> monitor_names = __prep_monitor_names__ ( monitor_names ) <EOL> accounts = __prep_accounts__ ( accounts ) <EOL> for monitor_name in monitor_names : <EOL> monitor = get_monitor ( monitor_name ) <EOL> for account in accounts : <EOL> _backup_items_in_account ( account , monitor , output_folder ) <EOL> def _backup_items_in_account ( account_name , monitor , output_folder ) : <EOL> technology_name = monitor . watcher_class . index <EOL> query = Item . query <EOL> query = query . join ( ( Account , Account . id == Item . account_id ) ) <EOL> query = query . join ( ( Technology , Technology . id == Item . tech_id ) ) <EOL> query = query . filter ( Account . name == account_name ) <EOL> query = query . filter ( Technology . name == technology_name ) <EOL> items_to_backup = query . all ( ) <EOL> for item in items_to_backup : <EOL> latest_revision = ItemRevision . query . filter ( ItemRevision . id == item . latest_revision_id ) . first ( ) <EOL> _serialize_item_to_file ( item , latest_revision , output_folder , account_name , technology_name ) <EOL> def standardize_name ( name ) : <EOL> """<STR_LIT>""" <EOL> return name . replace ( '<STR_LIT:/>' , '<STR_LIT:_>' ) if name else '<STR_LIT>' <EOL> def _serialize_item_to_file ( item , latest_revision , output_folder , account_name , technology_name ) : <EOL> output_folder = "<STR_LIT>" . format ( <EOL> output_folder , <EOL> account_name , <EOL> technology_name <EOL> ) <EOL> if not os . path . isdir ( output_folder ) : <EOL> os . makedirs ( output_folder , mode = <NUM_LIT> ) <EOL> output_file = "<STR_LIT>" . format ( <EOL> output_folder , <EOL> standardize_name ( item . name ) <EOL> ) <EOL> print "<STR_LIT>" . format ( item . name , output_file ) <EOL> with open ( output_file , '<STR_LIT:w>' ) as output : <EOL> output . write ( json . dumps ( latest_revision . config , indent = <NUM_LIT:2> ) ) </s>
