]> gitweb.factorcode.org Git - factor.git/commitdiff
http.download: fix downloading into a directory if it didnt exist
authorDoug Coleman <doug.coleman@gmail.com>
Sun, 14 Apr 2024 21:00:29 +0000 (16:00 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 14 Apr 2024 21:00:29 +0000 (16:00 -0500)
rename some stuff for consistency

22 files changed:
basis/fixups/fixups.factor
basis/http/download/download-docs.factor
basis/http/download/download.factor
basis/io/directories/directories.factor
basis/unicode/breaks/breaks-tests.factor
basis/unicode/collation/collation-tests.factor
basis/unicode/normalize/normalize-tests.factor
extra/bunny/model/model.factor
extra/curl/curl.factor
extra/google/translate/translate.factor
extra/gpu/demos/bunny/bunny.factor
extra/machine-learning/data-sets/data-sets.factor
extra/macho/macho-tests.factor
extra/project-euler/098/098.factor
extra/rosetta-code/anagrams-deranged/anagrams-deranged.factor
extra/rosetta-code/ordered-words/ordered-words.factor
extra/rosetta-code/text-processing/max-licenses/max-licenses.factor
extra/scryfall/scryfall.factor
extra/spelling/spelling.factor
extra/tldr/tldr.factor
extra/youtube/youtube.factor
extra/zealot/factor/factor.factor

index 9ec5d6b285719462761a1d93f70a11de00e1b53c..21cad6c9eb002162c04786533e3c4221c71fe27e 100644 (file)
@@ -74,7 +74,8 @@ CONSTANT: word-renames {
     { "assoc-all-value?" { "all-values?" "0.100" } }
     { "assoc-any-key?" { "any-key?" "0.100" } }
     { "assoc-any-value?" { "any-value?" "0.100" } }
-    { "?download-to" { "download-once-to" "0.100" } }
+    { "?download-to" { "download-once-into" "0.100" } }
+    { "download-to" { "download-into" "0.100" } }
 }
 
 : compute-assoc-fixups ( continuation name assoc -- seq )
index 093ce7b90545b70d707843c3dcb1e82aeaaac5b5..d3c97cdd68add378c15503c7cf12feb55bd3a015 100644 (file)
@@ -10,13 +10,13 @@ HELP: download
 { $notes "Use this to download the file every time." }
 { $errors "Throws an error if the HTTP request fails." } ;
 
-HELP: download-to
+HELP: download-as
 { $values { "url" { $or url string } } { "path" "a pathname string" } }
 { $description "Downloads the contents of the URL to a file with the given pathname and returns the pathname." }
 { $notes "Use this to download the file every time." }
 { $errors "Throws an error if the HTTP request fails." } ;
 
-HELP: download-once-to
+HELP: download-once-as
 { $values { "url" { $or url string } } { "path" "a pathname string" } }
 { $description "If the file exists on disk, returns that pathname without downloading anything. Otherwise, downloads the contents of the URL to a file with the given pathname and returns the pathname." }
 { $notes "Use this if the contents of the URL are not expected to change." }
@@ -29,8 +29,8 @@ ARTICLE: "http.download" "HTTP Download Utilities"
 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
 { $subsections
     download
-    download-to
-    download-once-to
+    download-as
+    download-once-as
 }
 ;
 
index 3f8b2be8ceb4976986a39fd9b349f088007e413b..618cfa2f9e74f2256b8c56b6aacbcf6a0ab1524d 100644 (file)
@@ -12,7 +12,7 @@ IN: http.download
     [ ago ] bi*
     over [ before? ] [ 2drop t ] if ;
 
-: delete-when-old ( path duration -- deleted? )
+: delete-when-old ( path duration -- deleted/missing? )
     dupd file-too-old-or-not-exists? [ ?delete-file t ] [ drop f ] if ;
 
 : file-matches-checksum? ( path checksum-type bytes -- ? )
@@ -84,20 +84,30 @@ IN: http.download
 
 PRIVATE>
 
-: download-to ( url path -- path )
-    [
-        [ download-temporary-name binary ] keep
-        '[ _ http-write-request ] with-unique-file-writer
-    ] dip [ move-file ] keep ;
+: download-to-temporary-file ( url -- path )
+    [ download-temporary-name binary ] keep
+    '[ _ http-write-request ] with-unique-file-writer ;
+
+: download-as ( url path -- path )
+    [ download-to-temporary-file ] dip [ ?move-file ] keep ;
+
+: download-into ( url path -- path )
+    [ [ download-to-temporary-file ] keep ] dip
+    dup make-directories to-directory nip
+    [ move-file ] keep ;
+
+: download-once-as ( url path -- path )
+    dup file-exists? [ nip ] [ download-as ] if ;
+
+: download-once-into ( url path -- path ) to-directory download-once-as ;
 
-: download-once-to ( url path -- path )
-    dup file-exists? [ nip ] [ download-to ] if ;
+: download-once ( url -- path ) "resource:" download-once-into ;
 
-: download-once ( url -- path )
-    dup download-name download-once-to ;
+: download-outdated-as ( url path duration -- path )
+    2dup delete-when-old [ drop download-as ] [ drop nip ] if ;
 
-: download-outdated-to ( url path duration -- path )
-    2dup delete-when-old [ drop download-to ] [ drop nip ] if ;
+: download-outdated-into ( url path duration -- path )
+    [ to-directory ] dip download-outdated-as ;
 
 : download ( url -- path )
-    dup download-name download-to ;
+    dup download-name download-as ;
index 5b30462c112f5f34458ccbb0b747574a37dfad65..6017361f9d5a662ffb8d12480dbd6f77aebaa9e5 100644 (file)
@@ -215,8 +215,13 @@ HOOK: delete-directory io-backend ( path -- )
 
 HOOK: move-file io-backend ( from to -- )
 
+: create-parent-directory ( path -- )
+    normalize-path parent-directory make-directories ;
+
 : ?move-file ( from to -- )
-    over file-exists? [ move-file ] [ 2drop ] if ;
+    over file-exists? [
+        dup create-parent-directory move-file
+    ] [ 2drop ] if ;
 
 HOOK: move-file-atomically io-backend ( from to -- )
 
index 801b486ad8ef3d64c071ef314544cab5be47c26f..3c25b953952c8284400ad6da6e9e3a9443a59426 100644 (file)
@@ -21,11 +21,11 @@ IN: unicode.breaks.tests
 
 : grapheme-break-test ( -- filename )
     "https://downloads.factorcode.org/misc/UCD/15.1.0/auxiliary/GraphemeBreakTest.txt"
-    "GraphemeBreakTest-15.1.0.txt" cache-file download-once-to ;
+    "GraphemeBreakTest-15.1.0.txt" cache-file download-once-as ;
 
 : word-break-test ( -- filename )
     "https://downloads.factorcode.org/misc/UCD/15.1.0/auxiliary/WordBreakTest.txt"
-    "WordBreakTest-15.1.0.txt" cache-file download-once-to ;
+    "WordBreakTest-15.1.0.txt" cache-file download-once-as ;
 
 : parse-test-file ( file-name -- tests )
     utf8 file-lines
index 6e985c2bab91d424b858aafc432c5642fee8429c..9e6a7eab2b159600468d0d46b3f5fcd9aecd8ba4 100644 (file)
@@ -19,7 +19,7 @@ IN: unicode.collation.tests
 
 : collation-test-lines ( -- lines )
     "https://downloads.factorcode.org/misc/UCA/15.1.0/CollationTest_SHIFTED.txt"
-    "CollationTest_SHIFTED_15.1.0.txt" cache-file download-once-to
+    "CollationTest_SHIFTED_15.1.0.txt" cache-file download-once-as
     utf8 file-lines [ "#" head? ] reject harvest ;
 
 : parse-collation-test-shifted ( -- lines )
index edfc27dba8af89305d03ddc5a46f0b798d7f7013..d62dfbb70c2144544d49fe1149b371bfb1bc5781 100644 (file)
@@ -27,7 +27,7 @@ IN: unicode.normalize.tests
 ! Could use simple-flat-file after some cleanup
 : parse-normalization-tests ( -- tests )
     "https://downloads.factorcode.org/misc/UCD/15.1.0/NormalizationTest.txt"
-    "NormalizationTest-15.1.0.txt" cache-file download-once-to
+    "NormalizationTest-15.1.0.txt" cache-file download-once-as
     utf8 file-lines [ "#" head? ] reject
     [ "@" head? ] split*-when
     2 <groups> [ first2 [ first ] dip 2array ] map
index dd2003a4c92683e4e16f340f7f8c665f79580495..88d1652488069f0eed95e34d50eac4e5fc33db33 100644 (file)
@@ -46,7 +46,7 @@ CONSTANT: model-url
 "https://downloads.factorcode.org/misc/bun_zipper.ply"
 
 : download-bunny ( -- path )
-    model-url model-path download-once-to ;
+    model-url model-path download-once-as ;
 
 :: (draw-triangle) ( ns vs triple -- )
     triple [| elt |
index 2207d213da8bcf310e0baf7431786e07f7adff1d..c8ae39ffcf5d3a745f42e3934c896b5b038cc17a 100644 (file)
@@ -34,7 +34,7 @@ DESTRUCTOR: fclose
 
 PRIVATE>
 
-: curl-download-to ( url path -- )
+: curl-download-as ( url path -- )
     [
         curl-init
         [ swap curl-set-file ]
index a8ddafa31fcbef820bbbe162b25b473c676002e6..175a66ee5264c3b136407db336b3cf4c03f3a0fd 100644 (file)
@@ -55,7 +55,7 @@ TUPLE: response-error response error ;
 : translate-tts ( text -- file )
     "https://translate.google.com/translate_tts?tl=en" >url
     swap "q" set-query-param [
-        "" ".mp3" unique-file download-to
+        "" ".mp3" unique-file download-as
     ] with-temp-directory ;
 
 ! Example:
index f5963e714e89dc848caacd3ff4476ec5b9c4ae84..c082f3484e90e958fdf45b3d502a8270bbc6abbd 100644 (file)
@@ -149,7 +149,7 @@ CONSTANT: bunny-model-url
 "https://downloads.factorcode.org/misc/bun_zipper.ply"
 
 : download-bunny ( -- path )
-    bunny-model-url bunny-model-path download-once-to ;
+    bunny-model-url bunny-model-path download-once-as ;
 
 : get-bunny-data ( bunny-state -- )
     download-bunny bunny-data
index 8de52112510cdea90acec6a7431e11947d675716..61daf8e33c7390ead14ee36f17a69968fca2b07e 100644 (file)
@@ -98,7 +98,7 @@ CONSTANT: datasets-path "resource:datasets/"
             "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/t10k-images-idx3-ubyte.gz"
             "https://github.com/golbin/TensorFlow-MNIST/raw/master/mnist/data/t10k-labels-idx1-ubyte.gz"
         }
-        [ [ download-once ] parallel-each ]
+        [ [ download-once-into ] parallel-each ]
         [ [ dup file-stem file-exists? [ drop ] [ file-name gzip-decompress-file ] if ] each ]
         [ [ file-stem binary file-contents ] map ] tri
         first4 {
index 5bcfa60378dfffb2b1e533e022a98d502936b8a5..73c7bb148af2a25a51e9395ce7ff2331d39470ca 100644 (file)
@@ -24,11 +24,11 @@ STRING: validation-output
 
 : a.macho ( -- path )
     URL" https://downloads.factorcode.org/misc/a.macho"
-    "a.macho" cache-file download-once-to ;
+    "a.macho" cache-file download-once-as ;
 
 : a2.macho ( -- path )
     URL" https://downloads.factorcode.org/misc/a2.macho"
-    "a2.macho" cache-file download-once-to ;
+    "a2.macho" cache-file download-once-as ;
 
 cpu ppc? [
     { $ validation-output }
index 4a97d916a900e2f0ea4c90d9cf9d3738ce6d116b..90003dfae2545f8a2d892b661a6c11a81a2b1ca2 100644 (file)
@@ -42,7 +42,7 @@ IN: project-euler.098
 
 : wordlist ( -- seq )
     "https://projecteuler.net/project/resources/p098_words.txt"
-    "p098_words.txt" temp-file download-once-to
+    "p098_words.txt" temp-file download-once-as
     utf8 file-contents "," split [ rest-slice but-last ] map ;
 
 : squarelist ( n -- seq )
index 53db81bb0036624e8eb3f26520aa13643cac6c7e..d2bd6b18427fdbe4a6ad021f133a5367ec4df2b1 100644 (file)
@@ -42,7 +42,7 @@ IN: rosettacode.anagrams-deranged
 
 : default-word-list ( -- path )
     URL" https://raw.githubusercontent.com/quinnj/Rosetta-Julia/master/unixdict.txt"
-    "unixdict.txt" temp-file download-once-to ;
+    "unixdict.txt" temp-file download-once-as ;
 
 : longest-deranged-anagrams ( -- anagrams )
     default-word-list (longest-deranged-anagrams) ;
index 8c37abfc3696542e6316a7294ea01c71874ebdfd..dff944af62461651a42ae86369fd59e8b76d1e38 100644 (file)
@@ -19,7 +19,7 @@ IN: rosetta-code.ordered-words
 MEMO: word-list ( -- seq )
     URL" https://raw.githubusercontent.com/quinnj/Rosetta-Julia/master/unixdict.txt"
     "unixdict.txt" temp-file
-    download-once-to utf8 file-lines ;
+    download-once-as utf8 file-lines ;
 
 : ordered-word? ( word -- ? )
     >lower [ <= ] monotonic? ;
index 2110ad19cda6f8d8fcf0216d69e5b80544a690af..6064dc87d6d964e5b6039679c98b900d42bbdc11 100644 (file)
@@ -63,7 +63,7 @@ TUPLE: maxlicense max-count current-count times ;
 
 MEMO: mlijobs ( -- lines )
     URL" https://raw.githubusercontent.com/def-/nim-unsorted/master/mlijobs.txt"
-    "mlijobs.txt" temp-file download-once-to ascii file-lines ;
+    "mlijobs.txt" temp-file download-once-as ascii file-lines ;
 
 PRIVATE>
 
index 47d401eebeae365705774946b11e9f4ef20404d0..e3f2d35bfcd91fec0a992e263d5afdc035667f59 100644 (file)
@@ -28,7 +28,7 @@ CONSTANT: scryfall-images-path "resource:scryfall-images/"
 
 : load-scryfall-json ( type path -- uri )
     [ find-scryfall-json "download_uri" of ] dip
-    6 hours download-outdated-to path>json ;
+    10 days download-outdated-as path>json ;
 
 MEMO: mtg-oracle-cards ( -- json )
     "oracle_cards" scryfall-oracle-json-path load-scryfall-json ;
@@ -68,7 +68,7 @@ MEMO: scryfall-rulings-json ( -- json )
 
 : download-scryfall-image ( assoc -- path )
     dup scryfall-local-image-path dup delete-when-zero-size
-    [ download-once-to ] [ nip ] if ;
+    [ download-once-as ] [ nip ] if ;
 
 : download-normal-images ( seq -- seq' )
     ensure-scryfall-images-directory
@@ -220,6 +220,11 @@ MEMO: scryfall-rulings-json ( -- json )
 : filter-premodern ( seq -- seq' ) "premodern" filter-legalities ;
 : filter-predh ( seq -- seq' ) "predh" filter-legalities ;
 
+: spanish-standard-cards ( -- seq )
+    scryfall-all-cards-json
+    filter-standard
+    [ "lang" of "es" = ] filter ;
+
 : filter-red-any ( seq -- seq' ) [ "colors" of "R" swap member? ] filter ;
 : filter-red-only ( seq -- seq' ) [ "colors" of { "R" } = ] filter ;
 : filter-blue-any ( seq -- seq' ) [ "colors" of "U" swap member? ] filter ;
index f3c512f9cd2a8eaafeb603eff2a273a4c7065dc9..52c28626875b6218348b9d3302814f2cdf0c770a 100644 (file)
@@ -58,7 +58,7 @@ CONSTANT: ALPHABET "abcdefghijklmnopqrstuvwxyz"
 
 MEMO: default-dictionary ( -- counts )
     URL" https://norvig.com/big.txt" "big.txt" temp-file
-    download-once-to load-dictionary ;
+    download-once-as load-dictionary ;
 
 : (correct) ( word dictionary -- word/f )
     corrections ?first ;
index 47560add8b70734c6363d15a7fdbc3b86c5f07ec..750795fa02b913583ed7ca6a49f04524bd443b3f 100644 (file)
@@ -26,7 +26,7 @@ CONSTANT: tldr-zip URL" https://tldr-pages.github.io/assets/tldr.zip"
 
 : download-tldr ( -- )
     "tldr" cache-file dup make-directory [
-        tldr-zip "tldr.zip" download-once-to drop
+        tldr-zip "tldr.zip" download-once-as drop
         { "unzip" "tldr.zip" } try-process
     ] with-directory ;
 
index 261165dbb454dd5e056b2e370943d7b8b6b58ab1..90a9e754aaa0339c152c46b91f03ec9f53d1d76f 100644 (file)
@@ -73,5 +73,5 @@ CONSTANT: video-info-url URL" https://www.youtube.com/get_video_info"
         video-formats [ "type" of "video/mp4" head? ] find nip
         video-download-url
     ] [
-        "title" of sanitize ".mp4" append download-once-to
+        "title" of sanitize ".mp4" append download-once-as
     ] tri ;
index dbf172aa47251d8f45ca7789e72371ba909105c0..86529461f7d99c0f7e3895366006deae1cc377a2 100644 (file)
@@ -26,7 +26,7 @@ IN: zealot.factor
     [ download-boot-checksum-branch ] if ;
 
 : download-boot-image ( path url -- )
-    '[ _ my-arch-name "boot.%s.image" sprintf download-to drop ] with-directory ;
+    '[ _ my-arch-name "boot.%s.image" sprintf download-as drop ] with-directory ;
 
 : arch-git-boot-image-path ( arch git-id -- str )
     "https://downloads.factorcode.org/images/build/boot.%s.image.%s" sprintf ;