herb/lens: add utilites for import/export

This commit is contained in:
Isaac Visintainer 2020-11-16 15:57:35 -08:00
parent bd4edb7fa4
commit 41ce22d6ae
5 changed files with 98 additions and 8 deletions

View File

@ -1144,6 +1144,8 @@
$listen-api !!
$export !!
$import !!
$export-all !!
$import-all !!
$as
:* %as mar.source.com
$(num +(num), source.com next.source.com)

View File

@ -14,6 +14,28 @@
job=(unit [eyre-id=@ta com=command:lens])
==
==
::
++ export-app
|= [app=@tas our=@p now=@da]
.^(@ %gx /(scot %p our)/[app]/(scot %da now)/export/noun)
++ export-all
|= [our=@p now=@da]
^- (list [@tas @])
%+ turn
^- (list @tas)
:~ %group-store
%metadata-store
%metadata-hook
%contact-store
%contact-hook
%invite-store
%chat-store
%chat-hook
%publish
%graph-store
==
|= app=@tas
[app (export-app app our now)]
--
::
=| =state
@ -43,12 +65,15 @@
=/ com=command:lens
(json:grab:lens-mark jon)
::
?: ?=(%export -.source.com)
~& [%export app.source.com]
?+ -.source.com
:_ this(job.state (some [eyre-id com]))
[%pass /sole %agent [our.bowl %dojo] %watch /sole/[eyre-id]]~
::
%export
:_ this(job.state (some [eyre-id com]))
[%pass /export %agent [our.bowl app.source.com] %watch /export]~
::
?: ?=(%import -.source.com)
%import
?~ enc=(de:base64 base64-jam.source.com)
!!
::
@ -57,8 +82,28 @@
:_ this(job.state (some [eyre-id com]))
[%pass /import %agent [our.bowl app.source.com] %poke %import !>(c)]~
::
:_ this(job.state (some [eyre-id com]))
[%pass /sole %agent [our.bowl %dojo] %watch /sole/[eyre-id]]~
%export-all
=/ output (crip "{<our.bowl>}-export/atom")
=/ jon
=/ =atom (jam (export-all our.bowl now.bowl))
=/ =octs [(met 3 atom) atom]
=/ enc (en:base64 octs)
(pairs:enjs:format file+s+output data+s+enc ~)
:_ this
%+ give-simple-payload:app eyre-id
(json-response:gen jon)
::
%import-all
=/ enc (de:base64 base64-jam.source.com)
?~ enc !!
=/ by-app ;;((list [@tas @]) (cue q.u.enc))
:_ this
%+ weld (give-simple-payload:app eyre-id not-found:gen)
%+ turn by-app
|= [app=@tas data=@]
^- card:agent:gall
[%pass /import-all %agent [our.bowl app] %poke %import !>(data)]
==
::
++ on-watch
|= =path
@ -68,7 +113,13 @@
(on-watch:def path)
::
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-peek
|= =path
^- (unit (unit cage))
?+ path (on-peek:def path)
[%x %export-all ~]
``noun+!>((jam (export-all our.bowl now.bowl)))
==
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card:agent:gall _this)

View File

@ -42,6 +42,8 @@
listen-api+(su ;~(plug sym ;~(pfix col sym)))
export+so
import+(ot app+so base64-jam+so ~)
export-all+none
import-all+(ot base64-jam+so ~)
as+(ot mark+(su sym) next+source ~)
hoon+(ot code+so next+source ~)
==

View File

@ -17,6 +17,8 @@
{$listen-api api/term event/term}
{$export app/@t}
{$import app/@t base64-jam/@t}
{$export-all ~}
{$import-all base64-jam/@t}
==
++ sink
$% {$stdout ~}

View File

@ -145,6 +145,34 @@ class importFileAction(argparse.Action):
res.source = {"import": {"app": new_value, "base64-jam": base_data}}
class importAllAction(argparse.Action):
"""Handles the import-all statement.
The --import-all statement reads in a jammed noun file from the path passed
in and stuffs it the base64 encoded version which gets passed into your
Urbit.
"""
def __call__(self, parser, res, new_value, option_string):
logging.debug('%r %r' % (new_value, option_string))
logging.debug('source %s' % res.source)
logging.debug('level %s' % res.level)
# We check to see if there's a "{new_value}" file in the current
# working directory. If there isn't, we error
data = ""
filename = new_value
with open(filename, 'rb') as f:
data = f.read()
if data == "":
raise ValueError('Failed to read jamfile')
base_data = base64.b64encode(data)
res.source = {"import-all": {"base64-jam": base_data}}
class transformerAction(argparse.Action):
"""Handle transformer flag.
@ -350,6 +378,13 @@ parser.add_argument('-i', '--import',
metavar='app-name',
help='imports the application state',
action=importFileAction)
parser.add_argument('-E', '--export-all', const={'export-all': None},
help='exports data from all landscape apps',
action='store_const', dest='source')
parser.add_argument('-I', '--import-all',
metavar='jam-file',
help='imports data for all landscape apps',
action=importAllAction)
parser.add_argument('-m', '--mark', which='as',
metavar='mark',
help='transform a source to another mark',
@ -398,10 +433,8 @@ sinks.add_argument('-p', '--app', which='app',
metavar='app',
action=sinkAction)
args = parser.parse_args(args)
if args.source is None:
args.source = {"data": ''.join(sys.stdin)}