[logaling-commit] logaling/logaling-command [change-glossarysourcepath-fullpath-to-relativepath] Change full-path to relative-path

Back to archive index

SUZUKI Miho null+****@clear*****
Wed Nov 21 03:44:37 JST 2012


SUZUKI Miho	2012-11-21 03:44:37 +0900 (Wed, 21 Nov 2012)

  New Revision: 370676510e82ae5a0743819d48eb398522450440
  https://github.com/logaling/logaling-command/commit/370676510e82ae5a0743819d48eb398522450440

  Log:
    Change full-path to relative-path

  Modified files:
    lib/logaling/command/application.rb
    lib/logaling/glossary.rb
    lib/logaling/glossary_db.rb
    lib/logaling/glossary_sources/base.rb
    lib/logaling/glossary_sources/glossary_csv_source.rb
    lib/logaling/glossary_sources/glossary_tsv_source.rb
    lib/logaling/glossary_sources/glossary_yaml_source.rb
    lib/logaling/project.rb
    lib/logaling/repository.rb
    spec/logaling/command_spec.rb
    spec/logaling/glossary_db_spec.rb
    spec/logaling/glossary_spec.rb
    spec/logaling/repository_spec.rb

  Modified: lib/logaling/command/application.rb (+1 -1)
===================================================================
--- lib/logaling/command/application.rb    2012-11-14 11:21:34 +0900 (b06a0a4)
+++ lib/logaling/command/application.rb    2012-11-21 03:44:37 +0900 (e8a28b8)
@@ -90,7 +90,7 @@ module Logaling::Command
       else
         raise Logaling::CommandFailed, "[TARGET-LANGUAGE] is required when you use '--personal'" unless target_language
         personal_project =****@repos*****_personal_project(project_name, source_language, target_language)
-        say "Successfully created #{personal_project.path}"
+        say "Successfully created #{@repository.make_full_path(personal_project.path)}"
       end
     rescue Logaling::CommandFailed => e
       say e.message

  Modified: lib/logaling/glossary.rb (+7 -3)
===================================================================
--- lib/logaling/glossary.rb    2012-11-14 11:21:34 +0900 (1bba103)
+++ lib/logaling/glossary.rb    2012-11-21 03:44:37 +0900 (bf9115d)
@@ -84,9 +84,10 @@ module Logaling
         @glossary_source
       else
         file_name = [@name, @source_language, @target_language, 'yml'].join('.')
-        source_dir =****@proje*****_source_path
+        source_dir =****@proje*****_project? ? @project.glossary_source_path : File.dirname(@project.glossary_source_path)
         FileUtils.mkdir_p(source_dir)
-        source_path = File.join(source_dir, file_name)
+        source_path_full = File.join(source_dir, file_name)
+        source_path =****@proje*****_relative_path(source_path_full)
         @glossary_source = Logaling::GlossarySource.create(source_path, self)
       end
     end
@@ -136,7 +137,10 @@ module Logaling
         file_name = [self.to_s, type].join('.')
         File.join(@project.glossary_source_path, file_name)
       end
-      Dir.glob(glob_condition).map {|source_path| GlossarySource.create(source_path, self)}
+      Dir.glob(glob_condition).map do |source_path_full|
+        source_path =****@proje*****_relative_path(source_path_full)
+        GlossarySource.create(source_path, self)
+      end
     end
   end
 end

  Modified: lib/logaling/glossary_db.rb (+1 -1)
===================================================================
--- lib/logaling/glossary_db.rb    2012-11-14 11:21:34 +0900 (f03e6e1)
+++ lib/logaling/glossary_db.rb    2012-11-21 03:44:37 +0900 (37da575)
@@ -20,7 +20,7 @@ require 'cgi'
 
 module Logaling
   class GlossaryDB
-    VERSION = 2
+    VERSION = 3
 
     def self.open(base_path, encoding, &blk)
       blk ? GlossaryDB.new.open(base_path, encoding, &blk) : GlossaryDB.new.open(base_path, encoding)

  Modified: lib/logaling/glossary_sources/base.rb (+8 -4)
===================================================================
--- lib/logaling/glossary_sources/base.rb    2012-11-14 11:21:34 +0900 (9dfced5)
+++ lib/logaling/glossary_sources/base.rb    2012-11-21 03:44:37 +0900 (38801b2)
@@ -20,14 +20,14 @@ module Logaling
     class Base
       attr_reader :source_path, :glossary
 
-      def initialize(source_path, glossary)
-        @source_path = source_path
+      def initialize(relative_source_path, glossary)
+        @source_path = relative_source_path
         @glossary = glossary
       end
 
       def eql?(other)
         return false unless self.class == other.class
-        @source_path == other.source_path
+        source_path == other.source_path
       end
 
       def hash
@@ -35,7 +35,11 @@ module Logaling
       end
 
       def mtime
-        File.mtime(@source_path)
+        File.mtime(source_path_full)
+      end
+
+      def source_path_full
+        @glossary.project.repository.make_full_path(@source_path)
       end
     end
   end

  Modified: lib/logaling/glossary_sources/glossary_csv_source.rb (+1 -1)
===================================================================
--- lib/logaling/glossary_sources/glossary_csv_source.rb    2012-11-14 11:21:34 +0900 (a111bc2)
+++ lib/logaling/glossary_sources/glossary_csv_source.rb    2012-11-21 03:44:37 +0900 (778a632)
@@ -22,7 +22,7 @@ module Logaling::GlossarySources
   class GlossaryCsvSource < Base
     def load
       glossary_source = []
-      CSV.open(source_path, "r:utf-8",  {:col_sep => ','}) do |csv|
+      CSV.open(source_path_full, "r:utf-8",  {:col_sep => ','}) do |csv|
         csv.each do |row|
           glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2
         end

  Modified: lib/logaling/glossary_sources/glossary_tsv_source.rb (+1 -1)
===================================================================
--- lib/logaling/glossary_sources/glossary_tsv_source.rb    2012-11-14 11:21:34 +0900 (44f6a48)
+++ lib/logaling/glossary_sources/glossary_tsv_source.rb    2012-11-21 03:44:37 +0900 (b73dd66)
@@ -22,7 +22,7 @@ module Logaling::GlossarySources
   class GlossaryTsvSource < Base
     def load
       glossary_source = []
-      CSV.open(source_path, "r:utf-8",  {:col_sep => "\t"}) do |tsv|
+      CSV.open(source_path_full, "r:utf-8",  {:col_sep => "\t"}) do |tsv|
         tsv.each do |row|
           glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2
         end

  Modified: lib/logaling/glossary_sources/glossary_yaml_source.rb (+6 -6)
===================================================================
--- lib/logaling/glossary_sources/glossary_yaml_source.rb    2012-11-14 11:21:34 +0900 (e4c5d5f)
+++ lib/logaling/glossary_sources/glossary_yaml_source.rb    2012-11-21 03:44:37 +0900 (0ceb71b)
@@ -28,13 +28,13 @@ require "fileutils"
 module Logaling::GlossarySources
   class GlossaryYamlSource < Base
     def load
-      YAML::load_file(source_path) || []
+      YAML::load_file(source_path_full) || []
     rescue TypeError
       []
     end
 
     def add(source_term, target_term, note)
-      initialize_source unless File.exist?(source_path)
+      initialize_source unless File.exist?(source_path_full)
 
       glossary_source = self.load
       glossary_source << build_term(source_term, target_term, note)
@@ -44,7 +44,7 @@ module Logaling::GlossarySources
     end
 
     def update(source_term, target_term, new_target_term, note)
-      raise Logaling::GlossaryNotFound unless File.exist?(source_path)
+      raise Logaling::GlossaryNotFound unless File.exist?(source_path_full)
 
       glossary_source = self.load
       target_index = find_term_index(glossary_source, source_term, target_term)
@@ -57,7 +57,7 @@ module Logaling::GlossarySources
     end
 
     def delete(source_term, target_term)
-      raise Logaling::GlossaryNotFound unless File.exist?(source_path)
+      raise Logaling::GlossaryNotFound unless File.exist?(source_path_full)
 
       glossary_source = self.load
       target_index = find_term_index(glossary_source, source_term, target_term)
@@ -70,7 +70,7 @@ module Logaling::GlossarySources
     end
 
     def delete_all(source_term, force=false)
-      raise Logaling::GlossaryNotFound unless File.exist?(source_path)
+      raise Logaling::GlossaryNotFound unless File.exist?(source_path_full)
 
       glossary_source = self.load
       delete_candidates = target_terms(glossary_source, source_term)
@@ -121,7 +121,7 @@ module Logaling::GlossarySources
     end
 
     def dump_glossary_source(glossary_source)
-      File.open(source_path, "w") do |f|
+      File.open(source_path_full, "w") do |f|
         f << YAML.dump(glossary_source)
       end
     end

  Modified: lib/logaling/project.rb (+24 -12)
===================================================================
--- lib/logaling/project.rb    2012-11-14 11:21:34 +0900 (f684a22)
+++ lib/logaling/project.rb    2012-11-21 03:44:37 +0900 (99aa8d4)
@@ -44,8 +44,8 @@ module Logaling
     end
     attr_reader :path, :repository
 
-    def initialize(path, repository=nil)
-      @path = path
+    def initialize(relative_path, repository=nil)
+      @path = relative_path
       @repository = repository
     end
 
@@ -58,7 +58,8 @@ module Logaling
     end
 
     def glossary_source_path
-      File.join(@path, "glossary")
+      basepath =****@repos*****_full_path(@path)
+      File.join(basepath, "glossary")
     end
 
     def glossary_db_path
@@ -74,8 +75,9 @@ module Logaling
     end
 
     def glossary_sources
-      all_glossary_source_path.map do |source_path|
-        name, source_language, target_language, type = File.basename(source_path).split(/\./)
+      all_glossary_source_path.map do |source_path_full|
+        name, source_language, target_language, type = File.basename(source_path_full).split(/\./)
+        source_path =****@repos*****_relative_path(source_path_full)
         GlossarySource.create(source_path, glossary(source_language, target_language))
       end
     end
@@ -121,7 +123,7 @@ module Logaling
     end
 
     def glossary_source_path
-      File.dirname(@path)
+      @repository.make_full_path(@path)
     end
 
     def imported?
@@ -131,23 +133,28 @@ module Logaling
     def normal_project?
       false
     end
+
+    private
+    def all_glossary_source_path
+      Dir.glob(File.join(File.dirname(glossary_source_path), "*"))
+    end
   end
 
   class PersonalProject < Project
     class << self
-      def create(root_path, glossary_name, source_language, target_language, repository=nil)
+      def create(relative_root_path, glossary_name, source_language, target_language, repository=nil)
         project_name = [glossary_name, source_language, target_language, 'yml'].join('.')
-        project_path = File.join(root_path, project_name)
+        project_path = File.join(relative_root_path, project_name)
         project = PersonalProject.new(project_path, repository)
         project.initialize_glossary(source_language, target_language)
         project
       end
 
-      def remove(root_path, glossary_name, source_language, target_language, repository=nil)
+      def remove(relative_root_path, glossary_name, source_language, target_language, repository)
         project_name = [glossary_name, source_language, target_language, 'yml'].join('.')
-        project_path = File.join(root_path, project_name)
+        project_path = File.join(relative_root_path, project_name)
         project = PersonalProject.new(project_path, repository)
-        FileUtils.rm_rf(project_path, :secure => true)
+        FileUtils.rm_rf(repository.make_full_path(project_path), :secure => true)
         project
       end
     end
@@ -162,7 +169,7 @@ module Logaling
     end
 
     def glossary_source_path
-      File.dirname(@path)
+      @repository.make_full_path(@path)
     end
 
     def initialize_glossary(source_language, target_language)
@@ -176,5 +183,10 @@ module Logaling
     def normal_project?
       false
     end
+
+    private
+    def all_glossary_source_path
+      Dir.glob(File.join(File.dirname(glossary_source_path), "*"))
+    end
   end
 end

  Modified: lib/logaling/repository.rb (+20 -6)
===================================================================
--- lib/logaling/repository.rb    2012-11-14 11:21:34 +0900 (acc3f7a)
+++ lib/logaling/repository.rb    2012-11-21 03:44:37 +0900 (bf02ff5)
@@ -21,6 +21,7 @@ require "logaling/project"
 
 module Logaling
   class Repository
+    attr_reader :logaling_home
     def initialize(path)
       @logaling_home = path
     end
@@ -42,7 +43,7 @@ module Logaling
 
     def unregister(project)
       raise Logaling::ProjectNotFound unless project
-      FileUtils.rm_rf(project.path, :secure => true)
+      FileUtils.rm_rf(make_full_path(project.path), :secure => true)
       index
     end
 
@@ -50,14 +51,16 @@ module Logaling
       if glossary_exists?(project_name, source_language, target_language)
         raise Logaling::GlossaryAlreadyRegistered, "The glossary '#{project_name}' already exists."
       end
-      PersonalProject.create(personal_glossary_root_path, project_name, source_language, target_language, self)
+      personal_project_path = make_relative_path(personal_glossary_root_path)
+      PersonalProject.create(personal_project_path, project_name, source_language, target_language, self)
     end
 
     def remove_personal_project(project_name, source_language, target_language)
       unless glossary_exists?(project_name, source_language, target_language)
         raise Logaling::GlossaryNotFound, "The glossary '#{project_name}' not found."
       end
-      PersonalProject.remove(personal_glossary_root_path, project_name, source_language, target_language, self)
+      personal_project_path = make_relative_path(personal_glossary_root_path)
+      PersonalProject.remove(personal_project_path, project_name, source_language, target_language, self)
       index
     rescue Logaling::GlossaryNotFound => e
       raise e
@@ -110,13 +113,13 @@ module Logaling
 
     def projects
       projects = registered_project_paths.map do |project_path|
-        Logaling::Project.new(project_path, self)
+        Logaling::Project.new(make_relative_path(project_path), self)
       end
       projects += personal_glossary_paths.map do |personal_glossary_path|
-        Logaling::PersonalProject.new(personal_glossary_path, self)
+        Logaling::PersonalProject.new(make_relative_path(personal_glossary_path), self)
       end
       projects += imported_glossary_paths.map do |imported_project_path|
-        Logaling::ImportedProject.new(imported_project_path, self)
+        Logaling::ImportedProject.new(make_relative_path(imported_project_path), self)
       end
       projects.sort_by(&:path)
     end
@@ -165,6 +168,17 @@ module Logaling
       File.join(@logaling_home, "db")
     end
 
+    def make_full_path(relative_path)
+      File.expand_path(File.join(@logaling_home, relative_path))
+    end
+
+    def make_relative_path(full_path)
+      require 'pathname'
+      path = Pathname.new(full_path)
+      base = Pathname.new(@logaling_home)
+      path.relative_path_from(base).to_s
+    end
+
     private
     def logaling_projects_path
       File.join(@logaling_home, "projects")

  Modified: spec/logaling/command_spec.rb (+1 -1)
===================================================================
--- spec/logaling/command_spec.rb    2012-11-14 11:21:34 +0900 (39e6cf0)
+++ spec/logaling/command_spec.rb    2012-11-21 03:44:37 +0900 (06ef27a)
@@ -25,7 +25,7 @@ describe Logaling::Command::Application do
   let(:target_project_path) { File.join(logaling_home, "projects", "spec") }
   let(:repository) { Logaling::Repository.new(logaling_home) }
   let(:glossary) { repository.find_project('spec').glossary('en', 'ja') }
-  let(:glossary_source_path) { glossary.glossary_source.source_path }
+  let(:glossary_source_path) { glossary.glossary_source.source_path_full }
 
   before do
     FileUtils.rm_rf(File.join(logaling_home, 'projects', 'spec'))

  Modified: spec/logaling/glossary_db_spec.rb (+2 -1)
===================================================================
--- spec/logaling/glossary_db_spec.rb    2012-11-14 11:21:34 +0900 (0644198)
+++ spec/logaling/glossary_db_spec.rb    2012-11-21 03:44:37 +0900 (58c4527)
@@ -32,13 +32,14 @@ module Logaling
     before do
       FileUtils.rm_rf(File.join(project_path, 'spec'))
       FileUtils.mkdir_p(cache_path)
+      FileUtils.mkdir_p(personal_path)
     end
 
     describe "#get_all_glossary_sources" do
       before do
         command.new('spec', 'en', 'fr')
         command.add('spec', 'スペック')
-        command.copy('spec', 'en', 'fr', 'spec', 'en', 'ja')
+        command.copy('spec', 'en', 'fr', 'personal_spec', 'en', 'ja')
         csv_path = File.join(cache_path, 'imported_spec.en.ja.csv')
         FileUtils.touch(csv_path)
         File.open(csv_path, "w"){|f| f.puts "test_logaling,テストろがりん"}

  Modified: spec/logaling/glossary_spec.rb (+11 -7)
===================================================================
--- spec/logaling/glossary_spec.rb    2012-11-14 11:21:34 +0900 (fed5e15)
+++ spec/logaling/glossary_spec.rb    2012-11-21 03:44:37 +0900 (dc5bf28)
@@ -21,13 +21,19 @@ require "fileutils"
 module Logaling
   describe Glossary do
     let(:logaling_home) { @logaling_home }
+    let(:logaling_db_home) { File.join(logaling_home, 'db') }
     let(:repository) { Logaling::Repository.new(logaling_home) }
     let(:glossary) { repository.find_project('spec').glossary('en', 'ja') }
     let(:glossary_source_path) { glossary.glossary_source.source_path }
+    let(:glossary_source_path_full) { glossary.glossary_source.source_path_full }
 
     before do
+      # clear directories and files
       FileUtils.rm_rf(File.join(logaling_home, 'projects', 'spec'), :secure => true)
-      FileUtils.mkdir_p(File.join(logaling_home, 'projects', 'spec'))
+      FileUtils.mkdir_p(File.join(logaling_home, 'projects', 'spec', 'glossary'))
+      FileUtils.touch(glossary_source_path_full)
+      # and clear db too
+      glossary.index!
     end
 
     describe '#add' do
@@ -37,7 +43,7 @@ module Logaling
         end
 
         it 'glossary yaml should have that bilingual pair' do
-          yaml = YAML::load_file(glossary_source_path)
+          yaml = YAML::load_file(glossary_source_path_full)
           term = yaml.index({"source_term"=>"spec", "target_term"=>"スペック", "note"=>"テストスペック"})
           term.should_not be_nil
         end
@@ -49,7 +55,7 @@ module Logaling
         end
 
         it "should create the glossary and add term" do
-          yaml = YAML::load_file(glossary_source_path)
+          yaml = YAML::load_file(glossary_source_path_full)
           term = yaml.index({"source_term"=>"test", "target_term"=>"テスト", "note"=>"テスト"})
           term.should_not be_nil
         end
@@ -85,7 +91,7 @@ module Logaling
         end
 
         it 'should clear note' do
-          yaml = YAML::load_file(glossary_source_path)
+          yaml = YAML::load_file(glossary_source_path_full)
           term = yaml.index({"source_term"=>"user", "target_term"=>"ユーザ", "note"=>""})
           term.should_not be_nil
         end
@@ -97,7 +103,7 @@ module Logaling
         end
 
         it {
-          -> { glossary.update("user", "ゆーざー", "ユーザ", "") }.should raise_error(Logaling::TermError)
+          -> { glossary.update("user", "ゆーざ", "ユーザ", "") }.should raise_error(Logaling::TermError)
         }
       end
     end
@@ -108,7 +114,6 @@ module Logaling
           glossary.add("delete_logaling", "てすと1", "備考")
           glossary.add("delete_logaling", "てすと2", "備考")
           glossary.delete("delete_logaling", "てすと1")
-          repository.index
           @result = repository.lookup("delete_logaling", glossary)
         end
 
@@ -145,7 +150,6 @@ module Logaling
           before do
             glossary.add("user_logaling", "ユーザ", "備考")
             glossary.delete_all("user_logaling")
-            repository.index
             @result = repository.lookup("user_logaling", glossary)
           end
 

  Modified: spec/logaling/repository_spec.rb (+8 -5)
===================================================================
--- spec/logaling/repository_spec.rb    2012-11-14 11:21:34 +0900 (ad87845)
+++ spec/logaling/repository_spec.rb    2012-11-21 03:44:37 +0900 (d38752e)
@@ -25,10 +25,12 @@ module Logaling
     let(:repository) { Logaling::Repository.new(logaling_home) }
     let(:glossary) { repository.find_project('spec').glossary('en', 'ja') }
     let(:glossary_source_path) { glossary.glossary_source.source_path }
+    let(:glossary_source_path_full) { glossary.glossary_source.source_path_full }
 
     before do
       FileUtils.rm_rf(File.join(logaling_home, 'projects', 'spec'), :secure => true)
       FileUtils.mkdir_p(File.join(logaling_home, 'projects', 'spec'))
+      repository.index
     end
 
     describe '#lookup' do
@@ -117,7 +119,7 @@ module Logaling
       end
 
       context 'when tsv file as glossary exists' do
-        let(:tsv_path) { glossary_source_path.sub(/yml$/, 'tsv') }
+        let(:tsv_path) { glossary_source_path_full.sub(/yml$/, 'tsv') }
 
         before do
           FileUtils.touch(tsv_path)
@@ -137,12 +139,12 @@ module Logaling
     end
 
     describe '#index' do
-      let(:tsv_path) { File.join(File.dirname(glossary_source_path), "spec.en.ja.tsv") }
-      let(:csv_path) { File.join(File.dirname(glossary_source_path), "spec.en.ja.csv") }
+      let(:tsv_path) { File.join(File.dirname(glossary_source_path_full), "spec.en.ja.tsv") }
+      let(:csv_path) { File.join(File.dirname(glossary_source_path_full), "spec.en.ja.csv") }
 
       context 'when yml file as glossary exists' do
         before do
-          File.open(glossary_source_path, 'w') do |f|
+          File.open(glossary_source_path_full, 'w') do |f|
             YAML.dump([], f)
           end
           glossary.add("spec_logaling", "スペック", "備考")
@@ -155,7 +157,7 @@ module Logaling
         end
 
         after do
-          FileUtils.rm_rf(glossary_source_path, :secure => true)
+          FileUtils.rm_rf(glossary_source_path_full, :secure => true)
         end
       end
 
@@ -201,6 +203,7 @@ module Logaling
       before do
         FileUtils.rm_rf(File.join(logaling_home, 'personal'), :secure => true)
         repository.create_personal_project(rm_glossary_name, rm_source_language, rm_target_language)
+        repository.index
       end
 
       context "when target personal project exists" do
-------------- next part --------------
An HTML attachment was scrubbed...
Télécharger 



More information about the logaling-commit mailing list
Back to archive index