user_space 1.0.0 → 4.0.210114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 532cc3386ae1ec0bdec441e5d25d5279f82e8547
4
- data.tar.gz: 0a7e250d95306c27bfdc8d11781ec949eed1cb86
2
+ SHA256:
3
+ metadata.gz: e2c25f25272f9f0af6d080d708bc07b09b580c273943d654ff51562c54e78073
4
+ data.tar.gz: 6d33316656f0d3536fb76bde0d0c1f2980c1c6f122ad084ab254b081a64964d4
5
5
  SHA512:
6
- metadata.gz: c1a002a63985694c84a738c3028fd39fd33e0caeb19d135dbc7f2fd08c8c77bd75fdef6cc83d0263190d053b7d52dded3406883df5eba35b0127d4897bbc189e
7
- data.tar.gz: 03b8b6d9a28934c68ca6f820a8b1d2026d7eb4bf5b1e1168ae68d763671dd2de63d0192ec5781894b95570c28923529553e7f7e4ec638fd7091783d9230929d4
6
+ metadata.gz: e3db74fdfba700b3bab9139a7b728dc20edf901d47ee5498cc4a7d546eaa21f31f197ff1b82ec487b584118e4746952ffea0160e770e962ae14093bc8e68ccc1
7
+ data.tar.gz: 76728fce0b2081d921ba4258607464f228f2279a055e9c1b13db4665ce4eabea35cfccb58251461ab90d0fa49059a53ea021fd92aa377346dba2d4cec6ff93db
@@ -0,0 +1,81 @@
1
+ # UserSpace
2
+
3
+ * [VERSION 4.0.210114](https://github.com/carlosjhr64/user_space/releases)
4
+ * [github](https://www.github.com/carlosjhr64/user_space)
5
+ * [rubygems](https://rubygems.org/gems/user_space)
6
+
7
+ ## DESCRIPTION:
8
+
9
+ Maintains the user's cache, config, and data directories
10
+ with the gem-apps cache, config, and data files.
11
+
12
+ ## SYNOPSIS:
13
+
14
+ ```ruby
15
+ require 'json' # Using JSON parser for the config file.
16
+ require 'user_space'
17
+
18
+ module App
19
+ # ::App::CONFIG is your app's configuration.
20
+ CONFIG = {:tts=>'espeak', }
21
+ VERSION = '1.2.3'
22
+ end
23
+
24
+ USERSPACE = UserSpace.new(parser:JSON, appname:'myapp') #~> ^#<UserSpace:
25
+ # Will maintain these directories:
26
+ ['~/.cache/myapp',
27
+ '~/.config/myapp',
28
+ '~/.local/share/myapp'
29
+ ].all?{File.directory? File.expand_path _1} #=> true
30
+
31
+ # Unless this version has been installed,
32
+ # we copy over our data and cache directories.
33
+ USERSPACE.install unless USERSPACE.version == App::VERSION
34
+ # We have a version file:
35
+ File.exist? File.expand_path '~/.local/share/myapp/VERSION' #=> true
36
+
37
+ if USERSPACE.config?
38
+ # Because JSON hashes by String, converting to Symbol.
39
+ # We pad up App::CONFIG with user's preferences:
40
+ USERSPACE.config.each{|opt, value| App::CONFIG[opt.to_sym] = value}
41
+ else
42
+ # We initialize user preferences with our initial App::CONFIG
43
+ STDERR.puts "Writting '#{USERSPACE.config_file_name}'"
44
+ USERSPACE.config = App::CONFIG
45
+ end
46
+ # To do the same thing, you can also say:
47
+ # USERSPACE.configures(App::CONFIG)
48
+ # We have a config file:
49
+ File.exist? File.expand_path '~/.config/myapp/config.json' #=> true
50
+ ```
51
+
52
+ ## INSTALL:
53
+
54
+ ```shell
55
+ $ gem install user_space
56
+ ```
57
+
58
+ ## LICENSE:
59
+
60
+ (The MIT License)
61
+
62
+ Copyright (c) 2021 CarlosJHR64
63
+
64
+ Permission is hereby granted, free of charge, to any person obtaining
65
+ a copy of this software and associated documentation files (the
66
+ 'Software'), to deal in the Software without restriction, including
67
+ without limitation the rights to use, copy, modify, merge, publish,
68
+ distribute, sublicense, and/or sell copies of the Software, and to
69
+ permit persons to whom the Software is furnished to do so, subject to
70
+ the following conditions:
71
+
72
+ The above copyright notice and this permission notice shall be
73
+ included in all copies or substantial portions of the Software.
74
+
75
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
76
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
77
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
78
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
79
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
80
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
81
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,8 +1,126 @@
1
- require 'English'
2
1
  require 'fileutils'
3
- require 'json' # default parser
4
- require 'xdg'
5
- require 'user_space/version.rb'
6
- require 'user_space/user_space.rb'
7
2
  # Requires:
8
3
  #`ruby`
4
+
5
+ class UserSpace
6
+ VERSION = '4.0.210114'
7
+ XDG = {
8
+ 'cache' => ENV['XDG_CACHE_HOME'] || File.expand_path('~/.cache'),
9
+ 'config' => ENV['XDG_CONFIG_HOME'] || File.expand_path('~/.config'),
10
+ 'data' => ENV['XDG_DATA_HOME'] || File.expand_path('~/.local/share'),
11
+ }
12
+
13
+ attr_reader :parser,:ext,:appname,:xdgbases,:appdir,:config
14
+ def initialize( parser:,
15
+ ext: parser.to_s.downcase,
16
+ appname: File.basename($0),
17
+ xdgbases: ['cache', 'config', 'data'],
18
+ appdir: File.dirname(__dir__),
19
+ config: 'config')
20
+ @parser,@ext,@appname,@xdgbases,@appdir,@config = parser,ext,appname,xdgbases,appdir,config
21
+ install(false) # install with no overwrite
22
+ end
23
+
24
+ def xdg_pairs
25
+ @xdgbases.each do |base|
26
+ # yield basedir, userdir
27
+ yield File.join(@appdir, base), File.join(XDG[base], @appname)
28
+ end
29
+ end
30
+
31
+ # Note that initialize will not overwrite anything.
32
+ # This overwrites the user's data directory with a fresh install.
33
+ # App should consider being nice about this,
34
+ # like warn the user or something.
35
+ def install(overwrite=true)
36
+ xdg_pairs do |basedir, userdir|
37
+ if File.exist?(userdir)
38
+ # Sanity check
39
+ raise "Not a directory: #{userdir}" unless File.directory?(userdir)
40
+ # Pre-existing directory.
41
+ # Return unless user wants to overwrite.
42
+ next unless overwrite
43
+ else
44
+ Dir.mkdir(userdir, 0700)
45
+ end
46
+ if File.directory? basedir
47
+ Dir.glob("#{basedir}/**/*").each do |src|
48
+ dest = src.sub(basedir, userdir)
49
+ if File.directory? src
50
+ Dir.mkdir dest unless File.exist? dest
51
+ else
52
+ FileUtils.cp src, dest
53
+ end
54
+ FileUtils.chmod('u+rwX,go-rwx', dest)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def cachedir
61
+ File.join XDG['cache'], @appname
62
+ end
63
+
64
+ def configdir
65
+ File.join XDG['config'], @appname
66
+ end
67
+
68
+ def datadir
69
+ File.join XDG['data'], @appname
70
+ end
71
+
72
+ # Not really for public use.
73
+ def config_file_name
74
+ File.join XDG['config'], @appname, "#{@config}.#{@ext}"
75
+ end
76
+
77
+ # Not really for public use.
78
+ def version_file_name
79
+ File.join XDG['data'], @appname, 'VERSION'
80
+ end
81
+
82
+ def config?
83
+ File.exist?(config_file_name)
84
+ end
85
+
86
+ # Reads config
87
+ def config
88
+ @parser.load File.read(config_file_name)
89
+ rescue
90
+ $stderr.puts $!.message if $VERBOSE
91
+ $stderr.puts $!.backtrace if $DEBUG
92
+ nil
93
+ end
94
+
95
+ # Saves config
96
+ def config=(obj)
97
+ dump = (@parser.respond_to?(:pretty_generate))? @parser.pretty_generate(obj) : @parser.dump(obj)
98
+ File.open(config_file_name, 'w', 0600){|fh|fh.puts dump}
99
+ end
100
+
101
+ def configures(hash)
102
+ if config? # file exists
103
+ config.each{|opt, value| hash[opt.to_sym] = value}
104
+ else
105
+ $stderr.puts config_file_name if $VERBOSE
106
+ self.config = hash
107
+ end
108
+ end
109
+
110
+ def version?
111
+ File.exist?(version_file_name)
112
+ end
113
+
114
+ # This reads the data directory's version file
115
+ def version
116
+ File.read(version_file_name).strip
117
+ rescue
118
+ $stderr.puts $!.message if $VERBOSE
119
+ $stderr.puts $!.backtrace if $DEBUG
120
+ nil
121
+ end
122
+
123
+ def version=(v)
124
+ File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
125
+ end
126
+ end
metadata CHANGED
@@ -1,55 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: user_space
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 4.0.210114
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: xdg
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.2'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 2.2.3
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '2.2'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 2.2.3
11
+ date: 2021-01-14 00:00:00.000000000 Z
12
+ dependencies: []
33
13
  description: |
34
- Automates certain XDG features.
14
+ Maintains the user's cache, config, and data directories
15
+ with the gem-apps cache, config, and data files.
35
16
  email: carlosjhr64@gmail.com
36
17
  executables: []
37
18
  extensions: []
38
- extra_rdoc_files:
39
- - README.rdoc
19
+ extra_rdoc_files: []
40
20
  files:
41
- - README.rdoc
21
+ - README.md
42
22
  - lib/user_space.rb
43
- - lib/user_space/user_space.rb
44
- - lib/user_space/version.rb
45
23
  homepage: https://github.com/carlosjhr64/user_space
46
24
  licenses:
47
25
  - MIT
48
26
  metadata: {}
49
- post_install_message:
50
- rdoc_options:
51
- - "--main"
52
- - README.rdoc
27
+ post_install_message:
28
+ rdoc_options: []
53
29
  require_paths:
54
30
  - lib
55
31
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -63,11 +39,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
39
  - !ruby/object:Gem::Version
64
40
  version: '0'
65
41
  requirements:
66
- - 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
67
- rubyforge_project:
68
- rubygems_version: 2.4.1
69
- signing_key:
42
+ - 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
43
+ rubygems_version: 3.2.3
44
+ signing_key:
70
45
  specification_version: 4
71
- summary: Automates certain XDG features.
46
+ summary: Maintains the user's cache, config, and data directories with the gem-apps
47
+ cache, config, and data files.
72
48
  test_files: []
73
- has_rdoc:
@@ -1,64 +0,0 @@
1
- = user_space
2
-
3
- github :: https://www.github.com/carlosjhr64/user_space
4
- rubygems :: https://rubygems.org/gems/user_space
5
-
6
- == DESCRIPTION:
7
-
8
- Automates certain XDG features.
9
-
10
- == SYNOPSIS:
11
-
12
- require 'user_space'
13
- # APP::CONFIG is your app's configuration.
14
- # Perhaps like...
15
- APP::CONFIG = {:tts=>'espeak', }
16
- # By default, UserSpace works with the JSON parser.
17
- USERSPACE = USER_SPACE::UserSpace.new
18
- # Unless this version has been installed,
19
- # we copy over our data and cache directories.
20
- USERSPACE.install unless USERSPACE.version == APP::VERSION
21
- if USERSPACE.config?
22
- # Because JSON hashes by String, converting to Symbol.
23
- # We pad up APP::CONFIG with user's preferences:
24
- USERSPACE.config.each{|opt, value| APP::CONFIG[opt.to_sym] = value}
25
- else
26
- # We initialize user preferences with our initial APP::CONFIG
27
- STDERR.puts "Writting '#{USERSPACE.config_file_name}'"
28
- USERSPACE.config = APP::CONFIG
29
- end
30
- # To do the same thing, you can also say:
31
- # USERSPACE.configures(APP::CONFIG)
32
-
33
- == FEATURES:
34
-
35
- * used in mplay[https://www.rubygems.org/gems/mplay]
36
-
37
- == INSTALL:
38
-
39
- sudo gem install user_space
40
-
41
- == LICENSE:
42
-
43
- (The MIT License)
44
-
45
- Copyright (c) 2013 CarlosJHR64
46
-
47
- Permission is hereby granted, free of charge, to any person obtaining
48
- a copy of this software and associated documentation files (the
49
- 'Software'), to deal in the Software without restriction, including
50
- without limitation the rights to use, copy, modify, merge, publish,
51
- distribute, sublicense, and/or sell copies of the Software, and to
52
- permit persons to whom the Software is furnished to do so, subject to
53
- the following conditions:
54
-
55
- The above copyright notice and this permission notice shall be
56
- included in all copies or substantial portions of the Software.
57
-
58
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
59
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
61
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
62
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
63
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
64
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,132 +0,0 @@
1
- module USER_SPACE
2
- class UserSpace
3
-
4
- OPTIONS = {:config => 'config',
5
- :parser => JSON,
6
- :ext => nil,
7
- :version => 'VERSION'}
8
-
9
- PARAMETERS = {:appname => File.basename($PROGRAM_NAME),
10
- :appdir => nil, # Can't guess at this point yet?
11
- :xdgbases => ['CACHE', 'CONFIG', 'DATA']}
12
-
13
- attr_accessor :trace, :verbose
14
- attr_reader :appname, :appdir, :xdgbases, :options
15
- def initialize(parameters=PARAMETERS)
16
-
17
- @appname = parameters[:appname] || PARAMETERS[:appname]
18
- @appdir = parameters[:appdir] || PARAMETERS[:appdir]
19
- @xdgbases = parameters[:xdgbases] || PARAMETERS[:xdgbases]
20
- @options = OPTIONS
21
-
22
- unless @appdir
23
- appdir = File.dirname File.dirname caller_locations(1,1)[0].path
24
- appdir = File.dirname appdir if File.basename(appdir)=='lib'
25
- @appdir = File.expand_path appdir
26
- verbose.puts "Warning: UserSpace#appdir heuristics used" if verbose
27
- end
28
-
29
- # install with no overwrite
30
- install(false)
31
- end
32
-
33
- def xdg_pairs
34
- @xdgbases.each do |base|
35
- xdg = XDG[base].to_s
36
- userdir = File.join(xdg, @appname)
37
- basedir = File.join @appdir, base.downcase
38
- yield basedir, userdir
39
- end
40
- end
41
-
42
- # Note that initialize will not overwrite anything.
43
- # This overwrites the user's data directory with a fresh install.
44
- # App should consider being nice about this,
45
- # like warn the user or something.
46
- def install(overwrite=true)
47
- xdg_pairs do |basedir, userdir|
48
- if File.exist?(userdir)
49
- # Sanity check
50
- raise "Not a directory: #{userdir}" unless File.directory?(userdir)
51
- # Pre-existing directory.
52
- # Return unless user wants to overwrite.
53
- next unless overwrite
54
- else
55
- Dir.mkdir(userdir, 0700)
56
- end
57
- FileUtils.cp_r(Dir.glob("#{basedir}/*"), userdir) if File.directory? basedir
58
- FileUtils.chmod_R 'og-rwx', userdir
59
- FileUtils.chmod_R 'u+rwX', userdir
60
- end
61
- end
62
-
63
- def cachedir
64
- File.join XDG['CACHE'].to_s, @appname
65
- end
66
-
67
- def configdir
68
- File.join XDG['CONFIG'].to_s, @appname
69
- end
70
-
71
- def datadir
72
- File.join XDG['DATA'].to_s, @appname
73
- end
74
-
75
- # Not really for public use.
76
- def config_file_name(options=@options)
77
- parser = options[:parser]
78
- ext = options[:ext] || parser.to_s.downcase
79
- basename = "#{options[:config]}.#{ext}"
80
- File.join XDG['CONFIG'].to_s, @appname, basename
81
- end
82
-
83
- # Not really for public use.
84
- def version_file_name
85
- File.join XDG['DATA'].to_s, @appname, @options[:version]
86
- end
87
-
88
- def config?(options=@options)
89
- File.exist?(config_file_name(options))
90
- end
91
-
92
- # Reads config
93
- def config(options=@options)
94
- options[:parser].parse File.read(config_file_name(options))
95
- rescue
96
- trace.puts $!.message if trace
97
- verbose.puts $!.backtrace if verbose
98
- nil
99
- end
100
-
101
- # Saves config
102
- def config=(obj, options=@options)
103
- File.open(config_file_name, 'w', 0600){|fh| fh.puts options[:parser].pretty_generate obj}
104
- end
105
-
106
- def configures(hash)
107
- if self.config? # file exists
108
- self.config.each{|opt, value| hash[opt.to_sym] = value}
109
- else
110
- trace.puts config_file_name if trace
111
- self.config = hash
112
- end
113
- end
114
-
115
- def version?
116
- File.exist?(version_file_name)
117
- end
118
-
119
- # This reads the data directory's version file
120
- def version
121
- File.read(version_file_name).strip
122
- rescue
123
- trace.puts $!.message if trace
124
- verbose.puts $!.backtrace if verbose
125
- nil
126
- end
127
-
128
- def version=(v)
129
- File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
130
- end
131
- end
132
- end
@@ -1,3 +0,0 @@
1
- module USER_SPACE
2
- VERSION = '1.0.0'
3
- end