Skip to content

Commit

Permalink
Added pharo7 model package
Browse files Browse the repository at this point in the history
  • Loading branch information
noha committed Aug 20, 2018
1 parent bd5102c commit 7012ee2
Show file tree
Hide file tree
Showing 56 changed files with 305 additions and 2 deletions.
5 changes: 5 additions & 0 deletions source/Magritte-Pharo7-Model.package/.filetree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"separateMethodMetaAndSource" : false,
"noMethodMetaData" : true,
"useCypressPropertiesFile" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*Magritte-Pharo7-Model
magrittePharoModel
^ self new
name: 'Magritte-Pharo-Model';
addDependency: 'Magritte-Model';
url: #magritteUrl;
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "GRPackage"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*Magritte-Pharo7-Model
magritteAllSubInstancesOf: aClass do: aBlock
"Evaluate the aBlock for all instances of aClass and all its subclasses."

aClass allSubInstancesDo: aBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*Magritte-Pharo7-Model
magritteClassNamed: aString
"Return the class named aString, nil if the class can't be found."

^ Smalltalk classNamed: aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*Magritte-Pharo7-Model
magritteColorClass
"Return a Color class"

^ Color
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*Magritte-Pharo7-Model
magritteEvaluatorClassFor: aClass
"Answer an evaluator class appropriate for evaluating expressions in the
context of this class."

^ aClass compilerClass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*Magritte-Pharo7-Model
magritteTimeStampClass
"Return the platform's TimeStamp class. It is currently assumed that all platforms
have one, though this may not be correct and could require moving timestamp stuff
to its own package in order to resolve."

^ DateAndTime
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*Magritte-Pharo7-Model
magritteTimeStampIfAbsent: absentBlock
"Return the TimeStamp class, or if the platform does not have a TimeStamp
return the result of evaluating absentBlock."

^ DateAndTime
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*Magritte-Pharo7-Model
magritteUniqueObject
"Answer a random object, such as a UUID, that is extremely likely to
be unique over space and time."

^ ByteArray withAll: UUID new
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "GRPharoPlatform"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I manage the file-data I represent on the file-system. From the programmer this looks the same as if the file would be in memory (==*MAMemoryFileModel*==), as it is transparently loaded and written out as necessary.

I delegate my actual location on disk to MAFileDatabase (see class comment).
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseDirectoryPath: aString
" deprecated, but still used by pier "
MAFileDatabase baseDirectory: aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseDirectoryPath
" deprecated: use MAFileDatabase baseDirectoryPath instead "
^MAFileDatabase baseDirectory fullName
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseUrl: aString
" deprecated: use MAFileDatabase baseUrl instead "
^ MAFileDatabase baseUrl: aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseUrl
" deprecated: use MAFileDatabase baseDirectoryPath instead "
^ MAFileDatabase baseUrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comparing
= anObject
^ super = anObject and: [ self directory = anObject directory ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
configuration
baseUrl
^ self database baseUrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
accessing
contents: aByteArray
| stream |
stream := self writeStream.
[ stream nextPutAll: aByteArray asByteArray ]
ensure: [ stream close ].
super contents: aByteArray
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
accessing
contents
| stream |
^ self file exists
ifFalse: [ ByteArray new ]
ifTrue: [
stream := self readStream.
[ stream contents ]
ensure: [ stream close ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
private
database

^ MAFileDatabase new.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing-dynamic
directory

^ directory ifNil: [ directory := self database uniqueLocation ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing-dynamic
file
^(self directory / self filename) asFileReference
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
initialization
finalize
| dir |
dir := self directory.
dir exists
ifTrue: [ dir deleteAll ].
[ (dir := dir parent) hasChildren ]
whileFalse: [ dir ensureDelete ].
super finalize.
location := nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comparing
hash
^ super hash bitXor: self directory hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
copying
postCopy
| previous |
super postCopy.
previous := self contents.
location := nil.
self contents: previous
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing-dynamic
readStream
^ self file binaryReadStream
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing-dynamic
writeStream
^ self file binaryWriteStream
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"commentStamp" : "<historical>",
"super" : "MAFileModel",
"category" : "Magritte-Pharo7-Model",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"location",
"directory"
],
"name" : "MAExternalFileModel",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
I provide locations for external files.

- The ==#baseDirectory== is the place where Magritte puts its file-database. Keep this value to nil to make it default to a subdirectory next to the Smalltalk image.
- The ==#baseUrl== is a nice optimization to allow Apache (or any other Web Server) to directly serve the files. ==#baseUrl== is an absolute URL-prefix that is used to generate the path to the file. If you have specified one the file data does not go trough the image anymore, but instead is directly served trough the properly configured Web Server.

The files are currently stored using the following scheme:

=/files/9d/bsy8kyp45g0q7blphknk48zujap2wd/earthmap1k.jpg
=1 2 3 4

1. is the #baseDirectory as specified in the settings.
2. Are 256 directories named '00' to 'ff' to avoid having thousands of files in the same directory. Unfortunately this leads to problems with the Squeak file primitives and some filesystems don't handle that well. This part is generated at random.
3. This is a secure ID, similar to the Seaside session key. It is generated at random and provides a security system that even works through Apache (you have to disable directory listings of course): if you don't know the filename you can not access the file.
4. This is the original filename. Subclasses might want to store other cached versions of the same file there, for example resized images, etc.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessing
baseDirectory: aStringOrDirectory
"Defines where the files are stored. If this value is set to nil, it defaults to a subdirectory of of the current image-location."

baseDirectory := aStringOrDirectory asFileReference.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
baseDirectory
^ baseDirectory ifNil: [ FileLocator imageDirectory / 'files' ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
accessing
baseUrl: aString
"Defines the base-URL where the files are served from, when using an external web server. This setting is left to nil by default, causing the files to be served trough the image."

baseUrl := aString isNil ifFalse: [
aString last = $/
ifFalse: [ aString ]
ifTrue: [ aString copyUpToLast: $/ ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
baseUrl
^ baseUrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public
garbageCollect
"Remove obsolete files from the file-system that are not referenced from the image anymore. This method has to be called manually and is not intended to be portable."

| mark sweep |
mark := self allInstances
collect: [ :each | each directory ].
sweep := Array with: self baseDirectory.
self locationDefinition do: [ :definition |
sweep := sweep gather: [ :directory |
directory hasChildren ifFalse: [ directory delete ].
FileLocator imageDirectory directories select: [ :e | e basename size = definition first ] ] ].
sweep do: [ :directory |
(mark includes: directory) ifFalse: [ directory recursiveDelete ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public
locationDefinition
^ #( ( 2 '63450af8d9c2e17b' ) ( 30 'iaojv41bw67e0tud5m9rgplqfy8x3cs2kznh' ) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseDirectory

^ self class baseDirectory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
baseUrl

^ self class baseUrl.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
private
randomIdentifierName

| digits idLength allowedCharacters |
digits := $0 to: $9.
allowedCharacters := digits, Character alphabet.
idLength := 30.
^ String streamContents: [ :str |
idLength timesRepeat: [ str nextPut: allowedCharacters atRandom ] ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
private
randomPartitionFolderName
"Returns a string between '00' to 'FF'"

| twoCharacterHexString |
twoCharacterHexString := 16rFF atRandom printStringBase: 16 length: 2 padded: true.
^ twoCharacterHexString asLowercase.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public
uniqueLocation
"Finds an unique path to be used and create the necessary sub directories."

| partition id result |
[
partition := self randomPartitionFolderName.
id := self randomIdentifierName.
result := self baseDirectory ensureCreateDirectory / partition / id.
] doWhileTrue: [ result exists ].

^ result ensureCreateDirectory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"commentStamp" : "<historical>",
"super" : "Object",
"category" : "Magritte-Pharo7-Model",
"classinstvars" : [
"baseDirectory",
"baseUrl"
],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "MAFileDatabase",
"type" : "normal"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
paths
extensionFor: aString

^ aString asFileReference extension.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
entries
fileSizeFor: filenameString
^ filenameString asFileReference size
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
paths
fullNameFor: aString

^ aString asFileReference fullName.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
locations
imageDirectory

^ FileLocator imageDirectory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mime types
mimeTypesForExtension: aString

| types |
types := MIMEType forExtensionReturnMimeTypesOrNil: aString.
^ types ifNotNil: [ types collect: [ :e | e asString ] ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "",
"super" : "Object",
"category" : "Magritte-Pharo7-Model",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "MAFileSystem",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*Magritte-Pharo7-Model
chooseFile: windowTitle in: directoryString

| chooser |
chooser := FileDialogWindow newWithTheme: UITheme current.
chooser
title: windowTitle;
selectDirectory: directoryString;
answerFileEntry.
^ chooser openModal answer ifNotNil: [ :answer | answer asFileReference ].
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "MorphicUIManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SystemOrganization addCategory: #'Magritte-Pharo7-Model'!
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(name 'Magritte-Pharo7-Model')
Loading

0 comments on commit 7012ee2

Please sign in to comment.