2017-07-03 22:12:09 +00:00
|
|
|
"
|
2017-07-05 11:17:44 +00:00
|
|
|
I contain simple general functionality used by Grafoscopio, Dataviz
|
|
|
|
or other related projects.
|
2017-07-03 22:12:09 +00:00
|
|
|
"
|
|
|
|
Class {
|
2017-07-06 21:23:22 +00:00
|
|
|
#name : #GrafoscopioUtils,
|
2017-07-03 22:12:09 +00:00
|
|
|
#superclass : #Object,
|
|
|
|
#category : #'Grafoscopio-Utils'
|
|
|
|
}
|
|
|
|
|
2017-08-19 19:16:59 +00:00
|
|
|
{ #category : #utilities }
|
2017-08-18 02:23:18 +00:00
|
|
|
GrafoscopioUtils class >> checksumFor: aFileReference [
|
|
|
|
^ (SHA1 new hashMessage: aFileReference binaryReadStream contents) hex.
|
|
|
|
]
|
|
|
|
|
2017-12-07 11:21:36 +00:00
|
|
|
{ #category : #updating }
|
|
|
|
GrafoscopioUtils class >> download: fileNameWithRelativePath from: urlString into: aFolder [
|
|
|
|
| fileName parentFolder |
|
|
|
|
fileName := (fileNameWithRelativePath splitOn: $/) last.
|
|
|
|
parentFolder := self ensureCreateDirectory: fileNameWithRelativePath into: aFolder.
|
|
|
|
self
|
|
|
|
downloadingFrom: urlString, fileNameWithRelativePath
|
|
|
|
withMessage: 'Downloading ', fileName
|
|
|
|
into: parentFolder
|
|
|
|
]
|
|
|
|
|
2017-07-03 22:12:09 +00:00
|
|
|
{ #category : #'graphical interface' }
|
2017-07-06 21:23:22 +00:00
|
|
|
GrafoscopioUtils class >> downloadingFrom: downloadUrl withMessage: aString into: location [
|
2017-07-03 22:12:09 +00:00
|
|
|
| fileName |
|
2017-08-15 11:54:55 +00:00
|
|
|
fileName := (self sanitize: downloadUrl) segments last.
|
2017-08-11 13:37:35 +00:00
|
|
|
(location / fileName) ensureDelete.
|
2017-07-03 22:12:09 +00:00
|
|
|
[: bar |
|
|
|
|
bar title: aString.
|
|
|
|
[ZnClient new
|
|
|
|
enforceHttpSuccess: true;
|
|
|
|
url: downloadUrl;
|
|
|
|
downloadTo: location;
|
|
|
|
signalProgress: true
|
|
|
|
]
|
|
|
|
on: HTTPProgress
|
|
|
|
do: [ :progress |
|
|
|
|
progress isEmpty ifFalse: [ bar current: progress percentage ].
|
|
|
|
progress resume ].
|
|
|
|
] asJob run.
|
2019-02-08 17:51:39 +00:00
|
|
|
|
|
|
|
^ (location / fileName) asFileReference
|
2017-07-03 22:12:09 +00:00
|
|
|
]
|
2017-08-11 13:37:35 +00:00
|
|
|
|
2017-12-07 11:21:36 +00:00
|
|
|
{ #category : #utilities }
|
|
|
|
GrafoscopioUtils class >> ensureCreateDirectory: fileNameWithRelativePath into: aFolder [
|
|
|
|
| relativePathFolders parentFolder newPath |
|
|
|
|
relativePathFolders := (fileNameWithRelativePath splitOn: $/) allButLast.
|
|
|
|
newPath := aFolder path.
|
|
|
|
relativePathFolders do: [ :folder | newPath := newPath / folder ].
|
|
|
|
parentFolder := newPath asFileReference.
|
|
|
|
parentFolder ensureCreateDirectory.
|
|
|
|
^ parentFolder
|
|
|
|
|
|
|
|
]
|
|
|
|
|
2019-04-22 19:54:07 +00:00
|
|
|
{ #category : #persistence }
|
|
|
|
GrafoscopioUtils class >> exportAsSton: anObject on: aFileReference [
|
2019-04-22 20:15:48 +00:00
|
|
|
aFileReference exists ifTrue: [ aFileReference ensureDelete ].
|
|
|
|
aFileReference ensureCreateFile.
|
|
|
|
aFileReference writeStreamDo: [ :stream |
|
|
|
|
(STON writer on: stream)
|
|
|
|
newLine: String crlf;
|
|
|
|
prettyPrint: true;
|
|
|
|
keepNewLines: true;
|
|
|
|
nextPut: anObject ].
|
|
|
|
|
2019-04-22 19:54:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
]
|
|
|
|
|
2017-08-15 11:54:55 +00:00
|
|
|
{ #category : #'graphical interface' }
|
|
|
|
GrafoscopioUtils class >> getContentsFrom: url withMessage: aString [
|
|
|
|
| client |
|
|
|
|
[: bar |
|
|
|
|
bar title: aString.
|
|
|
|
[client := ZnClient new
|
|
|
|
enforceHttpSuccess: true;
|
|
|
|
get: (url);
|
|
|
|
signalProgress: true
|
|
|
|
]
|
|
|
|
on: HTTPProgress
|
|
|
|
do: [ :progress |
|
|
|
|
progress isEmpty ifFalse: [ bar current: progress percentage ].
|
|
|
|
progress resume ].
|
|
|
|
] asJob run.
|
|
|
|
^ client contents.
|
|
|
|
]
|
|
|
|
|
2019-11-09 11:43:22 +00:00
|
|
|
{ #category : #persistence }
|
|
|
|
GrafoscopioUtils class >> joinLinesFor: aCollection [
|
|
|
|
|
|
|
|
| joinedLines |
|
|
|
|
|
|
|
|
joinedLines := WriteStream on: ''.
|
|
|
|
aCollection do: [ :line | joinedLines nextPutAll: line; crlf ].
|
|
|
|
^ joinedLines contents
|
|
|
|
]
|
|
|
|
|
2017-08-19 19:16:59 +00:00
|
|
|
{ #category : #utilities }
|
|
|
|
GrafoscopioUtils class >> perform: aString on: anObject [
|
|
|
|
|
|
|
|
| msg |
|
|
|
|
msg := self selectorAndArgumentsFrom: aString.
|
|
|
|
msg
|
|
|
|
at: 'args'
|
|
|
|
ifPresent: [ ^ anObject perform: (msg at: 'selector') withArguments: (msg at: 'args') ]
|
|
|
|
ifAbsent: [ ^ anObject perform: (msg at: 'selector') ]
|
|
|
|
]
|
|
|
|
|
2017-08-11 13:37:35 +00:00
|
|
|
{ #category : #private }
|
|
|
|
GrafoscopioUtils class >> sanitize: url [
|
|
|
|
"I remove white spaces in url's and prepend 'http://' to urls when it is ommited, so
|
|
|
|
operations that rely on sane and well formed urls don't throw error messages."
|
|
|
|
| sanitized modUrl |
|
|
|
|
modUrl := url.
|
2017-09-25 09:43:59 +00:00
|
|
|
[modUrl asString endsWith: ' ']
|
2017-08-11 13:37:35 +00:00
|
|
|
whileTrue: [ modUrl := modUrl copyFrom: 1 to: (modUrl size - 1) ].
|
2018-09-26 18:22:40 +00:00
|
|
|
(url asString beginsWith: 'http') "http or https"
|
2017-08-11 13:37:35 +00:00
|
|
|
ifFalse: [ sanitized := ('http://', modUrl) asUrl ]
|
|
|
|
ifTrue: [ sanitized := modUrl asUrl ].
|
|
|
|
^ sanitized
|
|
|
|
]
|
|
|
|
|
2017-08-19 19:16:59 +00:00
|
|
|
{ #category : #utilities }
|
|
|
|
GrafoscopioUtils class >> selectorAndArgumentsFrom: aString [
|
2017-08-26 12:31:46 +00:00
|
|
|
"I return from aString a dictionary that contains a message and an array of arguments used
|
|
|
|
in such string. Notice that the keyword message must contain spaces between the ':' and its
|
|
|
|
respective argument.
|
|
|
|
See GrafoscopioUtilsTest for examples"
|
|
|
|
| msgArray answer keywords selector args |
|
2017-08-19 19:16:59 +00:00
|
|
|
answer := OrderedDictionary new.
|
|
|
|
msgArray := aString splitOn: ':'.
|
|
|
|
msgArray size = 1
|
|
|
|
ifTrue: [
|
|
|
|
^ answer
|
|
|
|
at: 'selector' put: (msgArray at: 1) asSymbol;
|
|
|
|
yourself ].
|
2017-08-26 12:31:46 +00:00
|
|
|
selector := ''.
|
|
|
|
args := OrderedCollection new.
|
|
|
|
aString
|
|
|
|
splitOn: Character space
|
|
|
|
do: [ :part |
|
|
|
|
part endsWithAColon
|
|
|
|
ifTrue: [ selector := selector, part ]
|
|
|
|
ifFalse: [ args add: part ] ].
|
|
|
|
answer
|
|
|
|
at: 'selector' put: selector asSymbol;
|
|
|
|
at: 'args' put: args asArray.
|
|
|
|
^ answer
|
2017-08-19 19:16:59 +00:00
|
|
|
]
|
|
|
|
|
2017-08-11 13:37:35 +00:00
|
|
|
{ #category : #updating }
|
|
|
|
GrafoscopioUtils class >> update [
|
|
|
|
"Updates GrafoscopioUtils with new versions of itself take from the source code repository."
|
|
|
|
Gofer new
|
|
|
|
smalltalkhubUser: 'Offray' project: 'Grafoscopio';
|
2017-08-11 13:39:08 +00:00
|
|
|
package: 'Grafoscopio-Utils';
|
2017-08-11 13:37:35 +00:00
|
|
|
load.
|
|
|
|
]
|