Grafoscopio/src/Grafoscopio-Utils/GrafoscopioUtils.class.st

165 lines
4.7 KiB
Smalltalk

"
I contain simple general functionality used by Grafoscopio, Dataviz
or other related projects.
"
Class {
#name : #GrafoscopioUtils,
#superclass : #Object,
#category : #'Grafoscopio-Utils'
}
{ #category : #utilities }
GrafoscopioUtils class >> checksumFor: aFileReference [
^ (SHA1 new hashMessage: aFileReference binaryReadStream contents) hex.
]
{ #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
]
{ #category : #'graphical interface' }
GrafoscopioUtils class >> downloadingFrom: downloadUrl withMessage: aString into: location [
| fileName |
fileName := (self sanitize: downloadUrl) segments last.
(location / fileName) ensureDelete.
[: 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.
^ (location / fileName) asFileReference
]
{ #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
]
{ #category : #persistence }
GrafoscopioUtils class >> exportAsSton: anObject on: aFileReference [
aFileReference exists ifTrue: [ aFileReference ensureDelete ].
aFileReference ensureCreateFile.
aFileReference writeStreamDo: [ :stream |
(STON writer on: stream)
newLine: String crlf;
prettyPrint: true;
keepNewLines: true;
nextPut: anObject ].
]
{ #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.
]
{ #category : #persistence }
GrafoscopioUtils class >> joinLinesFor: aCollection [
| joinedLines |
joinedLines := WriteStream on: ''.
aCollection do: [ :line | joinedLines nextPutAll: line; crlf ].
^ joinedLines contents
]
{ #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') ]
]
{ #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.
[modUrl asString endsWith: ' ']
whileTrue: [ modUrl := modUrl copyFrom: 1 to: (modUrl size - 1) ].
(url asString beginsWith: 'http') "http or https"
ifFalse: [ sanitized := ('http://', modUrl) asUrl ]
ifTrue: [ sanitized := modUrl asUrl ].
^ sanitized
]
{ #category : #utilities }
GrafoscopioUtils class >> selectorAndArgumentsFrom: aString [
"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 |
answer := OrderedDictionary new.
msgArray := aString splitOn: ':'.
msgArray size = 1
ifTrue: [
^ answer
at: 'selector' put: (msgArray at: 1) asSymbol;
yourself ].
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
]
{ #category : #updating }
GrafoscopioUtils class >> update [
"Updates GrafoscopioUtils with new versions of itself take from the source code repository."
Gofer new
smalltalkhubUser: 'Offray' project: 'Grafoscopio';
package: 'Grafoscopio-Utils';
load.
]