user_space 2.1.0 → 5.0.210123

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e034bee576504dcd3ed7c19ab8a9b81ffaff6e0a
4
- data.tar.gz: 338145b2ac30b0c6d65650ec78eb103510198459
2
+ SHA256:
3
+ metadata.gz: 1d7d345bdf4350375456747427362e151414f4e2eac1e62e890ef7692db500f4
4
+ data.tar.gz: f0cf7bf35d981425f444630a22e68348a829863dc5a9eba9728368fac959af99
5
5
  SHA512:
6
- metadata.gz: 6fd690fe1deefc8a39101d0b72670c2b81352176cf350a58096bf48181b68348a7f4030f510a86b5df57e6e1f7294aea92bb37689667259dc01fea0540d64a5a
7
- data.tar.gz: dd9749bfa20b3178ab471e7c9325d0553bd762ca29dd8b153779edf161d55bb3a5d8d76cc5a960eaba683add364dc8bd0171eb4a02a5b8f02f0086de0c7ca7b3
6
+ metadata.gz: c91c634ae02ab53e00108491be15b8cf077caf62bc796bc275f6e1fc548940a55b0a3f87760b61dd02a15561b1098a026dc88c9097c252583c3746957c323c0a
7
+ data.tar.gz: '095f378b67969f6628f56bce47c12fcdd0d9b8596a434c4effd21239853e0fbd962c3e36ada329f4abd24ec62ec0e59ec11159a3e55df0bedd866db8ec2facc2'
@@ -0,0 +1,74 @@
1
+ # UserSpace
2
+
3
+ * [VERSION 5.0.210123](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 'rbon' # Using RBON(is not 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:RBON, 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
+ if USERSPACE.config?
32
+ # Update APP::CONFIG with user's preferences.
33
+ App::CONFIG.merge! USERSPACE.config
34
+ else
35
+ # Write initial configuration with App::CONFIG
36
+ STDERR.puts "Writting '#{USERSPACE.config_file_name}'"
37
+ USERSPACE.config = App::CONFIG
38
+ end
39
+ # To do the same thing, you can also say:
40
+ # USERSPACE.configures(App::CONFIG)
41
+ # We have a config file:
42
+ File.exist? File.expand_path '~/.config/myapp/config.rbon' #=> true
43
+ ```
44
+
45
+ ## INSTALL:
46
+
47
+ ```shell
48
+ $ gem install user_space
49
+ ```
50
+
51
+ ## LICENSE:
52
+
53
+ (The MIT License)
54
+
55
+ Copyright (c) 2021 CarlosJHR64
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,7 +1,114 @@
1
1
  require 'fileutils'
2
- require 'json' # default parser
3
- require 'xdg'
4
- require 'user_space/version.rb'
5
- require 'user_space/user_space.rb'
6
2
  # Requires:
7
3
  #`ruby`
4
+
5
+ class UserSpace
6
+ VERSION = '5.0.210123'
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
+ def self.appdir(lib='/lib')
14
+ (_ = caller(1..2)[-1]&.split(':',2)&.fetch(0)) and File.dirname(File.dirname(File.expand_path(_)))&.chomp(lib)
15
+ end
16
+
17
+ attr_reader :parser,:ext,:appname,:xdgbases,:appdir,:config
18
+ def initialize( parser:,
19
+ appdir: UserSpace.appdir,
20
+ ext: parser.to_s.downcase,
21
+ appname: File.basename($0),
22
+ xdgbases: ['cache', 'config', 'data'],
23
+ config: 'config')
24
+ @parser,@ext,@appname,@xdgbases,@appdir,@config = parser,ext,appname,xdgbases,appdir,config
25
+ install
26
+ end
27
+
28
+ def xdg_pairs
29
+ @xdgbases.each do |base|
30
+ # yield basedir, userdir
31
+ yield File.join(@appdir, base), File.join(XDG[base], @appname)
32
+ end
33
+ end
34
+
35
+ # Will not overwrite anything.
36
+ # Only copies over missing directories and files.
37
+ # Verifies directory expectations.
38
+ def install
39
+ xdg_pairs do |basedir, userdir|
40
+ if File.exist?(userdir)
41
+ # Sanity check
42
+ assert_directory(userdir)
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.exist? dest
50
+ # Sanity checks
51
+ assert_directory(dest) if File.directory? src
52
+ else
53
+ if File.directory? src
54
+ Dir.mkdir dest
55
+ else
56
+ FileUtils.cp src, dest
57
+ end
58
+ FileUtils.chmod('u+rwX,go-rwx', dest)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def cachedir
66
+ File.join XDG['cache'], @appname
67
+ end
68
+
69
+ def configdir
70
+ File.join XDG['config'], @appname
71
+ end
72
+
73
+ def datadir
74
+ File.join XDG['data'], @appname
75
+ end
76
+
77
+ def config_file_name
78
+ File.join XDG['config'], @appname, "#{@config}.#{@ext}"
79
+ end
80
+
81
+ def config?
82
+ File.exist?(config_file_name)
83
+ end
84
+
85
+ # Reads config
86
+ def config
87
+ @parser.load File.read(config_file_name)
88
+ rescue
89
+ $stderr.puts $!.message if $VERBOSE
90
+ $stderr.puts $!.backtrace if $DEBUG
91
+ nil
92
+ end
93
+
94
+ # Saves config
95
+ def config=(obj)
96
+ dump = (@parser.respond_to?(:pretty_generate))? @parser.pretty_generate(obj) : @parser.dump(obj)
97
+ File.open(config_file_name, 'w', 0600){|fh|fh.puts dump}
98
+ end
99
+
100
+ def configures(hash)
101
+ if config? # file exists
102
+ hash.merge! config
103
+ else
104
+ $stderr.puts config_file_name if $VERBOSE
105
+ self.config = hash
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def assert_directory(dir)
112
+ raise "Not a directory: #{dir}" unless File.directory?(dir)
113
+ end
114
+ 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: 2.1.0
4
+ version: 5.0.210123
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-19 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-23 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,10 +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.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]'
67
- rubyforge_project:
68
- rubygems_version: 2.4.5.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: []
@@ -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 = 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,153 +0,0 @@
1
- module FileUtils
2
- class << self
3
- def user_space_cpr(src, dest)
4
- fu_each_src_dest(src, dest) do |s, d|
5
- copy_entry(s, d)
6
- chmod('u+rwX,go-rwx', d)
7
- end
8
- end
9
- end
10
- end
11
-
12
- class UserSpace
13
- OPTIONS = {
14
- config: 'config',
15
- version: 'VERSION',
16
- ext: nil,
17
- parser: nil,
18
- }
19
- ['JSON','YAML','Marshal'].each do |parser|
20
- begin
21
- OPTIONS[:parser] = Object.const_get parser
22
- $stderr.puts "USER_SPACE will use #{parser} by default." if $VERBOSE
23
- break
24
- rescue NameError
25
- $stderr.puts "#{parser} was not available for USER_SPACE." if $DEBUG
26
- end
27
- end
28
-
29
- PARAMETERS = {
30
- appname: File.basename($0),
31
- appdir: nil, # Can't guess at this point yet?
32
- xdgbases: ['CACHE', 'CONFIG', 'DATA'],
33
- }
34
-
35
- attr_reader :appname, :appdir, :xdgbases, :options
36
- def initialize(parameters=PARAMETERS)
37
-
38
- @appname = parameters[:appname] || PARAMETERS[:appname]
39
- @appdir = parameters[:appdir] || PARAMETERS[:appdir]
40
- @xdgbases = parameters[:xdgbases] || PARAMETERS[:xdgbases]
41
- @options = OPTIONS.dup
42
-
43
- unless @appdir
44
- appdir = File.dirname File.dirname caller_locations(1,1)[0].path
45
- appdir = File.dirname appdir if File.basename(appdir)=='lib'
46
- @appdir = File.expand_path appdir
47
- $stderr.puts "UserSpace#appdir=\"#{@appdir}\" # heuristics used" if $VERBOSE
48
- end
49
-
50
- # install with no overwrite
51
- install(false)
52
- end
53
-
54
- def xdg_pairs
55
- @xdgbases.each do |base|
56
- xdg = XDG[base].to_s
57
- userdir = File.join(xdg, @appname)
58
- basedir = File.join @appdir, base.downcase
59
- yield basedir, userdir
60
- end
61
- end
62
-
63
- # Note that initialize will not overwrite anything.
64
- # This overwrites the user's data directory with a fresh install.
65
- # App should consider being nice about this,
66
- # like warn the user or something.
67
- def install(overwrite=true)
68
- xdg_pairs do |basedir, userdir|
69
- if File.exist?(userdir)
70
- # Sanity check
71
- raise "Not a directory: #{userdir}" unless File.directory?(userdir)
72
- # Pre-existing directory.
73
- # Return unless user wants to overwrite.
74
- next unless overwrite
75
- else
76
- Dir.mkdir(userdir, 0700)
77
- end
78
- FileUtils.user_space_cpr(Dir.glob("#{basedir}/*"), userdir) if File.directory? basedir
79
- end
80
- end
81
-
82
- def cachedir
83
- File.join XDG['CACHE'].to_s, @appname
84
- end
85
-
86
- def configdir
87
- File.join XDG['CONFIG'].to_s, @appname
88
- end
89
-
90
- def datadir
91
- File.join XDG['DATA'].to_s, @appname
92
- end
93
-
94
- # Not really for public use.
95
- def config_file_name
96
- parser = @options[:parser]
97
- ext = @options[:ext] || parser.to_s.downcase
98
- basename = "#{@options[:config]}.#{ext}"
99
- File.join XDG['CONFIG'].to_s, @appname, basename
100
- end
101
-
102
- # Not really for public use.
103
- def version_file_name
104
- File.join XDG['DATA'].to_s, @appname, @options[:version]
105
- end
106
-
107
- def config?
108
- File.exist?(config_file_name)
109
- end
110
-
111
- # Reads config
112
- def config
113
- p, d = @options[:parser], File.read(config_file_name)
114
- p.load(d)
115
- rescue
116
- $stderr.puts $!.message if $VERBOSE
117
- $stderr.puts $!.backtrace if $DEBUG
118
- nil
119
- end
120
-
121
- # Saves config
122
- def config=(obj)
123
- p = @options[:parser]
124
- d = (p.respond_to?(:pretty_generate))? p.pretty_generate(obj) : p.dump(obj)
125
- File.open(config_file_name, 'w', 0600){|fh| fh.puts d}
126
- end
127
-
128
- def configures(hash)
129
- if self.config? # file exists
130
- self.config.each{|opt, value| hash[opt.to_sym] = value}
131
- else
132
- $stderr.puts config_file_name if $VERBOSE
133
- self.config = hash
134
- end
135
- end
136
-
137
- def version?
138
- File.exist?(version_file_name)
139
- end
140
-
141
- # This reads the data directory's version file
142
- def version
143
- File.read(version_file_name).strip
144
- rescue
145
- $stderr.puts $!.message if $VERBOSE
146
- $stderr.puts $!.backtrace if $DEBUG
147
- nil
148
- end
149
-
150
- def version=(v)
151
- File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
152
- end
153
- end
@@ -1,3 +0,0 @@
1
- class UserSpace
2
- VERSION = '2.1.0'
3
- end