This repository was archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of ricardian contract abi importer
- Loading branch information
Christopher Allnutt
committed
May 30, 2018
1 parent
c8be550
commit f5b9fe0
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import sys | ||
import os.path | ||
|
||
def add_ricardian_contracts_to_actions(source_abi_directory, contract_name, abi_actions): | ||
abi_actions_with_ricardian_contracts = [] | ||
|
||
for abi_action in abi_actions: | ||
action_name = abi_action["name"] | ||
contract_action_filename = f'{contract_name}-{action_name}-rc.md' | ||
|
||
# check for rc file | ||
rcContractPath = os.path.join(source_abi_directory, contract_action_filename) | ||
if os.path.exists(rcContractPath): | ||
print('Importing Contract {contract_action_filename} for {contract_name}:{action_name}'.format( | ||
contract_action_filename = contract_action_filename, | ||
contract_name = contract_name, | ||
action_name = action_name | ||
)) | ||
|
||
with open(rcContractPath) as contract_file_handle: | ||
contract_contents = contract_file_handle.read() | ||
|
||
abi_action["ricardian_contract"] = contract_contents | ||
else: | ||
print('Did not find recardian contract file {contract_action_filename} for {contract_name}:{action_name}, skipping inclusion'.format( | ||
contract_action_filename = contract_action_filename, | ||
contract_name = contract_name, | ||
action_name = action_name | ||
)) | ||
|
||
abi_actions_with_ricardian_contracts.append(abi_action) | ||
|
||
return abi_actions_with_ricardian_contracts | ||
|
||
def add_ricardian_contracts_to_abi(source_abi, output_abi): | ||
source_abi_directory = os.path.dirname(source_abi) | ||
contract_name = os.path.split(source_abi)[1].rpartition(".")[0] | ||
|
||
print('Creating {output_abi} with ricardian contracts included'.format(output_abi = output_abi)) | ||
|
||
source_abi_file = open(source_abi, 'r') | ||
source_abi_json = json.load(source_abi_file) | ||
|
||
source_abi_json['actions'] = add_ricardian_contracts_to_actions(source_abi_directory, contract_name, source_abi_json['actions']) | ||
|
||
with open(output_abi, 'w') as output_abi_file: | ||
json.dump(source_abi_json, output_abi_file, indent=2) | ||
|
||
|
||
def import_ricardian_to_abi(source_abi, output_abi): | ||
if not os.path.exists(source_abi): | ||
print(f'Source ABI not found in {source_abi}') | ||
sys.exit(0) | ||
|
||
if os.path.exists(output_abi): | ||
overwrite_prompt_response = input('Output ABI {output_abi} already exists, do you want to proceed? (y|n): '.format(output_abi = output_abi)) | ||
if overwrite_prompt_response == 'y': | ||
print('Overwriting existing output abi') | ||
add_ricardian_contracts_to_abi(source_abi, output_abi) | ||
sys.exit(0) | ||
else: | ||
print('User aborted, not overwriting existing abi') | ||
sys.exit(0) | ||
else: | ||
add_ricardian_contracts_to_abi(source_abi, output_abi) | ||
|
||
def main(): | ||
if len(sys.argv) == 1: | ||
print('Please specify an operation of export or import: ./ricardeos.py <import|export>') | ||
sys.exit(1) | ||
|
||
if sys.argv[1] == 'import': | ||
if len(sys.argv) != 4: | ||
print('Please specify a source and destination abi:') | ||
print('Usage: ./ricardeos.py import /eos/contracts/contract/mycontract.abi /eos/contracts/contract/withricardian-mycontract.abi') | ||
|
||
sys.exit(0) | ||
else: | ||
import_ricardian_to_abi(sys.argv[2], sys.argv[3]) | ||
|
||
sys.exit(0) | ||
elif sys.argv[2] == 'export': | ||
print('exporting') | ||
else: | ||
print('Operation not recognized only import and export operations are supported') | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|