user_space 1.0.0 → 2.0.1

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
2
  SHA1:
3
- metadata.gz: 532cc3386ae1ec0bdec441e5d25d5279f82e8547
4
- data.tar.gz: 0a7e250d95306c27bfdc8d11781ec949eed1cb86
3
+ metadata.gz: ad4946e7879c8d5182a601dca296fec845c37785
4
+ data.tar.gz: b7844d0201b7a83e4e9987bdcf94e714a7c7139b
5
5
  SHA512:
6
- metadata.gz: c1a002a63985694c84a738c3028fd39fd33e0caeb19d135dbc7f2fd08c8c77bd75fdef6cc83d0263190d053b7d52dded3406883df5eba35b0127d4897bbc189e
7
- data.tar.gz: 03b8b6d9a28934c68ca6f820a8b1d2026d7eb4bf5b1e1168ae68d763671dd2de63d0192ec5781894b95570c28923529553e7f7e4ec638fd7091783d9230929d4
6
+ metadata.gz: ab588bdaff70c0fa820c9ab243867a0c28fdc84a96cf9bbac240fbf1b3e50ca9d3daead487548c21939950161dce225cddef928882a7213f423605bc70225596
7
+ data.tar.gz: b113e412b3c652622502535402c64092f11ee2e5ecb72819820d18f1cea6c53a103763323f8e567a3340f4da71164c448509835ee82f3321d0fc0c61a95932b7
data/README.rdoc CHANGED
@@ -14,7 +14,7 @@ Automates certain XDG features.
14
14
  # Perhaps like...
15
15
  APP::CONFIG = {:tts=>'espeak', }
16
16
  # By default, UserSpace works with the JSON parser.
17
- USERSPACE = USER_SPACE::UserSpace.new
17
+ USERSPACE = UserSpace.new
18
18
  # Unless this version has been installed,
19
19
  # we copy over our data and cache directories.
20
20
  USERSPACE.install unless USERSPACE.version == APP::VERSION
@@ -1,132 +1,144 @@
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
1
+ class UserSpace
2
+ OPTIONS = {
3
+ config: 'config',
4
+ version: 'VERSION',
5
+ ext: nil,
6
+ parser: nil,
7
+ }
8
+ ['JSON','YAML','Marshal'].each do |parser|
9
+ begin
10
+ OPTIONS[:parser] = Object.const_get parser
11
+ $stderr.puts "USER_SPACE will use #{parser} by default." if $VERBOSE
12
+ break
13
+ rescue NameError
14
+ $stderr.puts "#{parser} was not available for USER_SPACE." if $DEBUG
15
+ end
16
+ end
28
17
 
29
- # install with no overwrite
30
- install(false)
18
+ PARAMETERS = {
19
+ appname: File.basename($0),
20
+ appdir: nil, # Can't guess at this point yet?
21
+ xdgbases: ['CACHE', 'CONFIG', 'DATA'],
22
+ }
23
+
24
+ attr_reader :appname, :appdir, :xdgbases, :options
25
+ def initialize(parameters=PARAMETERS)
26
+
27
+ @appname = parameters[:appname] || PARAMETERS[:appname]
28
+ @appdir = parameters[:appdir] || PARAMETERS[:appdir]
29
+ @xdgbases = parameters[:xdgbases] || PARAMETERS[:xdgbases]
30
+ @options = OPTIONS.dup
31
+
32
+ unless @appdir
33
+ appdir = File.dirname File.dirname caller_locations(1,1)[0].path
34
+ appdir = File.dirname appdir if File.basename(appdir)=='lib'
35
+ @appdir = File.expand_path appdir
36
+ $stderr.puts "UserSpace#appdir=\"#{@appdir}\" # heuristics used" if $VERBOSE
31
37
  end
32
38
 
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
39
+ # install with no overwrite
40
+ install(false)
41
+ end
42
+
43
+ def xdg_pairs
44
+ @xdgbases.each do |base|
45
+ xdg = XDG[base].to_s
46
+ userdir = File.join(xdg, @appname)
47
+ basedir = File.join @appdir, base.downcase
48
+ yield basedir, userdir
40
49
  end
50
+ end
41
51
 
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
52
+ # Note that initialize will not overwrite anything.
53
+ # This overwrites the user's data directory with a fresh install.
54
+ # App should consider being nice about this,
55
+ # like warn the user or something.
56
+ def install(overwrite=true)
57
+ xdg_pairs do |basedir, userdir|
58
+ if File.exist?(userdir)
59
+ # Sanity check
60
+ raise "Not a directory: #{userdir}" unless File.directory?(userdir)
61
+ # Pre-existing directory.
62
+ # Return unless user wants to overwrite.
63
+ next unless overwrite
64
+ else
65
+ Dir.mkdir(userdir, 0700)
60
66
  end
67
+ FileUtils.cp_r(Dir.glob("#{basedir}/*"), userdir) if File.directory? basedir
68
+ FileUtils.chmod_R 'og-rwx', userdir
69
+ FileUtils.chmod_R 'u+rwX', userdir
61
70
  end
71
+ end
62
72
 
63
- def cachedir
64
- File.join XDG['CACHE'].to_s, @appname
65
- end
73
+ def cachedir
74
+ File.join XDG['CACHE'].to_s, @appname
75
+ end
66
76
 
67
- def configdir
68
- File.join XDG['CONFIG'].to_s, @appname
69
- end
77
+ def configdir
78
+ File.join XDG['CONFIG'].to_s, @appname
79
+ end
70
80
 
71
- def datadir
72
- File.join XDG['DATA'].to_s, @appname
73
- end
81
+ def datadir
82
+ File.join XDG['DATA'].to_s, @appname
83
+ end
74
84
 
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
85
+ # Not really for public use.
86
+ def config_file_name
87
+ parser = @options[:parser]
88
+ ext = @options[:ext] || parser.to_s.downcase
89
+ basename = "#{@options[:config]}.#{ext}"
90
+ File.join XDG['CONFIG'].to_s, @appname, basename
91
+ end
82
92
 
83
- # Not really for public use.
84
- def version_file_name
85
- File.join XDG['DATA'].to_s, @appname, @options[:version]
86
- end
93
+ # Not really for public use.
94
+ def version_file_name
95
+ File.join XDG['DATA'].to_s, @appname, @options[:version]
96
+ end
87
97
 
88
- def config?(options=@options)
89
- File.exist?(config_file_name(options))
90
- end
98
+ def config?
99
+ File.exist?(config_file_name)
100
+ end
91
101
 
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
102
+ # Reads config
103
+ def config
104
+ p, d = @options[:parser], File.read(config_file_name)
105
+ p.load(d)
106
+ rescue
107
+ $stderr.puts $!.message if $VERBOSE
108
+ $stderr.puts $!.backtrace if $DEBUG
109
+ nil
110
+ end
100
111
 
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
112
+ # Saves config
113
+ def config=(obj)
114
+ p = @options[:parser]
115
+ d = (p.respond_to?(:pretty_generate))? p.pretty_generate(obj) : p.dump(obj)
116
+ File.open(config_file_name, 'w', 0600){|fh| fh.puts d}
117
+ end
105
118
 
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
119
+ def configures(hash)
120
+ if self.config? # file exists
121
+ self.config.each{|opt, value| hash[opt.to_sym] = value}
122
+ else
123
+ $stderr.puts config_file_name if $VERBOSE
124
+ self.config = hash
113
125
  end
126
+ end
114
127
 
115
- def version?
116
- File.exist?(version_file_name)
117
- end
128
+ def version?
129
+ File.exist?(version_file_name)
130
+ end
118
131
 
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
132
+ # This reads the data directory's version file
133
+ def version
134
+ File.read(version_file_name).strip
135
+ rescue
136
+ $stderr.puts $!.message if $VERBOSE
137
+ $stderr.puts $!.backtrace if $DEBUG
138
+ nil
139
+ end
127
140
 
128
- def version=(v)
129
- File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
130
- end
141
+ def version=(v)
142
+ File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
131
143
  end
132
144
  end
@@ -1,3 +1,3 @@
1
- module USER_SPACE
2
- VERSION = '1.0.0'
1
+ class UserSpace
2
+ VERSION = '2.0.1'
3
3
  end
data/lib/user_space.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'English'
2
1
  require 'fileutils'
3
2
  require 'json' # default parser
4
3
  require 'xdg'
metadata CHANGED
@@ -1,14 +1,14 @@
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: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
11
+ date: 2014-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xdg