relaton-cli 1.7.2 → 1.7.3

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
  SHA256:
3
- metadata.gz: 9983d2d6b80b1d1ac3a1ad7da07f4de43d8509c44c61c050ab2046c11580c8bc
4
- data.tar.gz: '09593a750543ab10dae21731732f7858c5467bd30494613e5b59c00f0967bbb9'
3
+ metadata.gz: d320bdb2aed574b0d56fb8bb94e4b7128d5261c8e88ac9fbeb25739392c96ca8
4
+ data.tar.gz: 88adb33264d4bfee902cff4f12a0cfc3e50ce99b61bc5c7841e6b09400f85003
5
5
  SHA512:
6
- metadata.gz: 1419b4f0fdc45ca4dc5f9674eff0a9784ae89d6159a8bf23e4168d3f61776f28ddfd8caf76dc0540bf310e7b730095d94db03e7a5be95340cf668896d5446ed0
7
- data.tar.gz: 5e4c6b9d094841f358566238406a6a08b1ad1080e33b48b190807c126c41fa17498f8f1b5707d66ac6f97aea06a04d73b567663fd6328e65db8dfc04b96d2736
6
+ metadata.gz: d7c396fe7319104f8433ebaf1778b291e2526fd95f8f8a6d7f8b12d375d767d4ce4cc494df8c8d2f7bf442b27154c1a9ef2059205e0468510a6c4ec2ab3dbbce
7
+ data.tar.gz: 81399b3e5b6685c76602282b4b95ece2865fa666c840bad188b150f530984d5da4113dd4f6414f9648e2ae7997fe44ae05acbd09475f73e418567fe5bdd279d6
data/docs/README.adoc CHANGED
@@ -10,6 +10,7 @@ Please refer to https://github.com/relaton/relaton.
10
10
 
11
11
  == Commands
12
12
 
13
+ Each command has an option `--verbose` (short form is `-v`). Use the option to get warnings in the commands outpput.
13
14
  The following commands are provided.
14
15
 
15
16
  === relaton concatenate
@@ -41,14 +42,17 @@ suports an additional `-x` or `--extension` options to use different extension.
41
42
 
42
43
  [source,console]
43
44
  ----
44
- $ relaton fetch CODE -t TYPE -f FORMAT -y YEAR
45
+ $ relaton fetch CODE -t TYPE -f FORMAT -y YEAR -r RETRIES --all-parts --keep-year
45
46
  ----
46
47
 
47
48
  Fetch the Relaton XML entry corresponding to the document identifier `CODE`.
48
49
 
49
50
  * `YEAR` is optional, and specifies the year of publication of the standard.
50
- * `FORMAT` is optional, and specifies the output format; the recognised values for `FORMAT` are `xml` (default), `bibtex`.
51
- * `TYPE` specifies the standards class library to be used, that the identifier is part of. The recognised values for `TYPE` are `BIPM`, `CC`, `CN`, `IEC`, `IEEE`, `IETF`, `IHO`, `ISO`, `ITU`, `NIST`, `OGC`, `OMG`, `UN`, `W3C`.
51
+ * `FORMAT` is optional, and specifies the output format; the recognised values for `FORMAT` are `xml` (default), yaml, `bibtex`.
52
+ * `TYPE` is optional, specifies the standards class library to be used, that the identifier is part of. The recognised values for `TYPE` are `BIPM`, `CC`, `CIE`, `CN`, `ECMA`, `IEC`, `IEEE`, `IETF`, `IHO`, `ISO`, `ITU`, `NIST`, `OGC`, `OMG`, `UN`, `W3C`.
53
+ * `RETRIES` is optional, number of network retries (default 1).
54
+ * `--all-parts` fetch all parts.
55
+ * `--keep-year` undated reference should return actual reference with year.
52
56
 
53
57
  === relaton extract
54
58
 
@@ -305,3 +309,107 @@ Import document or collection from XML `FILE` into `COLLECTION`.
305
309
 
306
310
  * `COLLECTION` is optional. If collection doesn't exist then it will be created.
307
311
  * `DIRECTORY` is optional, and specifies path to a directory with collections. Default is `$HOME/.relaton/collections`.
312
+
313
+ === Cache DB manipulation
314
+
315
+ ==== Create cache DB
316
+
317
+ ----
318
+ $ relaton db create DIR
319
+ ----
320
+
321
+ Creates a new cache DB in a directory `DIR` (optional, deafult is `/home/USER/.relaton/dbpath`). In case the target directory exists it will be used as a cache DB.
322
+
323
+ ----
324
+ $ relaton db create
325
+ Cache DB is in "/Users/user/.relaton/cache"
326
+
327
+ $ relaton db create cachedb
328
+ Cache DB is in "/Users/user/RubyProjects/relaton-cli/cachedb"
329
+ ----
330
+
331
+ ==== Move cache DB
332
+
333
+ ----
334
+ $ relaton db mv DIR
335
+ ----
336
+
337
+ Move cache DB to another place `DIR`.
338
+
339
+ ----
340
+ $ relaton db mv cache_dir
341
+ Cache DB is moved to "/Users/user/RubyProjects/relaton-cli/cache_dir"
342
+ ----
343
+
344
+ ==== Clear cache DB
345
+
346
+ Delete all entries from a chache DB.
347
+
348
+ ----
349
+ $ relaton db clear
350
+ ----
351
+
352
+ ==== Fetch from cache DB
353
+
354
+ ----
355
+ $ relaton db fetch -t TYPE -f FORMAT -y YEAR
356
+ ----
357
+
358
+ Fetch an entry from a cache DB. See [relaton fetch](#relaton-fetch) for the arguments explanation.
359
+
360
+ ==== Fetch all
361
+
362
+ Fetch all entries from a chache DB.
363
+
364
+ ----
365
+ $ relaton db fetch_all TEXT -e EDITION -y YEAR -f FORMAT
366
+ ----
367
+
368
+ * `TEXT` (optional) search for a certan string
369
+ * `EDITION` (optional) filter documets with a certain edition
370
+ * `YEAR` (optional) filter documents by a year
371
+ * `FORMAT` (optional) specify the output format. Recognised values are `xml` (default), yaml, `bibtex`.
372
+
373
+ ----
374
+ $ relaton db fetch_all
375
+ <bibitem id="ISO/IECDIR1" type="international-standard">
376
+ ...
377
+
378
+ $ relaton db fetch_all 'Procedures for the technical work'
379
+ <bibitem id="ISO/IECDIR1" type="international-standard">
380
+ <fetched>2021-04-01</fetched>
381
+ <title type="title-main" format="text/plain" language="en" script="Latn">Procedures for the technical work</title>
382
+ ...
383
+
384
+ $ relaton db fetch_all -e 3
385
+ <bibitem id="ISO2146-2010" type="standard">
386
+ ...
387
+ <edition>3</edition>
388
+ ...
389
+
390
+ $ relaton db fetch_all -e 8 -y 2018
391
+ <bibitem id="ISO/IECDIR2IEC" type="international-standard">
392
+ <fetched>2021-04-01</fetched>
393
+ <title type="title-main" format="text/plain" language="en" script="Latn">Principles and rules for the structure and drafting of ISO and IEC documents</title>
394
+ <uri type="obp">https://www.iec.ch/members_experts/refdocs/iec/isoiecdir2%7Bed8.0.RLV%7Den.pdf</uri>
395
+ <docidentifier type="ISO">ISO/IEC DIR 2 IEC</docidentifier>
396
+ <date type="published">
397
+ <on>2018-05-01</on>
398
+ </date>
399
+ <edition>8</edition>
400
+ ...
401
+ ----
402
+
403
+ ==== Get document type
404
+
405
+ ----
406
+ $ relaton db doctype REF
407
+ ----
408
+
409
+ Takes a reference `REF` and retuern a document type.
410
+
411
+ ----
412
+ $ relaton db doctype 'CN(GB/T 1.1)'
413
+ Chinese Standard
414
+ GB/T 1.1
415
+ ----
data/lib/relaton/cli.rb CHANGED
@@ -1,18 +1,38 @@
1
1
  require "thor"
2
+ require "thor/hollaback"
2
3
  require "relaton"
3
4
  require_relative "cli/command"
4
5
 
5
6
  module Relaton
6
- def self.db
7
- Cli.relaton
7
+ def self.db(dir = nil)
8
+ Cli.relaton dir
8
9
  end
9
10
 
10
11
  module Cli
11
12
  class RelatonDb
12
13
  include Singleton
13
14
 
14
- def db
15
- @db ||= Relaton::Db.new("#{Dir.home}/.relaton/cache", nil)
15
+ DBCONF = "#{Dir.home}/.relaton/dbpath".freeze
16
+
17
+ # @param dir [String, nil]
18
+ # @return [Relaton::Db]
19
+ def db(dir)
20
+ if dir
21
+ File.write DBCONF, dir, encoding: "UTF-8"
22
+ @db = Relaton::Db.new dir, nil
23
+ else
24
+ @db ||= Relaton::Db.new dbpath, nil
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ # @return [String] path to DB
31
+ def dbpath
32
+ if File.exist?(DBCONF)
33
+ File.read(DBCONF, encoding: "UTF-8")
34
+ else "#{Dir.home}/.relaton/cache"
35
+ end
16
36
  end
17
37
  end
18
38
 
@@ -28,8 +48,10 @@ module Relaton
28
48
  # easier we have added it as a class method so we can use this
29
49
  # whenever necessary.
30
50
  #
31
- def relaton
32
- RelatonDb.instance.db
51
+ # @param dir [String, nil]
52
+ # @return [Relaton::Db]
53
+ def relaton(dir)
54
+ RelatonDb.instance.db dir
33
55
  end
34
56
 
35
57
  # @param content [Nokogiri::XML::Document]
@@ -2,21 +2,30 @@ require "relaton/cli/relaton_file"
2
2
  require "relaton/cli/xml_convertor"
3
3
  require "relaton/cli/yaml_convertor"
4
4
  require "relaton/cli/subcommand_collection"
5
+ require "relaton/cli/subcommand_db"
5
6
  require "fcntl"
6
7
 
7
8
  module Relaton
8
9
  module Cli
9
10
  class Command < Thor
11
+ include Relaton::Cli
12
+ class_before :relaton_config
13
+ class_option :verbose, aliases: :v, type: :boolean, desc: "Output warnings"
14
+
10
15
  desc "fetch CODE", "Fetch Relaton XML for Standard identifier CODE"
11
- option :type, aliases: :t, required: true, desc: "Type of standard to "\
16
+ option :type, aliases: :t, desc: "Type of standard to "\
12
17
  "get bibliographic entry for"
13
- option :format, aliases: :f, desc: "Output format (xml, bibtex). "\
18
+ option :format, aliases: :f, desc: "Output format (xml, yaml, bibtex). "\
14
19
  "Default xml."
15
20
  option :year, aliases: :y, type: :numeric, desc: "Year the standard was "\
16
21
  "published"
22
+ option :"all-parts", type: :boolean, desc: "Fetch all parts"
23
+ option :"keep-year", type: :boolean, desc: "Undated reference should "\
24
+ "return actual reference with year"
25
+ option :retries, aliases: :r, type: :numeric, desc: "Number of network "\
26
+ "retries. Default 1."
17
27
 
18
28
  def fetch(code)
19
- Relaton.db
20
29
  io = IO.new(STDOUT.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
21
30
  io.puts(fetch_document(code, options) || supported_type_message)
22
31
  end
@@ -35,8 +44,6 @@ module Relaton
35
44
  option :title, aliases: :t, desc: "Title of resulting Relaton collection"
36
45
  option :organization, aliases: :g, desc: "Organization owner of Relaton "\
37
46
  "collection"
38
- option :new, aliases: :n, type: :boolean, desc: "Use the new Relaton "\
39
- "YAML format"
40
47
  option :extension, aliases: :x, desc: "File extension of destination "\
41
48
  "Relaton file, defaults to 'rxl'"
42
49
 
@@ -48,8 +55,6 @@ module Relaton
48
55
  "Relaton Collection into multiple files"
49
56
  option :extension, aliases: :x, default: "rxl", desc: "File extension "\
50
57
  "of Relaton XML files, defaults to 'rxl'"
51
- option :new, aliases: :n, type: :boolean, desc: "Use the new Relaton "\
52
- "YAML format"
53
58
 
54
59
  def split(source, outdir)
55
60
  Relaton::Cli::RelatonFile.split(source, outdir, options)
@@ -139,34 +144,60 @@ module Relaton
139
144
  desc "collection SUBCOMMAND", "Collection manipulations"
140
145
  subcommand "collection", SubcommandCollection
141
146
 
142
- private
143
-
144
- # @param code [String]
145
- # @param options [Hash]
146
- # @option options [String] :type
147
- # @option options [String, NilClass] :format
148
- # @option options [Integer, NilClass] :year
149
- def fetch_document(code, options)
150
- if registered_types.include?(options[:type])
151
- doc = Cli.relaton.fetch(code, options[:year]&.to_s)
152
- if doc
153
- options[:format] == "bibtex" ? doc.to_bibtex : doc.to_xml
154
- else
155
- "No matching bibliographic entry found"
147
+ desc "db SUBCOMMAND", "Cache DB manipulation"
148
+ subcommand "db", SubcommandDb
149
+
150
+ no_commands do
151
+ def relaton_config
152
+ log_types = %i[info error]
153
+ log_types << :warning if options[:verbose]
154
+ Relaton.configure do |conf|
155
+ conf.logs = log_types
156
156
  end
157
157
  end
158
- rescue RelatonBib::RequestError => e
159
- e.message
160
158
  end
159
+ end
161
160
 
162
- def supported_type_message
163
- ["Recognised types:", registered_types.sort.join(", ")].join(" ")
161
+ private
162
+
163
+ # @param code [String]
164
+ # @param options [Hash]
165
+ # @option options [String] :type
166
+ # @option options [String, NilClass] :format
167
+ # @option options [Integer, NilClass] :year
168
+ # @return [String, nil]
169
+ def fetch_document(code, options) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/AbcSize
170
+ year = options[:year]&.to_s
171
+ if (processor = Relaton::Registry.instance.by_type options[:type]&.upcase)
172
+ doc = Relaton.db.fetch_std code, year, processor.short, options.dup
173
+ elsif options[:type] then return
174
+ else doc = Relaton.db.fetch(code, year, options.dup)
164
175
  end
176
+ return "No matching bibliographic entry found" unless doc
177
+
178
+ serialize doc, options[:format]
179
+ rescue RelatonBib::RequestError => e
180
+ e.message
181
+ end
165
182
 
166
- def registered_types
167
- @registered_types ||=
168
- Relaton::Registry.instance.processors.each.map { |_n, pr| pr.prefix }
183
+ # @param doc [RelatonBib::BibliographicItem]
184
+ # @param format [String]
185
+ # @return [String]
186
+ def serialize(doc, format)
187
+ case format
188
+ when "yaml", "yml" then doc.to_hash.to_yaml
189
+ when "bibtex" then doc.to_bibtex
190
+ else doc.to_xml
169
191
  end
170
192
  end
193
+
194
+ def supported_type_message
195
+ ["Recognised types:", registered_types.sort.join(", ")].join(" ")
196
+ end
197
+
198
+ def registered_types
199
+ @registered_types ||=
200
+ Relaton::Registry.instance.processors.each.map { |_n, pr| pr.prefix }
201
+ end
171
202
  end
172
203
  end
@@ -3,6 +3,9 @@ require "relaton/cli/full_text_search"
3
3
  module Relaton
4
4
  module Cli
5
5
  class SubcommandCollection < Thor
6
+ include Relaton::Cli
7
+ class_option :verbose, aliases: :v, type: :boolean, desc: "Output warnings"
8
+
6
9
  desc "create COLLECTION", "Create collection"
7
10
  option :dir, aliases: :d, desc: "Directory to store collection. Default "\
8
11
  "is $HOME/.relaton/collections."
@@ -104,21 +107,21 @@ module Relaton
104
107
  option :dir, aliases: :d, desc: "Directory with collections. Default is "\
105
108
  "$HOME/.relaton/collections."
106
109
 
107
- def fetch(code)
108
- doc = Cli.relaton.fetch(code, options[:year]&.to_s)
110
+ def fetch(code) # rubocop:disable Metrics/AbcSize
111
+ doc = Relaton.db.fetch(code, options[:year]&.to_s)
109
112
  if doc
110
113
  colfile = File.join directory, options[:collection]
111
114
  coll = read_collection colfile
112
115
  coll << doc
113
116
  File.write colfile, coll.to_yaml, encoding: "UTF-8"
114
- else "No matching bibliographic entry found"
117
+ else warn "No matching bibliographic entry found"
115
118
  end
116
119
  end
117
120
 
118
121
  desc "import FILE", "Import document or collection from an XML file "\
119
122
  "into another collection"
120
- option :collection, aliases: :c, required: true, desc: "Collection "\
121
- "to store a document. If collection doesn't exist then it'll be created."
123
+ option :collection, aliases: :c, required: true, desc: "Collection to "\
124
+ "store a document. If collection doesn't exist then it'll be created."
122
125
  option :dir, aliases: :d, desc: "Directory with collections. Default is "\
123
126
  "$HOME/.relaton/collections."
124
127
 
@@ -0,0 +1,74 @@
1
+ module Relaton
2
+ module Cli
3
+ class SubcommandDb < Thor
4
+ include Relaton::Cli
5
+ class_option :verbose, aliases: :v, type: :boolean, desc: "Output warnings"
6
+
7
+ desc "create DIR", "Create new cache DB. Default DIR is "\
8
+ "/home/user/.relaon/cache/"
9
+
10
+ def create(dir = nil)
11
+ db = Relaton.db (dir && File.expand_path(dir))
12
+ path = db.instance_variable_get(:@db).dir
13
+ warn "Cache DB is in \"#{path}\""
14
+ end
15
+
16
+ desc "mv DIR", "Move cache DB to a new directory"
17
+
18
+ def mv(dir)
19
+ new_path = File.expand_path dir
20
+ path = Relaton.db.mv new_path
21
+ if path
22
+ File.write Cli::RelatonDb::DBCONF, path, encoding: "UTF-8"
23
+ warn "Cache DB is moved to \"#{path}\""
24
+ end
25
+ end
26
+
27
+ desc "clear", "Clear cache DB"
28
+
29
+ def clear
30
+ Relaton.db.clear
31
+ warn "Cache DB is cleared"
32
+ end
33
+
34
+ desc "fetch CODE", "Fetch Relaton XML for Standard identifier CODE from "\
35
+ "cache DB"
36
+ option :type, aliases: :t, desc: "Type of standard to "\
37
+ "get bibliographic entry for"
38
+ option :format, aliases: :f, desc: "Output format (xml, yaml, bibtex). "\
39
+ "Default xml."
40
+ option :year, aliases: :y, type: :numeric, desc: "Year the standard was "\
41
+ "published"
42
+
43
+ def fetch(code)
44
+ io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
45
+ opts = options.merge(fetch_db: true)
46
+ io.puts(fetch_document(code, opts) || supported_type_message)
47
+ end
48
+
49
+ desc "fetch_all TEXT", "Query for all documents in a cache DB for a "\
50
+ "certain string"
51
+ option :edition, aliases: :e, desc: "Filter entries by edition"
52
+ option :year, aliases: :y, desc: "Filter entries by year"
53
+ option :format, aliases: :f, desc: "Output format (xml, yaml, bibtex). "\
54
+ "Default xml."
55
+
56
+ def fetch_all(text = nil) # rubocop:disable Metrics/AbcSize
57
+ io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
58
+ opts = options.each_with_object({}) do |(k, v), o|
59
+ o[k.to_sym] = v unless k == "format"
60
+ end
61
+ Relaton.db.fetch_all(text, **opts).each do |doc|
62
+ io.puts serialize(doc, options[:format])
63
+ end
64
+ end
65
+
66
+ desc "doctype REF", "Detect document type from REF"
67
+
68
+ def doctype(ref)
69
+ io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
70
+ io.puts Relaton.db.docid_type(ref)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Cli
3
- VERSION = "1.7.2".freeze
3
+ VERSION = "1.7.3".freeze
4
4
  end
5
5
  end
data/relaton-cli.gemspec CHANGED
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "webmock"
37
37
 
38
38
  spec.add_runtime_dependency "liquid", "~> 4"
39
- spec.add_runtime_dependency "relaton", "~> 1.7.0"
39
+ spec.add_runtime_dependency "relaton", ">=1.7.7"
40
40
  spec.add_runtime_dependency "thor"
41
+ spec.add_runtime_dependency "thor-hollaback"
41
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-20 00:00:00.000000000 Z
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -168,16 +168,16 @@ dependencies:
168
168
  name: relaton
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - "~>"
171
+ - - ">="
172
172
  - !ruby/object:Gem::Version
173
- version: 1.7.0
173
+ version: 1.7.7
174
174
  type: :runtime
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - "~>"
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
- version: 1.7.0
180
+ version: 1.7.7
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: thor
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +192,20 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: thor-hollaback
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
195
209
  description: Relaton Command-line Interface
196
210
  email:
197
211
  - open.source@ribose.com
@@ -226,6 +240,7 @@ files:
226
240
  - lib/relaton/cli/full_text_search.rb
227
241
  - lib/relaton/cli/relaton_file.rb
228
242
  - lib/relaton/cli/subcommand_collection.rb
243
+ - lib/relaton/cli/subcommand_db.rb
229
244
  - lib/relaton/cli/version.rb
230
245
  - lib/relaton/cli/xml_convertor.rb
231
246
  - lib/relaton/cli/xml_to_html_renderer.rb